Skip to content

Commit bb195ad

Browse files
Merge pull request #673 from ava-labs/wallet-client
Wallet client
2 parents db84499 + b1bfe78 commit bb195ad

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

vms/avm/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ func (c *Client) GetTxStatus(txID ids.ID) (choices.Status, error) {
5151
return res.Status, err
5252
}
5353

54+
// ConfirmTx attempts to confirm [txID] by checking its status [attempts] times
55+
// with a [delay] in between each attempt. If the transaction has not been decided
56+
// by the final attempt, it returns the status of the last attempt.
57+
// Note: ConfirmTx will block until either the last attempt finishes or the client
58+
// returns a decided status.
59+
func (c *Client) ConfirmTx(txID ids.ID, attempts int, delay time.Duration) (choices.Status, error) {
60+
for i := 0; i < attempts-1; i++ {
61+
status, err := c.GetTxStatus(txID)
62+
if err != nil {
63+
return status, err
64+
}
65+
66+
if status.Decided() {
67+
return status, nil
68+
}
69+
time.Sleep(delay)
70+
}
71+
72+
return c.GetTxStatus(txID)
73+
}
74+
5475
// GetTx returns the byte representation of [txID]
5576
func (c *Client) GetTx(txID ids.ID) ([]byte, error) {
5677
res := &api.FormattedTx{}

vms/avm/wallet_client.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)