Skip to content

Commit e44b74d

Browse files
committed
feat_: gas estimator
1 parent 15dc95e commit e44b74d

23 files changed

+3648
-0
lines changed

examples/gas-comparison/go.mod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module gas-comparison
2+
3+
go 1.23.0
4+
5+
replace github.com/status-im/go-wallet-sdk => ../../
6+
7+
require (
8+
github.com/ethereum/go-ethereum v1.16.0
9+
github.com/status-im/go-wallet-sdk v0.0.0-00010101000000-000000000000
10+
)
11+
12+
require (
13+
github.com/Microsoft/go-winio v0.6.2 // indirect
14+
github.com/StackExchange/wmi v1.2.1 // indirect
15+
github.com/bits-and-blooms/bitset v1.20.0 // indirect
16+
github.com/consensys/gnark-crypto v0.18.0 // indirect
17+
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
18+
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
19+
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
20+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
21+
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
22+
github.com/ethereum/go-verkle v0.2.2 // indirect
23+
github.com/go-ole/go-ole v1.3.0 // indirect
24+
github.com/gorilla/websocket v1.4.2 // indirect
25+
github.com/holiman/uint256 v1.3.2 // indirect
26+
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
27+
github.com/supranational/blst v0.3.14 // indirect
28+
github.com/tklauser/go-sysconf v0.3.12 // indirect
29+
github.com/tklauser/numcpus v0.6.1 // indirect
30+
golang.org/x/crypto v0.36.0 // indirect
31+
golang.org/x/sync v0.12.0 // indirect
32+
golang.org/x/sys v0.31.0 // indirect
33+
)

examples/gas-comparison/go.sum

Lines changed: 179 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package old
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"time"
7+
8+
ethereum "github.com/ethereum/go-ethereum"
9+
ethCommon "github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/common/hexutil"
11+
gethParams "github.com/ethereum/go-ethereum/params"
12+
)
13+
14+
const (
15+
UnknownChainID uint64 = 0
16+
EthereumMainnet uint64 = 1
17+
EthereumSepolia uint64 = 11155111
18+
OptimismMainnet uint64 = 10
19+
OptimismSepolia uint64 = 11155420
20+
ArbitrumMainnet uint64 = 42161
21+
ArbitrumSepolia uint64 = 421614
22+
BSCMainnet uint64 = 56
23+
BSCTestnet uint64 = 97
24+
AnvilMainnet uint64 = 31337
25+
BaseMainnet uint64 = 8453
26+
BaseSepolia uint64 = 84532
27+
StatusNetworkSepolia uint64 = 1660990954
28+
TestnetChainID uint64 = 777333
29+
)
30+
31+
var AverageBlockDurationForChain = map[uint64]time.Duration{
32+
UnknownChainID: time.Duration(12000) * time.Millisecond,
33+
EthereumMainnet: time.Duration(12000) * time.Millisecond,
34+
OptimismMainnet: time.Duration(2000) * time.Millisecond,
35+
ArbitrumMainnet: time.Duration(250) * time.Millisecond,
36+
BaseMainnet: time.Duration(2000) * time.Millisecond,
37+
BSCMainnet: time.Duration(3000) * time.Millisecond,
38+
StatusNetworkSepolia: time.Duration(2000) * time.Millisecond,
39+
}
40+
41+
func GweiToEth(val *big.Float) *big.Float {
42+
return new(big.Float).Quo(val, big.NewFloat(1000000000))
43+
}
44+
45+
func WeiToGwei(val *big.Int) *big.Float {
46+
result := new(big.Float)
47+
result.SetInt(val)
48+
49+
unit := new(big.Int)
50+
unit.SetInt64(gethParams.GWei)
51+
52+
return result.Quo(result, new(big.Float).SetInt(unit))
53+
}
54+
55+
func GetBlockCreationTimeForChain(chainID uint64) time.Duration {
56+
blockDuration, found := AverageBlockDurationForChain[chainID]
57+
if !found {
58+
blockDuration = AverageBlockDurationForChain[UnknownChainID]
59+
}
60+
return blockDuration
61+
}
62+
63+
// Special functions to hardcode the nature of some special chains (eg. Status Network), where we cannot deduce EIP-1559 compatibility in a generic way
64+
65+
// IsPartiallyOrFullyGaslessChain returns true if the chain is fully or partially (no base or no priority fee) gasless
66+
func IsPartiallyOrFullyGaslessChain(chainID uint64) bool {
67+
return chainID == StatusNetworkSepolia
68+
}
69+
70+
// IsPartiallyOrFullyGaslessChainEIP1559Compatible throws an error if the chain is not partially or fully gasless, if it is, returns true if the chain is EIP-1559 compatible
71+
func IsPartiallyOrFullyGaslessChainEIP1559Compatible(chainID uint64) (bool, error) {
72+
if !IsPartiallyOrFullyGaslessChain(chainID) {
73+
return false, fmt.Errorf("chain %d is not supposed to be gasless", chainID) // for non-gasless chains, we should not use this function
74+
}
75+
return chainID == StatusNetworkSepolia, nil
76+
}
77+
78+
func ZeroAddress() ethCommon.Address {
79+
return ethCommon.Address{}
80+
}
81+
82+
func ZeroBigIntValue() *big.Int {
83+
return big.NewInt(0)
84+
}
85+
86+
func ZeroHash() ethCommon.Hash {
87+
return ethCommon.Hash{}
88+
}
89+
90+
func ToCallArg(msg ethereum.CallMsg) interface{} {
91+
arg := map[string]interface{}{
92+
"from": msg.From,
93+
"to": msg.To,
94+
}
95+
if len(msg.Data) > 0 {
96+
arg["data"] = hexutil.Bytes(msg.Data)
97+
}
98+
if msg.Value != nil {
99+
arg["value"] = (*hexutil.Big)(msg.Value)
100+
}
101+
if msg.Gas != 0 {
102+
arg["gas"] = hexutil.Uint64(msg.Gas)
103+
}
104+
if msg.GasPrice != nil {
105+
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
106+
}
107+
return arg
108+
}

0 commit comments

Comments
 (0)