mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-12-22 08:27:31 +01:00
add: (wip) on-chain balance query (#5)
* add: smart-contracts * add: implement and test changes to contract: - returns 0 instead of false error status - func return type is []big.Int * fix: test name * fix: (test) env var name
This commit is contained in:
parent
098ad25b39
commit
8ebcbbf479
@ -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_CONTRACT"),
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBatchBalance_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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
48
batch_balance/smart-contract/Balances.sol
Normal file
48
batch_balance/smart-contract/Balances.sol
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// Adapted from https://github.com/MyCryptoHQ/eth-scan
|
||||||
|
|
||||||
|
pragma solidity >= 0.8.0;
|
||||||
|
|
||||||
|
contract Balances {
|
||||||
|
struct Result {
|
||||||
|
bool success;
|
||||||
|
uint256 balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tokensBalance(address owner, address[] calldata contracts) external view returns (Result[] memory results) {
|
||||||
|
results = new Result[](contracts.length);
|
||||||
|
|
||||||
|
bytes memory data = abi.encodeWithSignature("balanceOf(address)", owner);
|
||||||
|
|
||||||
|
for (uint256 i = 0; i < contracts.length; i++) {
|
||||||
|
results[i] = staticCall(contracts[i], data, 8000000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function staticCall(
|
||||||
|
address target,
|
||||||
|
bytes memory data,
|
||||||
|
uint256 gas
|
||||||
|
) private view returns (Result memory) {
|
||||||
|
uint256 size = codeSize(target);
|
||||||
|
|
||||||
|
if (size > 0) {
|
||||||
|
(bool success, bytes memory result) = target.staticcall{ gas: gas }(data);
|
||||||
|
if (success) {
|
||||||
|
uint256 balance = abi.decode(result, (uint256));
|
||||||
|
return Result(success, balance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result(false, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function codeSize(address _address) private view returns (uint256 size) {
|
||||||
|
// solhint-disable-next-line no-inline-assembly
|
||||||
|
assembly {
|
||||||
|
size := extcodesize(_address)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
38
batch_balance/smart-contract/artifacts/abi.json
Normal file
38
batch_balance/smart-contract/artifacts/abi.json
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"internalType": "address",
|
||||||
|
"name": "owner",
|
||||||
|
"type": "address"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"internalType": "address[]",
|
||||||
|
"name": "contracts",
|
||||||
|
"type": "address[]"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "tokensBalance",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"components": [
|
||||||
|
{
|
||||||
|
"internalType": "bool",
|
||||||
|
"name": "success",
|
||||||
|
"type": "bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"internalType": "uint256",
|
||||||
|
"name": "balance",
|
||||||
|
"type": "uint256"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"internalType": "struct Balances.Result[]",
|
||||||
|
"name": "results",
|
||||||
|
"type": "tuple[]"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"stateMutability": "view",
|
||||||
|
"type": "function"
|
||||||
|
}
|
||||||
|
]
|
8
batch_balance/smart-contract/artifacts/bytecode.json
Normal file
8
batch_balance/smart-contract/artifacts/bytecode.json
Normal file
File diff suppressed because one or more lines are too long
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