Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

1.optimize API 2.invole config #24

Merged
merged 11 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"AAStarCommunity/EthPaymaster_BackService/config"
"AAStarCommunity/EthPaymaster_BackService/envirment"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/routers"
"AAStarCommunity/EthPaymaster_BackService/service/dashboard_service"
"flag"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,6 +68,7 @@ func initEngine(strategyPath string, basicConfigPath string, secretPath string)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
dashboard_service.Init()
logrus.Infof("Environment: %s", envirment.Environment.Name)
logrus.Infof("Debugger: %v", envirment.Environment.Debugger)
Engine = routers.SetRouters()
Expand Down
6 changes: 6 additions & 0 deletions common/global_const/common_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"math/big"
)

type StrategyStatus string

const (
DummyPrivateKeyText = "0a82406dc7fcf16090e05215ff394c7465608dd1a698632471b1eb37b8ece2f7"
DummySignature = "0x3054659b5e29460a8f3ac9afc3d5fcbe4b76f92aed454b944e9b29e55d80fde807716530b739540e95cfa4880d69f710a9d45910f2951a227675dc1fb0fdf2c71c"
Expand All @@ -17,6 +19,10 @@ const (
DummyVerificationGas = 50000
DummyPaymasterPostOpGasLimit = 2000000
DummyPaymasterVerificationGasLimit = 5000000

ContextKeyApiMoDel = "api_model"
StrategyStatusDisable StrategyStatus = "disable"
StrategyStatusAchieve StrategyStatus = "enable"
)

var (
Expand Down
9 changes: 5 additions & 4 deletions common/global_const/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func IsStableToken(token TokenType) bool {
}

const (
TokenTypeUSDT TokenType = "USDT"
TokenTypeUSDC TokenType = "USDC"
TokenTypeETH TokenType = "ETH"
TokenTypeOP TokenType = "OP"
TokenTypeUSDT TokenType = "USDT"
TokenTypeUSDC TokenType = "USDC"
TokenTypeETH TokenType = "ETH"
TokenTypeOP TokenType = "OP"
TokenTypeAAStar TokenType = "AAStar"
)
9 changes: 9 additions & 0 deletions common/model/api_key_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

import "golang.org/x/time/rate"

type ApiKeyModel struct {
Disable bool `gorm:"column:disable;type:bool" json:"disable"`
ApiKey string `gorm:"column:api_key;type:varchar(255)" json:"api_key"`
RateLimit rate.Limit `gorm:"column:rate_limit;type:int" json:"rate_limit"`
}
24 changes: 12 additions & 12 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
)

type UserOpRequest struct {
ForceStrategyId string `json:"force_strategy_id"`
ForceNetwork global_const.Network `json:"force_network"`
Erc20Token global_const.TokenType `json:"force_token"`
ForceEntryPointAddress string `json:"force_entrypoint_address"`
UserOp map[string]any `json:"user_operation"`
Extra interface{} `json:"extra"`
EstimateOpGas bool `json:"estimate_op_gas"`
EntryPointVersion global_const.EntrypointVersion `json:"entrypoint_version"`
StrategyCode string `json:"strategy_code"`
Network global_const.Network `json:"network"`
UserPayErc20Token global_const.TokenType `json:"user_pay_erc20_token"`
UserOp map[string]any `json:"user_operation"`
Extra interface{} `json:"extra"`
EstimateOpGas bool `json:"estimate_op_gas"`
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
15 changes: 15 additions & 0 deletions common/model/secret_config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package model

type DBConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
DBName string `json:"db_name"`
TimeZone string `json:"tz"`
SslMode string `json:"ssl_mode"`
}

type SecretConfig struct {
PriceOracleApiKey string `json:"price_oracle_api_key"`

NetWorkSecretConfigMap map[string]NetWorkSecretConfig `json:"network_secret_configs"`

ConfigDBConfig DBConfig `json:"config_db_config"`
RelayDBConfig DBConfig `json:"relay_db_config"`
ApiKeyTableName string `json:"api_key_table_name"`
StrategyConfigTableName string `json:"strategy_config_table_name"`
}

type NetWorkSecretConfig struct {
Expand Down
43 changes: 22 additions & 21 deletions common/model/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ import (
)

type Strategy struct {
Id string `json:"id"`
StrategyCode string `json:"strategy_code"`
PaymasterInfo *PaymasterInfo `json:"paymaster_info"`
NetWorkInfo *NetWorkInfo `json:"network_info"`
EntryPointInfo *EntryPointInfo `json:"entrypoint_info"`
Description string `json:"description"`
ExecuteRestriction StrategyExecuteRestriction `json:"execute_restriction"`
Id string `json:"id"`
StrategyCode string `json:"strategy_code"`
PaymasterInfo *PaymasterInfo `json:"paymaster_info"`
NetWorkInfo *NetWorkInfo `json:"network_info"`
EntryPointInfo *EntryPointInfo `json:"entrypoint_info"`
Description string `json:"description"`
ExecuteRestriction *StrategyExecuteRestriction `json:"execute_restriction"`
Erc20TokenType global_const.TokenType
}
type PaymasterInfo struct {
PayMasterAddress *common.Address `json:"paymaster_address"`
PayType global_const.PayType `json:"pay_type"`
PayMasterAddress *common.Address `json:"paymaster_address"`
PayType global_const.PayType `json:"pay_type"`
IsProjectErc20PayEnable bool `json:"is_project_erc20_pay_enable"`
}
type NetWorkInfo struct {
NetWork global_const.Network `json:"network"`
Token global_const.TokenType `json:"tokens"`
NetWork global_const.Network `json:"network"`
GasToken global_const.TokenType `json:"tokens"`
}
type EntryPointInfo struct {
EntryPointAddress *common.Address `json:"entrypoint_address"`
Expand All @@ -41,29 +42,29 @@ func (strategy *Strategy) GetNewWork() global_const.Network {
}

func (strategy *Strategy) GetUseToken() global_const.TokenType {
return strategy.NetWorkInfo.Token
return strategy.NetWorkInfo.GasToken
}
func (strategy *Strategy) GetPayType() global_const.PayType {
return strategy.PaymasterInfo.PayType
}
func (strategy *Strategy) GetStrategyEntrypointVersion() global_const.EntrypointVersion {
return strategy.EntryPointInfo.EntryPointVersion
}
func (strategy Strategy) IsCurrencyPayEnable() bool {
func (strategy *Strategy) IsCurrencyPayEnable() bool {
return false
}

type StrategyExecuteRestriction struct {
BanSenderAddress string `json:"ban_sender_address"`
EffectiveStartTime *big.Int `json:"effective_start_time"`
EffectiveEndTime *big.Int `json:"effective_end_time"`
GlobalMaxUSD int64 `json:"global_max_usd"`
GlobalMaxOpCount int64 `json:"global_max_op_count"`
DayMaxUSD int64 `json:"day_max_usd"`
StartTime int64 `json:"start_time"`
EndTime int64 `json:"end_time"`
BanSenderAddress mapset.Set[string] `json:"ban_sender_address"`
EffectiveStartTime *big.Int `json:"start_time"`
EffectiveEndTime *big.Int `json:"end_time"`
GlobalMaxUSD *big.Float `json:"global_max_usd"`
GlobalMaxOpCount *big.Int `json:"global_max_op_count"`
DayMaxUSD *big.Float `json:"day_max_usd"`
AccessProject mapset.Set[string] `json:"access_project"`
AccessErc20 mapset.Set[string] `json:"access_erc20"`
ChainIdWhiteList mapset.Set[string] `json:"chain_id_whitelist"`
Status global_const.StrategyStatus
}

type StrategyValidateConfig struct {
Expand Down
16 changes: 10 additions & 6 deletions config/basic_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ func GetChainId(networkParam global_const.Network) string {
return networkConfig.ChainId
}

func GetPaymasterAddress(network global_const.Network, version global_const.EntrypointVersion) common.Address {
func GetPaymasterAddress(network global_const.Network, version global_const.EntrypointVersion) *common.Address {
networkConfig := basicConfig.NetworkConfigMap[network]
if version == global_const.EntrypointV07 {
return networkConfig.V07PaymasterAddress
return &networkConfig.V07PaymasterAddress
}
return networkConfig.V06PaymasterAddress
return &networkConfig.V06PaymasterAddress
}

func GetEntrypointAddress(network global_const.Network, version global_const.EntrypointVersion) common.Address {
func GetEntrypointAddress(network global_const.Network, version global_const.EntrypointVersion) *common.Address {
networkConfig := basicConfig.NetworkConfigMap[network]
if version == global_const.EntrypointV07 {
return networkConfig.V07EntryPointAddress
return &networkConfig.V07EntryPointAddress
}
return networkConfig.V06EntryPointAddress
return &networkConfig.V06EntryPointAddress

}

Expand All @@ -170,8 +170,12 @@ var (
global_const.ScrollMainnet: common.HexToAddress("0x5300000000000000000000000000000000000002"),
}
Disable1559Chain = mapset.NewSet(global_const.ScrollSepolia, global_const.ScrollMainnet)
PErc20TokenSet = mapset.NewSet(global_const.TokenTypeAAStar)
)

func IsPErc20Token(token global_const.TokenType) bool {
return PErc20TokenSet.Contains(token)
}
func IsDisable1559Chain(network global_const.Network) bool {
return Disable1559Chain.Contains(network)
}
Expand Down
8 changes: 4 additions & 4 deletions config/basic_strategy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var suitableStrategyMap = make(map[global_const.Network]map[global_const.Entrypo
func GetBasicStrategyConfig(strategyCode global_const.BasicStrategyCode) *model.Strategy {
strategy := basicStrategyConfig[string(strategyCode)]
paymasterAddress := GetPaymasterAddress(strategy.GetNewWork(), strategy.GetStrategyEntrypointVersion())
strategy.PaymasterInfo.PayMasterAddress = &paymasterAddress
strategy.PaymasterInfo.PayMasterAddress = paymasterAddress
entryPointAddress := GetEntrypointAddress(strategy.GetNewWork(), strategy.GetStrategyEntrypointVersion())
strategy.EntryPointInfo.EntryPointAddress = &entryPointAddress
strategy.EntryPointInfo.EntryPointAddress = entryPointAddress
return strategy

}
Expand Down Expand Up @@ -79,7 +79,7 @@ func convertMapToStrategyConfig(data map[string]map[string]any) (map[string]*mod
EntryPointVersion: global_const.EntrypointVersion(value["entrypoint_version"].(string)),
},

ExecuteRestriction: model.StrategyExecuteRestriction{
ExecuteRestriction: &model.StrategyExecuteRestriction{
EffectiveStartTime: effectiveStartTime,
EffectiveEndTime: effectiveEndTime,
AccessProject: utils.ConvertStringToSet(accessProjectStr, ","),
Expand All @@ -90,7 +90,7 @@ func convertMapToStrategyConfig(data map[string]map[string]any) (map[string]*mod
}
if strategy.GetPayType() == global_const.PayTypeERC20 {
erc20TokenStr := value["access_erc20"].(string)
strategy.NetWorkInfo.Token = global_const.TokenType(erc20TokenStr)
strategy.NetWorkInfo.GasToken = global_const.TokenType(erc20TokenStr)
strategy.ExecuteRestriction.AccessErc20 = utils.ConvertStringToSet(erc20TokenStr, ",")
}

Expand Down
32 changes: 32 additions & 0 deletions config/secret_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
)

var dsnTemplate = "host=%s port=%v user=%s password=%s dbname=%s TimeZone=%s sslmode=%s"

var secretConfig *model.SecretConfig
var signerConfig = make(SignerConfigMap)

Expand Down Expand Up @@ -42,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 All @@ -54,3 +60,29 @@ func GetSignerKey(network global_const.Network) string {
func GetSigner(network global_const.Network) *global_const.EOA {
return signerConfig[network]
}
func GetAPIKeyTableName() string {
return secretConfig.ApiKeyTableName
}
func GetStrategyConfigTableName() string {
return secretConfig.StrategyConfigTableName
}
func GetConfigDBDSN() string {
return fmt.Sprintf(dsnTemplate,
secretConfig.ConfigDBConfig.Host,
secretConfig.ConfigDBConfig.Port,
secretConfig.ConfigDBConfig.User,
secretConfig.ConfigDBConfig.Password,
secretConfig.ConfigDBConfig.DBName,
secretConfig.ConfigDBConfig.TimeZone,
secretConfig.ConfigDBConfig.SslMode)
}
func GetRelayDBDSN() string {
return fmt.Sprintf(dsnTemplate,
secretConfig.RelayDBConfig.Host,
secretConfig.RelayDBConfig.Port,
secretConfig.RelayDBConfig.User,
secretConfig.RelayDBConfig.Password,
secretConfig.RelayDBConfig.DBName,
secretConfig.RelayDBConfig.TimeZone,
secretConfig.RelayDBConfig.SslMode)
}
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
Loading
Loading