Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize get API ,gas compute, pay #20

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2e58ace
optimize get API
cherry-yl-sh Mar 7, 2024
9bb3951
add GetSupportStrategy
cherry-yl-sh Mar 7, 2024
cd7aeda
change response
cherry-yl-sh Mar 8, 2024
3e5c9f7
add Support gas
cherry-yl-sh Mar 11, 2024
abf8840
add userop convert
cherry-yl-sh Mar 12, 2024
9ffba1d
add userop gasCompute
cherry-yl-sh Mar 12, 2024
9419434
add paymaster_generator impl
cherry-yl-sh Mar 12, 2024
38ea028
update
cherry-yl-sh Mar 12, 2024
23f1117
update
cherry-yl-sh Mar 13, 2024
fa2f161
support get token price
cherry-yl-sh Mar 13, 2024
e53c549
fix debug
cherry-yl-sh Mar 13, 2024
ab09981
fix debug
cherry-yl-sh Mar 13, 2024
00ebc7d
optimize gas compute
cherry-yl-sh Mar 14, 2024
400eeb8
update API
cherry-yl-sh Mar 14, 2024
22a9b18
update API
cherry-yl-sh Mar 14, 2024
5d699a3
update API
cherry-yl-sh Mar 14, 2024
8568936
update API
cherry-yl-sh Mar 14, 2024
d846788
fix bug
cherry-yl-sh Mar 15, 2024
4635c64
fix bug
cherry-yl-sh Mar 15, 2024
f13a8ca
fix bug
cherry-yl-sh Mar 15, 2024
4d1594d
fix bug
cherry-yl-sh Mar 15, 2024
b15c57b
generatePaymasterData
cherry-yl-sh Mar 18, 2024
8efed7c
add GetCoinPrice
cherry-yl-sh Mar 18, 2024
37316b1
update PackUserOp
cherry-yl-sh Mar 18, 2024
fd48082
update PackUserOp
cherry-yl-sh Mar 18, 2024
2480c96
update PackUserOp
cherry-yl-sh Mar 18, 2024
ac2e4d5
update PackUserOp
cherry-yl-sh Mar 19, 2024
d68773d
update PackUserOp
cherry-yl-sh Mar 19, 2024
1282794
update PackUserOp
cherry-yl-sh Mar 19, 2024
ca6fcc5
update PackUserOp
cherry-yl-sh Mar 19, 2024
7c58358
update PackUserOp
cherry-yl-sh Mar 19, 2024
6921a7b
feat: change call methods function
0xRory Mar 19, 2024
dd79a98
Merge remote-tracking branch 'origin/v1.0-gas_compute-pay_execute' in…
cherry-yl-sh Mar 19, 2024
9778517
feat: split string
0xRory Mar 19, 2024
86c5be9
Merge remote-tracking branch 'origin/v1.0-gas_compute-pay_execute' in…
cherry-yl-sh Mar 19, 2024
bbb2fd9
update PackUserOp
cherry-yl-sh Mar 19, 2024
8624b27
fix packOp bug
cherry-yl-sh Mar 20, 2024
ee3bb14
fix packOp bug
cherry-yl-sh Mar 20, 2024
5e19e9a
fix packOp bug
cherry-yl-sh Mar 20, 2024
9a515d5
fix packOp bug
cherry-yl-sh Mar 21, 2024
f543784
fix sign bug
cherry-yl-sh Mar 21, 2024
306a3f3
fix sign bug
cherry-yl-sh Mar 22, 2024
1839351
fix sign bug
cherry-yl-sh Mar 22, 2024
2e8da13
fix sign bug
cherry-yl-sh Mar 22, 2024
db26616
fix sign bug
cherry-yl-sh Mar 22, 2024
7aac1e0
fix sign bug
cherry-yl-sh Mar 22, 2024
faf8a78
change paymasterAddress
cherry-yl-sh Mar 22, 2024
0c6b428
change paymasterAddress
cherry-yl-sh Mar 22, 2024
8720635
change paymasterAddress
cherry-yl-sh Mar 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
)

type TryPayUserOpRequest struct {
ForceStrategyId string `json:"force_strategy_id"`
ForceNetwork types.NetWork `json:"force_network"`
ForceToken string `json:"force_token"`
ForceEntryPointAddress string `json:"force_entrypoint_address"`
UserOperation UserOperationItem `json:"user_operation"`
Extra interface{} `json:"extra"`
ForceStrategyId string `json:"force_strategy_id"`
ForceNetwork types.Network `json:"force_network"`
ForceToken string `json:"force_token"`
ForceEntryPointAddress string `json:"force_entrypoint_address"`
UserOp map[string]any `json:"user_operation"`
Extra interface{} `json:"extra"`
}

func (request *TryPayUserOpRequest) Validate() error {
Expand All @@ -22,17 +22,3 @@ func (request *TryPayUserOpRequest) Validate() error {
}
return nil
}

type GetSupportEntrypointRequest struct {
}

func (request *GetSupportEntrypointRequest) Validate() error {
return nil
}

type GetSupportStrategyRequest struct {
}

func (request *GetSupportStrategyRequest) Validate() error {
return nil
}
30 changes: 22 additions & 8 deletions common/model/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,35 @@ type TryPayUserOpResponse struct {
EntryPointAddress string `json:"entrypoint_address"`
PayMasterAddress string `json:"paymaster_address"`
PayMasterSignature string `json:"paymaster_signature"`
PayMasterAndData string `json:"paymaster_and_data"`
PayReceipt *PayReceipt `json:"pay_receipt"`
GasInfo *ComputeGasResponse `json:"gas_info"`
}

type ComputeGasResponse struct {
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 *big.Float `json:"token_cost"`
Network types.Network `json:"network"`
Token types.TokenType `json:"token"`
UsdCost float64 `json:"usd_cost"`
BlobEnable bool `json:"blob_enable"`
MaxFee big.Int `json:"max_fee"`
}
type PayReceipt struct {
TransactionHash string `json:"transaction_hash"`
Sponsor string `json:"sponsor"`
}

type GetSupportEntryPointResponse struct {
EntrypointDomains *[]EntrypointDomain `json:"entrypoints"`
}
type EntrypointDomain struct {
Address string `json:"address"`
Desc string `json:"desc"`
NetWork types.Network `json:"network"`
StrategyId string `json:"strategy_id"`
}

type GetSupportStrategyResponse struct {
Strategies *[]Strategy `json:"strategies"`
}
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 float64 `json:"max_base_price_gwei"`
MaxBasePriceEther *big.Float `json:"max_base_price_ether"`
MaxPriorityPriceWei *big.Int `json:"max_priority_price_wei"`
MaxPriorityPriceGwei float64 `json:"max_priority_price_gwei"`
MaxPriorityPriceEther *big.Float `json:"max_priority_price_ether"`
}
4 changes: 3 additions & 1 deletion common/model/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import "AAStarCommunity/EthPaymaster_BackService/common/types"
type Strategy struct {
Id string `json:"id"`
EntryPointAddress string `json:"entrypoint_address"`
EntryPointTag types.EntrypointTag `json:"entrypoint_tag"`
PayMasterAddress string `json:"paymaster_address"`
NetWork types.NetWork `json:"network"`
PayType types.PayType `json:"pay_type"`
NetWork types.Network `json:"network"`
Token types.TokenType `json:"token"`
Description string `json:"description"`
ExecuteRestriction StrategyExecuteRestriction `json:"execute_restriction"`
Expand Down
146 changes: 139 additions & 7 deletions common/model/user_operation.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,147 @@
package model

type UserOperationItem struct {
Sender string `json:"sender" binding:"required"`
import (
"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"golang.org/x/xerrors"
"math/big"
"reflect"
"sync"
)

var (
validate = validator.New()
onlyOnce = sync.Once{}
)

// 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:"init_code" `
CallData []byte `json:"callData" mapstructure:"call_data" binding:"required"`
CallGasLimit *big.Int `json:"callGasLimit" mapstructure:"call_gas_limit" binding:"required"`
VerificationGasLimit *big.Int `json:"verificationGasLimit" mapstructure:"verification_gas_limit" binding:"required"`
PreVerificationGas *big.Int `json:"preVerificationGas" mapstructure:"pre_verification_gas" binding:"required"`
MaxFeePerGas *big.Int `json:"maxFeePerGas" mapstructure:"max_fee_per_gas" binding:"required"`
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas" mapstructure:"max_priority_fee_per_gas" binding:"required"`
PaymasterAndData []byte `json:"paymasterAndData" mapstructure:"paymaster_and_data"`
Signature []byte `json:"signature" mapstructure:"signature" binding:"required"`
}
type UserOperationSimple struct {
Sender []byte `json:"sender" mapstructure:"sender" binding:"required,hexParam"`
}

// PackUserOperation entrypoint v0.0.67
type PackUserOperation struct {
Sender string `json:"sender" binding:"required,hexParam"`
Nonce string `json:"nonce" binding:"required"`
InitCode string `json:"init_code"`
CallData string `json:"call_data" binding:"required"`
CallGasLimit string `json:"call_gas_limit" binding:"required"`
VerificationGasList string `json:"verification_gas_list" binding:"required"`
PreVerificationGas string `json:"per_verification_gas" binding:"required"`
AccountGasLimit string `json:"account_gas_limit" binding:"required"`
PreVerificationGas string `json:"pre_verification_gas" binding:"required"`
MaxFeePerGas string `json:"max_fee_per_gas" binding:"required"`
MaxPriorityFeePerGas string `json:"max_priority_fee_per_gas" binding:"required"`
Signature string `json:"signature"`
//paymasterAndData string `json:"paymaster_and_data"`
PaymasterAndData string `json:"paymaster_and_data"`
Signature string `json:"signature" binding:"required"`
}

func NewUserOp(userOp *map[string]any) (*UserOperation, error) {
var result UserOperation
// Convert map to struct
decodeConfig := &mapstructure.DecoderConfig{
DecodeHook: decodeOpTypes,
Result: &result,
ErrorUnset: true,
MatchName: exactFieldMatch,
}
decoder, err := mapstructure.NewDecoder(decodeConfig)
if err != nil {
return nil, err
}
if err := decoder.Decode(userOp); err != nil {
return nil, xerrors.Errorf("data [%w] convert failed: [%w]", userOp, err)
}
onlyOnce.Do(func() {
validate.RegisterCustomTypeFunc(validateAddressType, common.Address{})
validate.RegisterCustomTypeFunc(validateBigIntType, big.Int{})
})
err = validate.Struct(result)
if err != nil {
return nil, err
}

return &result, nil
}

func validateAddressType(field reflect.Value) interface{} {
value, ok := field.Interface().(common.Address)
if !ok || value == common.HexToAddress("0x") {
return nil
}

return field
}

func validateBigIntType(field reflect.Value) interface{} {
value, ok := field.Interface().(big.Int)
if !ok || value.Cmp(big.NewInt(0)) == -1 {
return nil
}

return field
}

func exactFieldMatch(mapKey, fieldName string) bool {
return mapKey == fieldName
}

func decodeOpTypes(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
// String to common.Address conversion
if f == reflect.String && t == reflect.Array {
return common.HexToAddress(data.(string)), nil
}

// String to big.Int conversion
if f == reflect.String && t == reflect.Struct {
n := new(big.Int)
n, ok := n.SetString(data.(string), 0)
if !ok {
return nil, xerrors.Errorf("bigInt conversion failed")
}
return n, nil
}

// Float64 to big.Int conversion
if f == reflect.Float64 && t == reflect.Struct {
n, ok := data.(float64)
if !ok {
return nil, xerrors.Errorf("bigInt conversion failed")
}
return big.NewInt(int64(n)), nil
}

// String to []byte conversion
if f == reflect.String && t == reflect.Slice {
byteStr := data.(string)
if len(byteStr) < 2 || byteStr[:2] != "0x" {
return nil, xerrors.Errorf("not byte string")
}

b, err := hex.DecodeString(byteStr[2:])
if err != nil {
return nil, err
}
return b, nil
}

return data, nil
}
20 changes: 14 additions & 6 deletions common/types/chain.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

type NetworkInfo struct {
Name string `json:"main_net_name"`
RpcUrl string `json:"main_net_rpc_url"`
Name string `json:"main_net_name"`
RpcUrl string `json:"main_net_rpc_url"`
GasToken TokenType `json:"gas_token"`
}

//type Chain string
Expand All @@ -13,10 +14,17 @@ type NetworkInfo struct {
// Optimism Chain = "Optimism"
//)

type NetWork string
type Network string

const (
Ethereum NetWork = "ethereum"
Sepolia NetWork = "sepolia"
Arbitrum NetWork = "arbitrum"
Ethereum Network = "ethereum"
Sepolia Network = "sepolia"
Arbitrum Network = "arbitrum"
ArbTest Network = "arb-sepolia"
)

var TestNetWork = map[Network]bool{}

func init() {
TestNetWork[Sepolia] = true
}
8 changes: 8 additions & 0 deletions common/types/entrypoint_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

type EntrypointTag string

const (
EntrypointV06 EntrypointTag = "v0.6"
EntryPointV07 EntrypointTag = "v0.7"
)
9 changes: 9 additions & 0 deletions common/types/error_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

type ErrorPrefix string

const (
ValidateParamError ErrorPrefix = "AA2"
ValidateUserOpError ErrorPrefix = "AA3"
ValidateGasError ErrorPrefix = "AA4"
)
8 changes: 8 additions & 0 deletions common/types/pay_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

type PayType string

const (
PayTypeVerifying PayType = "00"
PayTypeERC20 PayType = "01"
)
19 changes: 17 additions & 2 deletions common/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ package types

type TokenType string

var StableCoinMap map[TokenType]bool

func init() {
StableCoinMap = map[TokenType]bool{
USDT: true,
USDC: true,
}
}
func IsStableToken(token TokenType) bool {
_, ok := StableCoinMap[token]
return ok
}

const (
USDT TokenType = "USDT"
ETH TokenType = "ETH"
USDT TokenType = "usdt"
USDC TokenType = "usdc"
ETH TokenType = "eth"
OP TokenType = "op"
)
Loading
Loading