|
| 1 | +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package avm |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanchego/api" |
| 11 | + "github.com/ava-labs/avalanchego/ids" |
| 12 | + "github.com/ava-labs/avalanchego/utils/formatting" |
| 13 | + cjson "github.com/ava-labs/avalanchego/utils/json" |
| 14 | + "github.com/ava-labs/avalanchego/utils/rpc" |
| 15 | +) |
| 16 | + |
| 17 | +// WalletClient ... |
| 18 | +type WalletClient struct { |
| 19 | + requester rpc.EndpointRequester |
| 20 | +} |
| 21 | + |
| 22 | +// NewWalletClient returns an AVM wallet client for interacting with avm managed wallet on [chain] |
| 23 | +func NewWalletClient(uri, chain string, requestTimeout time.Duration) *WalletClient { |
| 24 | + return &WalletClient{ |
| 25 | + requester: rpc.NewEndpointRequester(uri, fmt.Sprintf("/ext/bc/%s/wallet", chain), "wallet", requestTimeout), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// IssueTx issues a transaction to a node and returns the TxID |
| 30 | +func (c *WalletClient) IssueTx(txBytes []byte) (ids.ID, error) { |
| 31 | + txStr, err := formatting.Encode(formatting.Hex, txBytes) |
| 32 | + if err != nil { |
| 33 | + return ids.ID{}, err |
| 34 | + } |
| 35 | + res := &api.JSONTxID{} |
| 36 | + err = c.requester.SendRequest("issueTx", &api.FormattedTx{ |
| 37 | + Tx: txStr, |
| 38 | + Encoding: formatting.Hex, |
| 39 | + }, res) |
| 40 | + return res.TxID, err |
| 41 | +} |
| 42 | + |
| 43 | +// Send [amount] of [assetID] to address [to] |
| 44 | +func (c *WalletClient) Send( |
| 45 | + user api.UserPass, |
| 46 | + from []string, |
| 47 | + changeAddr string, |
| 48 | + amount uint64, |
| 49 | + assetID, |
| 50 | + to, |
| 51 | + memo string, |
| 52 | +) (ids.ID, error) { |
| 53 | + res := &api.JSONTxID{} |
| 54 | + err := c.requester.SendRequest("send", &SendArgs{ |
| 55 | + JSONSpendHeader: api.JSONSpendHeader{ |
| 56 | + UserPass: user, |
| 57 | + JSONFromAddrs: api.JSONFromAddrs{From: from}, |
| 58 | + JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr}, |
| 59 | + }, |
| 60 | + SendOutput: SendOutput{ |
| 61 | + Amount: cjson.Uint64(amount), |
| 62 | + AssetID: assetID, |
| 63 | + To: to, |
| 64 | + }, |
| 65 | + Memo: memo, |
| 66 | + }, res) |
| 67 | + return res.TxID, err |
| 68 | +} |
| 69 | + |
| 70 | +// SendMultiple sends a transaction from [user] funding all [outputs] |
| 71 | +func (c *WalletClient) SendMultiple( |
| 72 | + user api.UserPass, |
| 73 | + from []string, |
| 74 | + changeAddr string, |
| 75 | + outputs []SendOutput, |
| 76 | + memo string, |
| 77 | +) (ids.ID, error) { |
| 78 | + res := &api.JSONTxID{} |
| 79 | + err := c.requester.SendRequest("sendMultiple", &SendMultipleArgs{ |
| 80 | + JSONSpendHeader: api.JSONSpendHeader{ |
| 81 | + UserPass: user, |
| 82 | + JSONFromAddrs: api.JSONFromAddrs{From: from}, |
| 83 | + JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr}, |
| 84 | + }, |
| 85 | + Outputs: outputs, |
| 86 | + Memo: memo, |
| 87 | + }, res) |
| 88 | + return res.TxID, err |
| 89 | +} |
0 commit comments