-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider_test.go
98 lines (78 loc) · 4.14 KB
/
provider_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
92
93
94
95
96
97
98
package eth
import (
"context"
"math/big"
"os"
"testing"
"time"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/ethereum/go-ethereum/accounts"
"github.com/caarlos0/env/v6"
)
var (
TestAccountPath_A = accounts.DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 7}
TestAccountPath_B = accounts.DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 8}
TestAccountPath_C = accounts.DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 9}
)
type TestConfig struct {
Ethurl string `env:"ETHEREUM_JSONRPC_URL" envDefault:"http://localhost:8545"`
Mnemonic string `env:"ETHEREUM_MNEMONIC" envDefault:"concert load couple harbor equip island argue ramp clarify fence smart topic"`
}
func TestProvider(t *testing.T) {
if os.Getenv("ETHEREUM_JSONRPC_URL") == "" {
t.Skip("Set ETHEREUM_JSONRPC_URL env var in order to run this test")
}
cfg := TestConfig{}
if err := env.Parse(&cfg); err != nil {
t.Fatal(err)
}
// setup provider test instance
provider, err := NewProvider(cfg.Ethurl, cfg.Mnemonic, TestAccountPath_A)
require.NoError(t, err, "should have no error creating new provider")
// run test runners
t.Run("derive accounts", func(t *testing.T) { testDeriveAccounts(provider, t) })
t.Run("transfer ether", func(t *testing.T) { testTransferEther(provider, t) })
}
func testDeriveAccounts(provider *Provider, t *testing.T) {
assert.Equal(t, 1, len(provider.Accounts()), "should have one account available after construction")
require.NoError(t, provider.Derive(TestAccountPath_B))
assert.Equal(t, 2, len(provider.Accounts()), "should have two accounts available after deriving another")
}
func testTransferEther(provider *Provider, t *testing.T) {
fromAccount := provider.Accounts()[0]
toAccount := provider.Accounts()[1]
// fetch before balances
fromAccountBeforeBalance, err := provider.eth.BalanceAt(context.Background(), fromAccount.Address, nil)
require.NoError(t, err, "should be no error getting balance")
toAccountBeforeBalance, err := provider.eth.BalanceAt(context.Background(), toAccount.Address, nil)
require.NoError(t, err, "should be no error getting balance")
// prep transaction
transferAmt, ok := new(big.Int).SetString("10000000000000000000", 10) // 1e19 wei (10 ether)
assert.True(t, ok, "should be able to set transfer amount as 10 ether")
nonce, err := provider.Nonce(context.Background(), fromAccount.Address)
require.NoError(t, err, "should be no error getting nonce")
expectedNonce, err := provider.eth.NonceAt(context.Background(), fromAccount.Address, nil)
require.NoError(t, err, "should be no error getting nonce")
assert.Equal(t, expectedNonce, nonce, "nonce should be 0 before any transactions are submitter")
tx := types.NewTransaction(nonce, toAccount.Address, transferAmt, 21000, big.NewInt(1), nil)
// sign transaction
stx, err := provider.SignTx(context.Background(), fromAccount, tx)
require.NoError(t, err, "should be no error signing transaction")
// send transaction
err = provider.eth.SendTransaction(context.Background(), stx)
require.NoError(t, err, "should be no error sending transaction")
// wait for a block then get post-transfer balances
time.Sleep(1 * time.Second)
fromAccountAfterBalance, err := provider.eth.BalanceAt(context.Background(), fromAccount.Address, nil)
require.NoError(t, err, "should be no error getting balance")
toAccountAfterBalance, err := provider.eth.BalanceAt(context.Background(), toAccount.Address, nil)
require.NoError(t, err, "should be no error getting balance")
// get balance diffs and assert logical differences
fromAccountDiff := new(big.Int).Sub(fromAccountAfterBalance, fromAccountBeforeBalance)
toAccountDiff := new(big.Int).Sub(toAccountAfterBalance, toAccountBeforeBalance)
require.Equal(t, -1, fromAccountDiff.Cmp(new(big.Int).Sub(fromAccountBeforeBalance, transferAmt)), "from account balance should be < (before - amt)")
require.Equal(t, new(big.Int).Add(toAccountBeforeBalance, transferAmt), toAccountAfterBalance, "to account should be before + amt")
require.Equal(t, transferAmt, toAccountDiff, "to account diff should be transfer amount")
}