-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathoperate.go
More file actions
132 lines (124 loc) · 3.27 KB
/
operate.go
File metadata and controls
132 lines (124 loc) · 3.27 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package interchain
import (
_ "embed"
"math/big"
"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ethereum/go-ethereum/common"
)
func GetNextMessageID(
rpcURL string,
messengerAddress common.Address,
destinationBlockchainID ids.ID,
) (ids.ID, error) {
out, err := contract.CallToMethod(
rpcURL,
messengerAddress,
"getNextMessageID(bytes32)->(bytes32)",
destinationBlockchainID,
)
if err != nil {
return ids.Empty, err
}
return contract.GetMethodReturn[[32]byte]("getNextMessageID", out)
}
func MessageReceived(
rpcURL string,
messengerAddress common.Address,
messageID ids.ID,
) (bool, error) {
out, err := contract.CallToMethod(
rpcURL,
messengerAddress,
"messageReceived(bytes32)->(bool)",
messageID,
)
if err != nil {
return false, err
}
return contract.GetMethodReturn[bool]("messageReceived", out)
}
func SendCrossChainMessage(
rpcURL string,
messengerAddress common.Address,
privateKey string,
destinationBlockchainID ids.ID,
destinationAddress common.Address,
message []byte,
) (*types.Transaction, *types.Receipt, error) {
type FeeInfo struct {
FeeTokenAddress common.Address
Amount *big.Int
}
type Params struct {
DestinationBlockchainID [32]byte
DestinationAddress common.Address
FeeInfo FeeInfo
RequiredGasLimit *big.Int
AllowedRelayerAddresses []common.Address
Message []byte
}
params := Params{
DestinationBlockchainID: destinationBlockchainID,
DestinationAddress: destinationAddress,
FeeInfo: FeeInfo{
FeeTokenAddress: common.Address{},
Amount: big.NewInt(0),
},
RequiredGasLimit: big.NewInt(1),
AllowedRelayerAddresses: []common.Address{},
Message: message,
}
return contract.TxToMethod(
rpcURL,
false,
common.Address{},
privateKey,
messengerAddress,
nil,
"send cross chain message",
nil,
"sendCrossChainMessage((bytes32, address, (address, uint256), uint256, [address], bytes))->(bytes32)",
params,
)
}
// events
type ICMMessageReceipt struct {
ReceivedMessageNonce *big.Int
RelayerRewardAddress common.Address
}
type ICMFeeInfo struct {
FeeTokenAddress common.Address
Amount *big.Int
}
type ICMMessage struct {
MessageNonce *big.Int
OriginSenderAddress common.Address
DestinationBlockchainID [32]byte
DestinationAddress common.Address
RequiredGasLimit *big.Int
AllowedRelayerAddresses []common.Address
Receipts []ICMMessageReceipt
Message []byte
}
type ICMMessengerSendCrossChainMessage struct {
MessageID [32]byte
DestinationBlockchainID [32]byte
Message ICMMessage
FeeInfo ICMFeeInfo
}
func ParseSendCrossChainMessage(log types.Log) (*ICMMessengerSendCrossChainMessage, error) {
event := new(ICMMessengerSendCrossChainMessage)
if err := contract.UnpackLog(
"SendCrossChainMessage(bytes32,bytes32,(uint256,address,bytes32,address,uint256,[address],[(uint256,address)],bytes),(address,uint256))",
[]int{0, 1},
log,
event,
); err != nil {
return nil, err
}
return event, nil
}