Skip to content

Commit

Permalink
optimize API
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed May 21, 2024
1 parent 317455d commit 69d948b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
9 changes: 5 additions & 4 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ type UserOpRequest struct {
EntryPointVersion global_const.EntrypointVersion `json:"entrypoint_version"`
}
type JsonRpcRequest struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
Id int `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
Id int `json:"id"`
Network global_const.Network `json:"-"`
}
type ClientCredential struct {
ApiKey string `json:"apiKey"`
Expand Down
4 changes: 4 additions & 0 deletions config/secret_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func GetNetworkSecretConfig(network global_const.Network) model.NetWorkSecretCon
return secretConfig.NetWorkSecretConfigMap[string(network)]
}

func CheckNetworkSupport(network global_const.Network) bool {
_, ok := secretConfig.NetWorkSecretConfigMap[string(network)]
return ok
}
func GetPriceOracleApiKey() string {
return secretConfig.PriceOracleApiKey
}
Expand Down
9 changes: 8 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const docTemplate = `{
}
}
},
"/api/v1/paymaster": {
"/api/v1/paymaster{network}": {
"post": {
"security": [
{
Expand All @@ -76,6 +76,13 @@ const docTemplate = `{
"Paymaster"
],
"parameters": [
{
"type": "string",
"description": "Network",
"name": "network",
"in": "path",
"required": true
},
{
"description": "JsonRpcRequest Model",
"name": "rpcRequest",
Expand Down
9 changes: 8 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
}
},
"/api/v1/paymaster": {
"/api/v1/paymaster{network}": {
"post": {
"security": [
{
Expand All @@ -65,6 +65,13 @@
"Paymaster"
],
"parameters": [
{
"type": "string",
"description": "Network",
"name": "network",
"in": "path",
"required": true
},
{
"description": "JsonRpcRequest Model",
"name": "rpcRequest",
Expand Down
7 changes: 6 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ paths:
description: OK
tags:
- Healthz
/api/v1/paymaster:
/api/v1/paymaster{network}:
post:
consumes:
- application/json
description: Paymaster JSON-RPC API
parameters:
- description: Network
in: path
name: network
required: true
type: string
- description: JsonRpcRequest Model
in: body
name: rpcRequest
Expand Down
40 changes: 20 additions & 20 deletions rpc_server/api/v1/paymaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func init() {
// @Description Paymaster JSON-RPC API
// @Accept json
// @Product json
// @param network path string true "Network"
// @Param rpcRequest body model.JsonRpcRequest true "JsonRpcRequest Model"
// @Router /api/v1/paymaster [post]
// @Router /api/v1/paymaster{network} [post]
// @Success 200
// @Security JWT
func Paymaster(ctx *gin.Context) {
Expand All @@ -46,6 +47,18 @@ func Paymaster(ctx *gin.Context) {
}

}()
network := ctx.Param("network")
if network == "" {
errStr := fmt.Sprintf("Request Error [network is empty]")
response.SetHttpCode(http.StatusBadRequest).FailCode(ctx, http.StatusBadRequest, errStr)
return
}
if !config.CheckNetworkSupport(global_const.Network(network)) {
errStr := fmt.Sprintf("Request Error [network not support]")
response.SetHttpCode(http.StatusBadRequest).FailCode(ctx, http.StatusBadRequest, errStr)
return
}
jsonRpcRequest.Network = global_const.Network(network)

if err := ctx.ShouldBindJSON(&jsonRpcRequest); err != nil {
errStr := fmt.Sprintf("Request Error [%v]", err)
Expand Down Expand Up @@ -79,14 +92,8 @@ func Paymaster(ctx *gin.Context) {

func GetSupportPaymaster() MethodFunctionFunc {
return func(ctx *gin.Context, jsonRpcRequest model.JsonRpcRequest) (result interface{}, err error) {
if jsonRpcRequest.Params[0] == nil {
return nil, xerrors.Errorf("Request Error [network is empty]")
}
networkStr, ok := jsonRpcRequest.Params[0].(string)
if !ok {
return nil, xerrors.Errorf("Request Error [network is not string]")
}
paymasterSet, err := config.GetSupportPaymaster(global_const.Network(networkStr))

paymasterSet, err := config.GetSupportPaymaster(jsonRpcRequest.Network)
if err != nil {
return nil, err
}
Expand All @@ -96,14 +103,7 @@ func GetSupportPaymaster() MethodFunctionFunc {

func GetSupportEntryPointFunc() MethodFunctionFunc {
return func(ctx *gin.Context, jsonRpcRequest model.JsonRpcRequest) (result interface{}, err error) {
if jsonRpcRequest.Params[0] == nil {
return nil, xerrors.Errorf("Request Error [network is empty]")
}
networkStr, ok := jsonRpcRequest.Params[0].(string)
if !ok {
return nil, xerrors.Errorf("Request Error [network is not string]")
}
entryPoints, err := config.GetSupportEntryPoints(global_const.Network(networkStr))
entryPoints, err := config.GetSupportEntryPoints(jsonRpcRequest.Network)
if err != nil {
return nil, err
}
Expand All @@ -113,6 +113,7 @@ func GetSupportEntryPointFunc() MethodFunctionFunc {
func EstimateUserOpGasFunc() MethodFunctionFunc {
return func(ctx *gin.Context, jsonRpcRequest model.JsonRpcRequest) (result interface{}, err error) {
request, err := parseTryPayUserOperationParams(jsonRpcRequest.Params)
request.Network = jsonRpcRequest.Network
if err != nil {
return nil, xerrors.Errorf("parseTryPayUserOperationParams ERROR [%v]", err)
}
Expand All @@ -130,6 +131,7 @@ func EstimateUserOpGasFunc() MethodFunctionFunc {
func TryPayUserOperationMethod() MethodFunctionFunc {
return func(ctx *gin.Context, jsonRpcRequest model.JsonRpcRequest) (result interface{}, err error) {
request, err := parseTryPayUserOperationParams(jsonRpcRequest.Params)
request.Network = jsonRpcRequest.Network
logrus.Debug("parseTryPayUserOperationParams result: ", request)

if err != nil {
Expand Down Expand Up @@ -167,9 +169,7 @@ func parseTryPayUserOperationParams(params []interface{}) (*model.UserOpRequest,
if extra["strategy_code"] != nil {
result.StrategyCode = extra["strategy_code"].(string)
}
if extra["network"] != nil {
result.Network = extra["network"].(global_const.Network)
}

if extra["token"] != nil {
result.UserPayErc20Token = extra["token"].(global_const.TokenType)
}
Expand Down
2 changes: 1 addition & 1 deletion rpc_server/routers/routers_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ type Path string
const (
Auth Path = "api/auth"
Healthz Path = "api/healthz"
Paymaster Path = "api/v1/paymaster"
Paymaster Path = "api/v1/paymaster:network"
)

0 comments on commit 69d948b

Please sign in to comment.