Compare commits

...

17 Commits

Author SHA1 Message Date
Mohamed Sohail dbcc03d1e8
Update README.md 2022-08-09 15:02:00 +03:00
DeepSource Bot 5d2daa2c8e Add .deepsource.toml 2022-06-21 07:38:06 +00:00
Mohamed Sohail 2070f9afcd
Merge pull request #7 from grassrootseconomics/sohail/total-supply
add: (feat) erc20 total supply
2022-05-23 12:49:40 +03:00
Mohamed Sohail 9a7dc6457e
add: (feat) erc20 total supply 2022-05-23 12:47:10 +03:00
Mohamed Sohail a3052d9e21
fix: pointer param for provider 2022-05-18 13:41:55 +03:00
Mohamed Sohail 7eba3182ed
refactor: rpc provider (#6)
- provider now lives on its own
- existing modules need the provider as a param
2022-05-18 12:52:50 +03:00
Mohamed Sohail 7f771ccfb1
Merge branch 'sohail/balances-query' 2022-05-17 20:38:27 +03:00
Mohamed Sohail 07f74ad1ce
ci: fix missing env var 2022-05-17 20:36:30 +03:00
Mohamed Sohail 8ebcbbf479
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
2022-05-17 20:33:03 +03:00
Mohamed Sohail c090d30253
fix: (test) env var name 2022-05-17 20:27:35 +03:00
Mohamed Sohail c00bcd84e9
fix: test name 2022-05-17 16:28:30 +03:00
Mohamed Sohail c34e1444dc
add: implement and test
changes to contract:
- returns 0 instead of false error status
- func return type is []big.Int
2022-05-17 16:06:19 +03:00
Mohamed Sohail 2c8c45ba17
add: smart-contracts 2022-05-14 11:16:47 +03:00
Mohamed Sohail 098ad25b39
Rename depandabot.yml to dependabot.yml 2022-05-11 12:30:02 +03:00
Mohamed Sohail a759e7aa16
Merge branch 'master' of https://github.com/grassrootseconomics/cic_go 2022-05-09 11:06:16 +03:00
Mohamed Sohail 63cfc4e8a9
docs: release v1.1.0 2022-05-09 11:06:00 +03:00
Mohamed Sohail 79634affb8
refactor: tests and structure (#3)
module:
- cic-go

packages:
- cic/meta
- cic/net

other fixes:

- nonce is now a settable value
- package init outside test runner scope
2022-05-09 10:58:04 +03:00
27 changed files with 526 additions and 198 deletions

8
.deepsource.toml Normal file
View File

@ -0,0 +1,8 @@
version = 1
[[analyzers]]
name = "go"
enabled = true
[analyzers.meta]
import_root = "github.com/grassrootseconomics/cic-go"

View File

@ -3,4 +3,4 @@ updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
interval: "daily"

View File

@ -18,6 +18,7 @@ jobs:
TOKEN_INDEX: ${{ secrets.TOKEN_INDEX }}
RPC_PROVIDER: ${{ secrets.RPC_PROVIDER }}
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
BATCH_CONTRACT: ${{ secrets.BATCH_CONTRACT }}
run: go test -v -covermode atomic -coverprofile=covprofile ./...
- name: install goveralls
run: go install github.com/mattn/goveralls@latest

1
.gitignore vendored
View File

@ -1 +1,2 @@
covprofile
*.env

View File

@ -1,11 +1,27 @@
> DEPRECATED: Use https://github.com/grassrootseconomics/cic-go-sdk
[![Go Reference](https://pkg.go.dev/badge/github.com/grassrootseconomics/cic_go.svg)](https://pkg.go.dev/github.com/grassrootseconomics/cic_go)
[![Go](https://github.com/grassrootseconomics/cic_go/actions/workflows/go.yml/badge.svg)](https://github.com/grassrootseconomics/cic_go/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/grassrootseconomics/cic_go/badge.svg?branch=sohail/cic_net_updates)](https://coveralls.io/github/grassrootseconomics/cic_go?branch=sohail/cic_net_updates)
# cic_go
# cic-go
Go modules to access various parts of the cic stack
## Implemented
- cic_net
- cic_meta
- meta (web2 metadata store)
- net (cic smart contracts)
- batch balance query (`0xb9e215B789e9Ec6643Ba4ff7b98EA219F38c6fE5`)
## Installation
`go get -u github.com/grassrootseconomics/cic-go`
## Usage
```go
import (
cic_meta "github.com/grassrootseconomics/cic-go/meta"
cic_net "github.com/grassrootseconomics/cic-go/net"
)
```

View File

@ -0,0 +1,18 @@
package balance
import (
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
)
type BatchBalance struct {
provider *provider.Provider
batchContract common.Address
}
func NewBatchBalance(rpcProvider *provider.Provider, batchContract common.Address) (*BatchBalance, error) {
return &BatchBalance{
provider: rpcProvider,
batchContract: batchContract,
}, nil
}

View File

@ -0,0 +1,37 @@
package balance
import (
"os"
"testing"
"github.com/grassrootseconomics/cic-go/provider"
"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
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 := batchBalance.provider.EthClient.Close(); (err != nil) != wantErr {
t.Errorf("Close() error = %v, wantErr %v", err, wantErr)
}
})
}

View 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)
}
}
}

View 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"
}
]

File diff suppressed because one or more lines are too long

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.provider.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,64 @@
package balance
import (
"context"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"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,
},
}
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)
}
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

@ -1,48 +0,0 @@
package cic_net
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3"
"math/big"
)
const (
kitabuMainnetChainId = 6060
)
type CicNet struct {
ethClient *w3.Client
tokenIndex common.Address
kitabuSigner types.Signer
}
type WriteTx struct {
from common.Address
to common.Address
gasLimit 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{
ethClient: ethClient,
tokenIndex: tokenIndex,
kitabuSigner: types.NewEIP155Signer(big.NewInt(kitabuMainnetChainId)),
}, nil
}
func (c *CicNet) Close() error {
err := c.ethClient.Close()
if err != nil {
return err
}
return nil
}

View File

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

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/grassrootseconomics/cic_go
module github.com/grassrootseconomics/cic-go
go 1.18

View File

@ -1,4 +1,4 @@
package cic_meta
package meta
import (
"github.com/ethereum/go-ethereum/common"

View File

@ -1,4 +1,4 @@
package cic_meta
package meta
import (
"crypto/sha256"

18
net/cic_net.go Normal file
View File

@ -0,0 +1,18 @@
package net
import (
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
)
type CicNet struct {
provider *provider.Provider
tokenIndex common.Address
}
func NewCicNet(rpcProvider *provider.Provider, tokenIndex common.Address) (*CicNet, error) {
return &CicNet{
provider: rpcProvider,
tokenIndex: tokenIndex,
}, nil
}

39
net/cic_net_test.go Normal file
View File

@ -0,0 +1,39 @@
package net
import (
"os"
"testing"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
)
type tConfig struct {
rpcProvider string
tokenIndex string
privateKey string
}
var conf = &tConfig{
rpcProvider: os.Getenv("RPC_PROVIDER"),
tokenIndex: os.Getenv("TOKEN_INDEX"),
privateKey: os.Getenv("PRIVATE_KEY"),
}
func TestCicNet_Connect(t *testing.T) {
name := "Test CicNet connect"
wantErr := false
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) {
if err := cicnet.provider.EthClient.Close(); (err != nil) != wantErr {
t.Errorf("Error() error = %v, wantErr %v", err, wantErr)
}
})
}

View File

@ -1,11 +1,13 @@
package cic_net
package net
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
type DemurrageTokenInfo struct {
@ -34,7 +36,7 @@ func (c *CicNet) DemurrageTokenInfo(ctx context.Context, tokenAddress common.Add
redistributionCount big.Int
)
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("demurrageAmount()", "uint128"), tokenAddress).Returns(&demurrageAmount),
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) {
var balance big.Int
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
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
}
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")
input, err := sig.EncodeArgs()
if err != nil {
@ -93,7 +95,7 @@ func (c *CicNet) ChangePeriod(ctx context.Context, txData WriteTx) (common.Hash,
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")
input, err := sig.EncodeArgs(big.NewInt(rounds))
if err != nil {

View File

@ -1,13 +1,15 @@
package cic_net
package net
import (
"context"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/lmittmann/w3"
"math/big"
"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) {
@ -44,16 +46,20 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
},
}
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 {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.DemurrageTokenInfo(context.Background(), tt.args.contractAddress)
if (err != nil) != tt.wantErr {
@ -62,7 +68,7 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
if !tt.wantErr {
if got.DemurrageAmount.Cmp(big.NewInt(0)) < 1 {
t.Fatalf("DemurrageAmount = %v, want %d atleast", got, 1)
t.Errorf("DemurrageAmount = %v, want %d atleast", got, 1)
}
}
})
@ -109,16 +115,16 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
},
}
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))
for _, testcase := range tests {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.BaseBalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr {
@ -127,7 +133,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
if !tt.wantErr {
if got.Cmp(&tt.balanceGte) < 0 {
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64())
t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
}
}
})
@ -136,7 +142,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
type args struct {
writeTx WriteTx
writeTx provider.WriteTx
}
// Bootstrap signer
@ -151,6 +157,17 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
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))
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct {
name string
args args
@ -160,11 +177,12 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
{
name: "ChangePeriod for Sarafu",
args: args{
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
gasLimit: 12000000,
privateKey: *privateKey,
writeTx: provider.WriteTx{
From: fromAddress,
To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
Nonce: nonce + 1,
GasLimit: 12000000,
PrivateKey: *privateKey,
},
},
wantErr: false,
@ -175,11 +193,6 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
tx, err := cicnet.ChangePeriod(context.Background(), tt.args.writeTx)
t.Logf("ChangePeriod tx_hash %s", tx.String())
@ -194,7 +207,7 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
type args struct {
rounds int64
writeTx WriteTx
writeTx provider.WriteTx
}
// Bootstrap signer
@ -209,6 +222,17 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
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))
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct {
name string
args args
@ -219,11 +243,12 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
name: "ChangePeriod for Sarafu",
args: args{
rounds: 1000,
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
gasLimit: 12000000,
privateKey: *privateKey,
writeTx: provider.WriteTx{
From: fromAddress,
To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
Nonce: nonce + 1,
GasLimit: 12000000,
PrivateKey: *privateKey,
},
},
wantErr: false,
@ -234,12 +259,6 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
tx, err := cicnet.ApplyDemurrageLimited(context.Background(), tt.args.rounds, tt.args.writeTx)
t.Logf("ApplyDemurrageLimited tx_hash %s", tx.String())

View File

@ -1,17 +1,19 @@
package cic_net
package net
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
type ERC20Token struct {
Name string
Symbol string
Decimals big.Int
Name string
Symbol string
Decimals big.Int
TotalSupply big.Int
}
func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address) (ERC20Token, error) {
@ -19,28 +21,31 @@ func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address
tokenName string
tokenSymbol string
tokenDecimals big.Int
totalSupply big.Int
)
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("name()", "string"), tokenAddress).Returns(&tokenName),
eth.CallFunc(w3.MustNewFunc("symbol()", "string"), tokenAddress).Returns(&tokenSymbol),
eth.CallFunc(w3.MustNewFunc("decimals()", "uint256"), tokenAddress).Returns(&tokenDecimals),
eth.CallFunc(w3.MustNewFunc("totalSupply()", "uint256"), tokenAddress).Returns(&totalSupply),
)
if err != nil {
return ERC20Token{}, err
}
return ERC20Token{
Name: tokenName,
Symbol: tokenSymbol,
Decimals: tokenDecimals,
Name: tokenName,
Symbol: tokenSymbol,
Decimals: tokenDecimals,
TotalSupply: totalSupply,
}, nil
}
func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("balanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
)

View File

@ -1,11 +1,13 @@
package cic_net
package net
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
)
func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
@ -37,16 +39,20 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
},
}
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 {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.ERC20TokenInfo(context.Background(), tt.args.contractAddress)
if (err != nil) != tt.wantErr {
@ -54,7 +60,7 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
}
if got.Symbol != tt.symbol {
t.Fatalf("Token = %v, want %v", got, tt.symbol)
t.Errorf("Token = %v, want %v", got, tt.symbol)
}
})
}
@ -92,16 +98,16 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
},
}
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))
for _, testcase := range tests {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.BalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr {
@ -109,7 +115,7 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
}
if got.Cmp(&tt.balanceGte) < 0 {
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64())
t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
}
})
}

View File

@ -1,17 +1,18 @@
package cic_net
package net
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
func (c *CicNet) EntryCount(ctx context.Context) (big.Int, error) {
var tokenCount big.Int
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
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) {
var address common.Address
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entry(uint256 _idx)", "address"), c.tokenIndex, index).Returns(&address),
)

View File

@ -1,31 +1,37 @@
package cic_net
package net
import (
"context"
"github.com/lmittmann/w3"
"math/big"
"testing"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3"
)
func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
name := "Entry count"
wantErr := false
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 {
t.Fatalf("NewCicNet error = %v", err)
}
t.Run(name, func(t *testing.T) {
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := tokenIndex.EntryCount(context.Background())
got, err := cicnet.EntryCount(context.Background())
if (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
}
if got.Cmp(big.NewInt(0)) < 1 {
t.Fatalf("EntryCount() = %v, want %v", got, 1)
t.Errorf("EntryCount() = %v, want %v", got, 1)
}
})
}
@ -59,24 +65,28 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
},
}
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 {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests {
tt := testcase
t.Run(tt.name, func(t *testing.T) {
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := tokenIndex.AddressAtIndex(context.Background(), tt.args.index)
got, err := cicnet.AddressAtIndex(context.Background(), tt.args.index)
if (err != nil) != tt.wantErr {
t.Errorf("AddressAtIndex() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.address {
t.Fatalf("AddressAtIndex = %v, want %v", got, tt.address)
t.Errorf("AddressAtIndex = %v, want %v", got, tt.address)
}
})
}

View File

@ -1,17 +1,19 @@
package cic_net
package net
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/lmittmann/w3/module/eth"
"math/big"
)
func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint64, error) {
func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64, error) {
var nonce uint64
err := c.ethClient.CallCtx(
err := c.provider.EthClient.CallCtx(
ctx,
eth.Nonce(address, nil).Returns(&nonce),
)
@ -22,23 +24,18 @@ func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint6
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
nonce, err := c.latestNonce(ctx, txData.from)
if err != nil {
return [32]byte{}, err
}
tx, err := types.SignNewTx(&txData.privateKey, c.kitabuSigner, &types.LegacyTx{
To: &txData.to,
Nonce: nonce,
tx, err := types.SignNewTx(&txData.PrivateKey, c.provider.Signer, &types.LegacyTx{
To: &txData.To,
Nonce: txData.Nonce,
Data: input,
Gas: txData.gasLimit,
Gas: txData.GasLimit,
GasPrice: big.NewInt(1),
})
err = c.ethClient.CallCtx(
err = c.provider.EthClient.CallCtx(
ctx,
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
}