Skip to content

Commit

Permalink
mock v0
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Mar 5, 2024
1 parent ebabc9f commit 77570db
Show file tree
Hide file tree
Showing 34 changed files with 288 additions and 42 deletions.
1 change: 1 addition & 0 deletions common/model/Chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package model
11 changes: 11 additions & 0 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
@@ -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"`
}
18 changes: 18 additions & 0 deletions common/model/api_response.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package model

type ClientCredential struct {
ApiKey string `json:"apiKey"`
Expand Down
2 changes: 1 addition & 1 deletion rpc_server/models/response.go → common/model/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package model

import (
"github.com/gin-gonic/gin"
Expand Down
7 changes: 7 additions & 0 deletions common/model/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type Strategy struct {
Id string
EntryPointAddress string
PayMasterAddress string
}
14 changes: 14 additions & 0 deletions common/model/user_operation.go
Original file line number Diff line number Diff line change
@@ -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"`
}
4 changes: 0 additions & 4 deletions common/types/strategy.go

This file was deleted.

2 changes: 1 addition & 1 deletion conf/appsettings.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jwt:
security: hello-eth-paymaster
security: hello-ethereum-paymaster
realm: aastar
idkey: id
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions rpc_server/api/health.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions rpc_server/api/v1/get_support_entrypoint.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions rpc_server/api/v1/get_support_strategy.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions rpc_server/api/v1/try_pay_user_operation.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions rpc_server/middlewares/recovery.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
})
}

Expand Down
1 change: 0 additions & 1 deletion rpc_server/models/userOperation.go

This file was deleted.

4 changes: 2 additions & 2 deletions rpc_server/routers/boot.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions service/dashboard_service/dashboard_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dashboard_service

import "AAStarCommunity/EthPaymaster_BackService/common/model"

func GetStrategyById(strategyId string) model.Strategy {
return model.Strategy{}
}
11 changes: 11 additions & 0 deletions service/dashboard_service/strategy_selector.go
Original file line number Diff line number Diff line change
@@ -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

}
12 changes: 12 additions & 0 deletions service/executor/executor.go
Original file line number Diff line number Diff line change
@@ -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
}
90 changes: 90 additions & 0 deletions service/executor/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions service/executor/try_pay_user_op_test.go
Original file line number Diff line number Diff line change
@@ -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: "",
},
}
}
1 change: 0 additions & 1 deletion service/gasService/gasWeiGenerator.go

This file was deleted.

1 change: 1 addition & 0 deletions service/gas_service/gasWeiGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package gas_service
Loading

0 comments on commit 77570db

Please sign in to comment.