mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2026-05-27 09:17:57 +02:00
refactor: rpc provider (#6)
- provider now lives on its own - existing modules need the provider as a param
This commit is contained in:
@@ -2,31 +2,17 @@ package balance
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/lmittmann/w3"
|
||||
"github.com/grassrootseconomics/cic-go/provider"
|
||||
)
|
||||
|
||||
type BatchBalance struct {
|
||||
ethClient *w3.Client
|
||||
provider *provider.Provider
|
||||
batchContract common.Address
|
||||
}
|
||||
|
||||
func NewBatchBalance(rpcEndpoint string, batchContract common.Address) (*BatchBalance, error) {
|
||||
ethClient, err := w3.Dial(rpcEndpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func NewBatchBalance(rpcProvider provider.Provider, batchContract common.Address) (*BatchBalance, error) {
|
||||
return &BatchBalance{
|
||||
ethClient: ethClient,
|
||||
provider: &rpcProvider,
|
||||
batchContract: batchContract,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *BatchBalance) Close() error {
|
||||
err := c.ethClient.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/grassrootseconomics/cic-go/provider"
|
||||
"github.com/lmittmann/w3"
|
||||
)
|
||||
|
||||
@@ -21,10 +22,15 @@ func TestBatchBalance_Connect(t *testing.T) {
|
||||
name := "Test RPC connection"
|
||||
wantErr := false
|
||||
|
||||
cicnet, _ := NewBatchBalance(conf.rpcProvider, w3.A(conf.batchContract))
|
||||
newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
|
||||
if err != nil {
|
||||
t.Errorf("Creating an rpc instance failed = %v", err)
|
||||
}
|
||||
|
||||
batchBalance, _ := NewBatchBalance(*newProvider, w3.A(conf.batchContract))
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := cicnet.Close(); (err != nil) != wantErr {
|
||||
if err := batchBalance.provider.EthClient.Close(); (err != nil) != wantErr {
|
||||
t.Errorf("Close() error = %v, wantErr %v", err, wantErr)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
func (c *BatchBalance) TokensBalance(ctx context.Context, owner common.Address, tokens []common.Address) ([]*big.Int, error) {
|
||||
var balancesResults []*big.Int
|
||||
|
||||
err := c.ethClient.CallCtx(
|
||||
err := c.provider.EthClient.CallCtx(
|
||||
ctx,
|
||||
eth.CallFunc(w3.MustNewFunc("tokensBalance(address owner, address[] contracts)", "uint256[]"), c.batchContract, owner, tokens).Returns(&balancesResults),
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/grassrootseconomics/cic-go/provider"
|
||||
"github.com/lmittmann/w3"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,11 @@ func TestBatchBalance_TokensBalance(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
batchBalance, err := NewBatchBalance(conf.rpcProvider, w3.A(conf.batchContract))
|
||||
newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
|
||||
if err != nil {
|
||||
t.Errorf("Creating an rpc instance failed = %v", err)
|
||||
}
|
||||
batchBalance, err := NewBatchBalance(*newProvider, w3.A(conf.batchContract))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("NewBatchBalance error = %v", err)
|
||||
|
||||
Reference in New Issue
Block a user