-
Notifications
You must be signed in to change notification settings - Fork 1
/
aptos.go
93 lines (75 loc) · 2.01 KB
/
aptos.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 (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
type AptosClient struct {
nodeURL string
}
func NewAptosClient(nodeURL string) *AptosClient {
return &AptosClient{
nodeURL: nodeURL,
}
}
func (ac *AptosClient) AccountBalance(address string) (int64, error) {
res, err := ac.AccountResourceByType(address, "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>", "")
if err != nil {
return -1, err
}
v := res.Data["coin"].(map[string]interface{})
return strconv.ParseInt(v["value"].(string), 10, 0)
}
func (ac *AptosClient) SendCoins(account *AptosAccount, destAddr string, amount int64) (*Transaction, error) {
payload := &ScriptFunctionPayload{
Type: "script_function_payload",
Function: "0x1::coin::transfer",
TypeArguments: []string{"0x1::aptos_coin::AptosCoin"},
Arguments: []string{destAddr, strconv.FormatInt(amount, 10)},
}
unsignedTx, err := ac.GenerateTransaction(account, payload)
if err != nil {
return nil, err
}
signedTx, err := ac.SignTransaction(account, unsignedTx)
if err != nil {
return nil, err
}
return ac.SubmitTransaction(signedTx)
}
func (ac *AptosClient) SendCoinsSync(account *AptosAccount, destAddr string, amount int64) (*Transaction, error) {
tx, err := ac.SendCoins(account, destAddr, amount)
if err != nil {
return nil, err
}
err = ac.WaitForTransaction(tx.Hash)
if err != nil {
return nil, err
}
return tx, nil
}
//TODO: decimal places
func (ac *AptosClient) FundFromFaucet(faucetURL, address string, amount int64) error {
url := fmt.Sprintf("%s/mint?amount=%d&address=%s", faucetURL, amount, address)
resp, err := http.DefaultClient.Post(url, "application/json", nil)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(string(body))
}
var txnHashes []string
err = json.Unmarshal(body, &txnHashes)
if err != nil {
return err
}
return ac.WaitForTransaction(txnHashes[0])
}