-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount_test.go
91 lines (77 loc) · 2.36 KB
/
account_test.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
package base_example
import (
"context"
"fmt"
"github.com/motoko9/aptos-go/aptos"
"github.com/motoko9/aptos-go/rpc"
"github.com/motoko9/aptos-go/wallet"
"testing"
"time"
)
func TestNewExampleAccount(t *testing.T) {
// new account
exampleWallet := wallet.New()
exampleWallet.SaveToKeygenFile("account_example")
exampleAddress := exampleWallet.Address()
fmt.Printf("example address: %s\n", exampleAddress)
// new rpc
client := aptos.New(rpc.TestNet_RPC, false)
//
faultWallet, err := wallet.NewFromKeygenFile("./../account_fault")
if err != nil {
panic(err)
}
faultAddress := faultWallet.Address()
fmt.Printf("fault address: %s\n", faultAddress)
fmt.Printf("fault public key: %s\n", faultWallet.PublicKey().String())
fmt.Printf("fault private key: %s\n", faultWallet.PrivateKey.String())
// create account on aptos
txHash, aptosErr := client.CreateAccount(context.Background(), faultAddress, exampleAddress, faultWallet)
if aptosErr != nil {
panic(err)
}
fmt.Printf("create example account transacion: %s\n", txHash)
//
time.Sleep(time.Second * 5)
// fund
txHash, aptosErr = client.TransferCoin(context.Background(), faultAddress, "0x1::aptos_coin::AptosCoin", 100000000, exampleAddress, faultWallet)
if aptosErr != nil {
panic(err)
}
fmt.Printf("fund example account transacion: %s\n", txHash)
//
time.Sleep(time.Second * 5)
// latest ledger
ledger, aptosErr := client.Ledger(context.Background())
if aptosErr != nil {
panic(aptosErr)
}
// check account
balance, aptosErr := client.AccountBalance(context.Background(), exampleAddress, aptos.CoinAlias("APT", "aptos"), ledger.LedgerVersion)
if aptosErr != nil {
panic(aptosErr)
}
fmt.Printf("account balance: %d\n", balance)
}
func TestReadExampleAccount(t *testing.T) {
// new account
exampleWallet, err := wallet.NewFromKeygenFile("account_example")
if err != nil {
panic(err)
}
exampleAddress := exampleWallet.Address()
fmt.Printf("example address: %s\n", exampleAddress)
// new rpc
client := aptos.New(rpc.TestNet_RPC, false)
// latest ledger
ledger, aptosErr := client.Ledger(context.Background())
if aptosErr != nil {
panic(aptosErr)
}
// check account
balance, aptosErr := client.AccountBalance(context.Background(), exampleAddress, aptos.CoinAlias("APT", "aptos"), ledger.LedgerVersion)
if aptosErr != nil {
panic(aptosErr)
}
fmt.Printf("account balance: %d\n", balance)
}