github.com/cwntr/go-defi@v0.0.0-20210629134751-07f9ec2f7e66/gas_price.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"math/big"
     8  	"time"
     9  
    10  	"github.com/524119574/go-defi/client"
    11  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    12  	"github.com/ethereum/go-ethereum/crypto"
    13  	"github.com/ethereum/go-ethereum/ethclient"
    14  )
    15  
    16  func measureGasPrice() {
    17  	var (
    18  		blockNum  *big.Int
    19  		diffTotal *big.Int
    20  		exceedCnt int
    21  	)
    22  	key, err := crypto.HexToECDSA("b8c1b5c1d81f9475fdf2e334517d29f733bdfa40682207571b12fc1142cbf329")
    23  	if err != nil {
    24  		log.Fatalf("Failed to create private key: %v", err)
    25  	}
    26  	ethClient, err := ethclient.Dial("http://127.0.0.1:8545")
    27  	if err != nil {
    28  		log.Fatalf("Failed to connect to ETH: %v", err)
    29  	}
    30  
    31  	// Create a New Defi Client
    32  	defiClient := client.NewClient(bind.NewKeyedTransactor(key), ethClient)
    33  	if err != nil {
    34  		log.Fatalf("Error creating client: %v.", err)
    35  	}
    36  
    37  	blockNum = big.NewInt(0)
    38  	blockNum.SetString("11500000", 10)
    39  	exceedCnt = 0
    40  	diffTotal = big.NewInt(0)
    41  	cnt := 0
    42  
    43  	for cnt < 100 {
    44  		gasPrice, err := defiClient.SuggestGasPrice(blockNum)
    45  		if err != nil {
    46  			log.Printf("Error getting suggested gas price: %v. ", err)
    47  			blockNum = blockNum.Add(blockNum, big.NewInt(1))
    48  			continue
    49  		}
    50  		fmt.Printf("block %v gas price is: %v \n", blockNum, gasPrice)
    51  
    52  		blockNum = blockNum.Add(blockNum, big.NewInt(1))
    53  		block, err := ethClient.BlockByNumber(context.Background(), blockNum)
    54  		if err != nil {
    55  			log.Printf("Error getting next block transaction: %v. ", err)
    56  			continue
    57  		}
    58  
    59  		txs := block.Transactions()
    60  		if len(txs) == 0 {
    61  			log.Printf("jump: %v\n\n", blockNum)
    62  			continue
    63  		}
    64  
    65  		minGas := txs[0].GasPrice()
    66  		maxGas := txs[0].GasPrice()
    67  		for _, tx := range txs {
    68  			if minGas.Cmp(tx.GasPrice()) == 1 {
    69  				minGas = tx.GasPrice()
    70  			}
    71  			if maxGas.Cmp(tx.GasPrice()) == -1 {
    72  				maxGas = tx.GasPrice()
    73  			}
    74  		}
    75  
    76  		t := new(big.Int)
    77  		if gasPrice.Cmp(minGas) != -1 {
    78  			exceedCnt++
    79  			t.Sub(gasPrice, minGas)
    80  			diffTotal.Add(diffTotal, t)
    81  		}
    82  		cnt++
    83  	}
    84  
    85  	fmt.Printf("Diff total is: %v \n", diffTotal)
    86  	fmt.Printf("Exceed Cnt is: %v \n", exceedCnt)
    87  	fmt.Printf("Diff average is: %v \n", diffTotal.Div(diffTotal, big.NewInt(int64(exceedCnt))))
    88  }
    89  
    90  func measureExecutionTime() {
    91  	key, err := crypto.HexToECDSA("b8c1b5c1d81f9475fdf2e334517d29f733bdfa40682207571b12fc1142cbf329")
    92  	if err != nil {
    93  		log.Fatalf("Failed to create private key: %v", err)
    94  	}
    95  	ethClient, err := ethclient.Dial("http://127.0.0.1:8545")
    96  	if err != nil {
    97  		log.Fatalf("Failed to connect to ETH: %v", err)
    98  	}
    99  
   100  	// Create a New Defi Client
   101  	defiClient := client.NewClient(bind.NewKeyedTransactor(key), ethClient)
   102  	if err != nil {
   103  		log.Fatalf("Error creating client: %v.", err)
   104  	}
   105  
   106  	start := time.Now()
   107  	for i := 0; i < 1000; i++ {
   108  		actions := new(client.Actions)
   109  
   110  		// Add the flash loan action
   111  		actions.Add(
   112  			defiClient.Compound().SupplyActions(big.NewInt(1e18), client.ETH),
   113  		)
   114  		defiClient.CombineActions(actions)
   115  	}
   116  	duration := time.Since(start)
   117  	// Formatted string, such as "2h3m0.5s" or "4.503μs"
   118  	fmt.Println("Time take in seconds", duration.Seconds()/1000.0)
   119  
   120  }
   121  
   122  func main() {
   123  	// measureExecutionTime()
   124  	measureGasPrice()
   125  }