2024-08-28 11:17:59 +00:00
|
|
|
package fees
|
2022-03-29 21:12:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-12 12:25:32 +00:00
|
|
|
"math"
|
2022-03-29 21:12:05 +00:00
|
|
|
"math/big"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2024-03-25 12:40:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2024-06-20 09:03:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-03-29 21:12:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2024-03-25 12:40:00 +00:00
|
|
|
gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle"
|
2022-03-29 21:12:05 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-05-14 19:11:16 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2024-05-16 07:37:36 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/common"
|
2022-03-29 21:12:05 +00:00
|
|
|
)
|
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
type GasFeeMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
GasFeeLow GasFeeMode = iota
|
|
|
|
GasFeeMedium
|
|
|
|
GasFeeHigh
|
|
|
|
)
|
|
|
|
|
2024-06-19 09:20:41 +00:00
|
|
|
type MaxFeesLevels struct {
|
2024-06-20 09:03:42 +00:00
|
|
|
Low *hexutil.Big `json:"low"`
|
|
|
|
Medium *hexutil.Big `json:"medium"`
|
|
|
|
High *hexutil.Big `json:"high"`
|
2024-06-19 09:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SuggestedFees struct {
|
|
|
|
GasPrice *big.Int `json:"gasPrice"`
|
|
|
|
BaseFee *big.Int `json:"baseFee"`
|
|
|
|
MaxFeesLevels *MaxFeesLevels `json:"maxFeesLevels"`
|
|
|
|
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas"`
|
|
|
|
L1GasFee *big.Float `json:"l1GasFee,omitempty"`
|
|
|
|
EIP1559Enabled bool `json:"eip1559Enabled"`
|
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
// //////////////////////////////////////////////////////////////////////////////
|
2024-06-19 09:20:41 +00:00
|
|
|
// TODO: remove `SuggestedFeesGwei` struct once new router is in place
|
2024-05-14 19:11:16 +00:00
|
|
|
// //////////////////////////////////////////////////////////////////////////////
|
2024-06-19 09:20:41 +00:00
|
|
|
type SuggestedFeesGwei struct {
|
2022-03-29 21:12:05 +00:00
|
|
|
GasPrice *big.Float `json:"gasPrice"`
|
|
|
|
BaseFee *big.Float `json:"baseFee"`
|
|
|
|
MaxPriorityFeePerGas *big.Float `json:"maxPriorityFeePerGas"`
|
|
|
|
MaxFeePerGasLow *big.Float `json:"maxFeePerGasLow"`
|
|
|
|
MaxFeePerGasMedium *big.Float `json:"maxFeePerGasMedium"`
|
|
|
|
MaxFeePerGasHigh *big.Float `json:"maxFeePerGasHigh"`
|
2024-04-05 14:06:37 +00:00
|
|
|
L1GasFee *big.Float `json:"l1GasFee,omitempty"`
|
2022-06-09 13:09:56 +00:00
|
|
|
EIP1559Enabled bool `json:"eip1559Enabled"`
|
2022-03-29 21:12:05 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 11:17:59 +00:00
|
|
|
func (m *MaxFeesLevels) FeeFor(mode GasFeeMode) *big.Int {
|
2024-06-19 09:20:41 +00:00
|
|
|
if mode == GasFeeLow {
|
2024-07-25 12:15:30 +00:00
|
|
|
return m.Low.ToInt()
|
2024-06-19 09:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mode == GasFeeHigh {
|
2024-07-25 12:15:30 +00:00
|
|
|
return m.High.ToInt()
|
2024-06-19 09:20:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 12:15:30 +00:00
|
|
|
return m.Medium.ToInt()
|
2024-05-14 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 11:17:59 +00:00
|
|
|
func (s *SuggestedFees) FeeFor(mode GasFeeMode) *big.Int {
|
|
|
|
return s.MaxFeesLevels.FeeFor(mode)
|
2024-07-25 12:15:30 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2022-07-12 12:25:32 +00:00
|
|
|
const inclusionThreshold = 0.95
|
|
|
|
|
|
|
|
type TransactionEstimation int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown TransactionEstimation = iota
|
|
|
|
LessThanOneMinute
|
|
|
|
LessThanThreeMinutes
|
|
|
|
LessThanFiveMinutes
|
|
|
|
MoreThanFiveMinutes
|
|
|
|
)
|
|
|
|
|
2022-03-29 21:12:05 +00:00
|
|
|
type FeeHistory struct {
|
|
|
|
BaseFeePerGas []string `json:"baseFeePerGas"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FeeManager struct {
|
2024-09-19 18:24:06 +00:00
|
|
|
RPCClient rpc.ClientInterface
|
2022-03-29 21:12:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 14:00:32 +00:00
|
|
|
func (f *FeeManager) SuggestedFees(ctx context.Context, chainID uint64) (*SuggestedFees, error) {
|
2022-03-29 21:12:05 +00:00
|
|
|
backend, err := f.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gasPrice, err := backend.SuggestGasPrice(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-09 13:09:56 +00:00
|
|
|
maxPriorityFeePerGas, err := backend.SuggestGasTipCap(ctx)
|
2022-03-29 21:12:05 +00:00
|
|
|
if err != nil {
|
2022-06-09 13:09:56 +00:00
|
|
|
return &SuggestedFees{
|
2024-06-19 09:20:41 +00:00
|
|
|
GasPrice: gasPrice,
|
|
|
|
BaseFee: big.NewInt(0),
|
|
|
|
MaxPriorityFeePerGas: big.NewInt(0),
|
|
|
|
MaxFeesLevels: &MaxFeesLevels{
|
2024-07-25 12:15:30 +00:00
|
|
|
Low: (*hexutil.Big)(gasPrice),
|
|
|
|
Medium: (*hexutil.Big)(gasPrice),
|
|
|
|
High: (*hexutil.Big)(gasPrice),
|
2024-06-19 09:20:41 +00:00
|
|
|
},
|
|
|
|
EIP1559Enabled: false,
|
2022-06-09 13:09:56 +00:00
|
|
|
}, nil
|
2022-03-29 21:12:05 +00:00
|
|
|
}
|
2024-05-16 07:37:36 +00:00
|
|
|
baseFee, err := f.getBaseFee(ctx, backend)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-19 09:20:41 +00:00
|
|
|
return &SuggestedFees{
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
BaseFee: baseFee,
|
|
|
|
MaxPriorityFeePerGas: maxPriorityFeePerGas,
|
|
|
|
MaxFeesLevels: &MaxFeesLevels{
|
2024-06-20 09:03:42 +00:00
|
|
|
Low: (*hexutil.Big)(new(big.Int).Add(baseFee, maxPriorityFeePerGas)),
|
|
|
|
Medium: (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(baseFee, big.NewInt(2)), maxPriorityFeePerGas)),
|
|
|
|
High: (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(baseFee, big.NewInt(3)), maxPriorityFeePerGas)),
|
2024-06-19 09:20:41 +00:00
|
|
|
},
|
|
|
|
EIP1559Enabled: true,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-03-29 21:12:05 +00:00
|
|
|
|
2024-06-19 09:20:41 +00:00
|
|
|
func (f *FeeManager) SuggestedFeesGwei(ctx context.Context, chainID uint64) (*SuggestedFeesGwei, error) {
|
|
|
|
fees, err := f.SuggestedFees(ctx, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-03-29 21:12:05 +00:00
|
|
|
}
|
2024-06-19 09:20:41 +00:00
|
|
|
return &SuggestedFeesGwei{
|
2024-08-28 11:17:59 +00:00
|
|
|
GasPrice: common.WeiToGwei(fees.GasPrice),
|
|
|
|
BaseFee: common.WeiToGwei(fees.BaseFee),
|
|
|
|
MaxPriorityFeePerGas: common.WeiToGwei(fees.MaxPriorityFeePerGas),
|
|
|
|
MaxFeePerGasLow: common.WeiToGwei(fees.MaxFeesLevels.Low.ToInt()),
|
|
|
|
MaxFeePerGasMedium: common.WeiToGwei(fees.MaxFeesLevels.Medium.ToInt()),
|
|
|
|
MaxFeePerGasHigh: common.WeiToGwei(fees.MaxFeesLevels.High.ToInt()),
|
2024-06-19 09:20:41 +00:00
|
|
|
EIP1559Enabled: fees.EIP1559Enabled,
|
2022-03-29 21:12:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-07-12 12:25:32 +00:00
|
|
|
|
2024-05-16 07:37:36 +00:00
|
|
|
func (f *FeeManager) getBaseFee(ctx context.Context, client chain.ClientInterface) (*big.Int, error) {
|
2024-05-14 19:11:16 +00:00
|
|
|
header, err := client.HeaderByNumber(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-16 07:37:36 +00:00
|
|
|
chainID := client.NetworkID()
|
2024-05-14 19:11:16 +00:00
|
|
|
config := params.MainnetChainConfig
|
2024-05-16 07:37:36 +00:00
|
|
|
switch chainID {
|
2024-07-04 11:30:22 +00:00
|
|
|
case common.EthereumSepolia,
|
|
|
|
common.OptimismSepolia,
|
|
|
|
common.ArbitrumSepolia:
|
2024-05-14 19:11:16 +00:00
|
|
|
config = params.SepoliaChainConfig
|
2024-07-04 11:30:22 +00:00
|
|
|
case common.EthereumGoerli,
|
|
|
|
common.OptimismGoerli,
|
|
|
|
common.ArbitrumGoerli:
|
2024-05-16 07:37:36 +00:00
|
|
|
config = params.GoerliChainConfig
|
2024-05-14 19:11:16 +00:00
|
|
|
}
|
|
|
|
baseFee := misc.CalcBaseFee(config, header)
|
|
|
|
return baseFee, nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 09:20:41 +00:00
|
|
|
func (f *FeeManager) TransactionEstimatedTime(ctx context.Context, chainID uint64, maxFeePerGas *big.Int) TransactionEstimation {
|
2022-07-12 12:25:32 +00:00
|
|
|
fees, err := f.getFeeHistorySorted(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// pEvent represents the probability of the transaction being included in a block,
|
|
|
|
// we assume this one is static over time, in reality it is not.
|
|
|
|
pEvent := 0.0
|
|
|
|
for idx, fee := range fees {
|
2024-06-19 09:20:41 +00:00
|
|
|
if fee.Cmp(maxFeePerGas) == 1 || idx == len(fees)-1 {
|
2022-07-12 12:25:32 +00:00
|
|
|
pEvent = float64(idx) / float64(len(fees))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probability of next 4 blocks including the transaction (less than 1 minute)
|
|
|
|
// Generalising the formula: P(AUB) = P(A) + P(B) - P(A∩B) for 4 events and in our context P(A) == P(B) == pEvent
|
|
|
|
// The factors are calculated using the combinations formula
|
|
|
|
probability := pEvent*4 - 6*(math.Pow(pEvent, 2)) + 4*(math.Pow(pEvent, 3)) - (math.Pow(pEvent, 4))
|
|
|
|
if probability >= inclusionThreshold {
|
|
|
|
return LessThanOneMinute
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probability of next 12 blocks including the transaction (less than 5 minutes)
|
|
|
|
// Generalising the formula: P(AUB) = P(A) + P(B) - P(A∩B) for 20 events and in our context P(A) == P(B) == pEvent
|
|
|
|
// The factors are calculated using the combinations formula
|
|
|
|
probability = pEvent*12 -
|
|
|
|
66*(math.Pow(pEvent, 2)) +
|
|
|
|
220*(math.Pow(pEvent, 3)) -
|
|
|
|
495*(math.Pow(pEvent, 4)) +
|
|
|
|
792*(math.Pow(pEvent, 5)) -
|
|
|
|
924*(math.Pow(pEvent, 6)) +
|
|
|
|
792*(math.Pow(pEvent, 7)) -
|
|
|
|
495*(math.Pow(pEvent, 8)) +
|
|
|
|
220*(math.Pow(pEvent, 9)) -
|
|
|
|
66*(math.Pow(pEvent, 10)) +
|
|
|
|
12*(math.Pow(pEvent, 11)) -
|
|
|
|
math.Pow(pEvent, 12)
|
|
|
|
if probability >= inclusionThreshold {
|
|
|
|
return LessThanThreeMinutes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probability of next 20 blocks including the transaction (less than 5 minutes)
|
|
|
|
// Generalising the formula: P(AUB) = P(A) + P(B) - P(A∩B) for 20 events and in our context P(A) == P(B) == pEvent
|
|
|
|
// The factors are calculated using the combinations formula
|
|
|
|
probability = pEvent*20 -
|
|
|
|
190*(math.Pow(pEvent, 2)) +
|
|
|
|
1140*(math.Pow(pEvent, 3)) -
|
|
|
|
4845*(math.Pow(pEvent, 4)) +
|
|
|
|
15504*(math.Pow(pEvent, 5)) -
|
|
|
|
38760*(math.Pow(pEvent, 6)) +
|
|
|
|
77520*(math.Pow(pEvent, 7)) -
|
|
|
|
125970*(math.Pow(pEvent, 8)) +
|
|
|
|
167960*(math.Pow(pEvent, 9)) -
|
|
|
|
184756*(math.Pow(pEvent, 10)) +
|
|
|
|
167960*(math.Pow(pEvent, 11)) -
|
|
|
|
125970*(math.Pow(pEvent, 12)) +
|
|
|
|
77520*(math.Pow(pEvent, 13)) -
|
|
|
|
38760*(math.Pow(pEvent, 14)) +
|
|
|
|
15504*(math.Pow(pEvent, 15)) -
|
|
|
|
4845*(math.Pow(pEvent, 16)) +
|
|
|
|
1140*(math.Pow(pEvent, 17)) -
|
|
|
|
190*(math.Pow(pEvent, 18)) +
|
|
|
|
20*(math.Pow(pEvent, 19)) -
|
|
|
|
math.Pow(pEvent, 20)
|
|
|
|
if probability >= inclusionThreshold {
|
|
|
|
return LessThanFiveMinutes
|
|
|
|
}
|
|
|
|
|
|
|
|
return MoreThanFiveMinutes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FeeManager) getFeeHistorySorted(chainID uint64) ([]*big.Int, error) {
|
|
|
|
var feeHistory FeeHistory
|
|
|
|
|
|
|
|
err := f.RPCClient.Call(&feeHistory, chainID, "eth_feeHistory", 101, "latest", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fees := []*big.Int{}
|
|
|
|
for _, fee := range feeHistory.BaseFeePerGas {
|
|
|
|
i := new(big.Int)
|
|
|
|
i.SetString(strings.Replace(fee, "0x", "", 1), 16)
|
|
|
|
fees = append(fees, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(fees, func(i, j int) bool { return fees[i].Cmp(fees[j]) < 0 })
|
|
|
|
return fees, nil
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
// Returns L1 fee for placing a transaction to L1 chain, appicable only for txs made from L2.
|
|
|
|
func (f *FeeManager) GetL1Fee(ctx context.Context, chainID uint64, input []byte) (uint64, error) {
|
|
|
|
if chainID == common.EthereumMainnet || chainID == common.EthereumSepolia && chainID != common.EthereumGoerli {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2024-03-25 12:40:00 +00:00
|
|
|
ethClient, err := f.RPCClient.EthClient(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contractAddress, err := gaspriceoracle.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contract, err := gaspriceoracle.NewGaspriceoracleCaller(contractAddress, ethClient)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
callOpt := &bind.CallOpts{}
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
result, err := contract.GetL1Fee(callOpt, input)
|
2024-03-25 12:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Uint64(), nil
|
|
|
|
}
|