Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed May 13, 2024
1 parent 3eb601b commit 72e693b
Show file tree
Hide file tree
Showing 33 changed files with 413 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
envirment/appsettings.yaml
config/appsettings.yaml
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
# Go workspace file
go.work

.env

.idea
.vscode/
build/

vendor

conf/appsettings.*.yaml
config/secret_*.json
3 changes: 2 additions & 1 deletion cmd/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func APITestCall(engine *gin.Engine, method, url string, body any, response any,
}

func TestAPI(t *testing.T) {
initEngine("../../config/basic_strategy_dev_config.json", "../../config/business_dev_config.json")

initEngine("../../config/basic_strategy_config.json", "../../config/basic_config.json", "../../config/secret_config.json")
tests := []struct {
name string
test func(t *testing.T)
Expand Down
24 changes: 15 additions & 9 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"AAStarCommunity/EthPaymaster_BackService/envirment"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/routers"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"os"
"strings"
)

const (
strategyPath = "./config/basic_strategy_config.json"
basicConfigPath = "./config/basic_config.json"
)

var aPort = flag.String("port", "", "Port")

// runMode running mode
Expand Down Expand Up @@ -44,24 +48,26 @@ var Engine *gin.Engine
// @description Type 'Bearer \<TOKEN\>' to correctly set the AccessToken
// @BasePath /api
func main() {
strategyPath := fmt.Sprintf("./config/basic_strategy_%s_config.json", strings.ToLower(envirment.Environment.Name))
businessConfigPath := fmt.Sprintf("./config/business_%s_config.json", strings.ToLower(envirment.Environment.Name))

initEngine(strategyPath, businessConfigPath)
secretPath := os.Getenv("secret_config_path")
if secretPath == "" {
secretPath = "./config/secret_config.json"
}
initEngine(strategyPath, basicConfigPath, secretPath)
port := runMode()
os.Getenv("secret_config_path")
_ = Engine.Run(port)
}

func initEngine(strategyPath string, businessConfigPath string) {
config.BasicStrategyInit(strategyPath)
config.BusinessConfigInit(businessConfigPath)
func initEngine(strategyPath string, basicConfigPath string, secretPath string) {

logrus.Infof("secretPath: %s", secretPath)
config.InitConfig(strategyPath, basicConfigPath, secretPath)
if envirment.Environment.IsDevelopment() {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
logrus.Infof("Environment: %s", envirment.Environment.Name)
logrus.Infof("Debugger: %v", envirment.Environment.Debugger)
logrus.Infof("Action ENV : [%v]", os.Getenv("GITHUB_ACTIONS"))
Engine = routers.SetRouters()
}
6 changes: 3 additions & 3 deletions common/ethereum_contract/ethereum_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

func TestPaymasterV07(t *testing.T) {

config.BasicStrategyInit("../../config/basic_strategy_dev_config.json")
config.BusinessConfigInit("../../config/business_dev_config.json")
network := config.GetEthereumRpcUrl(global_const.EthereumSepolia)
config.InitConfig("../../config/basic_strategy_config.json", "../../config/basic_config.json", "../../config/secret_config.json")
network := config.GetNewWorkClientURl(global_const.EthereumSepolia)
contractAddress := common.HexToAddress("0x3Da96267B98a33267249734FD8FFeC75093D3085")
t.Logf("network URL %s", network)
client, err := ethclient.Dial(network)
if err != nil {
t.Fatalf("Error: %v", err)
Expand Down
13 changes: 5 additions & 8 deletions common/global_const/common_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func init() {
if err != nil {
panic(err)
}
signatureByte, err := hex.DecodeString(DummySignature[2:])
if err != nil {
panic(err)
}
DummySignatureByte = signatureByte
}

var GasOverHand = struct {
Expand All @@ -85,11 +90,3 @@ var GasOverHand = struct {
BundleSize: 1,
sigSize: 65,
}

func init() {
signatureByte, err := hex.DecodeString(DummySignature[2:])
if err != nil {
panic(err)
}
DummySignatureByte = signatureByte
}
12 changes: 12 additions & 0 deletions common/model/secret_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

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

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

type NetWorkSecretConfig struct {
RpcUrl string `json:"rpc_url"`
SignerKey string `json:"signer_key"`
}
3 changes: 1 addition & 2 deletions common/network/arbitrum/arbitrum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

func TestGetArbitrumGas(t *testing.T) {
config.BasicStrategyInit("../../../config/basic_strategy_dev_config.json")
config.BusinessConfigInit("../../../config/business_dev_config.json")
config.InitConfig("../../../config/basic_strategy_config.json", "../../../config/basic_config.json", "../../../config/secret_config.json")
strategy := config.GetBasicStrategyConfig("Arbitrum_Sepolia_v06_verifyPaymaster")
if strategy == nil {
t.Error("strategy is nil")
Expand Down
92 changes: 53 additions & 39 deletions common/network/ethereum_adaptable_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
"math/big"
"sync"
)

var executorMap = make(map[global_const.Network]*EthereumExecutor)
Expand Down Expand Up @@ -64,43 +65,56 @@ type EthereumExecutor struct {
ChainId *big.Int
}

var mu sync.Mutex

func GetEthereumExecutor(network global_const.Network) *EthereumExecutor {
executor, ok := executorMap[network]
if ok {
return executor
}

if executorMap[network] == nil {
// TODO need to check Out Client Connection Always Open
client, err := ethclient.Dial(config.GetEthereumRpcUrl(network))
if err != nil {
panic(err)
}
chainId := big.NewInt(0)
_, success := chainId.SetString(config.GetChainId(network), 10)
if !success {
panic(xerrors.Errorf("chainId %s is invalid", config.GetChainId(network)))
}
geth := gethclient.New(client.Client())
executorMap[network] = &EthereumExecutor{
network: network,
Client: client,
ChainId: chainId,
GethClient: geth,
}
mu.Lock()
defer mu.Unlock()
executor, ok = executorMap[network]
if ok {
return executor
}
rpcUrl := config.GetNewWorkClientURl(network)
if rpcUrl == "" {
panic(xerrors.Errorf("network [%s] is not supported", network))
}
client, err := ethclient.Dial(rpcUrl)
if err != nil {
panic(err)
}
chainId := big.NewInt(0)
_, success := chainId.SetString(config.GetChainId(network), 10)
if !success {
panic(xerrors.Errorf("chainId %s is invalid", config.GetChainId(network)))
}
geth := gethclient.New(client.Client())
executorMap[network] = &EthereumExecutor{
network: network,
Client: client,
ChainId: chainId,
GethClient: geth,
}

return executorMap[network]
}

func (executor EthereumExecutor) GetEntryPointV6Deposit(entryPoint *common.Address, depoist common.Address) (*big.Int, error) {
func (executor *EthereumExecutor) GetEntryPointV6Deposit(entryPoint *common.Address, deposit common.Address) (*big.Int, error) {
contract, err := executor.GetEntryPoint06(entryPoint)
if err != nil {
return nil, err
}
depoistInfo, err := contract.GetDepositInfo(&bind.CallOpts{}, depoist)
depoistInfo, err := contract.GetDepositInfo(&bind.CallOpts{}, deposit)
if err != nil {
return nil, err
}
return depoistInfo.Deposit, nil
}
func (executor EthereumExecutor) GetEntryPointV7Deposit(entryPoint *common.Address, depoist common.Address) (*big.Int, error) {
func (executor *EthereumExecutor) GetEntryPointV7Deposit(entryPoint *common.Address, depoist common.Address) (*big.Int, error) {
contract, err := executor.GetEntryPoint07(entryPoint)
if err != nil {
return nil, err
Expand All @@ -112,7 +126,7 @@ func (executor EthereumExecutor) GetEntryPointV7Deposit(entryPoint *common.Addre
return depositInfo.Deposit, nil
}

func (executor EthereumExecutor) GetUserTokenBalance(userAddress common.Address, token global_const.TokenType) (*big.Int, error) {
func (executor *EthereumExecutor) GetUserTokenBalance(userAddress common.Address, token global_const.TokenType) (*big.Int, error) {
tokenAddress := config.GetTokenAddress(executor.network, token) //TODO
if tokenAddress == "" {
return nil, xerrors.Errorf("tokenType [%s] is not supported in [%s] network", token, executor.network)
Expand All @@ -124,7 +138,7 @@ func (executor EthereumExecutor) GetUserTokenBalance(userAddress common.Address,
}
return tokenInstance.BalanceOf(&bind.CallOpts{}, userAddress)
}
func (executor EthereumExecutor) CheckContractAddressAccess(contract *common.Address) (bool, error) {
func (executor *EthereumExecutor) CheckContractAddressAccess(contract *common.Address) (bool, error) {
client := executor.Client

code, err := client.CodeAt(context.Background(), *contract, nil)
Expand All @@ -137,7 +151,7 @@ func (executor EthereumExecutor) CheckContractAddressAccess(contract *common.Add
return true, nil
}

func (executor EthereumExecutor) GetTokenContract(tokenAddress *common.Address) (*contract_erc20.Contract, error) {
func (executor *EthereumExecutor) GetTokenContract(tokenAddress *common.Address) (*contract_erc20.Contract, error) {
client := executor.Client
contract, ok := TokenContractCache[tokenAddress]
if !ok {
Expand All @@ -151,7 +165,7 @@ func (executor EthereumExecutor) GetTokenContract(tokenAddress *common.Address)
return contract, nil
}

func (executor EthereumExecutor) EstimateUserOpCallGas(entrypointAddress *common.Address, userOpParam *user_op.UserOpInput) (*big.Int, error) {
func (executor *EthereumExecutor) EstimateUserOpCallGas(entrypointAddress *common.Address, userOpParam *user_op.UserOpInput) (*big.Int, error) {
client := executor.Client
userOpValue := *userOpParam
res, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
Expand All @@ -164,7 +178,7 @@ func (executor EthereumExecutor) EstimateUserOpCallGas(entrypointAddress *common
}
return new(big.Int).SetUint64(res), nil
}
func (executor EthereumExecutor) EstimateCreateSenderGas(entrypointAddress *common.Address, userOpParam *user_op.UserOpInput) (*big.Int, error) {
func (executor *EthereumExecutor) EstimateCreateSenderGas(entrypointAddress *common.Address, userOpParam *user_op.UserOpInput) (*big.Int, error) {
client := executor.Client
userOpValue := *userOpParam
factoryAddress, err := userOpValue.GetFactoryAddress()
Expand All @@ -184,7 +198,7 @@ func (executor EthereumExecutor) EstimateCreateSenderGas(entrypointAddress *comm

// GetGasPrice uint256 gasFee = min(maxFeePerGas, maxPriorityFeePerGas + block.baseFee);
// maxPriorityFeePerGasBuffer = L1_fee / verificationGasLimit
func (executor EthereumExecutor) GetGasPrice() (*model.GasPrice, error) {
func (executor *EthereumExecutor) GetGasPrice() (*model.GasPrice, error) {

//The Arbitrum sequencer ignores priority fees and eth_maxPriorityFeePerGas always returns 0
//On Optimism we set maxPriorityFeePerGas = l1_gas / l2_base_fee
Expand Down Expand Up @@ -239,7 +253,7 @@ func (executor EthereumExecutor) GetGasPrice() (*model.GasPrice, error) {
// OpResource https://github.com/ethereum-optimism/optimism/blob/233ede59d16cb01bdd8e7ff662a153a4c3178bdd/packages/contracts/contracts/L2/predeploys/OVM_GasPriceOracle.sol#L109-L124
// l1Gas = zeros * TX_DATA_ZERO_GAS + (nonZeros + 4) * TX_DATA_NON_ZERO_GAS
// l1GasFee = ((l1Gas + overhead) * l1BaseFee * scalar) / PRECISION
func (executor EthereumExecutor) GetL1DataFee(data []byte) (*big.Int, error) {
func (executor *EthereumExecutor) GetL1DataFee(data []byte) (*big.Int, error) {
address, ok := config.L1GasOracleInL2[executor.network]
if !ok {
return nil, xerrors.Errorf("L1GasOracleInL2 not found in network %s", executor.network)
Expand All @@ -266,7 +280,7 @@ func (executor EthereumExecutor) GetL1DataFee(data []byte) (*big.Int, error) {
return fee, nil
}

func (executor EthereumExecutor) SimulateV06HandleOp(v06 *user_op.UserOpInput, entryPoint *common.Address) (*model.SimulateHandleOpResult, error) {
func (executor *EthereumExecutor) SimulateV06HandleOp(v06 *user_op.UserOpInput, entryPoint *common.Address) (*model.SimulateHandleOpResult, error) {
abi, err := contract_entrypoint_v06.ContractMetaData.GetAbi()
if err != nil {
return nil, err
Expand Down Expand Up @@ -303,7 +317,7 @@ func (executor EthereumExecutor) SimulateV06HandleOp(v06 *user_op.UserOpInput, e
}, nil
}

func (executor EthereumExecutor) SimulateV07HandleOp(userOpV07 user_op.UserOpInput, entryPoint *common.Address) (*model.SimulateHandleOpResult, error) {
func (executor *EthereumExecutor) SimulateV07HandleOp(userOpV07 user_op.UserOpInput, entryPoint *common.Address) (*model.SimulateHandleOpResult, error) {
var result *simulate_entrypoint.IEntryPointSimulationsExecutionResult
simulateAbi, err := simulate_entrypoint.ContractMetaData.GetAbi()
if err != nil {
Expand Down Expand Up @@ -353,7 +367,7 @@ func (executor EthereumExecutor) SimulateV07HandleOp(userOpV07 user_op.UserOpInp
TargetResult: result.TargetResult,
}, nil
}
func (executor EthereumExecutor) GetSimulateEntryPoint() (*simulate_entrypoint.Contract, error) {
func (executor *EthereumExecutor) GetSimulateEntryPoint() (*simulate_entrypoint.Contract, error) {
contract, ok := SimulateEntryPointContractCache[executor.network]
if !ok {
contractInstance, err := simulate_entrypoint.NewContract(common.HexToAddress("0x"), executor.Client)
Expand All @@ -365,7 +379,7 @@ func (executor EthereumExecutor) GetSimulateEntryPoint() (*simulate_entrypoint.C
}
return contract, nil
}
func (executor EthereumExecutor) GetPaymasterDeposit(paymasterAddress *common.Address) (*big.Int, error) {
func (executor *EthereumExecutor) GetPaymasterDeposit(paymasterAddress *common.Address) (*big.Int, error) {
contract, err := executor.GetPaymasterErc20AndVerifyV06(paymasterAddress)
if err != nil {
return nil, err
Expand All @@ -377,7 +391,7 @@ func (executor EthereumExecutor) GetPaymasterDeposit(paymasterAddress *common.Ad
return deposit, nil
}

func (executor EthereumExecutor) GetEntryPoint07(entryPoint *common.Address) (*contract_entrypoint_v07.Contract, error) {
func (executor *EthereumExecutor) GetEntryPoint07(entryPoint *common.Address) (*contract_entrypoint_v07.Contract, error) {
contract, ok := V07EntryPointContractCache[executor.network][*entryPoint]
if !ok {
contractInstance, err := contract_entrypoint_v07.NewContract(*entryPoint, executor.Client)
Expand All @@ -389,7 +403,7 @@ func (executor EthereumExecutor) GetEntryPoint07(entryPoint *common.Address) (*c
}
return contract, nil
}
func (executor EthereumExecutor) GetEntryPoint06(entryPoint *common.Address) (*contract_entrypoint_v06.Contract, error) {
func (executor *EthereumExecutor) GetEntryPoint06(entryPoint *common.Address) (*contract_entrypoint_v06.Contract, error) {
contract, ok := V06EntryPointContractCache[executor.network][*entryPoint]
if !ok {
contractInstance, err := contract_entrypoint_v06.NewContract(*entryPoint, executor.Client)
Expand All @@ -405,7 +419,7 @@ func (executor EthereumExecutor) GetEntryPoint06(entryPoint *common.Address) (*c
return contract, nil

}
func (executor EthereumExecutor) GetPaymasterErc20AndVerifyV06(paymasterAddress *common.Address) (*paymaster_verifying_erc20_v06.Contract, error) {
func (executor *EthereumExecutor) GetPaymasterErc20AndVerifyV06(paymasterAddress *common.Address) (*paymaster_verifying_erc20_v06.Contract, error) {
contract, ok := V06PaymasterErc20AndPaymasterCache[executor.network][*paymasterAddress]
if !ok {
contractInstance, err := paymaster_verifying_erc20_v06.NewContract(*paymasterAddress, executor.Client)
Expand All @@ -420,7 +434,7 @@ func (executor EthereumExecutor) GetPaymasterErc20AndVerifyV06(paymasterAddress
}
return contract, nil
}
func (executor EthereumExecutor) GetPaymasterErc20AndVerifyV07(paymasterAddress *common.Address) (*paymaster_verifying_erc20_v07.Contract, error) {
func (executor *EthereumExecutor) GetPaymasterErc20AndVerifyV07(paymasterAddress *common.Address) (*paymaster_verifying_erc20_v07.Contract, error) {
contract, ok := V07PaymasterErc20AndPaymasterCache[executor.network][*paymasterAddress]
if !ok {
contractInstance, err := paymaster_verifying_erc20_v07.NewContract(*paymasterAddress, executor.Client)
Expand All @@ -436,7 +450,7 @@ func (executor EthereumExecutor) GetPaymasterErc20AndVerifyV07(paymasterAddress
return contract, nil
}

func (executor EthereumExecutor) GetAuth() (*bind.TransactOpts, error) {
func (executor *EthereumExecutor) GetAuth() (*bind.TransactOpts, error) {
if executor.ChainId == nil {
return nil, xerrors.Errorf("chainId is nil")
}
Expand All @@ -460,7 +474,7 @@ func GetAuth(chainId *big.Int, privateKey *ecdsa.PrivateKey) (*bind.TransactOpts
Context: context.Background(),
}, nil
}
func (executor EthereumExecutor) GetUserOpHash(userOp *user_op.UserOpInput, strategy *model.Strategy) ([]byte, string, error) {
func (executor *EthereumExecutor) GetUserOpHash(userOp *user_op.UserOpInput, strategy *model.Strategy) ([]byte, string, error) {
version := strategy.GetStrategyEntrypointVersion()
erc20Token := common.HexToAddress("0x")
payType := strategy.GetPayType()
Expand Down Expand Up @@ -525,7 +539,7 @@ func (executor EthereumExecutor) GetUserOpHash(userOp *user_op.UserOpInput, stra
}

}
func (executor EthereumExecutor) GetPaymasterData(userOp *user_op.UserOpInput, strategy *model.Strategy, paymasterDataInput *paymaster_data.PaymasterDataInput) ([]byte, error) {
func (executor *EthereumExecutor) GetPaymasterData(userOp *user_op.UserOpInput, strategy *model.Strategy, paymasterDataInput *paymaster_data.PaymasterDataInput) ([]byte, error) {
userOpHash, _, err := executor.GetUserOpHash(userOp, strategy)
if err != nil {
logrus.Errorf("GetUserOpHash error [%v]", err)
Expand Down
Loading

0 comments on commit 72e693b

Please sign in to comment.