Skip to content

Commit

Permalink
add userop gasCompute
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Mar 12, 2024
1 parent abf8840 commit 9ffba1d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 32 deletions.
16 changes: 7 additions & 9 deletions common/model/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ type TryPayUserOpResponse struct {
}

type ComputeGasResponse struct {
CallGasLimit uint64 `json:"call_gas_limit"`
GasPriceInWei uint64 `json:"gas_price_wei"` // wei
GasPriceInGwei *big.Float `json:"gas_price_gwei"`
GasPriceInEther string `json:"gas_price_ether"`
TokenCost string `json:"token_cost"`
Network types.Network `json:"network"`
Token types.TokenType `json:"token"`
UsdCost string `json:"usd_cost"`
BlobEnable bool `json:"blob_enable"`
GasInfo *GasPrice `json:"gas_info"`
TokenCost string `json:"token_cost"`
Network types.Network `json:"network"`
Token types.TokenType `json:"token"`
UsdCost string `json:"usd_cost"`
BlobEnable bool `json:"blob_enable"`
MaxFee big.Int `json:"max_fee"`
}
type PayReceipt struct {
TransactionHash string `json:"transaction_hash"`
Expand Down
12 changes: 12 additions & 0 deletions common/model/gas_price.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

import "math/big"

type GasPrice struct {
MaxBasePriceWei *big.Int `json:"max_base_price_wei"`
MaxBasePriceGwei *big.Float `json:"max_base_price_gwei"`
MaxBasePriceEther *string `json:"max_base_price_ether"`
MaxPriorityPriceWei *big.Int `json:"max_priority_price_wei"`
MaxPriorityPriceGwei *big.Float `json:"max_priority_price_gwei"`
MaxPriorityPriceEther *string `json:"max_priority_price_ether"`
}
5 changes: 4 additions & 1 deletion common/model/user_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
)

// UserOperation entrypoint v0.0.6
// verificationGasLimit validateUserOp ,validatePaymasterUserOp limit
// callGasLimit calldata Execute gas limit
// preVerificationGas
type UserOperation struct {
Sender common.Address `json:"sender" mapstructure:"sender" binding:"required,hexParam"`
Nonce *big.Int `json:"nonce" mapstructure:"nonce" binding:"required"`
InitCode []byte `json:"initCode" mapstructure:"initCode" `
CallData []byte `json:"callData" mapstructure:"callData" binding:"required"`
CallGasLimit *big.Int `json:"callGasLimit" mapstructure:"callGasLimit" binding:"required"`
VerificationGasList *big.Int `json:"verificationGasLimit" mapstructure:"verificationGasLimit" binding:"required"`
VerificationGasLimit *big.Int `json:"verificationGasLimit" mapstructure:"verificationGasLimit" binding:"required"`
PreVerificationGas *big.Int `json:"preVerificationGas" mapstructure:"preVerificationGas" binding:"required"`
MaxFeePerGas *big.Int `json:"maxFeePerGas" mapstructure:"maxFeePerGas" binding:"required"`
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas" mapstructure:"maxPriorityFeePerGas" binding:"required"`
Expand Down
55 changes: 48 additions & 7 deletions service/chain_service/chain_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chain_service

import (
"AAStarCommunity/EthPaymaster_BackService/common/model"
"AAStarCommunity/EthPaymaster_BackService/common/types"
"context"
"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -33,23 +34,63 @@ func CheckContractAddressAccess(contract common.Address, chain types.Network) (b
}

// GetGasPrice return gas price in wei, gwei, ether
func GetGasPrice(chain types.Network) (*big.Int, *big.Float, *string, error) {
func GetGasPrice(chain types.Network) (*model.GasPrice, error) {
client, exist := EthCompatibleNetWorkClientMap[chain]
if !exist {
return nil, nil, nil, xerrors.Errorf("chain Client [%s] not exist", chain)
return nil, xerrors.Errorf("chain Client [%s] not exist", chain)
}
priceWei, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, nil, nil, err
priceWei, priceWeiErr := client.SuggestGasPrice(context.Background())
if priceWeiErr != nil {
return nil, priceWeiErr
}
priorityPriceWei, tiperr := client.SuggestGasTipCap(context.Background())
if tiperr != nil {
return nil, tiperr
}
result := model.GasPrice{}
result.MaxBasePriceWei = priceWei
result.MaxPriorityPriceWei = priorityPriceWei

gasPriceInGwei := new(big.Float).SetInt(priceWei)
gasPriceInGwei.Quo(gasPriceInGwei, GweiFactor)

gasPriceInEther := new(big.Float).SetInt(priceWei)
gasPriceInEther.Quo(gasPriceInEther, EthWeiFactor)
gasPriceInEtherStr := gasPriceInEther.Text('f', 18)
return priceWei, gasPriceInGwei, &gasPriceInEtherStr, nil
result.MaxBasePriceGwei = gasPriceInGwei
result.MaxBasePriceEther = &gasPriceInEtherStr

priorityPriceInGwei := new(big.Float).SetInt(priorityPriceWei)
priorityPriceInGwei.Quo(priorityPriceInGwei, GweiFactor)
priorityPriceInEther := new(big.Float).SetInt(priorityPriceWei)
priorityPriceInEther.Quo(priorityPriceInEther, EthWeiFactor)
priorityPriceInEtherStr := priorityPriceInEther.Text('f', 18)
result.MaxPriorityPriceGwei = priorityPriceInGwei
result.MaxPriorityPriceEther = &priorityPriceInEtherStr
result.MaxPriorityPriceGwei = priorityPriceInGwei
result.MaxPriorityPriceEther = &priorityPriceInEtherStr
return &result, nil
}

func GetGas(netWork types.Network) (*big.Int, error) {
client, exist := EthCompatibleNetWorkClientMap[netWork]
if !exist {
return nil, xerrors.Errorf("chain Client [%s] not exist", netWork)
}
head, erro := client.HeaderByNumber(context.Background(), nil)
if erro != nil {
return nil, erro
}
return head.BaseFee, nil
}
func GetPriorityFee(netWork types.Network) (*big.Int, *big.Float) {
client, exist := EthCompatibleNetWorkClientMap[netWork]
if !exist {
return nil, nil
}
priceWei, _ := client.SuggestGasTipCap(context.Background())
gasPriceInGwei := new(big.Float).SetInt(priceWei)
gasPriceInGwei.Quo(gasPriceInGwei, GweiFactor)
return priceWei, gasPriceInGwei
}

func GetEntryPointDeposit(entrypoint string, depositAddress string) uint256.Int {
Expand Down
5 changes: 5 additions & 0 deletions service/chain_service/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func TestGetGasPrice(t *testing.T) {
fmt.Printf("priceWeiInt %d\n", priceWeiInt)
fmt.Printf("gasPriceInGwei %f\n", gasPriceInGwei)
fmt.Printf("gasPriceInEtherStr %s\n", *gasPriceInEtherStr)
baseFee, _ := GetGas(types.Ethereum)
fmt.Printf("baseFee %d\n", baseFee.Uint64())

priorFee, priorFeeIGwei := GetPriorityFee(types.Ethereum)
fmt.Printf("priorFee %d\n", priorFee.Uint64())
fmt.Printf("priorFeeIGwei %f\n", priorFeeIGwei)
}

func TestGethClient(t *testing.T) {
Expand Down
39 changes: 25 additions & 14 deletions service/gas_service/gas_computor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package gas_service

import (
"AAStarCommunity/EthPaymaster_BackService/common/model"
"AAStarCommunity/EthPaymaster_BackService/common/types"
"AAStarCommunity/EthPaymaster_BackService/service/chain_service"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/xerrors"
"math/big"
)

func ComputeGas(userOp *model.UserOperation, strategy *model.Strategy) (*model.ComputeGasResponse, error) {
priceInWei, gasPriceInGwei, gasPriceInEtherStr, getGasErr := chain_service.GetGasPrice(types.Sepolia)
gasPrice, gasPriceErr := chain_service.GetGasPrice(strategy.NetWork)
//TODO calculate the maximum possible fee the account needs to pay (based on validation and call gas limits, and current gas values)
if getGasErr != nil {
return nil, getGasErr
if gasPriceErr != nil {
return nil, gasPriceErr
}
estimateCallGasLimit, _ := chain_service.EstimateGasLimitAndCost(strategy.NetWork, ethereum.CallMsg{
From: common.HexToAddress(strategy.EntryPointAddress),
Expand All @@ -24,21 +24,32 @@ func ComputeGas(userOp *model.UserOperation, strategy *model.Strategy) (*model.C
if estimateCallGasLimit > userOpCallGasLimit {
return nil, xerrors.Errorf("estimateCallGasLimit %d > userOpCallGasLimit %d", estimateCallGasLimit, userOpCallGasLimit)
}
//x := gasPrice.MaxBasePriceWei.Int64() + gasPrice.MaxPriorityPriceWei.Int64()
//maxFeePerGas := (x, userOp.MaxFeePerGas.Uint64())
payMasterPostGasLimit := GetPayMasterGasLimit()

// TODO get PaymasterCallGasLimit
maxGasLimit := big.NewInt(0).Add(userOp.CallGasLimit, userOp.VerificationGasLimit)
maxGasLimit = maxGasLimit.Add(maxGasLimit, payMasterPostGasLimit)

maxFee := new(big.Int).Mul(maxGasLimit, gasPrice.MaxBasePriceWei)
// TODO get PaymasterCallGasLimit
tokenCost := GetTokenCost(*maxFee, userOp, *strategy)
return &model.ComputeGasResponse{
GasPriceInWei: priceInWei.Uint64(),
GasPriceInGwei: gasPriceInGwei,
GasPriceInEther: *gasPriceInEtherStr,
CallGasLimit: estimateCallGasLimit,
TokenCost: "0.0001",
Network: strategy.NetWork,
Token: strategy.Token,
UsdCost: "0.4",
BlobEnable: strategy.Enable4844,
GasInfo: gasPrice,
TokenCost: tokenCost,
Network: strategy.NetWork,
Token: strategy.Token,
UsdCost: "0.4",
BlobEnable: strategy.Enable4844,
MaxFee: *maxFee,
}, nil
}
func GetPayMasterGasLimit() *big.Int {
return nil
}
func GetTokenCost(maxFee big.Int, userOp *model.UserOperation, strategy model.Strategy) string {
return "0.0001"
}

func ValidateGas(userOp *model.UserOperation, gasComputeResponse *model.ComputeGasResponse) error {
//1.if ERC20 check address balacnce
Expand Down
2 changes: 1 addition & 1 deletion service/validator_service/basic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ValidateStrategy(strategy *model.Strategy, userOp *model.UserOperation) err
}

func ValidateUserOp(userOp *model.UserOperation) error {
if userOp.PreVerificationGas.Cmp(MinPreVerificationGas) < 0 {
if userOp.PreVerificationGas.Cmp(MinPreVerificationGas) == -1 {
return xerrors.Errorf("preVerificationGas is less than 21000")
}

Expand Down

0 comments on commit 9ffba1d

Please sign in to comment.