From 90989a4e41c7d88161027f7e98967cc2ffe3d48d Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 6 Mar 2024 19:15:12 +0800 Subject: [PATCH] v1.0 tryPay --- common/model/api_request.go | 11 ++-- common/model/api_response.go | 24 ++++++-- common/model/strategy.go | 29 +++++++++- common/model/user_operation.go | 3 +- common/types/chain.go | 22 ++++++++ common/types/currency.go | 7 +++ common/types/token.go | 8 +++ common/utils/util.go | 56 +++++++++++++++++++ common/utils/util_test.go | 33 +++++++++++ conf/env.go | 2 +- go.mod | 23 ++++++++ go.sum | 52 +++++++++++++++++ service/chain_service/chain_config.go | 38 +++++++++++++ service/chain_service/chain_service.go | 52 +++++++++++++++++ service/chain_service/chain_test.go | 30 ++++++++++ service/contract_service/geth_recall.go | 1 - .../dashboard_service/dashboard_service.go | 16 +++++- .../dashboard_service/strategy_selector.go | 10 ---- service/gas_service/gas_compotor.go | 11 ---- service/gas_service/gas_computor.go | 28 ++++++++++ service/gas_service/gas_computor_test.go | 1 + service/operator/try_pay_user_op_execute.go | 40 +++++++++++-- .../operator/try_pay_user_op_execute_test.go | 22 +++----- service/pay_service/pay_service.go | 25 +++++++++ service/validator_service/basic_validator.go | 24 +++++++- 25 files changed, 508 insertions(+), 60 deletions(-) create mode 100644 common/types/chain.go create mode 100644 common/types/currency.go create mode 100644 common/types/token.go create mode 100644 common/utils/util_test.go create mode 100644 service/chain_service/chain_config.go create mode 100644 service/chain_service/chain_service.go create mode 100644 service/chain_service/chain_test.go delete mode 100644 service/contract_service/geth_recall.go delete mode 100644 service/dashboard_service/strategy_selector.go delete mode 100644 service/gas_service/gas_compotor.go create mode 100644 service/gas_service/gas_computor.go create mode 100644 service/gas_service/gas_computor_test.go create mode 100644 service/pay_service/pay_service.go diff --git a/common/model/api_request.go b/common/model/api_request.go index 51ea0f26..29a1c2f2 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -1,11 +1,14 @@ package model -import "errors" +import ( + "AAStarCommunity/EthPaymaster_BackService/common/types" + "errors" +) type TryPayUserOpRequest struct { ForceStrategyId string `json:"force_strategy_id"` - ForceNetWork string `json:"force_network"` - ForceTokens string `json:"force_tokens"` + 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"` @@ -13,7 +16,7 @@ type TryPayUserOpRequest struct { func (request *TryPayUserOpRequest) Validate() error { if len(request.ForceStrategyId) == 0 { - if len(request.ForceNetWork) == 0 || len(request.ForceTokens) == 0 || len(request.ForceEntryPointAddress) == 0 { + if len(request.ForceNetwork) == 0 || len(request.ForceToken) == 0 || len(request.ForceEntryPointAddress) == 0 { return errors.New("strategy configuration illegal") } } diff --git a/common/model/api_response.go b/common/model/api_response.go index 6272ba12..4a361925 100644 --- a/common/model/api_response.go +++ b/common/model/api_response.go @@ -1,18 +1,30 @@ package model +import ( + "AAStarCommunity/EthPaymaster_BackService/common/types" + "math/big" +) + type TryPayUserOpResponse struct { StrategyId string `json:"strategy_id"` EntryPointAddress string `json:"entrypoint_address"` PayMasterAddress string `json:"paymaster_address"` PayMasterSignature string `json:"paymaster_signature"` - PayReceipt interface{} `json:"pay_receipt"` + PayReceipt *PayReceipt `json:"pay_receipt"` GasInfo *ComputeGasResponse `json:"gas_info"` } type ComputeGasResponse struct { - StrategyId string `json:"strategy_id"` - TokenCost string `json:"token_cost"` - Network string `json:"network"` - Token string `json:"token"` - UsdCost string `json:"usd_cost"` + 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"` +} +type PayReceipt struct { + TransactionHash string `json:"transaction_hash"` + Sponsor string `json:"sponsor"` } diff --git a/common/model/strategy.go b/common/model/strategy.go index e23e5988..a86516c0 100644 --- a/common/model/strategy.go +++ b/common/model/strategy.go @@ -1,7 +1,30 @@ package model +import "AAStarCommunity/EthPaymaster_BackService/common/types" + type Strategy struct { - Id string - EntryPointAddress string - PayMasterAddress string + Id string `json:"id"` + EntryPointAddress string `json:"entrypoint_address"` + PayMasterAddress string `json:"paymaster_address"` + NetWork types.NetWork `json:"network"` + Token types.TokenType `json:"token"` + Description string `json:"description"` + ExecuteRestriction StrategyExecuteRestriction `json:"execute_restriction"` + EnableEoa bool `json:"enable_eoa"` + Enable7560 bool `json:"enable_7560"` + EnableErc20 bool `json:"enable_erc20"` + Enable4844 bool `json:"enable_4844"` + EnableCurrency bool `json:"enable_currency"` +} +type StrategyExecuteRestriction struct { + BanSenderAddress string `json:"ban_sender_address"` + EffectiveStartTime int64 `json:"effective_start_time"` + EffectiveEndTime int64 `json:"effective_end_time"` + GlobalMaxUSD int64 `json:"global_max_usd"` + GlobalMaxOpCount int64 `json:"global_max_op_count"` + DayMaxUSD int64 `json:"day_max_usd"` +} + +type StrategyValidateConfig struct { + ValidateContractAddress string `json:"validate_contract_address"` } diff --git a/common/model/user_operation.go b/common/model/user_operation.go index d7e2c9c1..c1fb22b5 100644 --- a/common/model/user_operation.go +++ b/common/model/user_operation.go @@ -4,9 +4,10 @@ type UserOperationItem struct { Sender string `json:"sender" binding:"required"` 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"` - PerVerificationGas string `json:"per_verification_gas" binding:"required"` + PreVerificationGas string `json:"per_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"` diff --git a/common/types/chain.go b/common/types/chain.go new file mode 100644 index 00000000..fa0e68d1 --- /dev/null +++ b/common/types/chain.go @@ -0,0 +1,22 @@ +package types + +type NetworkInfo struct { + Name string `json:"main_net_name"` + RpcUrl string `json:"main_net_rpc_url"` +} + +//type Chain string +// +//const ( +// Ethereum Chain = "Ethereum" +// Arbitrum Chain = "Arbitrum" +// Optimism Chain = "Optimism" +//) + +type NetWork string + +const ( + Ethereum NetWork = "ethereum" + Sepolia NetWork = "sepolia" + Arbitrum NetWork = "arbitrum" +) diff --git a/common/types/currency.go b/common/types/currency.go new file mode 100644 index 00000000..efb4511b --- /dev/null +++ b/common/types/currency.go @@ -0,0 +1,7 @@ +package types + +type Currency string + +const ( + USD Currency = "USD" +) diff --git a/common/types/token.go b/common/types/token.go new file mode 100644 index 00000000..f5321605 --- /dev/null +++ b/common/types/token.go @@ -0,0 +1,8 @@ +package types + +type TokenType string + +const ( + USDT TokenType = "USDT" + ETH TokenType = "ETH" +) diff --git a/common/utils/util.go b/common/utils/util.go index d4b585bf..202d7801 100644 --- a/common/utils/util.go +++ b/common/utils/util.go @@ -1 +1,57 @@ package utils + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + "encoding/json" + "github.com/ethereum/go-ethereum/crypto" +) + +func GenerateMockUserOperation() *model.UserOperationItem { + return &model.UserOperationItem{ + Sender: "0x4A2FD3215420376DA4eD32853C19E4755deeC4D1", + Nonce: "1", + InitCode: "0x", + CallData: "0xb61d27f6000000000000000000000000c206b552ab127608c3f666156c8e03a8471c72df000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000", + CallGasLimit: "39837", + VerificationGasList: "100000", + PreVerificationGas: "44020", + MaxFeePerGas: "1743509478", + MaxPriorityFeePerGas: "1500000000", + Signature: "0x760868cd7d9539c6e31c2169c4cab6817beb8247516a90e4301e929011451658623455035b83d38e987ef2e57558695040a25219c39eaa0e31a0ead16a5c925c1c", + } +} +func GenerateUserOperation() *model.UserOperationItem { + return &model.UserOperationItem{} +} + +func SignUserOp(privateKeyHex string, userOp *model.UserOperationItem) ([]byte, error) { + + serializedUserOp, err := json.Marshal(userOp) + if err != nil { + return nil, err + } + + signature, err := SignMessage(privateKeyHex, string(serializedUserOp)) + if err != nil { + return nil, err + } + + return signature, nil +} +func SignMessage(privateKeyHex string, message string) ([]byte, error) { + privateKey, err := crypto.HexToECDSA(privateKeyHex) + if err != nil { + return nil, err + } + + // Hash the message + hash := crypto.Keccak256([]byte(message)) + + // Sign the hash + signature, err := crypto.Sign(hash, privateKey) + if err != nil { + return nil, err + } + + return signature, nil +} diff --git a/common/utils/util_test.go b/common/utils/util_test.go new file mode 100644 index 00000000..f9574b11 --- /dev/null +++ b/common/utils/util_test.go @@ -0,0 +1,33 @@ +package utils + +import ( + "crypto/ecdsa" + "encoding/hex" + "fmt" + "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGenerateKeypair(t *testing.T) { + privateKey, _ := crypto.GenerateKey() + privateKeyHex := crypto.FromECDSA(privateKey) + publicKey := privateKey.Public() + publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey) + publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) + address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex() + fmt.Printf("privateKeyHex: %x\n", privateKeyHex) + fmt.Printf("publicKey: %x\n", publicKeyBytes) + fmt.Printf("address: %s\n", address) +} +func TestSignUserOp(t *testing.T) { + //privateKeyHex: 1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421 + //publicKey: 044eaed6b1f16e60354156fa334a094affc76d7b7061875a0b04290af9a14cc14ce2bce6ceba941856bd55c63f8199f408fff6495ce9d4c76899055972d23bdb3e + //address: 0x0E1375d18a4A2A867bEfe908E87322ad031386a6 + signByte, err := SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", GenerateMockUserOperation()) + assert.NoError(t, err) + fmt.Printf("signByte: %x\n", signByte) + singature := hex.EncodeToString(signByte) + fmt.Printf("singature: %s\n", singature) + +} diff --git a/conf/env.go b/conf/env.go index c6b677ac..5d8b7a7d 100644 --- a/conf/env.go +++ b/conf/env.go @@ -38,7 +38,7 @@ func getConfFilePath() *string { var Environment *Env func init() { - envName := ProdEnv + envName := DevEnv if len(os.Getenv(envKey)) > 0 { envName = os.Getenv(envKey) } diff --git a/go.mod b/go.mod index 410bb62c..b423578e 100644 --- a/go.mod +++ b/go.mod @@ -16,12 +16,24 @@ require ( require ( github.com/KyleBanks/depth v1.2.1 // indirect + github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/StackExchange/wmi v1.2.1 // indirect + github.com/bits-and-blooms/bitset v1.10.0 // indirect + github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect github.com/bytedance/sonic v1.11.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/consensys/bavard v0.1.13 // indirect + github.com/consensys/gnark-crypto v0.12.1 // indirect + github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/deckarep/golang-set/v2 v2.1.0 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect + github.com/ethereum/c-kzg-4844 v0.4.0 // indirect + github.com/ethereum/go-ethereum v1.13.14 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/jsonreference v0.20.4 // indirect github.com/go-openapi/spec v0.20.14 // indirect @@ -31,21 +43,31 @@ require ( github.com/go-playground/validator/v10 v10.18.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/holiman/uint256 v1.2.4 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect + github.com/supranational/blst v0.3.11 // indirect + github.com/tklauser/go-sysconf v0.3.12 // indirect + github.com/tklauser/numcpus v0.6.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.7.0 // indirect golang.org/x/crypto v0.20.0 // indirect + golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect + golang.org/x/mod v0.15.0 // indirect golang.org/x/net v0.21.0 // indirect + golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.18.0 // indirect @@ -53,6 +75,7 @@ require ( google.golang.org/protobuf v1.32.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + rsc.io/tmplfunc v0.0.3 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/go.sum b/go.sum index 106a0867..b72335d1 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,17 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= +github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/appleboy/gin-jwt/v2 v2.9.2 h1:GeS3lm9mb9HMmj7+GNjYUtpp3V1DAQ1TkUFa5poiZ7Y= github.com/appleboy/gin-jwt/v2 v2.9.2/go.mod h1:mxGjKt9Lrx9Xusy1SrnmsCJMZG6UJwmdHN9bN27/QDw= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= +github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88= +github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= +github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= +github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.11.1 h1:JC0+6c9FoWYYxakaoa+c5QTtJeiSZNeByOBhXtAFSn4= @@ -15,9 +23,24 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= +github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= +github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M= +github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= +github.com/crate-crypto/go-kzg-4844 v0.7.0 h1:C0vgZRk4q4EZ/JgPfzuSoxdCq3C3mOZMBShovmncxvA= +github.com/crate-crypto/go-kzg-4844 v0.7.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI= +github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= +github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= +github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R3nlY= +github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= +github.com/ethereum/go-ethereum v1.13.14 h1:EwiY3FZP94derMCIam1iW4HFVrSgIcpsu0HwTQtm6CQ= +github.com/ethereum/go-ethereum v1.13.14/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/cors v1.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk= @@ -28,6 +51,9 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= @@ -51,6 +77,11 @@ github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU= +github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -69,6 +100,9 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= +github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= +github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -80,6 +114,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= +github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -90,6 +126,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4= +github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= @@ -102,6 +140,10 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= +github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= +github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= +github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= @@ -114,6 +156,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= @@ -125,13 +169,19 @@ golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -167,6 +217,8 @@ k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8= k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= +rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= diff --git a/service/chain_service/chain_config.go b/service/chain_service/chain_config.go new file mode 100644 index 00000000..e686b131 --- /dev/null +++ b/service/chain_service/chain_config.go @@ -0,0 +1,38 @@ +package chain_service + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/types" + "github.com/ethereum/go-ethereum/ethclient" +) + +var NetworkInfoMap map[types.NetWork]types.NetworkInfo +var NetWorkClientMap map[types.NetWork]ethclient.Client + +func init() { + ConfigInit() + ClientInit() +} +func ConfigInit() { + NetworkInfoMap = map[types.NetWork]types.NetworkInfo{ + types.Ethereum: { + Name: "ethereum", + RpcUrl: "https://eth-mainnet.g.alchemy.com/v2/bIZQS43-rJMgv2_SiHqfVvXa-Z1UGoGt", + }, + types.Sepolia: { + Name: "sepolia", + RpcUrl: "https://eth-sepolia.g.alchemy.com/v2/wKeLycGxgYRykgf0aGfcpEkUtqyLQg4v", + }, + } +} + +func ClientInit() { + NetWorkClientMap = make(map[types.NetWork]ethclient.Client) + for chain, networkInfo := range NetworkInfoMap { + client, err := ethclient.Dial(networkInfo.RpcUrl) + if err != nil { + panic(err) + } + NetWorkClientMap[chain] = *client + continue + } +} diff --git a/service/chain_service/chain_service.go b/service/chain_service/chain_service.go new file mode 100644 index 00000000..0a43ca0e --- /dev/null +++ b/service/chain_service/chain_service.go @@ -0,0 +1,52 @@ +package chain_service + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/types" + "context" + "github.com/ethereum/go-ethereum/common" + "golang.org/x/xerrors" + "math/big" +) + +var GweiFactor = new(big.Float).SetInt(big.NewInt(1e9)) +var EthWeiFactor = new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + +func CheckContractAddressAccess(contract string, chain types.NetWork) (bool, error) { + if chain == "" { + return false, xerrors.Errorf("chain can not be empty") + } + contractAddress := common.HexToAddress(contract) + + client, exist := NetWorkClientMap[chain] + if !exist { + return false, xerrors.Errorf("chain Client [%s] not exist", chain) + } + code, err := client.CodeAt(context.Background(), contractAddress, nil) + if err != nil { + return false, err + } + if len(code) == 0 { + return false, xerrors.Errorf("contract [%s] address not exist in [%s] network", contract, chain) + } + return true, nil +} + +// GetGasPrice return gas price in wei, gwei, ether +func GetGasPrice(chain types.NetWork) (*big.Int, *big.Float, *string, error) { + client, exist := NetWorkClientMap[chain] + if !exist { + return nil, nil, nil, xerrors.Errorf("chain Client [%s] not exist", chain) + } + priceWei, err := client.SuggestGasPrice(context.Background()) + if err != nil { + return nil, nil, nil, err + } + + 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 +} diff --git a/service/chain_service/chain_test.go b/service/chain_service/chain_test.go new file mode 100644 index 00000000..6831ad39 --- /dev/null +++ b/service/chain_service/chain_test.go @@ -0,0 +1,30 @@ +package chain_service + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/types" + "context" + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCheckContractAddressAccess(t *testing.T) { + res, err := CheckContractAddressAccess("0x0576a174D229E3cFA37253523E645A78A0C91B57", types.Sepolia) + assert.NoError(t, err) + assert.True(t, res) +} +func TestGetGasPrice(t *testing.T) { + priceWei, gasPriceInGwei, gasPriceInEtherStr, _ := GetGasPrice(types.Ethereum) + priceWeiInt := priceWei.Uint64() + fmt.Printf("priceWeiInt %d\n", priceWeiInt) + fmt.Printf("gasPriceInGwei %f\n", gasPriceInGwei) + fmt.Printf("gasPriceInEtherStr %s\n", *gasPriceInEtherStr) + +} + +func TestGethClient(t *testing.T) { + client, _ := NetWorkClientMap[types.Sepolia] + num, _ := client.BlockNumber(context.Background()) + assert.NotEqual(t, 0, num) + fmt.Println(num) +} diff --git a/service/contract_service/geth_recall.go b/service/contract_service/geth_recall.go deleted file mode 100644 index f300daad..00000000 --- a/service/contract_service/geth_recall.go +++ /dev/null @@ -1 +0,0 @@ -package contract_service diff --git a/service/dashboard_service/dashboard_service.go b/service/dashboard_service/dashboard_service.go index a8acdeec..0aa0b4c0 100644 --- a/service/dashboard_service/dashboard_service.go +++ b/service/dashboard_service/dashboard_service.go @@ -1,16 +1,26 @@ package dashboard_service -import "AAStarCommunity/EthPaymaster_BackService/common/model" +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/common/types" + "errors" +) var mockStrategyMap = map[string]*model.Strategy{} func init() { mockStrategyMap["1"] = &model.Strategy{ Id: "1", - EntryPointAddress: "0x123", - PayMasterAddress: "0x123", + EntryPointAddress: "0x0576a174D229E3cFA37253523E645A78A0C91B57", + PayMasterAddress: "0x0000000000325602a77416A16136FDafd04b299f", + NetWork: types.Sepolia, + Token: types.USDT, } } func GetStrategyById(strategyId string) *model.Strategy { return mockStrategyMap[strategyId] } + +func GetSuitableStrategy(entrypoint string, chain types.NetWork, token string) (*model.Strategy, error) { + return nil, errors.New("not implemented") +} diff --git a/service/dashboard_service/strategy_selector.go b/service/dashboard_service/strategy_selector.go deleted file mode 100644 index e540d521..00000000 --- a/service/dashboard_service/strategy_selector.go +++ /dev/null @@ -1,10 +0,0 @@ -package dashboard_service - -import ( - "AAStarCommunity/EthPaymaster_BackService/common/model" - "errors" -) - -func GetSuitableStrategy(entrypoint string, network string, token string) (*model.Strategy, error) { - return nil, errors.New("not implemented") -} diff --git a/service/gas_service/gas_compotor.go b/service/gas_service/gas_compotor.go deleted file mode 100644 index d80a4526..00000000 --- a/service/gas_service/gas_compotor.go +++ /dev/null @@ -1,11 +0,0 @@ -package gas_service - -import "AAStarCommunity/EthPaymaster_BackService/common/model" - -func ComputeGas(userOp *model.UserOperationItem, strategy *model.Strategy) (*model.ComputeGasResponse, error) { - return &model.ComputeGasResponse{}, nil -} - -func ValidateGas(userOp *model.UserOperationItem, gasComputeResponse *model.ComputeGasResponse) error { - return nil -} diff --git a/service/gas_service/gas_computor.go b/service/gas_service/gas_computor.go new file mode 100644 index 00000000..37c0b65c --- /dev/null +++ b/service/gas_service/gas_computor.go @@ -0,0 +1,28 @@ +package gas_service + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/common/types" + "AAStarCommunity/EthPaymaster_BackService/service/chain_service" +) + +func ComputeGas(userOp *model.UserOperationItem, strategy *model.Strategy) (*model.ComputeGasResponse, error) { + priceInWei, gasPriceInGwei, gasPriceInEtherStr, getGasErr := chain_service.GetGasPrice(types.Sepolia) + if getGasErr != nil { + return nil, getGasErr + } + return &model.ComputeGasResponse{ + GasPriceInWei: priceInWei.Uint64(), + GasPriceInGwei: gasPriceInGwei, + GasPriceInEther: *gasPriceInEtherStr, + TokenCost: "0.0001", + Network: strategy.NetWork, + Token: strategy.Token, + UsdCost: "0.4", + BlobEnable: strategy.Enable4844, + }, nil +} + +func ValidateGas(userOp *model.UserOperationItem, gasComputeResponse *model.ComputeGasResponse) error { + return nil +} diff --git a/service/gas_service/gas_computor_test.go b/service/gas_service/gas_computor_test.go new file mode 100644 index 00000000..27fcf074 --- /dev/null +++ b/service/gas_service/gas_computor_test.go @@ -0,0 +1 @@ +package gas_service diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index d2edc05a..f7c01853 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -2,9 +2,13 @@ package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/common/utils" + "AAStarCommunity/EthPaymaster_BackService/service/chain_service" "AAStarCommunity/EthPaymaster_BackService/service/dashboard_service" "AAStarCommunity/EthPaymaster_BackService/service/gas_service" + "AAStarCommunity/EthPaymaster_BackService/service/pay_service" "AAStarCommunity/EthPaymaster_BackService/service/validator_service" + "encoding/hex" "golang.org/x/xerrors" ) @@ -35,6 +39,7 @@ func TryPayUserOpExecute(request *model.TryPayUserOpRequest) (*model.Result, err if err := gas_service.ValidateGas(&userOp, gasResponse); err != nil { return nil, err } + //pay payReceipt, payError := executePay(strategy, &userOp, gasResponse) if payError != nil { @@ -44,7 +49,7 @@ func TryPayUserOpExecute(request *model.TryPayUserOpRequest) (*model.Result, err var result = model.TryPayUserOpResponse{ StrategyId: strategy.Id, EntryPointAddress: strategy.EntryPointAddress, - PayMasterAddress: strategy.EntryPointAddress, + PayMasterAddress: strategy.PayMasterAddress, PayReceipt: payReceipt, PayMasterSignature: paymasterSignature, GasInfo: gasResponse, @@ -57,19 +62,44 @@ func TryPayUserOpExecute(request *model.TryPayUserOpRequest) (*model.Result, err Cost: "cost", }, nil } + func businessParamValidate(request *model.TryPayUserOpRequest) error { + if request.ForceStrategyId == "" && (request.ForceToken == "" || request.ForceNetwork == "") { + return xerrors.Errorf("Token And Network Must Set When ForceStrategyId Is Empty") + } //UserOp Validate + if err := validator_service.ValidateUserOp(&request.UserOperation); err != nil { + return err + } + if request.ForceEntryPointAddress != "" && request.ForceNetwork != "" { + // check Address is available in NetWork + if ok, err := chain_service.CheckContractAddressAccess(request.ForceEntryPointAddress, request.ForceNetwork); err != nil { + return err + } else if !ok { + return xerrors.Errorf("ForceEntryPointAddress: [%s] not exist in [%s] network", request.ForceEntryPointAddress, request.ForceNetwork) + } + } return nil } -func executePay(strategy *model.Strategy, userOp *model.UserOperationItem, gasResponse *model.ComputeGasResponse) (interface{}, error) { +func executePay(strategy *model.Strategy, userOp *model.UserOperationItem, gasResponse *model.ComputeGasResponse) (*model.PayReceipt, error) { //1.Recharge + ethereumPayservice := pay_service.EthereumPayService{} + if err := ethereumPayservice.Pay(); err != nil { + return nil, err + } //2.record account + ethereumPayservice.RecordAccount() //3.return Receipt - return nil, nil + ethereumPayservice.GetReceipt() + return &model.PayReceipt{ + TransactionHash: "0x110406d44ec1681fcdab1df2310181dee26ff43c37167b2c9c496b35cce69437", + Sponsor: "aastar", + }, nil } func getPayMasterSignature(strategy *model.Strategy, userOp *model.UserOperationItem) string { - return "" + signatureBytes, _ := utils.SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", userOp) + return hex.EncodeToString(signatureBytes) } func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, error) { @@ -82,7 +112,7 @@ func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, erro } } - suitableStrategy, err := dashboard_service.GetSuitableStrategy(request.ForceEntryPointAddress, request.ForceNetWork, request.ForceTokens) //TODO + suitableStrategy, err := dashboard_service.GetSuitableStrategy(request.ForceEntryPointAddress, request.ForceNetwork, request.ForceToken) //TODO if err != nil { return nil, err } diff --git a/service/operator/try_pay_user_op_execute_test.go b/service/operator/try_pay_user_op_execute_test.go index 937bcf85..39a11329 100644 --- a/service/operator/try_pay_user_op_execute_test.go +++ b/service/operator/try_pay_user_op_execute_test.go @@ -2,30 +2,24 @@ package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/common/utils" + "encoding/json" "fmt" "github.com/stretchr/testify/assert" - "testing" ) func TestTryPayUserOpExecute(t *testing.T) { request := getMockTryPayUserOpRequest() - result, err := TryPayUserOpExecute(&request) + result, err := TryPayUserOpExecute(request) assert.NoError(t, err) - fmt.Printf("Result: %v", result) + resultJson, _ := json.Marshal(result) + fmt.Printf("Result: %v", string(resultJson)) } -func getMockTryPayUserOpRequest() model.TryPayUserOpRequest { - return model.TryPayUserOpRequest{ +func getMockTryPayUserOpRequest() *model.TryPayUserOpRequest { + return &model.TryPayUserOpRequest{ ForceStrategyId: "1", - UserOperation: model.UserOperationItem{ - Sender: "0x123", - Nonce: "", - CallGasLimit: "", - VerificationGasList: "", - PerVerificationGas: "", - MaxFeePerGas: "", - MaxPriorityFeePerGas: "", - }, + UserOperation: *utils.GenerateMockUserOperation(), } } diff --git a/service/pay_service/pay_service.go b/service/pay_service/pay_service.go new file mode 100644 index 00000000..86ae6fc0 --- /dev/null +++ b/service/pay_service/pay_service.go @@ -0,0 +1,25 @@ +package pay_service + +type PayService interface { + Pay() error + RecordAccount() + getReceipt() +} + +type EthereumPayService struct { +} + +func (e *EthereumPayService) RecordAccount() { + //TODO implement me + +} + +func (e *EthereumPayService) GetReceipt() { + //TODO implement me + +} + +func (e *EthereumPayService) Pay() error { + //TODO implement me + return nil +} diff --git a/service/validator_service/basic_validator.go b/service/validator_service/basic_validator.go index d893f658..d52effca 100644 --- a/service/validator_service/basic_validator.go +++ b/service/validator_service/basic_validator.go @@ -1,7 +1,29 @@ package validator_service -import "AAStarCommunity/EthPaymaster_BackService/common/model" +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/service/chain_service" + "golang.org/x/xerrors" +) func ValidateStrategy(strategy *model.Strategy, userOp *model.UserOperationItem) error { + if strategy == nil { + return xerrors.Errorf("empty strategy") + } + if strategy.NetWork == "" { + return xerrors.Errorf("empty strategy network") + } + // check Paymaster + ok, err := chain_service.CheckContractAddressAccess(strategy.PayMasterAddress, strategy.NetWork) + if !ok || err != nil { + return err + } + // check EntryPoint + return nil +} + +func ValidateUserOp(userOp *model.UserOperationItem) error { + // check Sender is valid ,if sender is invalid And InitCode empty, return error + // nonce is valid return nil }