mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-11-21 21:56:45 +01:00
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:
parent
2c8c45ba17
commit
c34e1444dc
@ -9,6 +9,7 @@ Go modules to access various parts of the cic stack
|
|||||||
|
|
||||||
- meta (web2 metadata store)
|
- meta (web2 metadata store)
|
||||||
- net (cic smart contracts)
|
- net (cic smart contracts)
|
||||||
|
- batch balance query (`0xb9e215B789e9Ec6643Ba4ff7b98EA219F38c6fE5`)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
32
batch_balance/batch_balance.go
Normal file
32
batch_balance/batch_balance.go
Normal 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
|
||||||
|
}
|
31
batch_balance/batch_balance_test.go
Normal file
31
batch_balance/batch_balance_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
24
batch_balance/token_balances.go
Normal file
24
batch_balance/token_balances.go
Normal 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
|
||||||
|
}
|
59
batch_balance/token_balances_test.go
Normal file
59
batch_balance/token_balances_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,11 @@ package net
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
"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/ethereum/go-ethereum/core/types"
|
||||||
"github.com/lmittmann/w3"
|
"github.com/lmittmann/w3"
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/lmittmann/w3"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/lmittmann/w3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tConfig struct {
|
type tConfig struct {
|
||||||
@ -26,7 +27,7 @@ func TestCicNet_Connect(t *testing.T) {
|
|||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
if err := cicnet.Close(); (err != nil) != wantErr {
|
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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user