aboutsummaryrefslogtreecommitdiff
path: root/examples/client/main.go
blob: 5f61e6a9a619d7e756cb1edfa984b73aeabc369c (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main

import (
	"context"
	"fmt"
	"time"

	"math/big"

	"github.com/ava-labs/avalanchego/utils/crypto"
	"github.com/ava-labs/avalanchego/utils/formatting"
	"github.com/ava-labs/coreth/ethclient"
	"github.com/ava-labs/coreth/plugin/evm"

	"github.com/ava-labs/coreth"
	"github.com/ava-labs/coreth/core/types"
	"github.com/ethereum/go-ethereum/common"
)

var (
	key                = "ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
	prefixedPrivateKey = fmt.Sprintf("PrivateKey-%s", key)
	ipAddr             = "127.0.0.1"
	port               = 9650
)

type ethWSAPITestExecutor struct {
	uri            string
	requestTimeout time.Duration
}

// ExecuteTest ...
func (e *ethWSAPITestExecutor) ExecuteTest() error {
	client, err := ethclient.Dial(e.uri)
	if err != nil {
		return fmt.Errorf("Failed to create ethclient: %w", err)
	}
	fmt.Printf("Created ethclient\n")

	ctx := context.Background()

	cb58 := formatting.CB58{}
	factory := crypto.FactorySECP256K1R{}
	_ = cb58.FromString(key)
	pk, _ := factory.ToPrivateKey(cb58.Bytes)
	secpKey := pk.(*crypto.PrivateKeySECP256K1R)
	ethAddr := evm.GetEthAddress(secpKey)

	if err := testSubscription(ctx, client); err != nil {
		return fmt.Errorf("Subscription Test failed: %w", err)
	}

	if err := testHeaderAndBlockCalls(ctx, client, ethAddr); err != nil {
		return fmt.Errorf("HeaderAndBlockCalls Test failed: %w", err)
	}

	return nil
}

type ethRPCAPITestExecutor struct {
	uri            string
	requestTimeout time.Duration
}

// ExecuteTest ...
func (e *ethRPCAPITestExecutor) ExecuteTest() error {
	client, err := ethclient.Dial(e.uri)
	if err != nil {
		return fmt.Errorf("Failed to create ethclient: %w", err)
	}
	fmt.Printf("Created ethclient\n")

	ctx := context.Background()

	cb58 := formatting.CB58{}
	factory := crypto.FactorySECP256K1R{}
	_ = cb58.FromString(key)
	pk, _ := factory.ToPrivateKey(cb58.Bytes)
	secpKey := pk.(*crypto.PrivateKeySECP256K1R)
	ethAddr := evm.GetEthAddress(secpKey)

	if err := testHeaderAndBlockCalls(ctx, client, ethAddr); err != nil {
		return fmt.Errorf("HeaderAndBlockCalls Test failed: %w", err)
	}

	return nil
}

func testSubscription(ctx context.Context, client *ethclient.Client) error {
	headerChan := make(chan *types.Header)
	subscription, err := client.SubscribeNewHead(ctx, headerChan)
	if err != nil {
		return fmt.Errorf("Failed to create subscription: %s", err)
	}
	fmt.Printf("Created subscription: %s\n", subscription)

	suggestedGasPrice, err := client.SuggestGasPrice(ctx)
	if err != nil {
		return fmt.Errorf("Failed to get suggested gas price: %s", err)
	}
	fmt.Printf("Suggested gas price: %d\n", suggestedGasPrice.Uint64())

	logChan := make(chan types.Log)
	query := coreth.FilterQuery{
		BlockHash: nil,
		FromBlock: nil,
		ToBlock:   nil,
		Addresses: []common.Address{},
		Topics:    [][]common.Hash{},
	}
	subscription, err = client.SubscribeFilterLogs(ctx, query, logChan)
	if err != nil {
		return fmt.Errorf("Failed to create subscription: %s", err)
	}
	fmt.Printf("Created subscription: %s\n", subscription)

	return nil
}

func testHeaderAndBlockCalls(ctx context.Context, client *ethclient.Client, ethAddr common.Address) error {
	// Test Header and Block ByNumber work for special cases
	for i := 0; i > -3; i-- {
		if err := checkHeaderAndBlocks(ctx, client, i, ethAddr); err != nil {
			return err
		}
	}

	return nil
}

func checkHeaderAndBlocks(ctx context.Context, client *ethclient.Client, i int, ethAddr common.Address) error {
	fmt.Printf("Checking HeaderAndBlocks for i = %d\n", i)

	header1, err := client.HeaderByNumber(ctx, big.NewInt(int64(i)))
	if err != nil {
		return fmt.Errorf("Failed to retrieve HeaderByNumber: %w", err)
	}
	fmt.Printf("HeaderByNumber (Block Number: %d, Block Hash: %s)\n", header1.Number, header1.Hash().Hex())

	originalHash := header1.Hash()
	originalBlockNumber := header1.Number
	if i >= 0 && int(originalBlockNumber.Int64()) != i {
		return fmt.Errorf("Requested block number %d, but found block number %d", i, originalBlockNumber)
	}

	header2, err := client.HeaderByHash(ctx, header1.Hash())
	if err != nil {
		return fmt.Errorf("Failed to retrieve HeaderByHash: %w", err)
	}
	fmt.Printf("HeaderByNumber (Block Number: %d, Block Hash: %s)\n", header2.Number, header2.Hash().Hex())

	if originalHash.Hex() != header2.Hash().Hex() {
		return fmt.Errorf("Expected HeaderByHash with Hash: %s, but found: %s", originalHash.Hex(), header2.Hash().Hex())
	}

	if originalBlockNumber.Cmp(header2.Number) != 0 {
		return fmt.Errorf("Expected HeaderByHash with Number: %d, but found %d", originalBlockNumber, header2.Number)
	}

	block1, err := client.BlockByNumber(ctx, big.NewInt(int64(i)))
	if err != nil {
		return fmt.Errorf("Failed to retrieve BlockByNumber: %w", err)
	}
	header3 := block1.Header()
	fmt.Printf("BlockByNumber (Block Number: %d, Block Hash: %s)\n", header3.Number, header3.Hash().Hex())

	if originalHash.Hex() != header3.Hash().Hex() {
		return fmt.Errorf("Expected HeaderByHash with Hash: %s, but found: %s", originalHash.Hex(), header3.Hash().Hex())
	}

	if originalBlockNumber.Cmp(header3.Number) != 0 {
		return fmt.Errorf("Expected HeaderByHash with Number: %d, but found %d", originalBlockNumber, header3.Number)
	}

	block2, err := client.BlockByHash(ctx, header1.Hash())
	if err != nil {
		return fmt.Errorf("Failed to retrieve BlockByHash: %w", err)
	}
	header4 := block2.Header()
	fmt.Printf("BlockByHash (Block Number: %d, Block Hash: %s)\n", header4.Number, header4.Hash().Hex())
	if originalHash.Hex() != header4.Hash().Hex() {
		return fmt.Errorf("Expected HeaderByHash with Hash: %s, but found: %s", originalHash.Hex(), header4.Hash().Hex())
	}

	if originalBlockNumber.Cmp(header4.Number) != 0 {
		return fmt.Errorf("Expected HeaderByHash with Number: %d, but found %d", originalBlockNumber, header4.Number)
	}

	balance, err := client.BalanceAt(ctx, ethAddr, big.NewInt(int64(i)))
	if err != nil {
		return fmt.Errorf("Failed to get balance: %s", err)
	}
	fmt.Printf("Balance: %d for address: %s\n", balance, ethAddr.Hex())

	return nil
}

func main() {
	wsURI := fmt.Sprintf("ws://%s:%d/ext/bc/C/ws", ipAddr, port)
	wsTest := &ethWSAPITestExecutor{
		uri:            wsURI,
		requestTimeout: 3 * time.Second,
	}
	if err := wsTest.ExecuteTest(); err != nil {
		fmt.Printf("WebSocket Test failed due to %s\n", err)
	} else {
		fmt.Printf("WebSocket Test succeeded!\n")
	}

	rpcURI := fmt.Sprintf("http://%s:%d/ext/bc/C/rpc", ipAddr, port)
	rpcTest := &ethRPCAPITestExecutor{
		uri:            rpcURI,
		requestTimeout: 3 * time.Second,
	}
	if err := rpcTest.ExecuteTest(); err != nil {
		fmt.Printf("RPC Test failed due to %s\n", err)
	} else {
		fmt.Printf("RPC Test succeeded!\n")
	}
}