Skip to content

Commit

Permalink
Merge PR: dds statistic (#1352)
Browse files Browse the repository at this point in the history
* add counter contract

* add counter contract

* add dds statistic

* add dds statistic (#1353)

* add dds statistic
  • Loading branch information
zhongqiuwood authored Dec 29, 2021
1 parent 36742c9 commit 86dede2
Show file tree
Hide file tree
Showing 19 changed files with 405 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export GO111MODULE=on
GithubTop=github.com


Version=v1.0.1
Version=v1.0.2
CosmosSDK=v0.39.2
Tendermint=v0.33.9
Iavl=v0.14.3
Expand Down
1 change: 1 addition & 0 deletions dev/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
client
5 changes: 3 additions & 2 deletions dev/client/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# transfer okt
# how to use
```
./run.sh
go build
./client
```
138 changes: 117 additions & 21 deletions dev/client/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
bytes2 "bytes"
"bytes"
"context"
"crypto/ecdsa"
"fmt"
Expand All @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/okex/exchain-ethereum-compatible/utils"

"io/ioutil"
"log"
Expand All @@ -35,24 +36,34 @@ type Contract struct {
address string
addr common.Address
abi abi.ABI
byteCode []byte
}

func NewContract(name, address, abiFile string) *Contract {
func newContract(name, address, abiFile string, byteCodeFile string) *Contract {
c := &Contract{
name: name,
address: address,
addr: common.HexToAddress(address),
}

bin, err := ioutil.ReadFile(byteCodeFile)
if err != nil {
log.Fatal(err)
}
c.byteCode = common.Hex2Bytes(string(bin))

abiByte, err := ioutil.ReadFile(abiFile)
if err != nil {
log.Fatal(err)
}
c.abi, err = abi.JSON(bytes2.NewReader(abiByte))
c.abi, err = abi.JSON(bytes.NewReader(abiByte))
if err != nil {
log.Fatal(err)
}

if len(address) > 0 {
c.addr = common.HexToAddress(address)
fmt.Printf("new contract: %s\n", address)
}
return c
}

Expand All @@ -62,7 +73,10 @@ func str2bigInt(input string) *big.Int{

func uint256Output(client *ethclient.Client, c *Contract, name string, args ...interface{}) (*big.Int) {

value := ReadContract(client, c, name, args...)
value := readContract(client, c, name, args...)
if len(value) == 0 {
return str2bigInt("0")
}
ret := value[0].(*big.Int)

arg0 := ""
Expand All @@ -74,43 +88,45 @@ func uint256Output(client *ethclient.Client, c *Contract, name string, args ...i

decRet := sdk.NewDecFromBigIntWithPrec(ret, sdk.Precision)

fmt.Printf(" <%s[%s(%s)]> uint256 output: %s\n", c.name, name, arg0, decRet)
fmt.Printf(" <%s[%s(%s)]>: %s\n", c.address, name, arg0, decRet)
return ret
}

func WriteContract(client *ethclient.Client,
func writeContract(client *ethclient.Client,
contract *Contract,
fromAddress common.Address,
privateKey *ecdsa.PrivateKey,
amount *big.Int,
sleep time.Duration,
name string,
args ...interface{}) {
args ...interface{}) error {
// 0. get the value of nonce, based on address
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatalf("failed to fetch the value of nonce from network: %+v", err)
log.Printf("failed to fetch the value of nonce from network: %+v", err)
panic(err)
}

// 0.5 get the gasPrice
gasPrice := big.NewInt(GasPrice)

fmt.Printf(
"==================================================\n"+
"write [%s<%s>]: \n"+
" msg sender: <%s>\n"+
" contract address: <%s>\n"+
" abi: <%s %s>\n"+
"%s: \n"+
" sender: <%s>, nonce<%d>\n"+
" contract: <%s>, abi: <%s %s>\n"+
"==================================================\n",
contract.name,
name,
fromAddress.Hex(),
nonce,
contract.address,
name, args)

data, err := contract.abi.Pack(name, args...)
if err != nil {
log.Fatal(err)
log.Printf("%s", err)
return err

}

if amount == nil {
Expand All @@ -121,16 +137,19 @@ func WriteContract(client *ethclient.Client,
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(big.NewInt(ChainId)), privateKey)
if err != nil {
log.Fatalf("failed to sign the unsignedTx offline: %+v", err)
log.Printf("failed to sign the unsignedTx offline: %+v", err)
return err
}

// 3. send rawTx
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
log.Printf("%s", err)
return err
}

time.Sleep(time.Second * sleep)
time.Sleep(sleep)
return nil
}


Expand Down Expand Up @@ -180,12 +199,14 @@ func transferOKT(client *ethclient.Client,
}
}

func sleep(second time.Duration) {
time.Sleep(second*time.Second)
}


func ReadContract(client *ethclient.Client, contract *Contract, name string, args ...interface{}) []interface{} {
func readContract(client *ethclient.Client, contract *Contract, name string, args ...interface{}) []interface{} {
data, err := contract.abi.Pack(name, args ...)
if err != nil {
log.Fatal(err)
panic(err)
}

msg := ethereum.CallMsg{
Expand Down Expand Up @@ -220,3 +241,78 @@ func initKey(key string) (*ecdsa.PrivateKey, common.Address){
return privateKey, senderAddress
}



func deployContract(client *ethclient.Client, fromAddress common.Address,
privateKey *ecdsa.PrivateKey, contract *Contract, blockTime time.Duration) error {

fmt.Printf("%s deploying contract\n", fromAddress.String())
chainID := big.NewInt(ChainId)
// 0. get the value of nonce, based on address
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Printf("failed to fetch the value of nonce from network: %+v", err)
return err
}

//1. simulate unsignedTx as you want, fill out the parameters into a unsignedTx
unsignedTx, err := deployContractTx(nonce, contract)
if err != nil {
return err
}
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Printf("failed to sign the unsignedTx offline: %+v", err)
return err
}

// 3. send rawTx
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Printf("SendTransaction err: %s", err)
return err
}

sleep(blockTime)
// 4. get the contract address based on tx hash
hash, err := utils.Hash(signedTx)
if err != nil {
log.Printf("Hash tx err: %s", err)
return err
}

receipt, err := client.TransactionReceipt(context.Background(), hash)
if err != nil {
log.Printf("TransactionReceipt err: %s", err)
return err
}

contract.address = receipt.ContractAddress.String()
contract.addr = receipt.ContractAddress

fmt.Printf("new contract address: %s\n", contract.address)
return nil
}

func deployContractTx(nonce uint64, contract *Contract) (*types.Transaction, error) {
value := big.NewInt(0)
// Constructor
input, err := contract.abi.Pack("")
if err != nil {
log.Printf("contract.abi.Pack err: %s", err)
return nil, err
}
data := append(contract.byteCode, input...)
return types.NewContractCreation(nonce, value, GasLimit, big.NewInt(GasPrice), data), err
}



func send(client *ethclient.Client, to, privKey string) {
privateKey, senderAddress := initKey(privKey)
toAddress := common.HexToAddress(to)

// send 0.001okt
transferOKT(client, senderAddress, toAddress, str2bigInt("0.001"), privateKey, 0)
}
67 changes: 67 additions & 0 deletions dev/client/contracts/counter/counter.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "counter",
"type": "uint256"
}
],
"name": "Added",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "counter",
"type": "uint256"
}
],
"name": "Changed",
"type": "event"
},
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "delta",
"type": "uint256"
}
],
"name": "add",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getCounter",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "subtract",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
1 change: 1 addition & 0 deletions dev/client/contracts/counter/counter.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60806040526000805534801561001457600080fd5b50610289806100246000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631003e2d2146100465780636deebae3146100745780638ada066e1461007e575b600080fd5b6100726004803603602081101561005c57600080fd5b810190808035906020019092919050505061009c565b005b61007c61011c565b005b61008661024b565b6040518082815260200191505060405180910390f35b80600054016000819055507f64a55044d1f2eddebe1b90e8e2853e8e96931cefadbfa0b2ceb34bee360619416000546040518082815260200191505060405180910390a17f938d2ee5be9cfb0f7270ee2eff90507e94b37625d9d2b3a61c97d30a4560b8296000546040518082815260200191505060405180910390a150565b60008054116040518060400160405280600f81526020017f434f554e5445525f544f4f5f4c4f570000000000000000000000000000000000815250906101fd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101c25780820151818401526020810190506101a7565b50505050905090810190601f1680156101ef5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506000808154809291906001900391905055507f938d2ee5be9cfb0f7270ee2eff90507e94b37625d9d2b3a61c97d30a4560b8296000546040518082815260200191505060405180910390a1565b6000805490509056fea265627a7a72315820c6f3b99c79c6b8f58139949ba74a7935659433d5296a69d6e828e5aab1c52d0864736f6c63430005110032
24 changes: 24 additions & 0 deletions dev/client/contracts/counter/counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity ^0.5.11;

contract Counter {
uint256 counter = 0;
string internal constant ERROR_TOO_LOW = "COUNTER_TOO_LOW";
event Changed(uint256 counter);
event Added(uint256 counter);

function add(uint256 delta) public {
counter = counter + delta;
emit Added(counter);
emit Changed(counter);
}

function subtract() public {
require(counter > 0, ERROR_TOO_LOW);
counter--;
emit Changed(counter);
}

function getCounter() public view returns (uint256) {
return counter;
}
}
7 changes: 2 additions & 5 deletions dev/client/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
module github.com/okex/exchain/dev/client

go 1.14

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/cosmos/cosmos-sdk v0.39.2
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/ethereum/go-ethereum v1.10.8
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/okex/exchain-ethereum-compatible v1.0.2
github.com/prometheus/tsdb v0.9.1 // indirect
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48 // indirect
)

replace github.com/cosmos/cosmos-sdk => github.com/okex/cosmos-sdk v0.39.2-exchain1
Loading

0 comments on commit 86dede2

Please sign in to comment.