refactor: rpc provider

- provider now lives on its own
- existing modules need the provider as a param
This commit is contained in:
Mohamed Sohail 2022-05-18 12:50:45 +03:00
parent 7f771ccfb1
commit 3601b61c20
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
14 changed files with 167 additions and 118 deletions

View File

@ -2,31 +2,17 @@ package balance
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3" "github.com/grassrootseconomics/cic-go/provider"
) )
type BatchBalance struct { type BatchBalance struct {
ethClient *w3.Client provider *provider.Provider
batchContract common.Address batchContract common.Address
} }
func NewBatchBalance(rpcEndpoint string, batchContract common.Address) (*BatchBalance, error) { func NewBatchBalance(rpcProvider provider.Provider, batchContract common.Address) (*BatchBalance, error) {
ethClient, err := w3.Dial(rpcEndpoint)
if err != nil {
return nil, err
}
return &BatchBalance{ return &BatchBalance{
ethClient: ethClient, provider: &rpcProvider,
batchContract: batchContract, batchContract: batchContract,
}, nil }, nil
} }
func (c *BatchBalance) Close() error {
err := c.ethClient.Close()
if err != nil {
return err
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
) )
@ -21,10 +22,15 @@ func TestBatchBalance_Connect(t *testing.T) {
name := "Test RPC connection" name := "Test RPC connection"
wantErr := false 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) { 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) t.Errorf("Close() error = %v, wantErr %v", err, wantErr)
} }
}) })

View File

@ -12,7 +12,7 @@ import (
func (c *BatchBalance) TokensBalance(ctx context.Context, owner common.Address, tokens []common.Address) ([]*big.Int, error) { func (c *BatchBalance) TokensBalance(ctx context.Context, owner common.Address, tokens []common.Address) ([]*big.Int, error) {
var balancesResults []*big.Int var balancesResults []*big.Int
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("tokensBalance(address owner, address[] contracts)", "uint256[]"), c.batchContract, owner, tokens).Returns(&balancesResults), eth.CallFunc(w3.MustNewFunc("tokensBalance(address owner, address[] contracts)", "uint256[]"), c.batchContract, owner, tokens).Returns(&balancesResults),
) )

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3" "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 { if err != nil {
t.Fatalf("NewBatchBalance error = %v", err) t.Fatalf("NewBatchBalance error = %v", err)

View File

@ -1,50 +1,18 @@
package net package net
import ( import (
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
)
const (
kitabuMainnetChainId = 6060
) )
type CicNet struct { type CicNet struct {
ethClient *w3.Client provider *provider.Provider
tokenIndex common.Address tokenIndex common.Address
kitabuSigner types.Signer
} }
type WriteTx struct { func NewCicNet(rpcProvider provider.Provider, tokenIndex common.Address) (*CicNet, error) {
from common.Address
to common.Address
gasLimit uint64
nonce uint64
privateKey ecdsa.PrivateKey
}
func NewCicNet(rpcEndpoint string, tokenIndex common.Address) (*CicNet, error) {
ethClient, err := w3.Dial(rpcEndpoint)
if err != nil {
return nil, err
}
return &CicNet{ return &CicNet{
ethClient: ethClient, provider: &rpcProvider,
tokenIndex: tokenIndex, tokenIndex: tokenIndex,
kitabuSigner: types.NewEIP155Signer(big.NewInt(kitabuMainnetChainId)),
}, nil }, nil
} }
func (c *CicNet) Close() error {
err := c.ethClient.Close()
if err != nil {
return err
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
) )
@ -20,13 +21,18 @@ var conf = &tConfig{
} }
func TestCicNet_Connect(t *testing.T) { func TestCicNet_Connect(t *testing.T) {
name := "Test RPC connection" name := "Test CicNet connect"
wantErr := false wantErr := false
cicnet, _ := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil {
t.Errorf("Creating an rpc instance failed = %v", err)
}
cicnet, _ := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
if err := cicnet.Close(); (err != nil) != wantErr { if err := cicnet.provider.EthClient.Close(); (err != nil) != wantErr {
t.Errorf("Error() error = %v, wantErr %v", err, wantErr) t.Errorf("Error() error = %v, wantErr %v", err, wantErr)
} }
}) })

View File

@ -2,10 +2,12 @@ package net
import ( import (
"context" "context"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth" "github.com/lmittmann/w3/module/eth"
"math/big"
) )
type DemurrageTokenInfo struct { type DemurrageTokenInfo struct {
@ -34,7 +36,7 @@ func (c *CicNet) DemurrageTokenInfo(ctx context.Context, tokenAddress common.Add
redistributionCount big.Int redistributionCount big.Int
) )
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("demurrageAmount()", "uint128"), tokenAddress).Returns(&demurrageAmount), eth.CallFunc(w3.MustNewFunc("demurrageAmount()", "uint128"), tokenAddress).Returns(&demurrageAmount),
eth.CallFunc(w3.MustNewFunc("demurrageTimestamp()", "uint256"), tokenAddress).Returns(&demurrageTimestamp), eth.CallFunc(w3.MustNewFunc("demurrageTimestamp()", "uint256"), tokenAddress).Returns(&demurrageTimestamp),
@ -67,7 +69,7 @@ func (c *CicNet) DemurrageTokenInfo(ctx context.Context, tokenAddress common.Add
func (c *CicNet) BaseBalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) { func (c *CicNet) BaseBalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int var balance big.Int
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("baseBalanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance), eth.CallFunc(w3.MustNewFunc("baseBalanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
) )
@ -78,7 +80,7 @@ func (c *CicNet) BaseBalanceOf(ctx context.Context, tokenAddress common.Address,
return balance, nil return balance, nil
} }
func (c *CicNet) ChangePeriod(ctx context.Context, txData WriteTx) (common.Hash, error) { func (c *CicNet) ChangePeriod(ctx context.Context, txData provider.WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("changePeriod()", "bool") sig := w3.MustNewFunc("changePeriod()", "bool")
input, err := sig.EncodeArgs() input, err := sig.EncodeArgs()
if err != nil { if err != nil {
@ -93,7 +95,7 @@ func (c *CicNet) ChangePeriod(ctx context.Context, txData WriteTx) (common.Hash,
return txHash, nil return txHash, nil
} }
func (c *CicNet) ApplyDemurrageLimited(ctx context.Context, rounds int64, txData WriteTx) (common.Hash, error) { func (c *CicNet) ApplyDemurrageLimited(ctx context.Context, rounds int64, txData provider.WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("applyDemurrageLimited(uint256 _rounds)", "bool") sig := w3.MustNewFunc("applyDemurrageLimited(uint256 _rounds)", "bool")
input, err := sig.EncodeArgs(big.NewInt(rounds)) input, err := sig.EncodeArgs(big.NewInt(rounds))
if err != nil { if err != nil {

View File

@ -3,11 +3,13 @@ package net
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/lmittmann/w3"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
) )
func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) { func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
@ -44,7 +46,11 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil {
t.Errorf("Creating an rpc instance failed = %v", err)
}
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Fatalf("NewCicNet error = %v", err)
@ -109,11 +115,11 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Errorf("Creating an rpc instance failed = %v", err)
} }
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
@ -136,7 +142,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) { func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
type args struct { type args struct {
writeTx WriteTx writeTx provider.WriteTx
} }
// Bootstrap signer // Bootstrap signer
@ -151,10 +157,11 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
} }
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Errorf("Creating an rpc instance failed = %v", err)
} }
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
nonce, err := cicnet.LastNonce(context.Background(), fromAddress) nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil { if err != nil {
@ -170,12 +177,12 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
{ {
name: "ChangePeriod for Sarafu", name: "ChangePeriod for Sarafu",
args: args{ args: args{
writeTx: WriteTx{ writeTx: provider.WriteTx{
from: fromAddress, From: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"), To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1, Nonce: nonce + 1,
gasLimit: 12000000, GasLimit: 12000000,
privateKey: *privateKey, PrivateKey: *privateKey,
}, },
}, },
wantErr: false, wantErr: false,
@ -200,7 +207,7 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) { func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
type args struct { type args struct {
rounds int64 rounds int64
writeTx WriteTx writeTx provider.WriteTx
} }
// Bootstrap signer // Bootstrap signer
@ -215,10 +222,11 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
} }
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Errorf("Creating an rpc instance failed = %v", err)
} }
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
nonce, err := cicnet.LastNonce(context.Background(), fromAddress) nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil { if err != nil {
@ -235,12 +243,12 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
name: "ChangePeriod for Sarafu", name: "ChangePeriod for Sarafu",
args: args{ args: args{
rounds: 1000, rounds: 1000,
writeTx: WriteTx{ writeTx: provider.WriteTx{
from: fromAddress, From: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"), To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1, Nonce: nonce + 1,
gasLimit: 12000000, GasLimit: 12000000,
privateKey: *privateKey, PrivateKey: *privateKey,
}, },
}, },
wantErr: false, wantErr: false,

View File

@ -2,10 +2,11 @@ package net
import ( import (
"context" "context"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth" "github.com/lmittmann/w3/module/eth"
"math/big"
) )
type ERC20Token struct { type ERC20Token struct {
@ -21,7 +22,7 @@ func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address
tokenDecimals big.Int tokenDecimals big.Int
) )
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("name()", "string"), tokenAddress).Returns(&tokenName), eth.CallFunc(w3.MustNewFunc("name()", "string"), tokenAddress).Returns(&tokenName),
eth.CallFunc(w3.MustNewFunc("symbol()", "string"), tokenAddress).Returns(&tokenSymbol), eth.CallFunc(w3.MustNewFunc("symbol()", "string"), tokenAddress).Returns(&tokenSymbol),
@ -40,7 +41,7 @@ func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address
func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) { func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int var balance big.Int
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("balanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance), eth.CallFunc(w3.MustNewFunc("balanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
) )

View File

@ -2,10 +2,12 @@ package net
import ( import (
"context" "context"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
) )
func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) { func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
@ -37,7 +39,11 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil {
t.Errorf("Creating an rpc instance failed = %v", err)
}
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Fatalf("NewCicNet error = %v", err)
@ -92,11 +98,11 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Errorf("Creating an rpc instance failed = %v", err)
} }
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase

View File

@ -2,16 +2,17 @@ package net
import ( import (
"context" "context"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth" "github.com/lmittmann/w3/module/eth"
"math/big"
) )
func (c *CicNet) EntryCount(ctx context.Context) (big.Int, error) { func (c *CicNet) EntryCount(ctx context.Context) (big.Int, error) {
var tokenCount big.Int var tokenCount big.Int
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), c.tokenIndex).Returns(&tokenCount), eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), c.tokenIndex).Returns(&tokenCount),
) )
@ -25,7 +26,7 @@ func (c *CicNet) EntryCount(ctx context.Context) (big.Int, error) {
func (c *CicNet) AddressAtIndex(ctx context.Context, index *big.Int) (string, error) { func (c *CicNet) AddressAtIndex(ctx context.Context, index *big.Int) (string, error) {
var address common.Address var address common.Address
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.CallFunc(w3.MustNewFunc("entry(uint256 _idx)", "address"), c.tokenIndex, index).Returns(&address), eth.CallFunc(w3.MustNewFunc("entry(uint256 _idx)", "address"), c.tokenIndex, index).Returns(&address),
) )

View File

@ -2,23 +2,29 @@ package net
import ( import (
"context" "context"
"github.com/lmittmann/w3"
"math/big" "math/big"
"testing" "testing"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
) )
func TestCicNet_TokenIndex_EntryCount(t *testing.T) { func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
name := "Entry count" name := "Entry count"
wantErr := false wantErr := false
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil {
t.Errorf("Creating an rpc instance failed = %v", err)
}
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Fatalf("NewCicNet error = %v", err)
} }
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
got, err := tokenIndex.EntryCount(context.Background()) got, err := cicnet.EntryCount(context.Background())
if (err != nil) != wantErr { if (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr) t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
@ -59,7 +65,11 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
}, },
} }
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex)) newProvider, err := provider.NewRpcProvider(conf.rpcProvider)
if err != nil {
t.Errorf("Creating an rpc instance failed = %v", err)
}
cicnet, err := NewCicNet(*newProvider, w3.A(conf.tokenIndex))
if err != nil { if err != nil {
t.Fatalf("NewCicNet error = %v", err) t.Fatalf("NewCicNet error = %v", err)
@ -69,7 +79,7 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := tokenIndex.AddressAtIndex(context.Background(), tt.args.index) got, err := cicnet.AddressAtIndex(context.Background(), tt.args.index)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("AddressAtIndex() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("AddressAtIndex() error = %v, wantErr %v", err, tt.wantErr)

View File

@ -2,16 +2,18 @@ package net
import ( import (
"context" "context"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3/module/eth" "github.com/lmittmann/w3/module/eth"
"math/big"
) )
func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64, error) { func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64, error) {
var nonce uint64 var nonce uint64
err := c.ethClient.CallCtx( err := c.provider.EthClient.CallCtx(
ctx, ctx,
eth.Nonce(address, nil).Returns(&nonce), eth.Nonce(address, nil).Returns(&nonce),
) )
@ -22,18 +24,18 @@ func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64,
return nonce, nil return nonce, nil
} }
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData WriteTx) (common.Hash, error) { func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData provider.WriteTx) (common.Hash, error) {
var txHash common.Hash var txHash common.Hash
tx, err := types.SignNewTx(&txData.privateKey, c.kitabuSigner, &types.LegacyTx{ tx, err := types.SignNewTx(&txData.PrivateKey, c.provider.Signer, &types.LegacyTx{
To: &txData.to, To: &txData.To,
Nonce: txData.nonce, Nonce: txData.Nonce,
Data: input, Data: input,
Gas: txData.gasLimit, Gas: txData.GasLimit,
GasPrice: big.NewInt(1), GasPrice: big.NewInt(1),
}) })
err = c.ethClient.CallCtx( err = c.provider.EthClient.CallCtx(
ctx, ctx,
eth.SendTransaction(tx).Returns(&txHash), eth.SendTransaction(tx).Returns(&txHash),
) )

48
provider/provider.go Normal file
View File

@ -0,0 +1,48 @@
package provider
import (
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3"
)
const (
kitabuMainnetChainId = 6060
)
type Provider struct {
EthClient *w3.Client
Signer types.Signer
}
type WriteTx struct {
From common.Address
To common.Address
GasLimit uint64
Nonce uint64
PrivateKey ecdsa.PrivateKey
}
func NewRpcProvider(rpcEndpoint string) (*Provider, error) {
ethClient, err := w3.Dial(rpcEndpoint)
if err != nil {
return nil, err
}
return &Provider{
EthClient: ethClient,
Signer: types.NewEIP155Signer(big.NewInt(kitabuMainnetChainId)),
}, nil
}
func (c *Provider) CLose() error {
err := c.EthClient.Close()
if err != nil {
return err
}
return nil
}