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
This commit is contained in:
Mohamed Sohail 2022-05-09 10:58:04 +03:00 committed by GitHub
parent 7731a23e35
commit 79634affb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 82 additions and 74 deletions

1
.gitignore vendored
View File

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

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"

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"crypto/ecdsa"
@ -22,6 +22,7 @@ type WriteTx struct {
from common.Address
to common.Address
gasLimit uint64
nonce uint64
privateKey ecdsa.PrivateKey
}

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"github.com/lmittmann/w3"
@ -22,9 +22,9 @@ 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))
cicnet, _ := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
t.Run(name, func(t *testing.T) {
if err := cicnet.Close(); (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
}

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"context"

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"context"
@ -44,16 +44,16 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
},
}
cicnet, err := NewCicNet(conf.rpcProvider, 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 +62,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 +109,16 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
},
}
cicnet, err := NewCicNet(conf.rpcProvider, 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.BaseBalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr {
@ -127,7 +127,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())
}
}
})
@ -151,6 +151,16 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct {
name string
args args
@ -163,6 +173,7 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1,
gasLimit: 12000000,
privateKey: *privateKey,
},
@ -175,11 +186,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())
@ -209,6 +215,16 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct {
name string
args args
@ -222,6 +238,7 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
writeTx: WriteTx{
from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1,
gasLimit: 12000000,
privateKey: *privateKey,
},
@ -234,12 +251,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,4 +1,4 @@
package cic_net
package net
import (
"context"

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"context"
@ -37,16 +37,16 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
},
}
cicnet, err := NewCicNet(conf.rpcProvider, 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 +54,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 +92,16 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
},
}
cicnet, err := NewCicNet(conf.rpcProvider, 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.BalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr {
@ -109,7 +109,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,4 +1,4 @@
package cic_net
package net
import (
"context"

View File

@ -1,4 +1,4 @@
package cic_net
package net
import (
"context"
@ -11,13 +11,13 @@ func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
name := "Entry count"
wantErr := false
tokenIndex, err := NewCicNet(conf.rpcProvider, 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())
if (err != nil) != wantErr {
@ -25,7 +25,7 @@ func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
}
if got.Cmp(big.NewInt(0)) < 1 {
t.Fatalf("EntryCount() = %v, want %v", got, 1)
t.Errorf("EntryCount() = %v, want %v", got, 1)
}
})
}
@ -59,16 +59,16 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
},
}
tokenIndex, err := NewCicNet(conf.rpcProvider, 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)
if (err != nil) != tt.wantErr {
@ -76,7 +76,7 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
}
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,4 +1,4 @@
package cic_net
package net
import (
"context"
@ -8,7 +8,7 @@ import (
"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(
@ -25,14 +25,9 @@ func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint6
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData 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,
Nonce: txData.nonce,
Data: input,
Gas: txData.gasLimit,
GasPrice: big.NewInt(1),