forked from aptos-labs/aptos-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinPayloads.go
93 lines (88 loc) · 2.38 KB
/
coinPayloads.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
package aptos
import "github.com/aptos-labs/aptos-go-sdk/bcs"
// CoinTransferPayload builds an EntryFunction payload for transferring coins
//
// Args:
// - coinType is the type of coin to transfer. If none is provided, it will transfer 0x1::aptos_coin:AptosCoin
// - dest is the destination [AccountAddress]
// - amount is the amount of coins to transfer
func CoinTransferPayload(coinType *TypeTag, dest AccountAddress, amount uint64) (payload *EntryFunction, err error) {
amountBytes, err := bcs.SerializeU64(amount)
if err != nil {
return nil, err
}
if coinType == nil || *coinType == AptosCoinTypeTag {
return &EntryFunction{
Module: ModuleId{
Address: AccountOne,
Name: "aptos_account",
},
Function: "transfer",
ArgTypes: []TypeTag{},
Args: [][]byte{
dest[:],
amountBytes,
},
}, nil
} else {
return &EntryFunction{
Module: ModuleId{
Address: AccountOne,
Name: "aptos_account",
},
Function: "transfer_coins",
ArgTypes: []TypeTag{*coinType},
Args: [][]byte{
dest[:],
amountBytes,
},
}, nil
}
}
// CoinBatchTransferPayload builds an EntryFunction payload for transferring coins to multiple receivers
//
// Args:
// - coinType is the type of coin to transfer. If none is provided, it will transfer 0x1::aptos_coin:AptosCoin
// - dests are the destination [AccountAddress]s
// - amounts are the amount of coins to transfer per destination
func CoinBatchTransferPayload(coinType *TypeTag, dests []AccountAddress, amounts []uint64) (payload *EntryFunction, err error) {
destBytes, err := bcs.SerializeSequenceOnly(dests)
if err != nil {
return nil, err
}
amountsBytes, err := bcs.SerializeSingle(func(ser *bcs.Serializer) {
bcs.SerializeSequenceWithFunction(amounts, ser, func(ser *bcs.Serializer, amount uint64) {
ser.U64(amount)
})
})
if err != nil {
return nil, err
}
if coinType == nil || *coinType == AptosCoinTypeTag {
return &EntryFunction{
Module: ModuleId{
Address: AccountOne,
Name: "aptos_account",
},
Function: "batch_transfer",
ArgTypes: []TypeTag{},
Args: [][]byte{
destBytes,
amountsBytes,
},
}, nil
} else {
return &EntryFunction{
Module: ModuleId{
Address: AccountOne,
Name: "aptos_account",
},
Function: "batch_transfer_coins",
ArgTypes: []TypeTag{*coinType},
Args: [][]byte{
destBytes,
amountsBytes,
},
}, nil
}
}