-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
vrf_coordinator_interface.go
100 lines (87 loc) · 3.06 KB
/
vrf_coordinator_interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package vrf
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/assets"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/solidity_vrf_coordinator_interface"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// RawRandomnessRequestLog is used to parse a RandomnessRequest log into types
// go-ethereum knows about.
type RawRandomnessRequestLog solidity_vrf_coordinator_interface.VRFCoordinatorRandomnessRequest
// RandomnessRequestLog contains the data for a RandomnessRequest log,
// represented as compatible golang types.
type RandomnessRequestLog struct {
KeyHash common.Hash
Seed *big.Int // uint256
JobID common.Hash
Sender common.Address
Fee *assets.Link // uint256
RequestID common.Hash
Raw RawRandomnessRequestLog
}
var dummyCoordinator, _ = solidity_vrf_coordinator_interface.NewVRFCoordinator(
common.Address{}, nil)
func toGethLog(log types.Log) types.Log {
return types.Log{
Address: log.Address,
Topics: log.Topics,
Data: []byte(log.Data),
BlockNumber: log.BlockNumber,
TxHash: log.TxHash,
TxIndex: log.TxIndex,
BlockHash: log.BlockHash,
Index: log.Index,
Removed: log.Removed,
}
}
// ParseRandomnessRequestLog returns the RandomnessRequestLog corresponding to
// the raw logData
func ParseRandomnessRequestLog(log types.Log) (*RandomnessRequestLog, error) {
rawLog, err := dummyCoordinator.ParseRandomnessRequest(toGethLog(log))
if err != nil {
return nil, errors.Wrapf(err,
"while parsing %x as RandomnessRequestLog", log.Data)
}
return RawRandomnessRequestLogToRandomnessRequestLog(
(*RawRandomnessRequestLog)(rawLog)), nil
}
// RawData returns the raw bytes corresponding to l in a solidity log
//
// This serialization does not include the JobID, because that's an indexed field.
func (l *RandomnessRequestLog) RawData() ([]byte, error) {
return randomnessRequestRawDataArgs().Pack(l.KeyHash,
l.Seed, l.Sender, (*big.Int)(l.Fee), l.RequestID)
}
// Equal(ol) is true iff l is the same log as ol, and both represent valid
// RandomnessRequest logs.
func (l *RandomnessRequestLog) Equal(ol RandomnessRequestLog) bool {
return l.KeyHash == ol.KeyHash &&
equal(l.Seed, ol.Seed) &&
l.JobID == ol.JobID &&
l.Sender == ol.Sender &&
l.Fee.Cmp(ol.Fee) == 0 &&
l.RequestID == ol.RequestID
}
func (l *RandomnessRequestLog) ComputedRequestID() common.Hash {
soliditySeed, err := utils.Uint256ToBytes(l.Seed)
if err != nil {
panic(errors.Wrapf(err, "vrf seed out of bounds in %#+v", l))
}
return utils.MustHash(string(append(l.KeyHash[:], soliditySeed...)))
}
func RawRandomnessRequestLogToRandomnessRequestLog(
l *RawRandomnessRequestLog) *RandomnessRequestLog {
return &RandomnessRequestLog{
KeyHash: l.KeyHash,
Seed: l.Seed,
JobID: l.JobID,
Sender: l.Sender,
Fee: (*assets.Link)(l.Fee),
RequestID: l.RequestID,
Raw: *l,
}
}
func equal(left, right *big.Int) bool { return left.Cmp(right) == 0 }