mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-11-24 22:56:45 +01:00
add: write tx methods and tests
- add ci for testing - coveralls for coverage
This commit is contained in:
parent
2886a4c628
commit
f75539713b
6
.github/depandabot.yml
vendored
Normal file
6
.github/depandabot.yml
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
22
.github/workflows/go.yml
vendored
Normal file
22
.github/workflows/go.yml
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
name: Go
|
||||
|
||||
on:
|
||||
- push
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
- uses: actions/checkout@v2
|
||||
- name: go test
|
||||
run: go test -v -covermode atomic -coverprofile=covprofile ./...
|
||||
- name: install goveralls
|
||||
run: go install github.com/mattn/goveralls@latest
|
||||
- name: send coverage
|
||||
env:
|
||||
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: goveralls -coverprofile=covprofile -service=github
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
covprofile
|
@ -1,13 +1,28 @@
|
||||
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
|
||||
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) {
|
||||
@ -17,8 +32,9 @@ func NewCicNet(rpcEndpoint string, tokenIndex common.Address) (*CicNet, error) {
|
||||
}
|
||||
|
||||
return &CicNet{
|
||||
ethClient: ethClient,
|
||||
tokenIndex: tokenIndex,
|
||||
ethClient: ethClient,
|
||||
tokenIndex: tokenIndex,
|
||||
kitabuSigner: types.NewEIP155Signer(big.NewInt(kitabuMainnetChainId)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -1,15 +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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -77,3 +77,33 @@ 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) {
|
||||
sig := w3.MustNewFunc("changePeriod()", "bool")
|
||||
input, err := sig.EncodeArgs()
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
txHash, err := c.signAndCall(ctx, input, txData)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
return txHash, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
txHash, err := c.signAndCall(ctx, input, txData)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
return txHash, nil
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ package cic_net
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/lmittmann/w3"
|
||||
"math/big"
|
||||
"testing"
|
||||
@ -131,3 +133,119 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
|
||||
type args struct {
|
||||
writeTx WriteTx
|
||||
}
|
||||
|
||||
// Bootstrap signer
|
||||
privateKey, err := crypto.HexToECDSA(conf.privateKey)
|
||||
if err != nil {
|
||||
t.Fatalf("ECDSA error = %v", err)
|
||||
}
|
||||
publicKey := privateKey.Public()
|
||||
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
|
||||
if !ok {
|
||||
t.Fatalf("ECDSA error = %v", err)
|
||||
}
|
||||
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
balanceGte big.Int
|
||||
}{
|
||||
{
|
||||
name: "ChangePeriod for Sarafu",
|
||||
args: args{
|
||||
writeTx: WriteTx{
|
||||
from: fromAddress,
|
||||
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
|
||||
gasLimit: 12000000,
|
||||
privateKey: *privateKey,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
tx, err := cicnet.ChangePeriod(context.Background(), tt.args.writeTx)
|
||||
t.Logf("ChangePeriod tx_hash %s", tx.String())
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ChangePeriod() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
|
||||
type args struct {
|
||||
rounds int64
|
||||
writeTx WriteTx
|
||||
}
|
||||
|
||||
// Bootstrap signer
|
||||
privateKey, err := crypto.HexToECDSA(conf.privateKey)
|
||||
if err != nil {
|
||||
t.Fatalf("ECDSA error = %v", err)
|
||||
}
|
||||
publicKey := privateKey.Public()
|
||||
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
|
||||
if !ok {
|
||||
t.Fatalf("ECDSA error = %v", err)
|
||||
}
|
||||
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
balanceGte big.Int
|
||||
}{
|
||||
{
|
||||
name: "ChangePeriod for Sarafu",
|
||||
args: args{
|
||||
rounds: 1000,
|
||||
writeTx: WriteTx{
|
||||
from: fromAddress,
|
||||
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
|
||||
gasLimit: 12000000,
|
||||
privateKey: *privateKey,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
tx, err := cicnet.ApplyDemurrageLimited(context.Background(), tt.args.rounds, tt.args.writeTx)
|
||||
t.Logf("ApplyDemurrageLimited tx_hash %s", tx.String())
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyDemurrageLimited() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
50
cic_net/util.go
Normal file
50
cic_net/util.go
Normal file
@ -0,0 +1,50 @@
|
||||
package cic_net
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/lmittmann/w3/module/eth"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint64, error) {
|
||||
var nonce uint64
|
||||
|
||||
err := c.ethClient.CallCtx(
|
||||
ctx,
|
||||
eth.Nonce(address, nil).Returns(&nonce),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return nonce, nil
|
||||
}
|
||||
|
||||
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,
|
||||
Data: input,
|
||||
Gas: txData.gasLimit,
|
||||
GasPrice: big.NewInt(1),
|
||||
})
|
||||
|
||||
err = c.ethClient.CallCtx(
|
||||
ctx,
|
||||
eth.SendTransaction(tx).Returns(&txHash),
|
||||
)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
return txHash, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user