-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathtransaction_test.go
79 lines (60 loc) · 1.63 KB
/
transaction_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
package e2e
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/testutil"
"github.com/umbracle/ethgo/wallet"
)
func TestSendSignedTransaction(t *testing.T) {
s := testutil.NewTestServer(t)
key, err := wallet.GenerateKey()
assert.NoError(t, err)
// add value to the new key
value := big.NewInt(1000000000000000000)
s.Transfer(key.Address(), value)
c, _ := jsonrpc.NewClient(s.HTTPAddr())
found, _ := c.Eth().GetBalance(key.Address(), ethgo.Latest)
assert.Equal(t, found, value)
chainID, err := c.Eth().ChainID()
assert.NoError(t, err)
// send a signed transaction
to := ethgo.Address{0x1}
transferVal := big.NewInt(1000)
gasPrice, err := c.Eth().GasPrice()
assert.NoError(t, err)
txn := ðgo.Transaction{
To: &to,
Value: transferVal,
Nonce: 0,
GasPrice: gasPrice,
}
{
msg := ðgo.CallMsg{
From: key.Address(),
To: &to,
Value: transferVal,
GasPrice: gasPrice,
}
limit, err := c.Eth().EstimateGas(msg)
assert.NoError(t, err)
txn.Gas = limit
}
signer := wallet.NewEIP155Signer(chainID.Uint64())
txn, err = signer.SignTx(txn, key)
assert.NoError(t, err)
from, err := signer.RecoverSender(txn)
assert.NoError(t, err)
assert.Equal(t, from, key.Address())
data, err := txn.MarshalRLPTo(nil)
assert.NoError(t, err)
hash, err := c.Eth().SendRawTransaction(data)
assert.NoError(t, err)
_, err = s.WaitForReceipt(hash)
assert.NoError(t, err)
balance, err := c.Eth().GetBalance(to, ethgo.Latest)
assert.NoError(t, err)
assert.Equal(t, balance, transferVal)
}