add: implement and test

changes to contract:
- returns 0 instead of false error status
- func return type is []big.Int
This commit is contained in:
Mohamed Sohail 2022-05-17 16:06:19 +03:00
parent 2c8c45ba17
commit c34e1444dc
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
10 changed files with 152 additions and 3 deletions

View File

@ -9,6 +9,7 @@ Go modules to access various parts of the cic stack
- meta (web2 metadata store)
- net (cic smart contracts)
- batch balance query (`0xb9e215B789e9Ec6643Ba4ff7b98EA219F38c6fE5`)
## Installation

View File

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

View File

@ -0,0 +1,31 @@
package balance
import (
"os"
"testing"
"github.com/lmittmann/w3"
)
type tConfig struct {
rpcProvider string
batchContract string
}
var conf = &tConfig{
rpcProvider: os.Getenv("RPC_PROVIDER"),
batchContract: os.Getenv("BATCH_BALANCE"),
}
func TestCicNet_Connect(t *testing.T) {
name := "Test RPC connection"
wantErr := false
cicnet, _ := NewBatchBalance(conf.rpcProvider, w3.A(conf.batchContract))
t.Run(name, func(t *testing.T) {
if err := cicnet.Close(); (err != nil) != wantErr {
t.Errorf("Close() error = %v, wantErr %v", err, wantErr)
}
})
}

View File

@ -0,0 +1,24 @@
package balance
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
)
func (c *BatchBalance) TokensBalance(ctx context.Context, owner common.Address, tokens []common.Address) ([]*big.Int, error) {
var balancesResults []*big.Int
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("tokensBalance(address owner, address[] contracts)", "uint256[]"), c.batchContract, owner, tokens).Returns(&balancesResults),
)
if err != nil {
return nil, err
}
return balancesResults, nil
}

View File

@ -0,0 +1,59 @@
package balance
import (
"context"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
)
func TestBatchBalance_TokensBalance(t *testing.T) {
type args struct {
owner common.Address
tokens []common.Address
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "A random members (min dust available) balances",
args: args{
owner: w3.A("0x4e956b5De3c33566c596754B4fa0ABd9F2789578"),
tokens: []common.Address{
w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
w3.A("0x982caeF20362ADEAC3f9a25E37E20E6787f27f85"),
w3.A("0x9ADd261033baA414c84FF84A4Fe396338C1ba13a"),
w3.A("0x7dF20b526318d37Cd7DA9518E51d4A51fec30c9A"),
},
},
want: true,
wantErr: false,
},
}
batchBalance, err := NewBatchBalance(conf.rpcProvider, w3.A(conf.batchContract))
if err != nil {
t.Fatalf("NewBatchBalance error = %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := batchBalance.TokensBalance(context.Background(), tt.args.owner, tt.args.tokens)
if (err != nil) != tt.wantErr {
t.Errorf("BatchBalance.TokensBalance() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got[2].Int64() > 0 {
t.Errorf("TokenBalance = %d, want %d", got[2].Int64(), 0)
}
})
}
}

View File

@ -2,10 +2,11 @@ package net
import (
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3"
"math/big"
)
const (

View File

@ -1,9 +1,10 @@
package net
import (
"github.com/lmittmann/w3"
"os"
"testing"
"github.com/lmittmann/w3"
)
type tConfig struct {
@ -26,7 +27,7 @@ func TestCicNet_Connect(t *testing.T) {
t.Run(name, func(t *testing.T) {
if err := cicnet.Close(); (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
t.Errorf("Error() error = %v, wantErr %v", err, wantErr)
}
})
}