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
/
client.go
422 lines (362 loc) · 11.9 KB
/
client.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
package cosmos
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
xc "github.com/jumpcrypto/crosschain"
// injectivecryptocodec "github.com/InjectiveLabs/sdk-go/chain/crypto/codec"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
// TxInput for Cosmos
type TxInput struct {
xc.TxInputEnvelope
AccountNumber uint64
Sequence uint64
GasLimit uint64
GasPrice float64
Memo string
FromPublicKey []byte
}
func (txInput *TxInput) SetPublicKey(publicKeyBytes xc.PublicKey) error {
txInput.FromPublicKey = publicKeyBytes
return nil
}
func (txInput *TxInput) SetPublicKeyFromStr(publicKeyStr string) error {
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyStr)
if err != nil {
return fmt.Errorf("invalid public key %v: %v", publicKeyStr, err)
}
return txInput.SetPublicKey(publicKeyBytes)
}
// NewTxInput returns a new Cosmos TxInput
func NewTxInput() *TxInput {
return &TxInput{
TxInputEnvelope: *xc.NewTxInputEnvelope(xc.DriverCosmos),
}
}
// Client for Cosmos
type Client struct {
Asset xc.ITask
Ctx client.Context
Prefix string
EstimateGasFunc xc.EstimateGasFunc
}
var _ xc.FullClientWithGas = &Client{}
// NewClient returns a new Client
func NewClient(cfgI xc.ITask) (*Client, error) {
asset := cfgI
cfg := cfgI.GetNativeAsset()
host := cfg.URL
httpClient, err := rpchttp.NewWithClient(
host,
"websocket",
&http.Client{
// Timeout: opts.Timeout,
// We override the transport layer with a custom implementation as
// there is an issue with the Cosmos SDK that causes it to
// incorrectly parse URLs.
Transport: newTransport(host, &http.Transport{}),
})
if err != nil {
panic(err)
}
cosmosCfg := MakeCosmosConfig()
cliCtx := client.Context{}.
WithClient(httpClient).
WithCodec(cosmosCfg.Marshaler).
WithTxConfig(cosmosCfg.TxConfig).
WithLegacyAmino(cosmosCfg.Amino).
WithInterfaceRegistry(cosmosCfg.InterfaceRegistry).
WithBroadcastMode("sync").
WithChainID(string(cfg.ChainIDStr))
return &Client{
Asset: asset,
Ctx: cliCtx,
Prefix: cfg.ChainPrefix,
EstimateGasFunc: nil,
}, nil
}
type transport struct {
remote string
proxy http.RoundTripper
}
func newTransport(remote string, proxy http.RoundTripper) *transport {
return &transport{
remote: remote,
proxy: proxy,
}
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
u, err := url.Parse(t.remote)
if err != nil {
return nil, err
}
req.URL = u
req.Host = u.Host
// Proxy request.
return t.proxy.RoundTrip(req)
}
// FetchTxInput returns tx input for a Cosmos tx
func (client *Client) FetchTxInput(ctx context.Context, from xc.Address, _ xc.Address) (xc.TxInput, error) {
txInput := NewTxInput()
account, err := client.GetAccount(ctx, from)
if err != nil || account == nil {
return txInput, fmt.Errorf("failed to get account data for %v: %v", from, err)
}
txInput.AccountNumber = account.GetAccountNumber()
txInput.Sequence = account.GetSequence()
gasPrice, err := client.EstimateGas(ctx)
if err != nil {
return txInput, fmt.Errorf("failed to estimate gas: %v", err)
}
txInput.GasPrice = gasPrice.UnmaskFloat64()
return txInput, nil
}
// SubmitTx submits a Cosmos tx
func (client *Client) SubmitTx(ctx context.Context, txInput xc.Tx) error {
tx := txInput.(*Tx)
txBytes, _ := tx.Serialize()
txID := tx.Hash()
res, err := client.Ctx.BroadcastTx(txBytes)
if err != nil {
return fmt.Errorf("failed to broadcast tx %v: %v", txID, err)
}
if res.Code != 0 {
return fmt.Errorf("tx %v failed code: %v, log: %v", txID, res.Code, res.RawLog)
}
return nil
}
// FetchTxInfo returns tx info for a Cosmos tx
func (client *Client) FetchTxInfo(ctx context.Context, txHash xc.TxHash) (xc.TxInfo, error) {
result := xc.TxInfo{
Fee: xc.AmountBlockchain{},
BlockIndex: 0,
BlockTime: 0,
Confirmations: 0,
}
hash, err := hex.DecodeString(string(txHash))
if err != nil {
return result, err
}
resultRaw, err := client.Ctx.Client.Tx(ctx, hash, false)
if err != nil {
return result, err
}
// log.Println(resultRaw)
blockResultRaw, err := client.Ctx.Client.Block(ctx, &resultRaw.Height)
if err != nil {
return result, err
}
abciInfo, err := client.Ctx.Client.ABCIInfo(ctx)
if err != nil {
return result, err
}
decoder := client.Ctx.TxConfig.TxDecoder()
decodedTx, err := decoder(resultRaw.Tx)
if err != nil {
return result, err
}
tx := &Tx{
CosmosTx: decodedTx,
CosmosTxEncoder: client.Ctx.TxConfig.TxEncoder(),
}
result.TxID = string(txHash)
result.ExplorerURL = client.Asset.GetNativeAsset().ExplorerURL + "/tx/" + result.TxID
tx.ParseTransfer()
// parse tx info - this should happen after ATA is set
// (in most cases it works also in case or error)
result.From = tx.From()
result.To = tx.To()
result.ContractAddress = tx.ContractAddress()
result.Amount = tx.Amount()
result.Fee = tx.Fee()
result.Sources = tx.Sources()
result.Destinations = tx.Destinations()
result.BlockIndex = resultRaw.Height
result.BlockTime = blockResultRaw.Block.Header.Time.Unix()
result.Confirmations = abciInfo.Response.LastBlockHeight - result.BlockIndex
if resultRaw.TxResult.Code != 0 {
result.Status = xc.TxStatusFailure
}
return result, nil
}
// GetAccount returns a Cosmos account
// Equivalent to client.Ctx.AccountRetriever.GetAccount(), but doesn't rely GetConfig()
func (client *Client) GetAccount(ctx context.Context, address xc.Address) (client.Account, error) {
_, err := types.GetFromBech32(string(address), client.Prefix)
if err != nil {
return nil, fmt.Errorf("bad address: '%v': %v", address, err)
}
res, err := authtypes.NewQueryClient(client.Ctx).Account(ctx, &authtypes.QueryAccountRequest{Address: string(address)})
if err != nil {
return nil, err
}
var acc authtypes.AccountI
if err := client.Ctx.InterfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
return nil, err
}
return acc, nil
}
func (client *Client) estimateGasFcd(ctx context.Context) (xc.AmountBlockchain, error) {
zero := xc.NewAmountBlockchainFromUint64(0)
asset := client.Asset
fdcURL := asset.GetNativeAsset().FcdURL
resp, err := http.Get(fdcURL + "/v1/txs/gas_prices")
if err != nil {
return zero, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return zero, err
}
prices := make(map[string]string)
err = json.Unmarshal(body, &prices)
if err != nil {
return zero, err
}
denom := asset.GetNativeAsset().ChainCoin
priceStr, ok := prices[denom]
if !ok {
return zero, fmt.Errorf("could not find %s in /gas_prices", denom)
}
gasPrice, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
return zero, err
}
multiplier := 1.0
if asset.GetNativeAsset().ChainGasMultiplier > 0 {
multiplier = asset.GetNativeAsset().ChainGasMultiplier
}
return xc.NewAmountBlockchainToMaskFloat64(gasPrice * multiplier), nil
}
// EstimateGas estimates gas price for a Cosmos chain
func (client *Client) EstimateGas(ctx context.Context) (xc.AmountBlockchain, error) {
// invoke EstimateGasFunc callback, if registered
if client.EstimateGasFunc != nil {
nativeAsset := client.Asset.GetNativeAsset().NativeAsset
res, err := client.EstimateGasFunc(nativeAsset)
if err != nil {
// continue with default implementation as fallback
} else {
return res, err
}
}
zero := xc.NewAmountBlockchainFromUint64(0)
if client.Asset.GetNativeAsset().FcdURL != "" {
return client.estimateGasFcd(ctx)
}
if client.Asset.GetNativeAsset().ChainGasPriceDefault > 0 {
return xc.NewAmountBlockchainToMaskFloat64(client.Asset.GetNativeAsset().ChainGasPriceDefault), nil
}
return zero, errors.New("not implemented")
}
// RegisterEstimateGasCallback registers a callback to get gas price
func (client *Client) RegisterEstimateGasCallback(fn xc.EstimateGasFunc) {
client.EstimateGasFunc = fn
}
// FetchBalance fetches balance for input asset for a Cosmos address
func (client *Client) FetchBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error) {
if isNativeAsset(client.Asset.GetNativeAsset()) {
return client.FetchNativeBalance(ctx, address)
}
_, err := types.GetFromBech32(client.Asset.GetNativeAsset().Contract, client.Prefix)
if err != nil {
// could be a custom denom. Try querying as a native balance.
return client.fetchBankModuleBalance(ctx, address, client.Asset)
}
return client.fetchContractBalance(ctx, address, client.Asset.GetAssetConfig().Contract)
}
func (client *Client) fetchContractBalance(ctx context.Context, address xc.Address, contractAddress string) (xc.AmountBlockchain, error) {
zero := xc.NewAmountBlockchainFromUint64(0)
_, err := types.GetFromBech32(string(address), client.Prefix)
if err != nil {
return zero, fmt.Errorf("bad address: '%v': %v", address, err)
}
input := json.RawMessage(`{"balance": {"address": "` + string(address) + `"}}`)
balResp, err := wasmtypes.NewQueryClient(client.Ctx).SmartContractState(ctx, &wasmtypes.QuerySmartContractStateRequest{
QueryData: wasmtypes.RawContractMessage(input),
Address: contractAddress,
})
if err != nil {
return zero, fmt.Errorf("failed to get token balance: '%v': %v", address, err)
}
type TokenBalance struct {
Balance string
}
var balResult TokenBalance
err = json.Unmarshal(balResp.Data.Bytes(), &balResult)
if err != nil {
return zero, fmt.Errorf("failed to parse token balance: '%v': %v", address, err)
}
balance := xc.NewAmountBlockchainFromStr(balResult.Balance)
return balance, nil
}
// FetchNativeBalance fetches account balance for a Cosmos address
func (client *Client) FetchNativeBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error) {
return client.fetchBankModuleBalance(ctx, address, client.Asset)
}
// Cosmos chains can have multiple native assets. This helper is necessary to query the
// native bank module for a given asset.
func (client *Client) fetchBankModuleBalance(ctx context.Context, address xc.Address, asset xc.ITask) (xc.AmountBlockchain, error) {
zero := xc.NewAmountBlockchainFromUint64(0)
_, err := types.GetFromBech32(string(address), client.Prefix)
if err != nil {
return zero, fmt.Errorf("bad address: '%v': %v", address, err)
}
denom := asset.GetNativeAsset().ChainCoin
if denom == "" {
if token, ok := asset.(*xc.TokenAssetConfig); ok {
if token.Contract != "" {
denom = token.Contract
}
}
}
if denom == "" {
return zero, fmt.Errorf("failed to account balance: no denom on asset %s.%s", asset.GetAssetConfig().Asset, asset.GetNativeAsset().NativeAsset)
}
queryClient := banktypes.NewQueryClient(client.Ctx)
balResp, err := queryClient.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: string(address),
Denom: denom,
})
if err != nil {
if strings.Contains(err.Error(), "invalid denom") {
// Some chains do not properly support getting balance by denom directly, but will support when getting all of the balances.
allBals, err := queryClient.AllBalances(ctx, &banktypes.QueryAllBalancesRequest{
Address: string(address),
Pagination: &query.PageRequest{
Limit: 100,
},
})
if err != nil {
return zero, fmt.Errorf("failed to get any account balance: '%v': %v", address, err)
}
for _, bal := range allBals.Balances {
if bal.Denom == denom {
return xc.AmountBlockchain(*bal.Amount.BigInt()), nil
}
}
}
return zero, fmt.Errorf("failed to get account balance: '%v': %v", address, err)
}
if balResp == nil || balResp.GetBalance() == nil {
return zero, fmt.Errorf("failed to get account balance: '%v': %v", address, err)
}
balance := balResp.GetBalance().Amount.BigInt()
return xc.AmountBlockchain(*balance), nil
}