Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
0g-wh committed Nov 18, 2024
1 parent 4a6d56f commit 34d64d7
Show file tree
Hide file tree
Showing 10 changed files with 1,419 additions and 113 deletions.
202 changes: 202 additions & 0 deletions contracts/UniswapV2Factory.json

Large diffs are not rendered by default.

982 changes: 982 additions & 0 deletions contracts/UniswapV2Router02.json

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions lib/cmd/run/ethlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (el *EthereumListener) listenForMessages() {
func (el *EthereumListener) handleNewHead(response map[string]interface{}) {
params := response["params"].(map[string]interface{})
result := params["result"].(map[string]interface{})

blockNo := result["number"].(string)

request := map[string]interface{}{
Expand All @@ -100,11 +99,28 @@ func (el *EthereumListener) handleNewHead(response map[string]interface{}) {
if err != nil {
log.Println("Failed to send block request:", err)
}

request = map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getLogs",
"params": []interface{}{
map[string]interface{}{
"fromBlock": blockNo,
"toBlock": blockNo,
},
},
}
err = el.conn.WriteJSON(request)
if err != nil {
log.Println("Failed to send log request:", err)
}
}

func (el *EthereumListener) handleBlockResponse(response map[string]interface{}) {
if result, ok := response["result"].(map[string]interface{}); ok {
if txns, ok := result["transactions"].([]interface{}); ok {
fmt.Println("BlockNo:", result["number"], "Txn[0]", txns[0])
el.limiter.IncreaseLimit(len(txns))
ts, _ := strconv.ParseInt(result["timestamp"].(string)[2:], 16, 64)
gasUsed, _ := strconv.ParseInt(result["gasUsed"].(string)[2:], 16, 64)
Expand Down Expand Up @@ -161,9 +177,12 @@ func (el *EthereumListener) handleBlockResponse(response map[string]interface{})
fmt.Printf("Best TPS: %d GasUsed%%: %.2f%%\n", el.bestTPS, el.gasUsedAtBestTPS*100)
el.Close()
}

}
}
} else {
if result, ok := response["result"].([]interface{}); ok {
fmt.Println("Logs:", len(result))
}
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/contract_meta_data/uniswap/meta_data.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/generator/gas_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const (
simpleTransferGasLimit = uint64(21000)
erc20ContractGasLimit = uint64(810000)
erc20TransferGasLimit = uint64(210000)
uniswapContractGasLimit = uint64(3210000)
uniswapCreatePairGasLimit = uint64(210000)
uniswapContractGasLimit = uint64(10000000)
uniswapCreatePairGasLimit = uint64(10000000)
uniswapMintGasLimit = uint64(210000)
uniswapSwapGasLimit = uint64(210000)
uniswapSwapGasLimit = uint64(10000000)
)
151 changes: 114 additions & 37 deletions lib/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package generator

import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"strings"
"time"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"

"github.com/0glabs/evmchainbench/lib/account"
"github.com/0glabs/evmchainbench/lib/contract_meta_data/erc20"
"github.com/0glabs/evmchainbench/lib/store"
"github.com/0glabs/evmchainbench/lib/util"
)
Expand Down Expand Up @@ -96,10 +99,86 @@ func NewGenerator(rpcUrl, faucetPrivateKey string, senderCount, txCount int, sho
}, nil
}

func (g *Generator) prepareSenders() error {
func (g *Generator) approveERC20(token common.Address, spender common.Address) {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
return err
panic(err)
}
defer client.Close()
txs := types.Transactions{}
for _, sender := range append(g.Senders, g.FaucetAccount) {
tx := GenerateContractCallingTx(
sender.PrivateKey,
token.Hex(),
sender.GetNonce(),
g.ChainID,
g.GasPrice,
erc20TransferGasLimit,
erc20.MyTokenABI,
"approve",
spender,
big.NewInt(10000000000000),
)

err = client.SendTransaction(context.Background(), tx)
if err != nil {
panic(err)
}

if g.ShouldPersist {
g.Store.AddPrepareTx(tx)
}

txs = append(txs, tx)
}

err = util.WaitForReceiptsOfTxs(client, txs, 20*time.Second)
if err != nil {
panic(err)
}
}

func (g *Generator) prepareERC20(contractAddressStr string) {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
panic(err)
}
defer client.Close()
txs := types.Transactions{}
for _, sender := range g.Senders {
tx := GenerateContractCallingTx(
g.FaucetAccount.PrivateKey,
contractAddressStr,
g.FaucetAccount.GetNonce(),
g.ChainID,
g.GasPrice,
erc20TransferGasLimit,
erc20.MyTokenABI,
"transfer",
common.HexToAddress(sender.Address.Hex()),
big.NewInt(10000000),
)

err = client.SendTransaction(context.Background(), tx)
if err != nil {
panic(err)
}

if g.ShouldPersist {
g.Store.AddPrepareTx(tx)
}
}

err = util.WaitForReceiptsOfTxs(client, txs, 20*time.Second)
if err != nil {
panic(err)
}
}

func (g *Generator) prepareSenders() {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
panic(err)
}
defer client.Close()

Expand All @@ -111,40 +190,39 @@ func (g *Generator) prepareSenders() error {
for _, recipient := range g.Senders {
signedTx, err := GenerateSimpleTransferTx(g.FaucetAccount.PrivateKey, recipient.Address.Hex(), g.FaucetAccount.GetNonce(), g.ChainID, g.GasPrice, value, g.EIP1559)
if err != nil {
return err
panic(err)
}

err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return err
panic(err)
}

if g.ShouldPersist {
g.Store.AddPrepareTx(signedTx)
if err != nil {
return err
}
}

txs = append(txs, signedTx)
}

err = util.WaitForReceiptsOfTxs(client, txs, 20*time.Second)
if err != nil {
return err
panic(err)
}

return nil
}

func (g *Generator) estimateGas(msg ethereum.CallMsg) (uint64, error) {
func (g *Generator) estimateGas(msg ethereum.CallMsg) uint64 {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
return 0, err
panic(err)
}
defer client.Close()

return client.EstimateGas(context.Background(), msg)
gas, err := client.EstimateGas(context.Background(), msg)
if err != nil {
panic(err)
}
return gas
}

func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI string, args ...interface{}) (common.Address, error) {
Expand All @@ -153,7 +231,6 @@ func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI str
return common.Address{}, err
}
defer client.Close()

tx, err := GenerateContractCreationTx(
g.FaucetAccount.PrivateKey,
g.FaucetAccount.GetNonce(),
Expand All @@ -165,17 +242,18 @@ func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI str
args...,
)
if err != nil {
return common.Address{}, err
panic(err)
}

err = client.SendTransaction(context.Background(), tx)
if err != nil {
return common.Address{}, err
panic(err)
}

ercContractAddress, err := bind.WaitDeployed(context.Background(), client, tx)
if err != nil {
return common.Address{}, err
fmt.Println("tx hash:", tx.Hash().Hex())
panic(err)
}

if g.ShouldPersist {
Expand All @@ -185,14 +263,14 @@ func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI str
return ercContractAddress, nil
}

func (g *Generator) executeContractFunction(gasLimit uint64, contractAddress common.Address, contractABI, methodName string, args ...interface{}) error {
func (g *Generator) executeContractFunction(gasLimit uint64, contractAddress common.Address, contractABI, methodName string, args ...interface{}) {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
return err
panic(err)
}
defer client.Close()

tx, err := GenerateContractCallingTx(
tx := GenerateContractCallingTx(
g.FaucetAccount.PrivateKey,
contractAddress.Hex(),
g.FaucetAccount.GetNonce(),
Expand All @@ -203,46 +281,45 @@ func (g *Generator) executeContractFunction(gasLimit uint64, contractAddress com
methodName,
args...,
)

err = client.SendTransaction(context.Background(), tx)
if err != nil {
return nil
panic(err)
}

err = client.SendTransaction(context.Background(), tx)
receipt, err := bind.WaitMined(context.Background(), client, tx)
if err != nil {
return err
panic(err)
}

_, err = bind.WaitMined(context.Background(), client, tx)
receiptJSON, err := json.MarshalIndent(receipt, "", " ")
if err != nil {
return err
log.Fatalf("Failed to marshal receipt to JSON: %v", err)
}

fmt.Println(string(receiptJSON))

if g.ShouldPersist {
g.Store.AddPrepareTx(tx)
if err != nil {
return err
}
}

return nil
}

func (g *Generator) callContractView(contractAddress common.Address, contractABI, methodName string, args ...interface{}) ([]interface{}, error) {
func (g *Generator) callContractView(contractAddress common.Address, contractABI, methodName string, args ...interface{}) []interface{} {
client, err := ethclient.Dial(g.RpcUrl)
if err != nil {
return []interface{}{}, err
panic(err)
}
defer client.Close()

// Parse the contract's ABI
parsedABI, err := abi.JSON(strings.NewReader(contractABI))
if err != nil {
return []interface{}{}, err
panic(err)
}

data, err := parsedABI.Pack(methodName, args...)
if err != nil {
return []interface{}{}, err
panic(err)
}

// Create a call message
Expand All @@ -254,13 +331,13 @@ func (g *Generator) callContractView(contractAddress common.Address, contractABI
// Send the call
result, err := client.CallContract(context.Background(), msg, nil)
if err != nil {
return []interface{}{}, err
panic(err)
}

unpacked, err := parsedABI.Unpack(methodName, result)
if err != nil {
return []interface{}{}, err
panic(err)
}

return unpacked, nil
return unpacked
}
Loading

0 comments on commit 34d64d7

Please sign in to comment.