This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.go
185 lines (158 loc) · 5.23 KB
/
builder.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
package cosmos
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
xc "github.com/jumpcrypto/crosschain"
)
// TxBuilder for Cosmos
type TxBuilder struct {
xc.TxBuilder
Asset xc.ITask
CosmosTxConfig client.TxConfig
CosmosTxBuilder client.TxBuilder
}
// NewTxBuilder creates a new Cosmos TxBuilder
func NewTxBuilder(asset xc.ITask) (xc.TxBuilder, error) {
cosmosCfg := MakeCosmosConfig()
return TxBuilder{
Asset: asset,
CosmosTxConfig: cosmosCfg.TxConfig,
CosmosTxBuilder: cosmosCfg.TxConfig.NewTxBuilder(),
}, nil
}
// NewTransfer creates a new transfer for an Asset, either native or token
func (txBuilder TxBuilder) NewTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
if isNativeAsset(txBuilder.Asset.GetAssetConfig()) {
return txBuilder.NewNativeTransfer(from, to, amount, input)
}
return txBuilder.NewTokenTransfer(from, to, amount, input)
}
// NewNativeTransfer creates a new transfer for a native asset
func (txBuilder TxBuilder) NewNativeTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
txInput := input.(*TxInput)
asset := txBuilder.Asset
amountInt := big.Int(amount)
if txInput.GasLimit == 0 {
txInput.GasLimit = 400_000
}
denom := asset.GetNativeAsset().ChainCoin
if token, ok := asset.(*xc.TokenAssetConfig); ok {
if token.Contract != "" {
denom = token.Contract
}
}
msgSend := &banktypes.MsgSend{
FromAddress: string(from),
ToAddress: string(to),
Amount: types.Coins{
{
Denom: denom,
Amount: types.NewIntFromBigInt(&amountInt),
},
},
}
return txBuilder.createTxWithMsg(from, to, amount, txInput, msgSend)
}
// NewTokenTransfer creates a new transfer for a token asset
func (txBuilder TxBuilder) NewTokenTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
txInput := input.(*TxInput)
asset := txBuilder.Asset
// Terra Classic: most tokens are actually native tokens
// in crosschain we can treat them interchangeably as native or non-native assets
// if contract isn't a valid address, they're native tokens
if isNativeAsset(asset.GetAssetConfig()) {
return txBuilder.NewNativeTransfer(from, to, amount, input)
}
if txInput.GasLimit == 0 {
txInput.GasLimit = 900_000
}
contractTransferMsg := fmt.Sprintf(`{"transfer": {"amount": "%s", "recipient": "%s"}}`, amount.String(), to)
msgSend := &wasmtypes.MsgExecuteContract{
Sender: string(from),
Contract: asset.GetAssetConfig().Contract,
Msg: wasmtypes.RawContractMessage(json.RawMessage(contractTransferMsg)),
}
return txBuilder.createTxWithMsg(from, to, amount, txInput, msgSend)
}
func accAddressFromBech32WithPrefix(address string, prefix string) ([]byte, error) {
if len(strings.TrimSpace(address)) == 0 {
return nil, errors.New("empty address string is not allowed")
}
addressBytes, err := types.GetFromBech32(address, prefix)
if err != nil {
return nil, err
}
err = types.VerifyAddressFormat(addressBytes)
if err != nil {
return nil, err
}
return addressBytes, nil
}
// createTxWithMsg creates a new Tx given Cosmos Msg
func (txBuilder TxBuilder) createTxWithMsg(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input *TxInput, msg types.Msg) (xc.Tx, error) {
asset := txBuilder.Asset
cosmosTxConfig := txBuilder.CosmosTxConfig
cosmosBuilder := txBuilder.CosmosTxBuilder
err := cosmosBuilder.SetMsgs(msg)
if err != nil {
return nil, err
}
_, err = accAddressFromBech32WithPrefix(string(from), asset.GetNativeAsset().ChainPrefix)
if err != nil {
return nil, err
}
_, err = accAddressFromBech32WithPrefix(string(to), asset.GetNativeAsset().ChainPrefix)
if err != nil {
return nil, err
}
cosmosBuilder.SetMemo(input.Memo)
cosmosBuilder.SetGasLimit(input.GasLimit)
cosmosBuilder.SetFeeAmount(types.Coins{
{
Denom: asset.GetNativeAsset().ChainCoin,
Amount: types.NewIntFromUint64(uint64(input.GasPrice * float64(input.GasLimit))),
},
})
sigMode := signingtypes.SignMode_SIGN_MODE_DIRECT
sigsV2 := []signingtypes.SignatureV2{
{
PubKey: getPublicKey(*asset.GetNativeAsset(), input.FromPublicKey),
Data: &signingtypes.SingleSignatureData{
SignMode: sigMode,
Signature: nil,
},
Sequence: input.Sequence,
},
}
err = cosmosBuilder.SetSignatures(sigsV2...)
if err != nil {
return nil, err
}
signerData := signing.SignerData{
AccountNumber: input.AccountNumber,
ChainID: asset.GetNativeAsset().ChainIDStr,
Sequence: input.Sequence,
}
sighashData, err := cosmosTxConfig.SignModeHandler().GetSignBytes(sigMode, signerData, cosmosBuilder.GetTx())
if err != nil {
return nil, err
}
sighash := getSighash(*asset.GetNativeAsset(), sighashData)
return &Tx{
CosmosTx: cosmosBuilder.GetTx(),
ParsedTransfers: []types.Msg{msg},
CosmosTxBuilder: cosmosBuilder,
CosmosTxEncoder: cosmosTxConfig.TxEncoder(),
SigsV2: sigsV2,
TxDataToSign: sighash,
}, nil
}