|
| 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