/
tx.go
193 lines (161 loc) · 5.1 KB
/
tx.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package polkadot
import (
"encoding/hex"
"math/big"
"strconv"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
gstypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/strangelove-ventures/interchaintest/v5/ibc"
)
// SendFundsTx sends funds to a wallet using the SubstrateAPI
func SendFundsTx(api *gsrpc.SubstrateAPI, senderKeypair signature.KeyringPair, amount ibc.WalletAmount) (gstypes.Hash, error) {
hash := gstypes.Hash{}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return hash, err
}
receiverPubKey, err := DecodeAddressSS58(amount.Address)
if err != nil {
return hash, err
}
receiver, err := gstypes.NewMultiAddressFromHexAccountID(hex.EncodeToString(receiverPubKey))
if err != nil {
return hash, err
}
call, err := gstypes.NewCall(meta, "Balances.transfer", receiver, gstypes.NewUCompactFromUInt(uint64(amount.Amount)))
if err != nil {
return hash, err
}
return CreateSignSubmitExt(api, meta, senderKeypair, call)
}
// Turns on sending and receiving ibc transfers
func EnableIbc(api *gsrpc.SubstrateAPI, senderKeypair signature.KeyringPair) (gstypes.Hash, error) {
hash := gstypes.Hash{}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return hash, err
}
c, err := gstypes.NewCall(meta, "Ibc.set_params", gstypes.NewBool(true), gstypes.NewBool(true))
if err != nil {
return hash, err
}
sc, err := gstypes.NewCall(meta, "Sudo.sudo", c)
if err != nil {
return hash, err
}
return CreateSignSubmitExt(api, meta, senderKeypair, sc)
}
// SendIbcFundsTx sends funds to a wallet using the SubstrateAPI
func SendIbcFundsTx(
api *gsrpc.SubstrateAPI,
senderKeypair signature.KeyringPair,
channelID string,
amount ibc.WalletAmount,
options ibc.TransferOptions,
) (gstypes.Hash, error) {
hash := gstypes.Hash{}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return hash, err
}
assetNum, err := strconv.ParseInt(amount.Denom, 10, 64)
if err != nil {
return hash, err
}
raw := gstypes.NewU8(1)
size := gstypes.NewU8(uint8(len(amount.Address) * 4))
to := gstypes.NewStorageDataRaw([]byte(amount.Address))
channel := gstypes.NewU64(0) // Parse channel number from string
timeout := gstypes.NewU8(1)
timestamp := gstypes.NewOptionU64(gstypes.NewU64(0))
height := gstypes.NewOptionU64(gstypes.NewU64(3000)) // Must set timestamp or height
assetId := gstypes.NewU128(*big.NewInt(assetNum))
amount2 := gstypes.NewU128(*big.NewInt(amount.Amount))
memo := gstypes.NewU8(0)
call, err := gstypes.NewCall(meta, "Ibc.transfer", raw, size, to, channel, timeout, timestamp, height, assetId, amount2, memo)
if err != nil {
return hash, err
}
return CreateSignSubmitExt(api, meta, senderKeypair, call)
}
// MintFunds mints an asset for a user on parachain, keyName must be the owner of the asset
func MintFundsTx(
api *gsrpc.SubstrateAPI,
senderKeypair signature.KeyringPair,
amount ibc.WalletAmount,
) (gstypes.Hash, error) {
hash := gstypes.Hash{}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return hash, err
}
assetNum, err := strconv.ParseInt(amount.Denom, 10, 64)
if err != nil {
return hash, err
}
receiverPubKey, err := DecodeAddressSS58(amount.Address)
if err != nil {
return hash, err
}
receiver, err := gstypes.NewMultiAddressFromHexAccountID(hex.EncodeToString(receiverPubKey))
if err != nil {
return hash, err
}
assetId := gstypes.NewU128(*big.NewInt(assetNum))
amount2 := gstypes.NewUCompactFromUInt(uint64(amount.Amount))
call, err := gstypes.NewCall(meta, "Assets.mint", assetId, receiver, amount2)
if err != nil {
return hash, err
}
return CreateSignSubmitExt(api, meta, senderKeypair, call)
}
// Common tx function to create an extrinsic and sign/submit it
func CreateSignSubmitExt(
api *gsrpc.SubstrateAPI,
meta *gstypes.Metadata,
senderKeypair signature.KeyringPair,
call gstypes.Call,
) (gstypes.Hash, error) {
hash := gstypes.Hash{}
ext := gstypes.NewExtrinsic(call)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
return hash, err
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
return hash, err
}
pubKey, err := DecodeAddressSS58(senderKeypair.Address)
if err != nil {
return hash, err
}
key, err := gstypes.CreateStorageKey(meta, "System", "Account", pubKey)
if err != nil {
return hash, err
}
var accountInfo AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
return hash, err
}
nonce := uint32(accountInfo.Nonce)
o := gstypes.SignatureOptions{
BlockHash: genesisHash,
Era: gstypes.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: gstypes.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: gstypes.NewUCompactFromUInt(0),
TransactionVersion: rv.TransactionVersion,
}
// Sign the transaction using Alice's default account
err = ext.Sign(senderKeypair, o)
if err != nil {
return hash, err
}
// Send the extrinsic
hash, err = api.RPC.Author.SubmitExtrinsic(ext)
return hash, err
}