From 77570db640ed87d411524769d12fb3958fccaa2d Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 17:09:15 +0800 Subject: [PATCH 01/15] mock v0 --- common/model/Chain.go | 1 + common/model/api_request.go | 11 +++ common/model/api_response.go | 18 ++++ .../model}/client_credential.go | 2 +- .../models => common/model}/response.go | 2 +- common/model/strategy.go | 7 ++ common/model/user_operation.go | 14 +++ common/types/strategy.go | 4 - conf/appsettings.yaml | 2 +- go.mod | 1 + go.sum | 2 + rpc_server/api/health.go | 4 +- rpc_server/api/v1/get_support_entrypoint.go | 8 +- rpc_server/api/v1/get_support_strategy.go | 8 +- rpc_server/api/v1/try_pay_user_operation.go | 17 ++-- rpc_server/middlewares/recovery.go | 4 +- rpc_server/models/userOperation.go | 1 - rpc_server/routers/boot.go | 4 +- .../dashboard_service/dashboard_service.go | 7 ++ .../dashboard_service/strategy_selector.go | 11 +++ service/executor/executor.go | 12 +++ service/executor/try_pay_user_op_execute.go | 90 +++++++++++++++++++ service/executor/try_pay_user_op_test.go | 28 ++++++ service/gasService/gasWeiGenerator.go | 1 - service/gas_service/gasWeiGenerator.go | 1 + service/gas_service/gas_compotor.go | 11 +++ service/tryPayUserOperationExecutor.go | 13 --- service/validatorService/userOpValidator.go | 1 - service/validator_service/basic_validator.go | 8 ++ service/validator_service/userOpValidator.go | 1 + validator/chain/IChainValidator.go | 10 +++ validator/chain/ethereum/EthereumValidator.go | 24 +++++ validator/eip/eip_4844/4844Validator.go | 1 + validator/other/eoaEnableValidator.go | 1 + 34 files changed, 288 insertions(+), 42 deletions(-) create mode 100644 common/model/Chain.go create mode 100644 common/model/api_request.go create mode 100644 common/model/api_response.go rename {rpc_server/models => common/model}/client_credential.go (81%) rename {rpc_server/models => common/model}/response.go (99%) create mode 100644 common/model/strategy.go create mode 100644 common/model/user_operation.go delete mode 100644 common/types/strategy.go delete mode 100644 rpc_server/models/userOperation.go create mode 100644 service/dashboard_service/dashboard_service.go create mode 100644 service/dashboard_service/strategy_selector.go create mode 100644 service/executor/executor.go create mode 100644 service/executor/try_pay_user_op_execute.go create mode 100644 service/executor/try_pay_user_op_test.go delete mode 100644 service/gasService/gasWeiGenerator.go create mode 100644 service/gas_service/gasWeiGenerator.go create mode 100644 service/gas_service/gas_compotor.go delete mode 100644 service/tryPayUserOperationExecutor.go delete mode 100644 service/validatorService/userOpValidator.go create mode 100644 service/validator_service/basic_validator.go create mode 100644 service/validator_service/userOpValidator.go create mode 100644 validator/chain/IChainValidator.go create mode 100644 validator/chain/ethereum/EthereumValidator.go create mode 100644 validator/eip/eip_4844/4844Validator.go create mode 100644 validator/other/eoaEnableValidator.go diff --git a/common/model/Chain.go b/common/model/Chain.go new file mode 100644 index 00000000..8b537907 --- /dev/null +++ b/common/model/Chain.go @@ -0,0 +1 @@ +package model diff --git a/common/model/api_request.go b/common/model/api_request.go new file mode 100644 index 00000000..45ec599a --- /dev/null +++ b/common/model/api_request.go @@ -0,0 +1,11 @@ +package model + +type TryPayUserOpRequest struct { + ForceStrategyId string `json:"strategy_id"` + ForceNetWork string `json:"force_network"` + ForceTokens string `json:"force_tokens"` + ForceEntryPointAddress string `json:"force_entry_point_address"` + UserOperation UserOperationItem `json:"user_operation"` + Apikey string `json:"apikey"` + Extra interface{} `json:"extra"` +} diff --git a/common/model/api_response.go b/common/model/api_response.go new file mode 100644 index 00000000..980f2e7e --- /dev/null +++ b/common/model/api_response.go @@ -0,0 +1,18 @@ +package model + +type TryPayUserOpResponse struct { + StrategyId string `json:"strategy_id"` + EntryPointAddress string `json:"entry_point_address"` + PayMasterAddress string `json:"pay_master_address"` + PayMasterSignature string `json:"pay_master_signature"` + PayReceipt interface{} `json:"pay_receipt"` + GasInfo ComputeGasResponse `json:"gaf_info"` +} + +type ComputeGasResponse struct { + StrategyId string + tokenCost string + network string + token string + UsdCost string +} diff --git a/rpc_server/models/client_credential.go b/common/model/client_credential.go similarity index 81% rename from rpc_server/models/client_credential.go rename to common/model/client_credential.go index 1a048c34..b38ab8ff 100644 --- a/rpc_server/models/client_credential.go +++ b/common/model/client_credential.go @@ -1,4 +1,4 @@ -package models +package model type ClientCredential struct { ApiKey string `json:"apiKey"` diff --git a/rpc_server/models/response.go b/common/model/response.go similarity index 99% rename from rpc_server/models/response.go rename to common/model/response.go index eabb3845..16cb2372 100644 --- a/rpc_server/models/response.go +++ b/common/model/response.go @@ -1,4 +1,4 @@ -package models +package model import ( "github.com/gin-gonic/gin" diff --git a/common/model/strategy.go b/common/model/strategy.go new file mode 100644 index 00000000..e23e5988 --- /dev/null +++ b/common/model/strategy.go @@ -0,0 +1,7 @@ +package model + +type Strategy struct { + Id string + EntryPointAddress string + PayMasterAddress string +} diff --git a/common/model/user_operation.go b/common/model/user_operation.go new file mode 100644 index 00000000..5b7c7e6b --- /dev/null +++ b/common/model/user_operation.go @@ -0,0 +1,14 @@ +package model + +type UserOperationItem struct { + Sender string `json:"sender"` + Nonce string `json:"nonce"` + InitCode string `json:"init_code"` + CallGasLimit string `json:"call_gas_limit"` + VerificationGasList string `json:"verification_gas_list"` + PerVerificationGas string `json:"per_verification_gas"` + MaxFeePerGas string `json:"max_fee_per_gas"` + MaxPriorityFeePerGas string `json:"max_priority_fee_per_gas"` + //paymasterAndData string `json:"paymaster_and_data"` + Signature string `json:"signature"` +} diff --git a/common/types/strategy.go b/common/types/strategy.go deleted file mode 100644 index d0cc8bc2..00000000 --- a/common/types/strategy.go +++ /dev/null @@ -1,4 +0,0 @@ -package types - -type Strategy struct { -} diff --git a/conf/appsettings.yaml b/conf/appsettings.yaml index 65f51631..e86f54a2 100644 --- a/conf/appsettings.yaml +++ b/conf/appsettings.yaml @@ -1,4 +1,4 @@ jwt: - security: hello-eth-paymaster + security: hello-ethereum-paymaster realm: aastar idkey: id diff --git a/go.mod b/go.mod index 2048c10f..410bb62c 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.18.0 // indirect + golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/protobuf v1.32.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index eae4b29c..106a0867 100644 --- a/go.sum +++ b/go.sum @@ -151,6 +151,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/rpc_server/api/health.go b/rpc_server/api/health.go index a2ec82ab..ec543049 100644 --- a/rpc_server/api/health.go +++ b/rpc_server/api/health.go @@ -1,8 +1,8 @@ package api import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" "AAStarCommunity/EthPaymaster_BackService/conf" - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" "github.com/gin-gonic/gin" "time" ) @@ -16,7 +16,7 @@ import ( // @Router /api/health [post,get,put,delete] // @Success 200 func Healthz(c *gin.Context) { - response := models.GetResponse() + response := model.GetResponse() response.WithDataSuccess(c, gin.H{ "hello": "Eth Paymaster", "environment": conf.Environment.Name, diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index 07304541..7ef0b1ff 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -1,9 +1,9 @@ package v1 import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" "AAStarCommunity/EthPaymaster_BackService/rpc_server/api/utils" - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" - "AAStarCommunity/EthPaymaster_BackService/service" + "AAStarCommunity/EthPaymaster_BackService/service/executor" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -18,13 +18,13 @@ import ( // @Success 200 // @Security JWT func GetSupportEntrypoint(c *gin.Context) { - response := models.GetResponse() + response := model.GetResponse() if ok, apiKey := utils.CurrentUser(c); ok { _ = apiKey //1.TODO API validate //2. recall service - result, err := service.GetSupportEntrypointExecute() + result, err := executor.GetSupportEntrypointExecute() if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index 6143c786..07272918 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -1,8 +1,8 @@ package v1 import ( - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" - "AAStarCommunity/EthPaymaster_BackService/service" + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/service/executor" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -19,8 +19,8 @@ import ( func GetSupportStrategy(c *gin.Context) { //1.TODO API validate //2. recall service - result, err := service.GetSupportStrategyExecute() - response := models.GetResponse() + result, err := executor.GetSupportStrategyExecute() + response := model.GetResponse() if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index cc6caad9..3e7f2c6d 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -1,8 +1,8 @@ package v1 import ( - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" - "AAStarCommunity/EthPaymaster_BackService/service" + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/service/executor" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -19,10 +19,17 @@ import ( func TryPayUserOperation(c *gin.Context) { //1.TODO API validate //2. recall service - result, err := service.TryPayUserOpExecute() - response := models.GetResponse() + request := model.TryPayUserOpRequest{} + response := model.GetResponse() + + if err := c.ShouldBindJSON(&request); err != nil { + errStr := fmt.Sprintf("Conver Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } + + result, err := executor.TryPayUserOpExecute(request) if err != nil { - errStr := fmt.Sprintf("%v", err) + errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) } response.WithData(result).Success(c) diff --git a/rpc_server/middlewares/recovery.go b/rpc_server/middlewares/recovery.go index 5bb6a889..c6807e03 100644 --- a/rpc_server/middlewares/recovery.go +++ b/rpc_server/middlewares/recovery.go @@ -1,8 +1,8 @@ package middlewares import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" "AAStarCommunity/EthPaymaster_BackService/conf" - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" "errors" "fmt" "github.com/gin-gonic/gin" @@ -18,7 +18,7 @@ func GenericRecoveryHandler() gin.HandlerFunc { if conf.Environment.Debugger { errStr = fmt.Sprintf("%v", err) } - models.GetResponse().SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) + model.GetResponse().SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) }) } diff --git a/rpc_server/models/userOperation.go b/rpc_server/models/userOperation.go deleted file mode 100644 index 2640e7f9..00000000 --- a/rpc_server/models/userOperation.go +++ /dev/null @@ -1 +0,0 @@ -package models diff --git a/rpc_server/routers/boot.go b/rpc_server/routers/boot.go index 94abcdb0..70263d43 100644 --- a/rpc_server/routers/boot.go +++ b/rpc_server/routers/boot.go @@ -1,10 +1,10 @@ package routers import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" "AAStarCommunity/EthPaymaster_BackService/conf" "AAStarCommunity/EthPaymaster_BackService/docs" "AAStarCommunity/EthPaymaster_BackService/rpc_server/middlewares" - "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" @@ -19,7 +19,7 @@ func SetRouters() (routers *gin.Engine) { buildMod(routers) buildRoute(routers) routers.NoRoute(func(ctx *gin.Context) { - models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound) + model.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound) }) return diff --git a/service/dashboard_service/dashboard_service.go b/service/dashboard_service/dashboard_service.go new file mode 100644 index 00000000..a0a2e6e2 --- /dev/null +++ b/service/dashboard_service/dashboard_service.go @@ -0,0 +1,7 @@ +package dashboard_service + +import "AAStarCommunity/EthPaymaster_BackService/common/model" + +func GetStrategyById(strategyId string) model.Strategy { + return model.Strategy{} +} diff --git a/service/dashboard_service/strategy_selector.go b/service/dashboard_service/strategy_selector.go new file mode 100644 index 00000000..c4ac6d48 --- /dev/null +++ b/service/dashboard_service/strategy_selector.go @@ -0,0 +1,11 @@ +package dashboard_service + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" +) + +func GetSuitableStrategy(entrypoint string, network string, token string) (model.Strategy, error) { + + return model.Strategy{}, nil + +} diff --git a/service/executor/executor.go b/service/executor/executor.go new file mode 100644 index 00000000..8cd52808 --- /dev/null +++ b/service/executor/executor.go @@ -0,0 +1,12 @@ +package executor + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" +) + +func GetSupportEntrypointExecute() (model.Result, error) { + return model.Result{}, nil +} +func GetSupportStrategyExecute() (model.Result, error) { + return model.Result{}, nil +} diff --git a/service/executor/try_pay_user_op_execute.go b/service/executor/try_pay_user_op_execute.go new file mode 100644 index 00000000..445f6d88 --- /dev/null +++ b/service/executor/try_pay_user_op_execute.go @@ -0,0 +1,90 @@ +package executor + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + "AAStarCommunity/EthPaymaster_BackService/service/dashboard_service" + "AAStarCommunity/EthPaymaster_BackService/service/gas_service" + "AAStarCommunity/EthPaymaster_BackService/service/validator_service" + "golang.org/x/xerrors" +) + +func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error) { + //validator + if err := paramValidate(request); err != nil { + return model.Result{}, err + } + userOp := request.UserOperation + //getStrategy + strategy, err := strategyGenerate(request) + if err != nil { + return model.Result{}, err + } + if err := validator_service.ValidateStrategy(strategy, userOp); err != nil { + return model.Result{}, err + } + + //base Strategy and UserOp computeGas + gasResponse, gasComputeError := gas_service.ComputeGas(userOp, strategy) + if gasComputeError != nil { + return model.Result{}, gasComputeError + } + + //validate gas + if err := gas_service.ValidateGas(userOp, gasResponse); err != nil { + return model.Result{}, err + } + //pay + payReceipt, payError := executePay(strategy, request.UserOperation, gasResponse) + if payError != nil { + return model.Result{}, payError + } + paymasterSignature := getPayMasterSignature(strategy, request.UserOperation) + var result = model.TryPayUserOpResponse{ + StrategyId: strategy.Id, + EntryPointAddress: strategy.EntryPointAddress, + PayMasterAddress: strategy.EntryPointAddress, + PayReceipt: payReceipt, + PayMasterSignature: paymasterSignature, + GasInfo: gasResponse, + } + + return model.Result{ + Code: 0, + Data: result, + Message: "message", + Cost: "cost", + }, nil +} +func paramValidate(request model.TryPayUserOpRequest) error { + return nil +} + +func executePay(strategy model.Strategy, userOp model.UserOperationItem, gasResponse model.ComputeGasResponse) (interface{}, error) { + //1.Recharge + //2.record account + //3.return Receipt + return nil, nil +} +func getPayMasterSignature(strategy model.Strategy, userOp model.UserOperationItem) string { + return "" +} + +func strategyGenerate(request model.TryPayUserOpRequest) (model.Strategy, error) { + if forceStrategyId := request.ForceStrategyId; forceStrategyId != "" { + //force strategy + strategy := dashboard_service.GetStrategyById(forceStrategyId) + if strategy == (model.Strategy{}) { + return model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) + } + return strategy, nil + } + + suitableStrategy, err := dashboard_service.GetSuitableStrategy(request.ForceEntryPointAddress, request.ForceNetWork, request.ForceTokens) //TODO + if err != nil { + return model.Strategy{}, err + } + if suitableStrategy == (model.Strategy{}) { + return model.Strategy{}, xerrors.Errorf("Empty Strategies") + } + return suitableStrategy, nil +} diff --git a/service/executor/try_pay_user_op_test.go b/service/executor/try_pay_user_op_test.go new file mode 100644 index 00000000..4a41b057 --- /dev/null +++ b/service/executor/try_pay_user_op_test.go @@ -0,0 +1,28 @@ +package executor + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" + + "testing" +) + +func TestTryPayUserOpExecute(t *testing.T) { + request := getMockTryPayUserOpRequest() + TryPayUserOpExecute(request) + +} + +func getMockTryPayUserOpRequest() model.TryPayUserOpRequest { + return model.TryPayUserOpRequest{ + ForceStrategyId: "1", + UserOperation: model.UserOperationItem{ + Sender: "0x123", + Nonce: "", + CallGasLimit: "", + VerificationGasList: "", + PerVerificationGas: "", + MaxFeePerGas: "", + MaxPriorityFeePerGas: "", + }, + } +} diff --git a/service/gasService/gasWeiGenerator.go b/service/gasService/gasWeiGenerator.go deleted file mode 100644 index 457c50b0..00000000 --- a/service/gasService/gasWeiGenerator.go +++ /dev/null @@ -1 +0,0 @@ -package gasService diff --git a/service/gas_service/gasWeiGenerator.go b/service/gas_service/gasWeiGenerator.go new file mode 100644 index 00000000..27fcf074 --- /dev/null +++ b/service/gas_service/gasWeiGenerator.go @@ -0,0 +1 @@ +package gas_service diff --git a/service/gas_service/gas_compotor.go b/service/gas_service/gas_compotor.go new file mode 100644 index 00000000..fcae3d59 --- /dev/null +++ b/service/gas_service/gas_compotor.go @@ -0,0 +1,11 @@ +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/tryPayUserOperationExecutor.go b/service/tryPayUserOperationExecutor.go deleted file mode 100644 index 62894b27..00000000 --- a/service/tryPayUserOperationExecutor.go +++ /dev/null @@ -1,13 +0,0 @@ -package service - -import "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" - -func TryPayUserOpExecute() (models.Result, error) { - return models.Result{}, nil -} -func GetSupportEntrypointExecute() (models.Result, error) { - return models.Result{}, nil -} -func GetSupportStrategyExecute() (models.Result, error) { - return models.Result{}, nil -} diff --git a/service/validatorService/userOpValidator.go b/service/validatorService/userOpValidator.go deleted file mode 100644 index 0308dbe5..00000000 --- a/service/validatorService/userOpValidator.go +++ /dev/null @@ -1 +0,0 @@ -package validatorService diff --git a/service/validator_service/basic_validator.go b/service/validator_service/basic_validator.go new file mode 100644 index 00000000..f10077c5 --- /dev/null +++ b/service/validator_service/basic_validator.go @@ -0,0 +1,8 @@ +package validator_service + +import "AAStarCommunity/EthPaymaster_BackService/common/model" + +func ValidateStrategy(strategy model.Strategy, userOp model.UserOperationItem) error { + + return nil +} diff --git a/service/validator_service/userOpValidator.go b/service/validator_service/userOpValidator.go new file mode 100644 index 00000000..cb6263b0 --- /dev/null +++ b/service/validator_service/userOpValidator.go @@ -0,0 +1 @@ +package validator_service diff --git a/validator/chain/IChainValidator.go b/validator/chain/IChainValidator.go new file mode 100644 index 00000000..f86f2185 --- /dev/null +++ b/validator/chain/IChainValidator.go @@ -0,0 +1,10 @@ +package chain + +type IChainValidator interface { + PreValidate() (err error) + AfterGasValidate() (err error) + IsSupport() bool +} +type Base struct { + name string +} diff --git a/validator/chain/ethereum/EthereumValidator.go b/validator/chain/ethereum/EthereumValidator.go new file mode 100644 index 00000000..e84d4325 --- /dev/null +++ b/validator/chain/ethereum/EthereumValidator.go @@ -0,0 +1,24 @@ +package ethereum + +import ( + "AAStarCommunity/EthPaymaster_BackService/validator/chain" +) + +type EthValidator struct { + *chain.Base +} + +func (e EthValidator) IsSupport() bool { + //TODO implement me + panic("implement me") +} + +func (e EthValidator) PreValidate() (err error) { + //TODO implement me + panic("implement me") +} + +func (e EthValidator) AfterGasValidate() (err error) { + //TODO implement me + panic("implement me") +} diff --git a/validator/eip/eip_4844/4844Validator.go b/validator/eip/eip_4844/4844Validator.go new file mode 100644 index 00000000..c847b0f8 --- /dev/null +++ b/validator/eip/eip_4844/4844Validator.go @@ -0,0 +1 @@ +package eip_4844 diff --git a/validator/other/eoaEnableValidator.go b/validator/other/eoaEnableValidator.go new file mode 100644 index 00000000..58a9531f --- /dev/null +++ b/validator/other/eoaEnableValidator.go @@ -0,0 +1 @@ +package other From bd37c8b516b4c26e20a206b4b1de0b5b907a0c39 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 20:11:56 +0800 Subject: [PATCH 02/15] mock v0 update --- common/model/api_response.go | 6 +++--- rpc_server/api/v1/try_pay_user_operation.go | 1 + service/executor/try_pay_user_op_execute.go | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/common/model/api_response.go b/common/model/api_response.go index 980f2e7e..5e45ec29 100644 --- a/common/model/api_response.go +++ b/common/model/api_response.go @@ -11,8 +11,8 @@ type TryPayUserOpResponse struct { type ComputeGasResponse struct { StrategyId string - tokenCost string - network string - token string + TokenCost string + Network string + Token string UsdCost string } diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index 3e7f2c6d..8632938c 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -28,6 +28,7 @@ func TryPayUserOperation(c *gin.Context) { } result, err := executor.TryPayUserOpExecute(request) + //TODO paramValidate if err != nil { errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/service/executor/try_pay_user_op_execute.go b/service/executor/try_pay_user_op_execute.go index 445f6d88..9dfc3b9f 100644 --- a/service/executor/try_pay_user_op_execute.go +++ b/service/executor/try_pay_user_op_execute.go @@ -10,7 +10,7 @@ import ( func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error) { //validator - if err := paramValidate(request); err != nil { + if err := businessParamValidate(request); err != nil { return model.Result{}, err } userOp := request.UserOperation @@ -55,7 +55,8 @@ func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error Cost: "cost", }, nil } -func paramValidate(request model.TryPayUserOpRequest) error { +func businessParamValidate(request model.TryPayUserOpRequest) error { + //UserOp Validate return nil } From 74c8675faebc0dc498a5cd341bfa7062d8d200e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=B6=85=E8=B6=8A?= <993921@qq.com> Date: Tue, 5 Mar 2024 20:36:10 +0800 Subject: [PATCH 03/15] try pay user operation request model validator --- common/model/api_request.go | 16 +++ common/model/user_operation.go | 18 ++-- docs/docs.go | 103 +++++++++++++++++++- docs/swagger.json | 98 ++++++++++++++++++- docs/swagger.yaml | 68 ++++++++++++- rpc_server/api/auth.go | 2 +- rpc_server/api/health.go | 3 +- rpc_server/api/v1/try_pay_user_operation.go | 8 +- 8 files changed, 295 insertions(+), 21 deletions(-) diff --git a/common/model/api_request.go b/common/model/api_request.go index 45ec599a..c23fac63 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -1,5 +1,7 @@ package model +import "errors" + type TryPayUserOpRequest struct { ForceStrategyId string `json:"strategy_id"` ForceNetWork string `json:"force_network"` @@ -9,3 +11,17 @@ type TryPayUserOpRequest struct { Apikey string `json:"apikey"` Extra interface{} `json:"extra"` } + +func (sender *TryPayUserOpRequest) Validate() error { + if len(sender.Apikey) == 0 { + return errors.New("apikey mustn't empty") + } + + if len(sender.ForceStrategyId) == 0 { + if len(sender.ForceNetWork) == 0 || len(sender.ForceTokens) == 0 || len(sender.ForceEntryPointAddress) == 0 { + return errors.New("strategy configuration illegal") + } + } + + return nil +} diff --git a/common/model/user_operation.go b/common/model/user_operation.go index 5b7c7e6b..c13b4fb1 100644 --- a/common/model/user_operation.go +++ b/common/model/user_operation.go @@ -1,14 +1,14 @@ package model type UserOperationItem struct { - Sender string `json:"sender"` - Nonce string `json:"nonce"` - InitCode string `json:"init_code"` - CallGasLimit string `json:"call_gas_limit"` - VerificationGasList string `json:"verification_gas_list"` - PerVerificationGas string `json:"per_verification_gas"` - MaxFeePerGas string `json:"max_fee_per_gas"` - MaxPriorityFeePerGas string `json:"max_priority_fee_per_gas"` + Sender string `json:"sender" binding:"required"` + Nonce string `json:"nonce" binding:"required"` + InitCode string `json:"init_code" 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"` + MaxFeePerGas string `json:"max_fee_per_gas" binding:"required"` + MaxPriorityFeePerGas string `json:"max_priority_fee_per_gas" binding:"required"` + Signature string `json:"signature" binding:"required"` //paymasterAndData string `json:"paymaster_and_data"` - Signature string `json:"signature"` } diff --git a/docs/docs.go b/docs/docs.go index 89631010..85fa5d6a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,5 +1,4 @@ -// Package docs GENERATED BY SWAG; DO NOT EDIT -// This file was generated by swaggo/swag +// Package docs Code generated by swaggo/swag. DO NOT EDIT package docs import "github.com/swaggo/swag" @@ -35,7 +34,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/models.ClientCredential" + "$ref": "#/definitions/model.ClientCredential" } } ], @@ -46,6 +45,22 @@ const docTemplate = `{ } } }, + "/api/health": { + "get": { + "description": "Get Healthz", + "consumes": [ + "application/json" + ], + "tags": [ + "Healthz" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/api/v1/get-support-entrypoint": { "get": { "security": [ @@ -105,6 +120,17 @@ const docTemplate = `{ "tags": [ "Sponsor" ], + "parameters": [ + { + "description": "UserOp Request", + "name": "tryPay", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/model.TryPayUserOpRequest" + } + } + ], "responses": { "200": { "description": "OK" @@ -114,13 +140,80 @@ const docTemplate = `{ } }, "definitions": { - "models.ClientCredential": { + "model.ClientCredential": { "type": "object", "properties": { "apiKey": { "type": "string" } } + }, + "model.TryPayUserOpRequest": { + "type": "object", + "properties": { + "apikey": { + "type": "string" + }, + "extra": {}, + "force_entry_point_address": { + "type": "string" + }, + "force_network": { + "type": "string" + }, + "force_tokens": { + "type": "string" + }, + "strategy_id": { + "type": "string" + }, + "user_operation": { + "$ref": "#/definitions/model.UserOperationItem" + } + } + }, + "model.UserOperationItem": { + "type": "object", + "required": [ + "call_gas_limit", + "init_code", + "max_fee_per_gas", + "max_priority_fee_per_gas", + "nonce", + "per_verification_gas", + "sender", + "signature", + "verification_gas_list" + ], + "properties": { + "call_gas_limit": { + "type": "string" + }, + "init_code": { + "type": "string" + }, + "max_fee_per_gas": { + "type": "string" + }, + "max_priority_fee_per_gas": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "per_verification_gas": { + "type": "string" + }, + "sender": { + "type": "string" + }, + "signature": { + "type": "string" + }, + "verification_gas_list": { + "type": "string" + } + } } }, "securityDefinitions": { @@ -143,6 +236,8 @@ var SwaggerInfo = &swag.Spec{ Description: "", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", } func init() { diff --git a/docs/swagger.json b/docs/swagger.json index a50f25ab..d680a121 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -23,7 +23,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/models.ClientCredential" + "$ref": "#/definitions/model.ClientCredential" } } ], @@ -34,6 +34,22 @@ } } }, + "/api/health": { + "get": { + "description": "Get Healthz", + "consumes": [ + "application/json" + ], + "tags": [ + "Healthz" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/api/v1/get-support-entrypoint": { "get": { "security": [ @@ -93,6 +109,17 @@ "tags": [ "Sponsor" ], + "parameters": [ + { + "description": "UserOp Request", + "name": "tryPay", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/model.TryPayUserOpRequest" + } + } + ], "responses": { "200": { "description": "OK" @@ -102,13 +129,80 @@ } }, "definitions": { - "models.ClientCredential": { + "model.ClientCredential": { "type": "object", "properties": { "apiKey": { "type": "string" } } + }, + "model.TryPayUserOpRequest": { + "type": "object", + "properties": { + "apikey": { + "type": "string" + }, + "extra": {}, + "force_entry_point_address": { + "type": "string" + }, + "force_network": { + "type": "string" + }, + "force_tokens": { + "type": "string" + }, + "strategy_id": { + "type": "string" + }, + "user_operation": { + "$ref": "#/definitions/model.UserOperationItem" + } + } + }, + "model.UserOperationItem": { + "type": "object", + "required": [ + "call_gas_limit", + "init_code", + "max_fee_per_gas", + "max_priority_fee_per_gas", + "nonce", + "per_verification_gas", + "sender", + "signature", + "verification_gas_list" + ], + "properties": { + "call_gas_limit": { + "type": "string" + }, + "init_code": { + "type": "string" + }, + "max_fee_per_gas": { + "type": "string" + }, + "max_priority_fee_per_gas": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "per_verification_gas": { + "type": "string" + }, + "sender": { + "type": "string" + }, + "signature": { + "type": "string" + }, + "verification_gas_list": { + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index ab6e309b..73f33967 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,9 +1,56 @@ definitions: - models.ClientCredential: + model.ClientCredential: properties: apiKey: type: string type: object + model.TryPayUserOpRequest: + properties: + apikey: + type: string + extra: {} + force_entry_point_address: + type: string + force_network: + type: string + force_tokens: + type: string + strategy_id: + type: string + user_operation: + $ref: '#/definitions/model.UserOperationItem' + type: object + model.UserOperationItem: + properties: + call_gas_limit: + type: string + init_code: + type: string + max_fee_per_gas: + type: string + max_priority_fee_per_gas: + type: string + nonce: + type: string + per_verification_gas: + type: string + sender: + type: string + signature: + type: string + verification_gas_list: + type: string + required: + - call_gas_limit + - init_code + - max_fee_per_gas + - max_priority_fee_per_gas + - nonce + - per_verification_gas + - sender + - signature + - verification_gas_list + type: object info: contact: name: AAStar Support @@ -20,12 +67,22 @@ paths: name: credential required: true schema: - $ref: '#/definitions/models.ClientCredential' + $ref: '#/definitions/model.ClientCredential' responses: "200": description: OK tags: - Auth + /api/health: + get: + consumes: + - application/json + description: Get Healthz + responses: + "200": + description: OK + tags: + - Healthz /api/v1/get-support-entrypoint: get: consumes: @@ -57,6 +114,13 @@ paths: consumes: - application/json description: sponsor the userOp + parameters: + - description: UserOp Request + in: body + name: tryPay + required: true + schema: + $ref: '#/definitions/model.TryPayUserOpRequest' responses: "200": description: OK diff --git a/rpc_server/api/auth.go b/rpc_server/api/auth.go index 21e39341..cad8302f 100644 --- a/rpc_server/api/auth.go +++ b/rpc_server/api/auth.go @@ -10,7 +10,7 @@ import ( // @Description Get AccessToken By ApiKey // @Accept json // @Product json -// @Param credential body models.ClientCredential true "AccessToken Model" +// @Param credential body model.ClientCredential true "AccessToken Model" // @Router /api/auth [post] // @Success 200 func Auth(ctx *gin.Context) { diff --git a/rpc_server/api/health.go b/rpc_server/api/health.go index ec543049..8037073d 100644 --- a/rpc_server/api/health.go +++ b/rpc_server/api/health.go @@ -12,8 +12,7 @@ import ( // @Description Get Healthz // @Accept json // @Product json -// @Param -// @Router /api/health [post,get,put,delete] +// @Router /api/health [get] // @Success 200 func Healthz(c *gin.Context) { response := model.GetResponse() diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index 3e7f2c6d..65e61c93 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -14,6 +14,7 @@ import ( // @Accept json // @Product json // @Router /api/v1/try-pay-user-operation [POST] +// @Param tryPay body model.TryPayUserOpRequest true "UserOp Request" // @Success 200 // @Security JWT func TryPayUserOperation(c *gin.Context) { @@ -23,7 +24,12 @@ func TryPayUserOperation(c *gin.Context) { response := model.GetResponse() if err := c.ShouldBindJSON(&request); err != nil { - errStr := fmt.Sprintf("Conver Request Error [%v]", err) + errStr := fmt.Sprintf("Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } + + if err := request.Validate(); err != nil { + errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) } From 93f39a83048286390dc02f8eb1a6580713d03213 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 20:54:33 +0800 Subject: [PATCH 04/15] update swagger --- common/model/response.go | 2 +- docs/docs.go | 3 +++ docs/swagger.json | 4 ++-- docs/swagger.yaml | 2 +- service/executor/try_pay_user_op_execute.go | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/common/model/response.go b/common/model/response.go index 16cb2372..c335b834 100644 --- a/common/model/response.go +++ b/common/model/response.go @@ -10,7 +10,7 @@ func GetResponse() *Response { return &Response{ httpCode: http.StatusOK, Result: &Result{ - Code: 0, + Code: 200, Message: "", Data: nil, Cost: "", diff --git a/docs/docs.go b/docs/docs.go index dd696fee..10b2bca1 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -48,6 +48,9 @@ const docTemplate = `{ "/api/healthz": { "get": { "description": "Get Healthz", + "consumes": [ + "application/json" + ], "tags": [ "Healthz" ], diff --git a/docs/swagger.json b/docs/swagger.json index 32e1c1b6..d5c0fa64 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -34,7 +34,7 @@ } } }, - "/api/health": { + "/api/healthz": { "get": { "description": "Get Healthz", "consumes": [ @@ -213,4 +213,4 @@ "in": "header" } } -} +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 73f33967..e1f6f55d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -73,7 +73,7 @@ paths: description: OK tags: - Auth - /api/health: + /api/healthz: get: consumes: - application/json diff --git a/service/executor/try_pay_user_op_execute.go b/service/executor/try_pay_user_op_execute.go index 9dfc3b9f..0b7f5508 100644 --- a/service/executor/try_pay_user_op_execute.go +++ b/service/executor/try_pay_user_op_execute.go @@ -49,7 +49,7 @@ func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error } return model.Result{ - Code: 0, + Code: 200, Data: result, Message: "message", Cost: "cost", From 03b4fe97303ac3bc1df71cdd772b9062b4f4143f Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 21:29:20 +0800 Subject: [PATCH 05/15] update Operator --- common/model/api_request.go | 14 ++++++++ rpc_server/api/v1/get_support_entrypoint.go | 32 +++++++++++-------- rpc_server/api/v1/get_support_strategy.go | 16 ++++++++-- rpc_server/api/v1/try_pay_user_operation.go | 11 +++---- service/executor/executor.go | 12 ------- .../get_support_entry_point_execute.go | 7 ++++ .../operator/get_support_strategy_execute.go | 9 ++++++ .../try_pay_user_op_execute.go | 2 +- .../try_pay_user_op_test.go | 2 +- 9 files changed, 67 insertions(+), 38 deletions(-) delete mode 100644 service/executor/executor.go create mode 100644 service/operator/get_support_entry_point_execute.go create mode 100644 service/operator/get_support_strategy_execute.go rename service/{executor => operator}/try_pay_user_op_execute.go (99%) rename service/{executor => operator}/try_pay_user_op_test.go (97%) diff --git a/common/model/api_request.go b/common/model/api_request.go index c23fac63..b7d7966f 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -25,3 +25,17 @@ func (sender *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 +} diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index 7ef0b1ff..40200590 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -2,8 +2,7 @@ package v1 import ( "AAStarCommunity/EthPaymaster_BackService/common/model" - "AAStarCommunity/EthPaymaster_BackService/rpc_server/api/utils" - "AAStarCommunity/EthPaymaster_BackService/service/executor" + "AAStarCommunity/EthPaymaster_BackService/service/operator" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -18,19 +17,24 @@ import ( // @Success 200 // @Security JWT func GetSupportEntrypoint(c *gin.Context) { + request := model.GetSupportEntrypointRequest{} response := model.GetResponse() - if ok, apiKey := utils.CurrentUser(c); ok { - _ = apiKey + //1. API validate + if err := c.ShouldBindJSON(&request); err != nil { + errStr := fmt.Sprintf("Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } + if err := request.Validate(); err != nil { + errStr := fmt.Sprintf("Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } - //1.TODO API validate - //2. recall service - result, err := executor.GetSupportEntrypointExecute() - if err != nil { - errStr := fmt.Sprintf("%v", err) - response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) - } - response.WithData(result).Success(c) - } else { - response.SetHttpCode(http.StatusUnauthorized) + //2. recall service + result, err := operator.GetSupportEntrypointExecute(request) + if err != nil { + errStr := fmt.Sprintf("%v", err) + response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) } + response.WithData(result).Success(c) + } diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index 07272918..3f0bab8c 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -2,7 +2,7 @@ package v1 import ( "AAStarCommunity/EthPaymaster_BackService/common/model" - "AAStarCommunity/EthPaymaster_BackService/service/executor" + "AAStarCommunity/EthPaymaster_BackService/service/operator" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -17,10 +17,20 @@ import ( // @Router /api/v1/get-support-strategy [GET] // @Security JWT func GetSupportStrategy(c *gin.Context) { - //1.TODO API validate //2. recall service - result, err := executor.GetSupportStrategyExecute() + request := model.GetSupportStrategyRequest{} response := model.GetResponse() + + //1. API validate + if err := c.ShouldBindJSON(&request); err != nil { + errStr := fmt.Sprintf("Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } + if err := request.Validate(); err != nil { + errStr := fmt.Sprintf("Request Error [%v]", err) + response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + } + result, err := operator.GetSupportStrategyExecute(request) if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index c2f9dfd6..ecca7ed3 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -2,7 +2,7 @@ package v1 import ( "AAStarCommunity/EthPaymaster_BackService/common/model" - "AAStarCommunity/EthPaymaster_BackService/service/executor" + "AAStarCommunity/EthPaymaster_BackService/service/operator" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -18,11 +18,9 @@ import ( // @Success 200 // @Security JWT func TryPayUserOperation(c *gin.Context) { - //1.TODO API validate - //2. recall service request := model.TryPayUserOpRequest{} response := model.GetResponse() - + //1. API validate if err := c.ShouldBindJSON(&request); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) @@ -32,9 +30,8 @@ func TryPayUserOperation(c *gin.Context) { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) } - - result, err := executor.TryPayUserOpExecute(request) - //TODO paramValidate + //2. recall service + result, err := operator.TryPayUserOpExecute(request) if err != nil { errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/service/executor/executor.go b/service/executor/executor.go deleted file mode 100644 index 8cd52808..00000000 --- a/service/executor/executor.go +++ /dev/null @@ -1,12 +0,0 @@ -package executor - -import ( - "AAStarCommunity/EthPaymaster_BackService/common/model" -) - -func GetSupportEntrypointExecute() (model.Result, error) { - return model.Result{}, nil -} -func GetSupportStrategyExecute() (model.Result, error) { - return model.Result{}, nil -} diff --git a/service/operator/get_support_entry_point_execute.go b/service/operator/get_support_entry_point_execute.go new file mode 100644 index 00000000..2a03206e --- /dev/null +++ b/service/operator/get_support_entry_point_execute.go @@ -0,0 +1,7 @@ +package operator + +import "AAStarCommunity/EthPaymaster_BackService/common/model" + +func GetSupportEntrypointExecute(request model.GetSupportEntrypointRequest) (model.Result, error) { + return model.Result{}, nil +} diff --git a/service/operator/get_support_strategy_execute.go b/service/operator/get_support_strategy_execute.go new file mode 100644 index 00000000..ba50edc4 --- /dev/null +++ b/service/operator/get_support_strategy_execute.go @@ -0,0 +1,9 @@ +package operator + +import ( + "AAStarCommunity/EthPaymaster_BackService/common/model" +) + +func GetSupportStrategyExecute(request model.GetSupportStrategyRequest) (model.Result, error) { + return model.Result{}, nil +} diff --git a/service/executor/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go similarity index 99% rename from service/executor/try_pay_user_op_execute.go rename to service/operator/try_pay_user_op_execute.go index 0b7f5508..30fbfff5 100644 --- a/service/executor/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -1,4 +1,4 @@ -package executor +package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" diff --git a/service/executor/try_pay_user_op_test.go b/service/operator/try_pay_user_op_test.go similarity index 97% rename from service/executor/try_pay_user_op_test.go rename to service/operator/try_pay_user_op_test.go index 4a41b057..e6d03af2 100644 --- a/service/executor/try_pay_user_op_test.go +++ b/service/operator/try_pay_user_op_test.go @@ -1,4 +1,4 @@ -package executor +package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" From 19a5e244927f521f0468ca212fc27221a2d0f53d Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 22:11:42 +0800 Subject: [PATCH 06/15] add Test change name --- common/model/api_request.go | 12 +++--------- common/model/user_operation.go | 4 ++-- service/dashboard_service/dashboard_service.go | 11 ++++++++++- service/operator/get_support_entry_point_test.go | 1 + service/operator/get_support_strategy_text.go | 1 + service/operator/try_pay_user_op_test.go | 7 ++++++- 6 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 service/operator/get_support_entry_point_test.go create mode 100644 service/operator/get_support_strategy_text.go diff --git a/common/model/api_request.go b/common/model/api_request.go index b7d7966f..793721e5 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -8,21 +8,15 @@ type TryPayUserOpRequest struct { ForceTokens string `json:"force_tokens"` ForceEntryPointAddress string `json:"force_entry_point_address"` UserOperation UserOperationItem `json:"user_operation"` - Apikey string `json:"apikey"` Extra interface{} `json:"extra"` } -func (sender *TryPayUserOpRequest) Validate() error { - if len(sender.Apikey) == 0 { - return errors.New("apikey mustn't empty") - } - - if len(sender.ForceStrategyId) == 0 { - if len(sender.ForceNetWork) == 0 || len(sender.ForceTokens) == 0 || len(sender.ForceEntryPointAddress) == 0 { +func (request *TryPayUserOpRequest) Validate() error { + if len(request.ForceStrategyId) == 0 { + if len(request.ForceNetWork) == 0 || len(request.ForceTokens) == 0 || len(request.ForceEntryPointAddress) == 0 { return errors.New("strategy configuration illegal") } } - return nil } diff --git a/common/model/user_operation.go b/common/model/user_operation.go index c13b4fb1..d7e2c9c1 100644 --- a/common/model/user_operation.go +++ b/common/model/user_operation.go @@ -3,12 +3,12 @@ package model type UserOperationItem struct { Sender string `json:"sender" binding:"required"` Nonce string `json:"nonce" binding:"required"` - InitCode string `json:"init_code" binding:"required"` + InitCode string `json:"init_code"` CallGasLimit string `json:"call_gas_limit" binding:"required"` VerificationGasList string `json:"verification_gas_list" binding:"required"` PerVerificationGas 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" binding:"required"` + Signature string `json:"signature"` //paymasterAndData string `json:"paymaster_and_data"` } diff --git a/service/dashboard_service/dashboard_service.go b/service/dashboard_service/dashboard_service.go index a0a2e6e2..e3368b87 100644 --- a/service/dashboard_service/dashboard_service.go +++ b/service/dashboard_service/dashboard_service.go @@ -2,6 +2,15 @@ package dashboard_service import "AAStarCommunity/EthPaymaster_BackService/common/model" +var mockStrategyMap = map[string]model.Strategy{} + +func init() { + mockStrategyMap["1"] = model.Strategy{ + Id: "1", + EntryPointAddress: "0x123", + PayMasterAddress: "0x123", + } +} func GetStrategyById(strategyId string) model.Strategy { - return model.Strategy{} + return mockStrategyMap[strategyId] } diff --git a/service/operator/get_support_entry_point_test.go b/service/operator/get_support_entry_point_test.go new file mode 100644 index 00000000..18b97f2b --- /dev/null +++ b/service/operator/get_support_entry_point_test.go @@ -0,0 +1 @@ +package operator diff --git a/service/operator/get_support_strategy_text.go b/service/operator/get_support_strategy_text.go new file mode 100644 index 00000000..18b97f2b --- /dev/null +++ b/service/operator/get_support_strategy_text.go @@ -0,0 +1 @@ +package operator diff --git a/service/operator/try_pay_user_op_test.go b/service/operator/try_pay_user_op_test.go index e6d03af2..47cbe0b6 100644 --- a/service/operator/try_pay_user_op_test.go +++ b/service/operator/try_pay_user_op_test.go @@ -2,13 +2,18 @@ package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" + "fmt" "testing" ) func TestTryPayUserOpExecute(t *testing.T) { request := getMockTryPayUserOpRequest() - TryPayUserOpExecute(request) + result, err := TryPayUserOpExecute(request) + if err != nil { + t.Errorf("Error: %v", err) + } + fmt.Printf("Result: %v", result) } From ce8e82ee0e7e02fff2914766bb89e41e882ae401 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 22:24:04 +0800 Subject: [PATCH 07/15] API if Fail Return --- rpc_server/api/v1/get_support_entrypoint.go | 3 +++ rpc_server/api/v1/get_support_strategy.go | 3 +++ rpc_server/api/v1/try_pay_user_operation.go | 3 +++ 3 files changed, 9 insertions(+) diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index 40200590..c6d686e4 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -23,10 +23,12 @@ func GetSupportEntrypoint(c *gin.Context) { if err := c.ShouldBindJSON(&request); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } if err := request.Validate(); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } //2. recall service @@ -34,6 +36,7 @@ func GetSupportEntrypoint(c *gin.Context) { if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) + return } response.WithData(result).Success(c) diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index 3f0bab8c..243b5b9a 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -25,15 +25,18 @@ func GetSupportStrategy(c *gin.Context) { if err := c.ShouldBindJSON(&request); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } if err := request.Validate(); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } result, err := operator.GetSupportStrategyExecute(request) if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) + return } response.WithData(result).Success(c) } diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index ecca7ed3..ce5c1edd 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -24,17 +24,20 @@ func TryPayUserOperation(c *gin.Context) { if err := c.ShouldBindJSON(&request); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } if err := request.Validate(); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) + return } //2. recall service result, err := operator.TryPayUserOpExecute(request) if err != nil { errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) + return } response.WithData(result).Success(c) } From 48f83a26be198d8e4f7e0b63a03c45260bf37e59 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 22:27:14 +0800 Subject: [PATCH 08/15] change API param --- common/model/api_request.go | 2 +- docs/docs.go | 9 ++------- docs/swagger.json | 9 ++------- docs/swagger.yaml | 8 ++------ 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/common/model/api_request.go b/common/model/api_request.go index 793721e5..5f0be327 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -3,7 +3,7 @@ package model import "errors" type TryPayUserOpRequest struct { - ForceStrategyId string `json:"strategy_id"` + ForceStrategyId string `json:"force_strategy_id"` ForceNetWork string `json:"force_network"` ForceTokens string `json:"force_tokens"` ForceEntryPointAddress string `json:"force_entry_point_address"` diff --git a/docs/docs.go b/docs/docs.go index 10b2bca1..6dd24bed 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -151,9 +151,6 @@ const docTemplate = `{ "model.TryPayUserOpRequest": { "type": "object", "properties": { - "apikey": { - "type": "string" - }, "extra": {}, "force_entry_point_address": { "type": "string" @@ -161,10 +158,10 @@ const docTemplate = `{ "force_network": { "type": "string" }, - "force_tokens": { + "force_strategy_id": { "type": "string" }, - "strategy_id": { + "force_tokens": { "type": "string" }, "user_operation": { @@ -176,13 +173,11 @@ const docTemplate = `{ "type": "object", "required": [ "call_gas_limit", - "init_code", "max_fee_per_gas", "max_priority_fee_per_gas", "nonce", "per_verification_gas", "sender", - "signature", "verification_gas_list" ], "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index d5c0fa64..28ceaf51 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -140,9 +140,6 @@ "model.TryPayUserOpRequest": { "type": "object", "properties": { - "apikey": { - "type": "string" - }, "extra": {}, "force_entry_point_address": { "type": "string" @@ -150,10 +147,10 @@ "force_network": { "type": "string" }, - "force_tokens": { + "force_strategy_id": { "type": "string" }, - "strategy_id": { + "force_tokens": { "type": "string" }, "user_operation": { @@ -165,13 +162,11 @@ "type": "object", "required": [ "call_gas_limit", - "init_code", "max_fee_per_gas", "max_priority_fee_per_gas", "nonce", "per_verification_gas", "sender", - "signature", "verification_gas_list" ], "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index e1f6f55d..815bfda4 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -6,16 +6,14 @@ definitions: type: object model.TryPayUserOpRequest: properties: - apikey: - type: string extra: {} force_entry_point_address: type: string force_network: type: string - force_tokens: + force_strategy_id: type: string - strategy_id: + force_tokens: type: string user_operation: $ref: '#/definitions/model.UserOperationItem' @@ -42,13 +40,11 @@ definitions: type: string required: - call_gas_limit - - init_code - max_fee_per_gas - max_priority_fee_per_gas - nonce - per_verification_gas - sender - - signature - verification_gas_list type: object info: From f74e300c1ad30f2339f323a351f5448cc381d6e5 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 23:04:40 +0800 Subject: [PATCH 09/15] fix deep dup bug --- common/model/api_response.go | 12 ++--- rpc_server/api/v1/get_support_entrypoint.go | 2 +- rpc_server/api/v1/try_pay_user_operation.go | 2 +- .../dashboard_service/strategy_selector.go | 2 +- service/gas_service/gas_compotor.go | 6 +-- .../get_support_entry_point_execute.go | 4 +- .../operator/get_support_strategy_execute.go | 4 +- ...y_text.go => get_support_strategy_test.go} | 0 service/operator/try_pay_user_op_execute.go | 46 +++++++++---------- service/validator_service/basic_validator.go | 3 +- 10 files changed, 40 insertions(+), 41 deletions(-) rename service/operator/{get_support_strategy_text.go => get_support_strategy_test.go} (100%) diff --git a/common/model/api_response.go b/common/model/api_response.go index 5e45ec29..39c7bf36 100644 --- a/common/model/api_response.go +++ b/common/model/api_response.go @@ -1,12 +1,12 @@ package model type TryPayUserOpResponse struct { - StrategyId string `json:"strategy_id"` - EntryPointAddress string `json:"entry_point_address"` - PayMasterAddress string `json:"pay_master_address"` - PayMasterSignature string `json:"pay_master_signature"` - PayReceipt interface{} `json:"pay_receipt"` - GasInfo ComputeGasResponse `json:"gaf_info"` + StrategyId string `json:"strategy_id"` + EntryPointAddress string `json:"entry_point_address"` + PayMasterAddress string `json:"pay_master_address"` + PayMasterSignature string `json:"pay_master_signature"` + PayReceipt interface{} `json:"pay_receipt"` + GasInfo *ComputeGasResponse `json:"gaf_info"` } type ComputeGasResponse struct { diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index c6d686e4..07e4f0c1 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -32,7 +32,7 @@ func GetSupportEntrypoint(c *gin.Context) { } //2. recall service - result, err := operator.GetSupportEntrypointExecute(request) + result, err := operator.GetSupportEntrypointExecute(&request) if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index ce5c1edd..b4e1508e 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -33,7 +33,7 @@ func TryPayUserOperation(c *gin.Context) { return } //2. recall service - result, err := operator.TryPayUserOpExecute(request) + result, err := operator.TryPayUserOpExecute(&request) if err != nil { errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) diff --git a/service/dashboard_service/strategy_selector.go b/service/dashboard_service/strategy_selector.go index c4ac6d48..61ab5006 100644 --- a/service/dashboard_service/strategy_selector.go +++ b/service/dashboard_service/strategy_selector.go @@ -4,7 +4,7 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/model" ) -func GetSuitableStrategy(entrypoint string, network string, token string) (model.Strategy, error) { +func GetSuitableStrategy(entrypoint *string, network *string, token *string) (model.Strategy, error) { return model.Strategy{}, nil diff --git a/service/gas_service/gas_compotor.go b/service/gas_service/gas_compotor.go index fcae3d59..d80a4526 100644 --- a/service/gas_service/gas_compotor.go +++ b/service/gas_service/gas_compotor.go @@ -2,10 +2,10 @@ 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 ComputeGas(userOp *model.UserOperationItem, strategy *model.Strategy) (*model.ComputeGasResponse, error) { + return &model.ComputeGasResponse{}, nil } -func ValidateGas(userOp model.UserOperationItem, gasComputeResponse model.ComputeGasResponse) error { +func ValidateGas(userOp *model.UserOperationItem, gasComputeResponse *model.ComputeGasResponse) error { return nil } diff --git a/service/operator/get_support_entry_point_execute.go b/service/operator/get_support_entry_point_execute.go index 2a03206e..b3f5cd9c 100644 --- a/service/operator/get_support_entry_point_execute.go +++ b/service/operator/get_support_entry_point_execute.go @@ -2,6 +2,6 @@ package operator import "AAStarCommunity/EthPaymaster_BackService/common/model" -func GetSupportEntrypointExecute(request model.GetSupportEntrypointRequest) (model.Result, error) { - return model.Result{}, nil +func GetSupportEntrypointExecute(request *model.GetSupportEntrypointRequest) (*model.Result, error) { + return &model.Result{}, nil } diff --git a/service/operator/get_support_strategy_execute.go b/service/operator/get_support_strategy_execute.go index ba50edc4..b0cd18f4 100644 --- a/service/operator/get_support_strategy_execute.go +++ b/service/operator/get_support_strategy_execute.go @@ -4,6 +4,6 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/model" ) -func GetSupportStrategyExecute(request model.GetSupportStrategyRequest) (model.Result, error) { - return model.Result{}, nil +func GetSupportStrategyExecute(request *model.GetSupportStrategyRequest) (*model.Result, error) { + return &model.Result{}, nil } diff --git a/service/operator/get_support_strategy_text.go b/service/operator/get_support_strategy_test.go similarity index 100% rename from service/operator/get_support_strategy_text.go rename to service/operator/get_support_strategy_test.go diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 30fbfff5..3491526a 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -8,37 +8,37 @@ import ( "golang.org/x/xerrors" ) -func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error) { +func TryPayUserOpExecute(request *model.TryPayUserOpRequest) (*model.Result, error) { //validator if err := businessParamValidate(request); err != nil { - return model.Result{}, err + return &model.Result{}, err } userOp := request.UserOperation //getStrategy strategy, err := strategyGenerate(request) if err != nil { - return model.Result{}, err + return &model.Result{}, err } - if err := validator_service.ValidateStrategy(strategy, userOp); err != nil { - return model.Result{}, err + if err := validator_service.ValidateStrategy(strategy, &userOp); err != nil { + return &model.Result{}, err } //base Strategy and UserOp computeGas - gasResponse, gasComputeError := gas_service.ComputeGas(userOp, strategy) + gasResponse, gasComputeError := gas_service.ComputeGas(&userOp, strategy) if gasComputeError != nil { - return model.Result{}, gasComputeError + return &model.Result{}, gasComputeError } //validate gas - if err := gas_service.ValidateGas(userOp, gasResponse); err != nil { - return model.Result{}, err + if err := gas_service.ValidateGas(&userOp, gasResponse); err != nil { + return &model.Result{}, err } //pay - payReceipt, payError := executePay(strategy, request.UserOperation, gasResponse) + payReceipt, payError := executePay(strategy, &userOp, gasResponse) if payError != nil { - return model.Result{}, payError + return &model.Result{}, payError } - paymasterSignature := getPayMasterSignature(strategy, request.UserOperation) + paymasterSignature := getPayMasterSignature(strategy, &userOp) var result = model.TryPayUserOpResponse{ StrategyId: strategy.Id, EntryPointAddress: strategy.EntryPointAddress, @@ -48,44 +48,44 @@ func TryPayUserOpExecute(request model.TryPayUserOpRequest) (model.Result, error GasInfo: gasResponse, } - return model.Result{ + return &model.Result{ Code: 200, Data: result, Message: "message", Cost: "cost", }, nil } -func businessParamValidate(request model.TryPayUserOpRequest) error { +func businessParamValidate(request *model.TryPayUserOpRequest) error { //UserOp Validate 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) (interface{}, error) { //1.Recharge //2.record account //3.return Receipt return nil, nil } -func getPayMasterSignature(strategy model.Strategy, userOp model.UserOperationItem) string { +func getPayMasterSignature(strategy *model.Strategy, userOp *model.UserOperationItem) string { return "" } -func strategyGenerate(request model.TryPayUserOpRequest) (model.Strategy, error) { +func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, error) { if forceStrategyId := request.ForceStrategyId; forceStrategyId != "" { //force strategy strategy := dashboard_service.GetStrategyById(forceStrategyId) if strategy == (model.Strategy{}) { - return model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) + return &model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) } - return strategy, nil + return &strategy, nil } - suitableStrategy, err := dashboard_service.GetSuitableStrategy(request.ForceEntryPointAddress, request.ForceNetWork, request.ForceTokens) //TODO + suitableStrategy, err := dashboard_service.GetSuitableStrategy(&request.ForceEntryPointAddress, &request.ForceNetWork, &request.ForceTokens) //TODO if err != nil { - return model.Strategy{}, err + return &model.Strategy{}, err } if suitableStrategy == (model.Strategy{}) { - return model.Strategy{}, xerrors.Errorf("Empty Strategies") + return &model.Strategy{}, xerrors.Errorf("Empty Strategies") } - return suitableStrategy, nil + return &suitableStrategy, nil } diff --git a/service/validator_service/basic_validator.go b/service/validator_service/basic_validator.go index f10077c5..d893f658 100644 --- a/service/validator_service/basic_validator.go +++ b/service/validator_service/basic_validator.go @@ -2,7 +2,6 @@ package validator_service import "AAStarCommunity/EthPaymaster_BackService/common/model" -func ValidateStrategy(strategy model.Strategy, userOp model.UserOperationItem) error { - +func ValidateStrategy(strategy *model.Strategy, userOp *model.UserOperationItem) error { return nil } From ce96dbcd30f15ad6f90abb80ff27f9492fb9370f Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 23:09:31 +0800 Subject: [PATCH 10/15] fix deep dup bug --- service/operator/try_pay_user_op_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/operator/try_pay_user_op_test.go b/service/operator/try_pay_user_op_test.go index 47cbe0b6..54a0da98 100644 --- a/service/operator/try_pay_user_op_test.go +++ b/service/operator/try_pay_user_op_test.go @@ -9,7 +9,7 @@ import ( func TestTryPayUserOpExecute(t *testing.T) { request := getMockTryPayUserOpRequest() - result, err := TryPayUserOpExecute(request) + result, err := TryPayUserOpExecute(&request) if err != nil { t.Errorf("Error: %v", err) } From ac325361ef9f1d28343e2ffab1ce0ec786a6ff90 Mon Sep 17 00:00:00 2001 From: devops <993921@qq.com> Date: Tue, 5 Mar 2024 23:11:15 +0800 Subject: [PATCH 11/15] use pointer --- rpc_server/api/v1/get_support_strategy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index 243b5b9a..684d0fe1 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -32,7 +32,7 @@ func GetSupportStrategy(c *gin.Context) { response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) return } - result, err := operator.GetSupportStrategyExecute(request) + result, err := operator.GetSupportStrategyExecute(&request) if err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) From 7a575960cc44be8451af5a2a0b86c884ecc3c7c5 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 5 Mar 2024 23:36:36 +0800 Subject: [PATCH 12/15] fix bug --- common/model/api_response.go | 2 +- service/operator/try_pay_user_op_execute.go | 4 ++-- service/operator/try_pay_user_op_test.go | 5 ++--- .../{userOpValidator.go => user_op_validator.go} | 0 .../chain/{IChainValidator.go => base_chain_validator.go} | 0 .../{EthereumValidator.go => ethereum_validator.go} | 6 +++--- 6 files changed, 8 insertions(+), 9 deletions(-) rename service/validator_service/{userOpValidator.go => user_op_validator.go} (100%) rename validator/chain/{IChainValidator.go => base_chain_validator.go} (100%) rename validator/chain/ethereum/{EthereumValidator.go => ethereum_validator.go} (64%) diff --git a/common/model/api_response.go b/common/model/api_response.go index 39c7bf36..176d3ada 100644 --- a/common/model/api_response.go +++ b/common/model/api_response.go @@ -6,7 +6,7 @@ type TryPayUserOpResponse struct { PayMasterAddress string `json:"pay_master_address"` PayMasterSignature string `json:"pay_master_signature"` PayReceipt interface{} `json:"pay_receipt"` - GasInfo *ComputeGasResponse `json:"gaf_info"` + GasInfo *ComputeGasResponse `json:"gas_info"` } type ComputeGasResponse struct { diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 3491526a..496d1f12 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -74,7 +74,7 @@ func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, erro if forceStrategyId := request.ForceStrategyId; forceStrategyId != "" { //force strategy strategy := dashboard_service.GetStrategyById(forceStrategyId) - if strategy == (model.Strategy{}) { + if &strategy == nil { return &model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) } return &strategy, nil @@ -84,7 +84,7 @@ func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, erro if err != nil { return &model.Strategy{}, err } - if suitableStrategy == (model.Strategy{}) { + if &suitableStrategy == nil { return &model.Strategy{}, xerrors.Errorf("Empty Strategies") } return &suitableStrategy, nil diff --git a/service/operator/try_pay_user_op_test.go b/service/operator/try_pay_user_op_test.go index 54a0da98..cb6fe023 100644 --- a/service/operator/try_pay_user_op_test.go +++ b/service/operator/try_pay_user_op_test.go @@ -3,6 +3,7 @@ package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" "fmt" + "github.com/stretchr/testify/assert" "testing" ) @@ -10,9 +11,7 @@ import ( func TestTryPayUserOpExecute(t *testing.T) { request := getMockTryPayUserOpRequest() result, err := TryPayUserOpExecute(&request) - if err != nil { - t.Errorf("Error: %v", err) - } + assert.NoError(t, err) fmt.Printf("Result: %v", result) } diff --git a/service/validator_service/userOpValidator.go b/service/validator_service/user_op_validator.go similarity index 100% rename from service/validator_service/userOpValidator.go rename to service/validator_service/user_op_validator.go diff --git a/validator/chain/IChainValidator.go b/validator/chain/base_chain_validator.go similarity index 100% rename from validator/chain/IChainValidator.go rename to validator/chain/base_chain_validator.go diff --git a/validator/chain/ethereum/EthereumValidator.go b/validator/chain/ethereum/ethereum_validator.go similarity index 64% rename from validator/chain/ethereum/EthereumValidator.go rename to validator/chain/ethereum/ethereum_validator.go index e84d4325..542bc86b 100644 --- a/validator/chain/ethereum/EthereumValidator.go +++ b/validator/chain/ethereum/ethereum_validator.go @@ -8,17 +8,17 @@ type EthValidator struct { *chain.Base } -func (e EthValidator) IsSupport() bool { +func (e *EthValidator) IsSupport() bool { //TODO implement me panic("implement me") } -func (e EthValidator) PreValidate() (err error) { +func (e *EthValidator) PreValidate() (err error) { //TODO implement me panic("implement me") } -func (e EthValidator) AfterGasValidate() (err error) { +func (e *EthValidator) AfterGasValidate() (err error) { //TODO implement me panic("implement me") } From e567b1a27242771d49598feefe48b96df0a90bc1 Mon Sep 17 00:00:00 2001 From: devops <993921@qq.com> Date: Tue, 5 Mar 2024 23:36:36 +0800 Subject: [PATCH 13/15] code clean & style --- common/model/api_request.go | 2 +- common/model/api_response.go | 18 ++++---- common/model/{Chain.go => chain.go} | 0 docs/docs.go | 2 +- docs/swagger.json | 2 +- docs/swagger.yaml | 2 +- rpc_server/api/v1/get_support_entrypoint.go | 1 - rpc_server/api/v1/get_support_strategy.go | 9 ++-- rpc_server/api/v1/try_pay_user_operation.go | 9 ++-- service/contractService/gethRecall.go | 1 - service/contract_service/geth_recall.go | 1 + .../dashboard_service/dashboard_service.go | 6 +-- .../dashboard_service/strategy_selector.go | 7 ++-- ...asWeiGenerator.go => gas_wei_generator.go} | 0 service/monitorService/report.go | 1 - service/monitor_service/report.go | 1 + service/operator/try_pay_user_op_execute.go | 42 ++++++++++--------- ...serOpValidator.go => user_op_validator.go} | 0 ...{IChainValidator.go => chain_validator.go} | 2 +- ...reumValidator.go => ethereum_validator.go} | 8 ++-- .../{4844Validator.go => 4844_validator.go} | 0 ...eValidator.go => eoa_enabled_validator.go} | 0 22 files changed, 59 insertions(+), 55 deletions(-) rename common/model/{Chain.go => chain.go} (100%) delete mode 100644 service/contractService/gethRecall.go create mode 100644 service/contract_service/geth_recall.go rename service/gas_service/{gasWeiGenerator.go => gas_wei_generator.go} (100%) delete mode 100644 service/monitorService/report.go create mode 100644 service/monitor_service/report.go rename service/validator_service/{userOpValidator.go => user_op_validator.go} (100%) rename validator/chain/{IChainValidator.go => chain_validator.go} (79%) rename validator/chain/ethereum/{EthereumValidator.go => ethereum_validator.go} (58%) rename validator/eip/eip_4844/{4844Validator.go => 4844_validator.go} (100%) rename validator/other/{eoaEnableValidator.go => eoa_enabled_validator.go} (100%) diff --git a/common/model/api_request.go b/common/model/api_request.go index 5f0be327..51ea0f26 100644 --- a/common/model/api_request.go +++ b/common/model/api_request.go @@ -6,7 +6,7 @@ type TryPayUserOpRequest struct { ForceStrategyId string `json:"force_strategy_id"` ForceNetWork string `json:"force_network"` ForceTokens string `json:"force_tokens"` - ForceEntryPointAddress string `json:"force_entry_point_address"` + ForceEntryPointAddress string `json:"force_entrypoint_address"` UserOperation UserOperationItem `json:"user_operation"` Extra interface{} `json:"extra"` } diff --git a/common/model/api_response.go b/common/model/api_response.go index 39c7bf36..6272ba12 100644 --- a/common/model/api_response.go +++ b/common/model/api_response.go @@ -2,17 +2,17 @@ package model type TryPayUserOpResponse struct { StrategyId string `json:"strategy_id"` - EntryPointAddress string `json:"entry_point_address"` - PayMasterAddress string `json:"pay_master_address"` - PayMasterSignature string `json:"pay_master_signature"` + EntryPointAddress string `json:"entrypoint_address"` + PayMasterAddress string `json:"paymaster_address"` + PayMasterSignature string `json:"paymaster_signature"` PayReceipt interface{} `json:"pay_receipt"` - GasInfo *ComputeGasResponse `json:"gaf_info"` + GasInfo *ComputeGasResponse `json:"gas_info"` } type ComputeGasResponse struct { - StrategyId string - TokenCost string - Network string - Token string - UsdCost string + StrategyId string `json:"strategy_id"` + TokenCost string `json:"token_cost"` + Network string `json:"network"` + Token string `json:"token"` + UsdCost string `json:"usd_cost"` } diff --git a/common/model/Chain.go b/common/model/chain.go similarity index 100% rename from common/model/Chain.go rename to common/model/chain.go diff --git a/docs/docs.go b/docs/docs.go index 6dd24bed..a6959e2d 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -152,7 +152,7 @@ const docTemplate = `{ "type": "object", "properties": { "extra": {}, - "force_entry_point_address": { + "force_entrypoint_address": { "type": "string" }, "force_network": { diff --git a/docs/swagger.json b/docs/swagger.json index 28ceaf51..f5b5ef35 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -141,7 +141,7 @@ "type": "object", "properties": { "extra": {}, - "force_entry_point_address": { + "force_entrypoint_address": { "type": "string" }, "force_network": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 815bfda4..8852545d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7,7 +7,7 @@ definitions: model.TryPayUserOpRequest: properties: extra: {} - force_entry_point_address: + force_entrypoint_address: type: string force_network: type: string diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index 07e4f0c1..d3e0a0b8 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -39,5 +39,4 @@ func GetSupportEntrypoint(c *gin.Context) { return } response.WithData(result).Success(c) - } diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index 684d0fe1..76ce005a 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -17,7 +17,6 @@ import ( // @Router /api/v1/get-support-strategy [GET] // @Security JWT func GetSupportStrategy(c *gin.Context) { - //2. recall service request := model.GetSupportStrategyRequest{} response := model.GetResponse() @@ -32,11 +31,13 @@ func GetSupportStrategy(c *gin.Context) { response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) return } - result, err := operator.GetSupportStrategyExecute(&request) - if err != nil { + + if result, err := operator.GetSupportStrategyExecute(&request); err != nil { errStr := fmt.Sprintf("%v", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) return + } else { + response.WithData(result).Success(c) + return } - response.WithData(result).Success(c) } diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index b4e1508e..b9ce5e1a 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -20,6 +20,7 @@ import ( func TryPayUserOperation(c *gin.Context) { request := model.TryPayUserOpRequest{} response := model.GetResponse() + //1. API validate if err := c.ShouldBindJSON(&request); err != nil { errStr := fmt.Sprintf("Request Error [%v]", err) @@ -32,12 +33,14 @@ func TryPayUserOperation(c *gin.Context) { response.SetHttpCode(http.StatusBadRequest).FailCode(c, http.StatusBadRequest, errStr) return } + //2. recall service - result, err := operator.TryPayUserOpExecute(&request) - if err != nil { + if result, err := operator.TryPayUserOpExecute(&request); err != nil { errStr := fmt.Sprintf("TryPayUserOpExecute ERROR [%v]", err) response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) return + } else { + response.WithData(result).Success(c) + return } - response.WithData(result).Success(c) } diff --git a/service/contractService/gethRecall.go b/service/contractService/gethRecall.go deleted file mode 100644 index 31437cc7..00000000 --- a/service/contractService/gethRecall.go +++ /dev/null @@ -1 +0,0 @@ -package contractService diff --git a/service/contract_service/geth_recall.go b/service/contract_service/geth_recall.go new file mode 100644 index 00000000..f300daad --- /dev/null +++ b/service/contract_service/geth_recall.go @@ -0,0 +1 @@ +package contract_service diff --git a/service/dashboard_service/dashboard_service.go b/service/dashboard_service/dashboard_service.go index e3368b87..a8acdeec 100644 --- a/service/dashboard_service/dashboard_service.go +++ b/service/dashboard_service/dashboard_service.go @@ -2,15 +2,15 @@ package dashboard_service import "AAStarCommunity/EthPaymaster_BackService/common/model" -var mockStrategyMap = map[string]model.Strategy{} +var mockStrategyMap = map[string]*model.Strategy{} func init() { - mockStrategyMap["1"] = model.Strategy{ + mockStrategyMap["1"] = &model.Strategy{ Id: "1", EntryPointAddress: "0x123", PayMasterAddress: "0x123", } } -func GetStrategyById(strategyId string) model.Strategy { +func GetStrategyById(strategyId string) *model.Strategy { return mockStrategyMap[strategyId] } diff --git a/service/dashboard_service/strategy_selector.go b/service/dashboard_service/strategy_selector.go index 61ab5006..e540d521 100644 --- a/service/dashboard_service/strategy_selector.go +++ b/service/dashboard_service/strategy_selector.go @@ -2,10 +2,9 @@ package dashboard_service import ( "AAStarCommunity/EthPaymaster_BackService/common/model" + "errors" ) -func GetSuitableStrategy(entrypoint *string, network *string, token *string) (model.Strategy, error) { - - return model.Strategy{}, nil - +func GetSuitableStrategy(entrypoint string, network string, token string) (*model.Strategy, error) { + return nil, errors.New("not implemented") } diff --git a/service/gas_service/gasWeiGenerator.go b/service/gas_service/gas_wei_generator.go similarity index 100% rename from service/gas_service/gasWeiGenerator.go rename to service/gas_service/gas_wei_generator.go diff --git a/service/monitorService/report.go b/service/monitorService/report.go deleted file mode 100644 index 201cc69a..00000000 --- a/service/monitorService/report.go +++ /dev/null @@ -1 +0,0 @@ -package monitorService diff --git a/service/monitor_service/report.go b/service/monitor_service/report.go new file mode 100644 index 00000000..44ddfaa8 --- /dev/null +++ b/service/monitor_service/report.go @@ -0,0 +1 @@ +package monitor_service diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 3491526a..8fe8d278 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -9,34 +9,36 @@ import ( ) func TryPayUserOpExecute(request *model.TryPayUserOpRequest) (*model.Result, error) { - //validator + // validator if err := businessParamValidate(request); err != nil { - return &model.Result{}, err + return nil, err } userOp := request.UserOperation - //getStrategy - strategy, err := strategyGenerate(request) - if err != nil { - return &model.Result{}, err - } - if err := validator_service.ValidateStrategy(strategy, &userOp); err != nil { - return &model.Result{}, err + + // getStrategy + var strategy *model.Strategy + if stg, err := strategyGenerate(request); err != nil { + return nil, err + } else if err = validator_service.ValidateStrategy(stg, &userOp); err != nil { + return nil, err + } else { + strategy = stg } //base Strategy and UserOp computeGas gasResponse, gasComputeError := gas_service.ComputeGas(&userOp, strategy) if gasComputeError != nil { - return &model.Result{}, gasComputeError + return nil, gasComputeError } //validate gas if err := gas_service.ValidateGas(&userOp, gasResponse); err != nil { - return &model.Result{}, err + return nil, err } //pay payReceipt, payError := executePay(strategy, &userOp, gasResponse) if payError != nil { - return &model.Result{}, payError + return nil, payError } paymasterSignature := getPayMasterSignature(strategy, &userOp) var result = model.TryPayUserOpResponse{ @@ -73,19 +75,19 @@ func getPayMasterSignature(strategy *model.Strategy, userOp *model.UserOperation func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, error) { if forceStrategyId := request.ForceStrategyId; forceStrategyId != "" { //force strategy - strategy := dashboard_service.GetStrategyById(forceStrategyId) - if strategy == (model.Strategy{}) { + if strategy := dashboard_service.GetStrategyById(forceStrategyId); strategy == nil { return &model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) + } else { + return strategy, nil } - return &strategy, nil } - suitableStrategy, err := dashboard_service.GetSuitableStrategy(&request.ForceEntryPointAddress, &request.ForceNetWork, &request.ForceTokens) //TODO + suitableStrategy, err := dashboard_service.GetSuitableStrategy(request.ForceEntryPointAddress, request.ForceNetWork, request.ForceTokens) //TODO if err != nil { - return &model.Strategy{}, err + return nil, err } - if suitableStrategy == (model.Strategy{}) { - return &model.Strategy{}, xerrors.Errorf("Empty Strategies") + if suitableStrategy == nil { + return nil, xerrors.Errorf("Empty Strategies") } - return &suitableStrategy, nil + return suitableStrategy, nil } diff --git a/service/validator_service/userOpValidator.go b/service/validator_service/user_op_validator.go similarity index 100% rename from service/validator_service/userOpValidator.go rename to service/validator_service/user_op_validator.go diff --git a/validator/chain/IChainValidator.go b/validator/chain/chain_validator.go similarity index 79% rename from validator/chain/IChainValidator.go rename to validator/chain/chain_validator.go index f86f2185..1cb6d7fc 100644 --- a/validator/chain/IChainValidator.go +++ b/validator/chain/chain_validator.go @@ -1,6 +1,6 @@ package chain -type IChainValidator interface { +type Validator interface { PreValidate() (err error) AfterGasValidate() (err error) IsSupport() bool diff --git a/validator/chain/ethereum/EthereumValidator.go b/validator/chain/ethereum/ethereum_validator.go similarity index 58% rename from validator/chain/ethereum/EthereumValidator.go rename to validator/chain/ethereum/ethereum_validator.go index e84d4325..c6a7f7fe 100644 --- a/validator/chain/ethereum/EthereumValidator.go +++ b/validator/chain/ethereum/ethereum_validator.go @@ -4,21 +4,21 @@ import ( "AAStarCommunity/EthPaymaster_BackService/validator/chain" ) -type EthValidator struct { +type Validator struct { *chain.Base } -func (e EthValidator) IsSupport() bool { +func (e *Validator) IsSupport() bool { //TODO implement me panic("implement me") } -func (e EthValidator) PreValidate() (err error) { +func (e *Validator) PreValidate() (err error) { //TODO implement me panic("implement me") } -func (e EthValidator) AfterGasValidate() (err error) { +func (e *Validator) AfterGasValidate() (err error) { //TODO implement me panic("implement me") } diff --git a/validator/eip/eip_4844/4844Validator.go b/validator/eip/eip_4844/4844_validator.go similarity index 100% rename from validator/eip/eip_4844/4844Validator.go rename to validator/eip/eip_4844/4844_validator.go diff --git a/validator/other/eoaEnableValidator.go b/validator/other/eoa_enabled_validator.go similarity index 100% rename from validator/other/eoaEnableValidator.go rename to validator/other/eoa_enabled_validator.go From b0ab816bd8fc669ca5f85551463ce9b91529ce4e Mon Sep 17 00:00:00 2001 From: devops <993921@qq.com> Date: Wed, 6 Mar 2024 00:04:27 +0800 Subject: [PATCH 14/15] code clean & style --- service/operator/try_pay_user_op_execute.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 8fe8d278..d2edc05a 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -76,7 +76,7 @@ func strategyGenerate(request *model.TryPayUserOpRequest) (*model.Strategy, erro if forceStrategyId := request.ForceStrategyId; forceStrategyId != "" { //force strategy if strategy := dashboard_service.GetStrategyById(forceStrategyId); strategy == nil { - return &model.Strategy{}, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) + return nil, xerrors.Errorf("Not Support Strategy ID: [%w]", forceStrategyId) } else { return strategy, nil } From e872f0c3d896a1f4bbf7d8ce202ad730d2202f8b Mon Sep 17 00:00:00 2001 From: devops <993921@qq.com> Date: Wed, 6 Mar 2024 00:06:59 +0800 Subject: [PATCH 15/15] unit tests --- ...oint_test.go => get_support_entry_point_execute_test.go} | 0 ...trategy_test.go => get_support_strategy_execute_test.go} | 0 ..._pay_user_op_test.go => try_pay_user_op_execute_test.go} | 6 ++---- 3 files changed, 2 insertions(+), 4 deletions(-) rename service/operator/{get_support_entry_point_test.go => get_support_entry_point_execute_test.go} (100%) rename service/operator/{get_support_strategy_test.go => get_support_strategy_execute_test.go} (100%) rename service/operator/{try_pay_user_op_test.go => try_pay_user_op_execute_test.go} (91%) diff --git a/service/operator/get_support_entry_point_test.go b/service/operator/get_support_entry_point_execute_test.go similarity index 100% rename from service/operator/get_support_entry_point_test.go rename to service/operator/get_support_entry_point_execute_test.go diff --git a/service/operator/get_support_strategy_test.go b/service/operator/get_support_strategy_execute_test.go similarity index 100% rename from service/operator/get_support_strategy_test.go rename to service/operator/get_support_strategy_execute_test.go diff --git a/service/operator/try_pay_user_op_test.go b/service/operator/try_pay_user_op_execute_test.go similarity index 91% rename from service/operator/try_pay_user_op_test.go rename to service/operator/try_pay_user_op_execute_test.go index 54a0da98..937bcf85 100644 --- a/service/operator/try_pay_user_op_test.go +++ b/service/operator/try_pay_user_op_execute_test.go @@ -3,6 +3,7 @@ package operator import ( "AAStarCommunity/EthPaymaster_BackService/common/model" "fmt" + "github.com/stretchr/testify/assert" "testing" ) @@ -10,11 +11,8 @@ import ( func TestTryPayUserOpExecute(t *testing.T) { request := getMockTryPayUserOpRequest() result, err := TryPayUserOpExecute(&request) - if err != nil { - t.Errorf("Error: %v", err) - } + assert.NoError(t, err) fmt.Printf("Result: %v", result) - } func getMockTryPayUserOpRequest() model.TryPayUserOpRequest {