-
Notifications
You must be signed in to change notification settings - Fork 113
/
command.go
434 lines (376 loc) · 15.5 KB
/
command.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package types
import (
"encoding/binary"
"fmt"
"math/big"
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/gogo/protobuf/proto"
"github.com/stoewer/go-strcase"
multisig "github.com/axelarnetwork/axelar-core/x/multisig/exported"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/utils/funcs"
"github.com/axelarnetwork/utils/slices"
)
const (
mintTokenMaxGasCost = 100000
deployTokenMaxGasCost = 1400000
burnExternalTokenMaxGasCost = 400000
burnInternalTokenMaxGasCost = 120000
transferOperatorshipMaxGasCost = 120000
approveContractCallWithMintMaxGasCost = 100000
approveContractCallMaxGasCost = 100000
)
func (c CommandType) String() string {
return strcase.LowerCamelCase(strings.TrimPrefix(proto.EnumName(CommandType_name, int32(c)), "COMMAND_TYPE_"))
}
// ValidateBasic returns an error if the given command type is invalid
func (c CommandType) ValidateBasic() error {
if _, ok := CommandType_name[int32(c)]; !ok || c == COMMAND_TYPE_UNSPECIFIED {
return fmt.Errorf("%s is not a valid command type", c.String())
}
return nil
}
var (
stringType = funcs.Must(abi.NewType("string", "string", nil))
addressType = funcs.Must(abi.NewType("address", "address", nil))
addressesType = funcs.Must(abi.NewType("address[]", "address[]", nil))
bytes32Type = funcs.Must(abi.NewType("bytes32", "bytes32", nil))
uint8Type = funcs.Must(abi.NewType("uint8", "uint8", nil))
uint256Type = funcs.Must(abi.NewType("uint256", "uint256", nil))
uint256ArrayType = funcs.Must(abi.NewType("uint256[]", "uint256[]", nil))
deployTokenArguments = abi.Arguments{{Type: stringType}, {Type: stringType}, {Type: uint8Type}, {Type: uint256Type}, {Type: addressType}, {Type: uint256Type}}
mintTokenArguments = abi.Arguments{{Type: stringType}, {Type: addressType}, {Type: uint256Type}}
burnTokenArguments = abi.Arguments{{Type: stringType}, {Type: bytes32Type}}
transferMultisigArguments = abi.Arguments{{Type: addressesType}, {Type: uint256ArrayType}, {Type: uint256Type}}
approveContractCallArguments = abi.Arguments{{Type: stringType}, {Type: stringType}, {Type: addressType}, {Type: bytes32Type}, {Type: bytes32Type}, {Type: uint256Type}}
approveContractCallWithMintArguments = abi.Arguments{{Type: stringType}, {Type: stringType}, {Type: addressType}, {Type: bytes32Type}, {Type: stringType}, {Type: uint256Type}, {Type: bytes32Type}, {Type: uint256Type}}
)
// NewBurnTokenCommand creates a command to burn tokens with the given burner's information
func NewBurnTokenCommand(chainID sdk.Int, keyID multisig.KeyID, height int64, burnerInfo BurnerInfo, isTokenExternal bool) Command {
heightBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(heightBytes, uint64(height))
burnTokenMaxGasCost := burnInternalTokenMaxGasCost
if isTokenExternal {
burnTokenMaxGasCost = burnExternalTokenMaxGasCost
}
return Command{
ID: NewCommandID(append(burnerInfo.Salt.Bytes(), heightBytes...), chainID),
Type: COMMAND_TYPE_BURN_TOKEN,
Params: createBurnTokenParams(burnerInfo.Symbol, common.Hash(burnerInfo.Salt)),
KeyID: keyID,
MaxGasCost: uint32(burnTokenMaxGasCost),
}
}
// NewDeployTokenCommand creates a command to deploy a token
func NewDeployTokenCommand(chainID sdk.Int, keyID multisig.KeyID, asset string, tokenDetails TokenDetails, address Address, dailyMintLimit sdk.Uint) Command {
return Command{
ID: NewCommandID([]byte(fmt.Sprintf("%s_%s", asset, tokenDetails.Symbol)), chainID),
Type: COMMAND_TYPE_DEPLOY_TOKEN,
Params: createDeployTokenParams(tokenDetails.TokenName, tokenDetails.Symbol, tokenDetails.Decimals, tokenDetails.Capacity, address, dailyMintLimit),
KeyID: keyID,
MaxGasCost: deployTokenMaxGasCost,
}
}
// NewMintTokenCommand creates a command to mint token to the given address
func NewMintTokenCommand(keyID multisig.KeyID, id nexus.TransferID, symbol string, address common.Address, amount *big.Int) Command {
return Command{
ID: CommandIDFromTransferID(id),
Type: COMMAND_TYPE_MINT_TOKEN,
Params: createMintTokenParams(symbol, address, amount),
KeyID: keyID,
MaxGasCost: mintTokenMaxGasCost,
}
}
// NewMultisigTransferCommand creates a command to transfer operator of the multisig contract
func NewMultisigTransferCommand(chainID sdk.Int, keyID multisig.KeyID, nextKey multisig.Key) Command {
addresses, weights, threshold := GetMultisigAddressesAndWeights(nextKey)
var concat []byte
for _, addr := range addresses {
concat = append(concat, addr.Bytes()...)
}
return Command{
ID: NewCommandID(concat, chainID),
Type: COMMAND_TYPE_TRANSFER_OPERATORSHIP,
Params: createTransferMultisigParams(addresses, slices.Map(weights, sdk.Uint.BigInt), threshold.BigInt()),
KeyID: keyID,
MaxGasCost: transferOperatorshipMaxGasCost,
}
}
// NewApproveContractCallCommand creates a command to approve contract call
func NewApproveContractCallCommand(
chainID sdk.Int,
keyID multisig.KeyID,
sourceChain nexus.ChainName,
sourceTxID Hash,
sourceEventIndex uint64,
event EventContractCall,
) Command {
sourceEventIndexBz := make([]byte, 8)
binary.LittleEndian.PutUint64(sourceEventIndexBz, sourceEventIndex)
return Command{
ID: NewCommandID(append(sourceTxID.Bytes(), sourceEventIndexBz...), chainID),
Type: COMMAND_TYPE_APPROVE_CONTRACT_CALL,
Params: createApproveContractCallParams(sourceChain, sourceTxID, sourceEventIndex, event),
KeyID: keyID,
MaxGasCost: uint32(approveContractCallMaxGasCost),
}
}
// NewApproveContractCallCommandGeneric creates a command to approve contract call
func NewApproveContractCallCommandGeneric(
chainID sdk.Int,
keyID multisig.KeyID,
contractAddress common.Address,
payloadHash common.Hash,
sourceTxID common.Hash,
sourceChain nexus.ChainName,
sender string,
sourceEventIndex uint64,
ID string,
) Command {
commandID := NewCommandID([]byte(ID), chainID)
return Command{
ID: commandID,
Type: COMMAND_TYPE_APPROVE_CONTRACT_CALL,
Params: createApproveContractCallParamsGeneric(contractAddress, payloadHash, sourceTxID, string(sourceChain), sender, sourceEventIndex),
KeyID: keyID,
MaxGasCost: approveContractCallMaxGasCost,
}
}
// NewApproveContractCallWithMintCommand creates a command to approve contract call with token being minted
func NewApproveContractCallWithMintCommand(
chainID sdk.Int,
keyID multisig.KeyID,
sourceChain nexus.ChainName,
sourceTxID Hash,
sourceEventIndex uint64,
event EventContractCallWithToken,
amount sdk.Uint,
symbol string,
) Command {
sourceEventIndexBz := make([]byte, 8)
binary.LittleEndian.PutUint64(sourceEventIndexBz, sourceEventIndex)
return Command{
ID: NewCommandID(append(sourceTxID.Bytes(), sourceEventIndexBz...), chainID),
Type: COMMAND_TYPE_APPROVE_CONTRACT_CALL_WITH_MINT,
Params: createApproveContractCallWithMintParams(sourceChain, sourceTxID, sourceEventIndex, event, amount, symbol),
KeyID: keyID,
MaxGasCost: uint32(approveContractCallWithMintMaxGasCost),
}
}
// NewApproveContractCallWithMintGeneric creates a command to approve contract call with mint
func NewApproveContractCallWithMintGeneric(
chainID sdk.Int,
keyID multisig.KeyID,
sourceTxID common.Hash,
sourceEventIndex uint64,
message nexus.GeneralMessage,
symbol string,
) Command {
commandID := NewCommandID([]byte(message.ID), chainID)
contractAddress := common.HexToAddress(message.GetDestinationAddress())
payloadHash := common.BytesToHash(message.PayloadHash)
return Command{
ID: commandID,
Type: COMMAND_TYPE_APPROVE_CONTRACT_CALL_WITH_MINT,
Params: createApproveContractCallWithMintParamsGeneric(contractAddress, payloadHash, sourceTxID, message.Sender, sourceEventIndex, message.Asset.Amount.BigInt(), symbol),
KeyID: keyID,
MaxGasCost: approveContractCallWithMintMaxGasCost,
}
}
// DecodeParams returns the decoded parameters in the given command
func (m Command) DecodeParams() (map[string]string, error) {
params := make(map[string]string)
switch m.Type {
case COMMAND_TYPE_APPROVE_CONTRACT_CALL_WITH_MINT:
sourceChain, sourceAddress, contractAddress, payloadHash, symbol, amount, sourceTxID, sourceEventIndex := DecodeApproveContractCallWithMintParams(m.Params)
params["sourceChain"] = sourceChain
params["sourceAddress"] = sourceAddress
params["contractAddress"] = contractAddress.Hex()
params["payloadHash"] = payloadHash.Hex()
params["symbol"] = symbol
params["amount"] = amount.String()
params["sourceTxHash"] = sourceTxID.Hex()
params["sourceEventIndex"] = sourceEventIndex.String()
case COMMAND_TYPE_APPROVE_CONTRACT_CALL:
sourceChain, sourceAddress, contractAddress, payloadHash, sourceTxID, sourceEventIndex := DecodeApproveContractCallParams(m.Params)
params["sourceChain"] = sourceChain
params["sourceAddress"] = sourceAddress
params["contractAddress"] = contractAddress.Hex()
params["payloadHash"] = payloadHash.Hex()
params["sourceTxHash"] = sourceTxID.Hex()
params["sourceEventIndex"] = sourceEventIndex.String()
case COMMAND_TYPE_DEPLOY_TOKEN:
name, symbol, decs, cap, tokenAddress, dailyMintLimit := DecodeDeployTokenParams(m.Params)
params["name"] = name
params["symbol"] = symbol
params["decimals"] = strconv.FormatUint(uint64(decs), 10)
params["cap"] = cap.String()
params["tokenAddress"] = tokenAddress.Hex()
params["dailyMintLimit"] = dailyMintLimit.String()
case COMMAND_TYPE_MINT_TOKEN:
symbol, addr, amount := DecodeMintTokenParams(m.Params)
params["symbol"] = symbol
params["account"] = addr.Hex()
params["amount"] = amount.String()
case COMMAND_TYPE_BURN_TOKEN:
symbol, salt := DecodeBurnTokenParams(m.Params)
params["symbol"] = symbol
params["salt"] = salt.Hex()
case COMMAND_TYPE_TRANSFER_OPERATORSHIP:
addresses, weights, threshold := DecodeTransferMultisigParams(m.Params)
params["newOperators"] = strings.Join(slices.Map(addresses, common.Address.Hex), ";")
params["newWeights"] = strings.Join(slices.Map(weights, func(w *big.Int) string { return w.String() }), ";")
params["newThreshold"] = threshold.String()
default:
return nil, fmt.Errorf("unknown command type '%s'", m.Type)
}
return params, nil
}
// Clone returns an exacy copy of Command
func (m Command) Clone() Command {
clone := Command{
ID: m.ID,
Type: m.Type,
KeyID: m.KeyID,
MaxGasCost: m.MaxGasCost,
Params: make([]byte, len(m.Params)),
}
copy(clone.Params, m.Params)
return clone
}
func createBurnTokenParams(symbol string, salt common.Hash) []byte {
return funcs.Must(burnTokenArguments.Pack(symbol, salt))
}
func createDeployTokenParams(tokenName string, symbol string, decimals uint8, capacity sdk.Int, address Address, dailyMintLimit sdk.Uint) []byte {
return funcs.Must(deployTokenArguments.Pack(
tokenName,
symbol,
decimals,
capacity.BigInt(),
address,
dailyMintLimit.BigInt(),
))
}
func createMintTokenParams(symbol string, address common.Address, amount *big.Int) []byte {
return funcs.Must(mintTokenArguments.Pack(symbol, address, amount))
}
func createTransferMultisigParams(addresses []common.Address, weights []*big.Int, threshold *big.Int) []byte {
return funcs.Must(transferMultisigArguments.Pack(addresses, weights, threshold))
}
func createApproveContractCallParams(
sourceChain nexus.ChainName,
sourceTxID Hash,
sourceEventIndex uint64,
event EventContractCall) []byte {
return funcs.Must(approveContractCallArguments.Pack(
sourceChain,
event.Sender.Hex(),
common.HexToAddress(event.ContractAddress),
common.Hash(event.PayloadHash),
common.Hash(sourceTxID),
new(big.Int).SetUint64(sourceEventIndex),
))
}
func createApproveContractCallParamsGeneric(
contractAddress common.Address,
payloadHash common.Hash,
txID common.Hash,
sourceChain string,
sender string,
sourceEventIndex uint64) []byte {
return funcs.Must(approveContractCallArguments.Pack(
sourceChain,
sender,
contractAddress,
payloadHash,
txID,
new(big.Int).SetUint64(sourceEventIndex),
))
}
func createApproveContractCallWithMintParams(
sourceChain nexus.ChainName,
sourceTxID Hash,
sourceEventIndex uint64,
event EventContractCallWithToken,
amount sdk.Uint,
symbol string) []byte {
return funcs.Must(approveContractCallWithMintArguments.Pack(
sourceChain,
event.Sender.Hex(),
common.HexToAddress(event.ContractAddress),
common.Hash(event.PayloadHash),
symbol,
amount.BigInt(),
common.Hash(sourceTxID),
new(big.Int).SetUint64(sourceEventIndex),
))
}
func createApproveContractCallWithMintParamsGeneric(
contractAddress common.Address,
payloadHash common.Hash,
txID common.Hash,
sender nexus.CrossChainAddress,
sourceEventIndex uint64,
amount *big.Int,
symbol string) []byte {
return funcs.Must(approveContractCallWithMintArguments.Pack(
sender.Chain.Name,
sender.Address,
contractAddress,
payloadHash,
symbol,
amount,
txID,
new(big.Int).SetUint64(sourceEventIndex),
))
}
// DecodeApproveContractCallWithMintParams decodes the call arguments from the given contract call
func DecodeApproveContractCallWithMintParams(bz []byte) (string, string, common.Address, common.Hash, string, *big.Int, common.Hash, *big.Int) {
params := funcs.Must(StrictDecode(approveContractCallWithMintArguments, bz))
payloadHash := params[3].([common.HashLength]byte)
sourceTxID := params[6].([common.HashLength]byte)
return params[0].(string),
params[1].(string),
params[2].(common.Address),
common.BytesToHash(payloadHash[:]),
params[4].(string),
params[5].(*big.Int),
common.BytesToHash(sourceTxID[:]),
params[7].(*big.Int)
}
// DecodeApproveContractCallParams decodes the call arguments from the given contract call
func DecodeApproveContractCallParams(bz []byte) (string, string, common.Address, common.Hash, common.Hash, *big.Int) {
params := funcs.Must(StrictDecode(approveContractCallArguments, bz))
payloadHash := params[3].([common.HashLength]byte)
sourceTxID := params[4].([common.HashLength]byte)
return params[0].(string),
params[1].(string),
params[2].(common.Address),
common.BytesToHash(payloadHash[:]),
common.BytesToHash(sourceTxID[:]),
params[5].(*big.Int)
}
// DecodeMintTokenParams decodes the call arguments from the given contract call
func DecodeMintTokenParams(bz []byte) (string, common.Address, *big.Int) {
params := funcs.Must(StrictDecode(mintTokenArguments, bz))
return params[0].(string), params[1].(common.Address), params[2].(*big.Int)
}
// DecodeDeployTokenParams decodes the call arguments from the given contract call
func DecodeDeployTokenParams(bz []byte) (string, string, uint8, *big.Int, common.Address, sdk.Uint) {
params := funcs.Must(StrictDecode(deployTokenArguments, bz))
return params[0].(string), params[1].(string), params[2].(uint8), params[3].(*big.Int), params[4].(common.Address), sdk.NewUintFromBigInt(params[5].(*big.Int))
}
// DecodeBurnTokenParams decodes the call arguments from the given contract call
func DecodeBurnTokenParams(bz []byte) (string, common.Hash) {
params := funcs.Must(StrictDecode(burnTokenArguments, bz))
return params[0].(string), params[1].([common.HashLength]byte)
}
// DecodeTransferMultisigParams decodes the call arguments from the given contract call
func DecodeTransferMultisigParams(bz []byte) ([]common.Address, []*big.Int, *big.Int) {
params := funcs.Must(StrictDecode(transferMultisigArguments, bz))
return params[0].([]common.Address), params[1].([]*big.Int), params[2].(*big.Int)
}