Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

27 changed files with 197 additions and 525 deletions

View File

@ -1,8 +0,0 @@
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,7 +18,6 @@ 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,2 +1 @@
covprofile
*.env

View File

@ -1,27 +1,11 @@
> 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
- 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"
)
```
- cic_net
- cic_meta

View File

@ -1,18 +0,0 @@
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

@ -1,37 +0,0 @@
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

@ -1,48 +0,0 @@
// 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

@ -1,38 +0,0 @@
[
{
"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

@ -1,24 +0,0 @@
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

@ -1,64 +0,0 @@
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,4 +1,4 @@
package meta
package cic_meta
import (
"github.com/ethereum/go-ethereum/common"

View File

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

48
cic_net/cic_net.go Normal file
View File

@ -0,0 +1,48 @@
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
}

32
cic_net/cic_net_test.go Normal file
View File

@ -0,0 +1,32 @@
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)
}
})
}

View File

@ -1,13 +1,11 @@
package net
package cic_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 {
@ -36,7 +34,7 @@ func (c *CicNet) DemurrageTokenInfo(ctx context.Context, tokenAddress common.Add
redistributionCount big.Int
)
err := c.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("demurrageAmount()", "uint128"), tokenAddress).Returns(&demurrageAmount),
eth.CallFunc(w3.MustNewFunc("demurrageTimestamp()", "uint256"), tokenAddress).Returns(&demurrageTimestamp),
@ -69,7 +67,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.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("baseBalanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
)
@ -80,7 +78,7 @@ func (c *CicNet) BaseBalanceOf(ctx context.Context, tokenAddress common.Address,
return balance, nil
}
func (c *CicNet) ChangePeriod(ctx context.Context, txData provider.WriteTx) (common.Hash, error) {
func (c *CicNet) ChangePeriod(ctx context.Context, txData WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("changePeriod()", "bool")
input, err := sig.EncodeArgs()
if err != nil {
@ -95,7 +93,7 @@ func (c *CicNet) ChangePeriod(ctx context.Context, txData provider.WriteTx) (com
return txHash, nil
}
func (c *CicNet) ApplyDemurrageLimited(ctx context.Context, rounds int64, txData provider.WriteTx) (common.Hash, error) {
func (c *CicNet) ApplyDemurrageLimited(ctx context.Context, rounds int64, txData WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("applyDemurrageLimited(uint256 _rounds)", "bool")
input, err := sig.EncodeArgs(big.NewInt(rounds))
if err != nil {

View File

@ -1,15 +1,13 @@
package net
package cic_net
import (
"context"
"crypto/ecdsa"
"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"
"math/big"
"testing"
)
func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
@ -46,20 +44,16 @@ 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 {
@ -68,7 +62,7 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
if !tt.wantErr {
if got.DemurrageAmount.Cmp(big.NewInt(0)) < 1 {
t.Errorf("DemurrageAmount = %v, want %d atleast", got, 1)
t.Fatalf("DemurrageAmount = %v, want %d atleast", got, 1)
}
}
})
@ -115,16 +109,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 {
@ -133,7 +127,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
if !tt.wantErr {
if got.Cmp(&tt.balanceGte) < 0 {
t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64())
}
}
})
@ -142,7 +136,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
type args struct {
writeTx provider.WriteTx
writeTx WriteTx
}
// Bootstrap signer
@ -157,17 +151,6 @@ 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
@ -177,12 +160,11 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
{
name: "ChangePeriod for Sarafu",
args: args{
writeTx: provider.WriteTx{
From: fromAddress,
To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
Nonce: nonce + 1,
GasLimit: 12000000,
PrivateKey: *privateKey,
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
gasLimit: 12000000,
privateKey: *privateKey,
},
},
wantErr: false,
@ -193,6 +175,11 @@ 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())
@ -207,7 +194,7 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
type args struct {
rounds int64
writeTx provider.WriteTx
writeTx WriteTx
}
// Bootstrap signer
@ -222,17 +209,6 @@ 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
@ -243,12 +219,11 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
name: "ChangePeriod for Sarafu",
args: args{
rounds: 1000,
writeTx: provider.WriteTx{
From: fromAddress,
To: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
Nonce: nonce + 1,
GasLimit: 12000000,
PrivateKey: *privateKey,
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
gasLimit: 12000000,
privateKey: *privateKey,
},
},
wantErr: false,
@ -259,6 +234,12 @@ 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,19 +1,17 @@
package net
package cic_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
TotalSupply big.Int
Name string
Symbol string
Decimals big.Int
}
func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address) (ERC20Token, error) {
@ -21,31 +19,28 @@ func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address
tokenName string
tokenSymbol string
tokenDecimals big.Int
totalSupply big.Int
)
err := c.provider.EthClient.CallCtx(
err := c.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,
TotalSupply: totalSupply,
Name: tokenName,
Symbol: tokenSymbol,
Decimals: tokenDecimals,
}, nil
}
func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int
err := c.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("balanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
)

View File

@ -1,13 +1,11 @@
package net
package cic_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) {
@ -39,20 +37,16 @@ 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 {
@ -60,7 +54,7 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
}
if got.Symbol != tt.symbol {
t.Errorf("Token = %v, want %v", got, tt.symbol)
t.Fatalf("Token = %v, want %v", got, tt.symbol)
}
})
}
@ -98,16 +92,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 {
@ -115,7 +109,7 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
}
if got.Cmp(&tt.balanceGte) < 0 {
t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64())
}
})
}

View File

@ -1,18 +1,17 @@
package net
package cic_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.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), c.tokenIndex).Returns(&tokenCount),
)
@ -26,7 +25,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.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entry(uint256 _idx)", "address"), c.tokenIndex, index).Returns(&address),
)

View File

@ -1,37 +1,31 @@
package net
package cic_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) {
got, err := cicnet.EntryCount(context.Background())
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := tokenIndex.EntryCount(context.Background())
if (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
}
if got.Cmp(big.NewInt(0)) < 1 {
t.Errorf("EntryCount() = %v, want %v", got, 1)
t.Fatalf("EntryCount() = %v, want %v", got, 1)
}
})
}
@ -65,28 +59,24 @@ 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) {
got, err := cicnet.AddressAtIndex(context.Background(), tt.args.index)
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)
if (err != nil) != tt.wantErr {
t.Errorf("AddressAtIndex() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.address {
t.Errorf("AddressAtIndex = %v, want %v", got, tt.address)
t.Fatalf("AddressAtIndex = %v, want %v", got, tt.address)
}
})
}

View File

@ -1,19 +1,17 @@
package net
package cic_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) LastNonce(ctx context.Context, address common.Address) (uint64, error) {
func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint64, error) {
var nonce uint64
err := c.provider.EthClient.CallCtx(
err := c.ethClient.CallCtx(
ctx,
eth.Nonce(address, nil).Returns(&nonce),
)
@ -24,18 +22,23 @@ func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64,
return nonce, nil
}
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData provider.WriteTx) (common.Hash, error) {
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData WriteTx) (common.Hash, error) {
var txHash common.Hash
tx, err := types.SignNewTx(&txData.PrivateKey, c.provider.Signer, &types.LegacyTx{
To: &txData.To,
Nonce: txData.Nonce,
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,
Data: input,
Gas: txData.GasLimit,
Gas: txData.gasLimit,
GasPrice: big.NewInt(1),
})
err = c.provider.EthClient.CallCtx(
err = c.ethClient.CallCtx(
ctx,
eth.SendTransaction(tx).Returns(&txHash),
)

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,18 +0,0 @@
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
}

View File

@ -1,39 +0,0 @@
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,48 +0,0 @@
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
}