forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abi.go
64 lines (53 loc) · 1.57 KB
/
abi.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
package xchain
import (
"github.com/omni-network/omni/lib/errors"
"github.com/ethereum/go-ethereum/accounts/abi"
)
const (
typUint64 = "uint64"
typBytes32 = "bytes32"
typBytes = "bytes"
typAddress = "address"
)
//nolint:gochecknoglobals // Static ABI types
var (
headerABI = mustABITuple([]abi.ArgumentMarshaling{
{Name: "SourceChainID", Type: typUint64},
{Name: "BlockHeight", Type: typUint64},
{Name: "BlockHash", Type: typBytes32},
})
msgABI = mustABITuple([]abi.ArgumentMarshaling{
{Name: "SourceChainID", Type: typUint64},
{Name: "DestChainID", Type: typUint64},
{Name: "StreamOffset", Type: typUint64},
{Name: "SourceMsgSender", Type: typAddress},
{Name: "DestAddress", Type: typAddress},
{Name: "Data", Type: typBytes},
{Name: "DestGasLimit", Type: typUint64},
})
)
// encodeMsg ABI encodes a cross chain message into a byte slice.
func encodeMsg(msg Msg) ([]byte, error) {
resp, err := msgABI.Pack(msg)
if err != nil {
return nil, errors.Wrap(err, "pack xchain msg")
}
return resp, nil
}
// encodeHeader ABI encodes a cross chain block header into a byte slice.
func encodeHeader(header BlockHeader) ([]byte, error) {
resp, err := headerABI.Pack(header)
if err != nil {
return nil, errors.Wrap(err, "pack xchain header")
}
return resp, nil
}
// mustABITuple returns an ABI tuple typ with the provided components.
// It panics on error.
func mustABITuple(components []abi.ArgumentMarshaling) abi.Arguments {
typ, err := abi.NewType("tuple", "", components)
if err != nil {
panic(err)
}
return abi.Arguments{abi.Argument{Type: typ}}
}