Skip to content

Commit

Permalink
optimize test
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Apr 29, 2024
1 parent c10837a commit 65198f8
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 52 deletions.
2 changes: 2 additions & 0 deletions common/global_const/basic_strategy_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ const (
StrategyCodeArbitrumSpeoliaV06Verify BasicStrategyCode = "Arbitrum_Sepolia_v06_verifyPaymaster"
StrategyCodeScrollSepoliaV06Verify BasicStrategyCode = "Scroll_Sepolia_v06_verifyPaymaster"
StrategyCodeBaseSepoliaV06Verify BasicStrategyCode = "Base_Sepolia_v06_verifyPaymaster"

StrategyCodeEthereumSepoliaV06Erc20 BasicStrategyCode = "Ethereum_Sepolia_v06_erc20Paymaster"
)
14 changes: 7 additions & 7 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

type UserOpRequest struct {
ForceStrategyId string `json:"force_strategy_id"`
ForceNetwork global_const.Network `json:"force_network"`
Erc20Token string `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"`
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"`
}
2 changes: 1 addition & 1 deletion common/network/ethereum_adaptable_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (executor EthereumExecutor) GetUserOpHash(userOp *user_op.UserOpInput, stra
erc20Token := common.HexToAddress("0x")
paytype := strategy.GetPayType()
if paytype == global_const.PayTypeERC20 {
tokenType := strategy.GetUseToken()
tokenType := strategy.Erc20TokenType
tokenAddress := conf.GetTokenAddress(strategy.GetNewWork(), tokenType)
erc20Token = common.HexToAddress(tokenAddress)
}
Expand Down
2 changes: 1 addition & 1 deletion common/paymaster_data/paymaster_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type PaymasterData struct {
func NewPaymasterDataInput(strategy *model.Strategy) *PaymasterData {
start := strategy.ExecuteRestriction.EffectiveStartTime
end := strategy.ExecuteRestriction.EffectiveEndTime
tokenAddress := conf.GetTokenAddress(strategy.GetNewWork(), strategy.GetUseToken())
tokenAddress := conf.GetTokenAddress(strategy.GetNewWork(), strategy.Erc20TokenType)
return &PaymasterData{
Paymaster: *strategy.GetPaymasterAddress(),
ValidUntil: big.NewInt(end.Int64()),
Expand Down
2 changes: 2 additions & 0 deletions common/user_op/user_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ type UserOpInput struct {
VerificationGasLimit *big.Int `json:"verificationGasLimit" mapstructure:"verification_gas_limit" binding:"required"`
AccountGasLimits [32]byte `json:"accountGasLimits" mapstructure:"account_gas_limits" binding:"required"`
GasFees [32]byte `json:"gasFees" mapstructure:"gas_fees" binding:"required"`

ComputeGasOnly bool
}

func packUserOpV6ForUserOpHash(userOp *UserOperationV06) (string, []byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion common/utils/price_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetPriceUsd(tokenType global_const.TokenType) (float64, error) {
return 1, nil
}
if tokenType == global_const.ETH {
return 4000, nil
return 3100, nil
}
url, ok := URLMap[tokenType]
if !ok {
Expand Down
3 changes: 2 additions & 1 deletion conf/business_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func convertConfig(originConfig *OriginBusinessConfig) *BusinessConfig {
IsTest: originNetWorkConfig.IsTest,
RpcUrl: fmt.Sprintf("%s/%s", originNetWorkConfig.RpcUrl, originNetWorkConfig.ApiKey),
TokenConfig: originNetWorkConfig.TokenConfig,
GasToken: originNetWorkConfig.GasToken,
}
paymasterArr := originConfig.SupportPaymaster[network]
paymasterSet := mapset.NewSet[string]()
Expand Down Expand Up @@ -85,7 +86,7 @@ type OriginNetWorkConfig struct {
ApiKey string `json:"api_key"`
SignerKey string `json:"signer_key"`
TokenConfig map[global_const.TokenType]string `json:"token_config"`
GasToken global_const.TokenType
GasToken global_const.TokenType `json:"gas_token"`
GasOracleAddress string
}

Expand Down
32 changes: 19 additions & 13 deletions conf/business_dev_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"rpc_url": "https://eth-sepolia.g.alchemy.com/v2",
"api_key": "wKeLycGxgYRykgf0aGfcpEkUtqyLQg4v",
"signer_key" : "1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421",
"gas_token": "ETH",
"token_config": {
"USDT": "0xd6ad2fa83f2012d5296b58dfad6e8c9567138035",
"USDC": "0x8d7b4d4b876dfd8ae031f13978890119cf0da9b1"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
},
"optimism-sepolia": {
Expand All @@ -17,9 +18,10 @@
"rpc_url": "https://opt-sepolia.g.alchemy.com/v2",
"api_key": "_z0GaU6Zk8RfIR1guuli8nqMdb8RPdp0",
"signer_key" : "1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421",
"gas_token": "ETH",
"token_config": {
"USDT": "0xebca682b6c15d539284432edc5b960771f0009e8",
"USDC": "0x87350147a24099bf1e7e677576f01c1415857c75"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
},
"arbitrum-sepolia": {
Expand All @@ -28,20 +30,22 @@
"rpc_url": "https://arb-sepolia.g.alchemy.com/v2",
"api_key": "xSBkiidslrZmlcWUOSF3AluKx0A9g_kl",
"signer_key" : "752d81d71afd0b25a09f24718baf82c0846cc3b68122cfb3a1d41529c8a46b05",
"gas_token": "ETH",
"token_config": {
"USDT": "0xA552958397146e081C8358cd63db64d3555Ec4f8",
"USDC": "0xe7E15A3027b6A08d2a6a3a9dc9250E711c206767"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
},
"scroll-sepolia": {
"chain_id": "534351",
"is_test": true,
"rpc_url": "https://sepolia-rpc.scroll.io",
"api_key": "",
"api_key": "9DGRNUARDVGDPZWN2G55I3Y5NG7HQJBUTH",
"signer_key" : "752d81d71afd0b25a09f24718baf82c0846cc3b68122cfb3a1d41529c8a46b05",
"gas_token": "ETH",
"token_config": {
"USDT": "0x87027e19837264cd35b73b90ac10b9ce4b06b4f2",
"USDC": "0x29a840decf91547db4ed77f1e59542c4d1986915"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
},
"starknet-sepolia": {
Expand All @@ -50,9 +54,10 @@
"rpc_url": "https://starknet-sepolia.infura.io/v3",
"api_key": "0284f5a9fc55476698079b24e2f97909",
"signer_key" : "752d81d71afd0b25a09f24718baf82c0846cc3b68122cfb3a1d41529c8a46b05",
"gas_token": "ETH",
"token_config": {
"USDT": "0xaa8e23fb1079ea71e0a56f48a2aa51851d8433d0",
"USDC": "0xaa8e23fb1079ea71e0a56f48a2aa51851d8433d0"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
},
"base-sepolia": {
Expand All @@ -61,9 +66,10 @@
"rpc_url": "https://base-sepolia.g.alchemy.com/v2",
"api_key": "zUhtd18b2ZOTIJME6rv2Uwz9q7PBnnsa",
"signer_key" : "752d81d71afd0b25a09f24718baf82c0846cc3b68122cfb3a1d41529c8a46b05",
"gas_token": "ETH",
"token_config": {
"USDT": "0xaa8e23fb1079ea71e0a56f48a2aa51851d8433d0",
"USDC": "0xaa8e23fb1079ea71e0a56f48a2aa51851d8433d0"
"USDT": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B",
"USDC": "0x3870419Ba2BBf0127060bCB37f69A1b1C090992B"
}
}
},
Expand Down
20 changes: 16 additions & 4 deletions gas_executor/gas_computor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ func ComputeGas(userOp *user_op.UserOpInput, strategy *model.Strategy, paymaster

totalGasDetail := GetTotalCostByEstimateGas(opEstimateGas)
updateUserOp := getNewUserOpAfterCompute(userOp, opEstimateGas, strategy.GetStrategyEntrypointVersion())
var erc20TokenCost *big.Float
if !userOp.ComputeGasOnly {
erc20TokenCost, err = getErc20TokenCost(strategy, totalGasDetail.MaxTxGasCostInEther)
if err != nil {
return nil, nil, xerrors.Errorf("getErc20TokenCost error: %v", err)
}
}
return &model.ComputeGasResponse{
OpEstimateGas: opEstimateGas,
TotalGasDetail: totalGasDetail,
Erc20TokenCost: erc20TokenCost,
}, updateUserOp, nil
}

func GetTotalCostByEstimateGas(userOpGas *model.UserOpEstimateGas) *model.TotalGasDetail {
gasPrice := GetUserOpGasPrice(userOpGas)
totalGasLimit := new(big.Int)
Expand Down Expand Up @@ -178,22 +187,25 @@ func EstimateCallGasLimit(strategy *model.Strategy, simulateOpResult *model.Simu
}
}

func getTokenCost(strategy *model.Strategy, tokenCount *big.Float) (*big.Float, error) {
func getErc20TokenCost(strategy *model.Strategy, tokenCount *big.Float) (*big.Float, error) {
if strategy.GetPayType() == global_const.PayTypeERC20 {

if strategy.Erc20TokenType == "" {
return nil, xerrors.Errorf("strategy.Erc20TokenType is nil")
}
formTokenType := conf.GetGasToken(strategy.GetNewWork())
toTokenType := strategy.GetUseToken()
toTokenType := strategy.Erc20TokenType
toTokenPrice, err := utils.GetToken(formTokenType, toTokenType)
if err != nil {
return nil, err
}
if toTokenPrice == 0 {
return nil, xerrors.Errorf("toTokenPrice can not be 0")
}
logrus.Debugf("toTokenPrice: %v", toTokenPrice)
tokenCost := new(big.Float).Mul(tokenCount, big.NewFloat(toTokenPrice))
return tokenCost, nil
}
return tokenCount, nil
return nil, nil

}

Expand Down
25 changes: 25 additions & 0 deletions gas_executor/gas_computor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,36 @@ func TestComputeGas(t *testing.T) {
testEstimateCallGasLimit(t, conf.GetBasicStrategyConfig(global_const.StrategyCodeScrollSepoliaV06Verify), model.MockSimulateHandleOpResult, op, global_const.DummayPreverificationgasBigint)
},
},
{
"TestUSDTTokenCost",
func(t *testing.T) {
strategy := conf.GetBasicStrategyConfig(global_const.StrategyCodeEthereumSepoliaV06Erc20)
strategy.Erc20TokenType = global_const.TokenTypeUSDT
testErc20TokenCost(t, strategy, big.NewFloat(0.0001))
},
},
{
"TestErc20",
func(t *testing.T) {
strategy := conf.GetBasicStrategyConfig(global_const.StrategyCodeEthereumSepoliaV06Erc20)
strategy.Erc20TokenType = global_const.TokenTypeUSDT
testErc20TokenCost(t, strategy, big.NewFloat(0.0001))
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
}
}
func testErc20TokenCost(t *testing.T, strategy *model.Strategy, tokenCount *big.Float) {
erc20TokenCost, err := getErc20TokenCost(strategy, tokenCount)
if err != nil {
t.Error(err)
return
}
t.Logf("erc20TokenCost:%v", erc20TokenCost)

}
func testEstimateCallGasLimit(t *testing.T, strategy *model.Strategy, simulateOpResult *model.SimulateHandleOpResult, op *user_op.UserOpInput, simulateGasPrice *big.Int) {
callGasLimit, err := EstimateCallGasLimit(strategy, simulateOpResult, op, simulateGasPrice)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion gas_executor/gas_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Erc20GasValidate() ValidatePaymasterGasFunc {
return func(userOp *user_op.UserOpInput, gasComputeResponse *model.ComputeGasResponse, strategy *model.Strategy) error {
userOpValue := *userOp
sender := userOpValue.Sender
tokenBalance, getTokenBalanceErr := chain_service.GetAddressTokenBalance(strategy.GetNewWork(), *sender, strategy.GetUseToken())
tokenBalance, getTokenBalanceErr := chain_service.GetAddressTokenBalance(strategy.GetNewWork(), *sender, strategy.Erc20TokenType)
if getTokenBalanceErr != nil {
return getTokenBalanceErr
}
Expand Down
19 changes: 12 additions & 7 deletions service/chain_service/chain_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"testing"
)

func TestGetAddressTokenBalance(t *testing.T) {
res, err := GetAddressTokenBalance(global_const.EthereumSepolia, common.HexToAddress("0xDf7093eF81fa23415bb703A685c6331584D30177"), global_const.TokenTypeUSDC)
assert.NoError(t, err)
fmt.Println(res)
}

func TestChainService(t *testing.T) {
conf.BasicStrategyInit("../../conf/basic_strategy_dev_config.json")
conf.BusinessConfigInit("../../conf/business_dev_config.json")
Expand Down Expand Up @@ -58,12 +52,23 @@ func TestChainService(t *testing.T) {
testCheckContractAddressAccess(t)
},
},
{

"testGetAddressTokenBalance",
func(t *testing.T) {
testGetAddressTokenBalance(t)
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
}
}

func testGetAddressTokenBalance(t *testing.T) {
res, err := GetAddressTokenBalance(global_const.EthereumSepolia, common.HexToAddress("0xFD44DF0Fe211d5EFDBe1423483Fcb3FDeF84540f"), global_const.TokenTypeUSDC)
assert.NoError(t, err)
fmt.Println(res)
}
func testCheckContractAddressAccess(t *testing.T) {
addressStr := "0xF2147CA7f18e8014b76e1A98BaffC96ebB90a29f"
address := common.HexToAddress(addressStr)
Expand Down
1 change: 1 addition & 0 deletions service/operator/get_estimate_user_op_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func GetEstimateUserOpGas(request *model.UserOpRequest) (*model.ComputeGasRespon
if err != nil {
return nil, err
}
userOp.ComputeGasOnly = true
gasResponse, _, gasComputeError := gas_executor.ComputeGas(userOp, strategy, paymaster_data.NewPaymasterDataInput(strategy))
if gasComputeError != nil {
return nil, gasComputeError
Expand Down
40 changes: 35 additions & 5 deletions service/operator/operator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operator

import (
"AAStarCommunity/EthPaymaster_BackService/common/global_const"
"AAStarCommunity/EthPaymaster_BackService/common/model"
"AAStarCommunity/EthPaymaster_BackService/common/utils"
"AAStarCommunity/EthPaymaster_BackService/conf"
Expand Down Expand Up @@ -38,9 +39,39 @@ func TestOperator(t *testing.T) {
},
},
{
"TestVerifyTryPayUserOpExecute",
"Test_EthereumSepoliaV06Verify_TryPayUserOpExecute",
func(t *testing.T) {
testTryPayUserOpExecute(t)
mockRequest.ForceStrategyId = string(global_const.StrategyCodeEthereumSepoliaV06Verify)
testTryPayUserOpExecute(t, mockRequest)
},
},
{
"Test_OptimismSepoliaV06Verify_TryPayUserOpExecute",
func(t *testing.T) {
mockRequest.ForceStrategyId = string(global_const.StrategyCodeOptimismSepoliaV06Verify)
testTryPayUserOpExecute(t, mockRequest)
},
},
{
"Test_ArbitrumSpeoliaV06Verify_TryPayUserOpExecute",
func(t *testing.T) {
mockRequest.ForceStrategyId = string(global_const.StrategyCodeArbitrumSpeoliaV06Verify)
testTryPayUserOpExecute(t, mockRequest)
},
},
{
"Test_BaseSepoliaV06Verify_TryPayUserOpExecute",
func(t *testing.T) {
mockRequest.ForceStrategyId = string(global_const.StrategyCodeArbitrumSpeoliaV06Verify)
testTryPayUserOpExecute(t, mockRequest)
},
},
{
"Test_EthereumSepoliaV06Erc20_TryPayUserOpExecute",
func(t *testing.T) {
mockRequest.Erc20Token = global_const.TokenTypeUSDT
mockRequest.ForceStrategyId = string(global_const.StrategyCodeEthereumSepoliaV06Erc20)
testTryPayUserOpExecute(t, mockRequest)
},
},
{
Expand Down Expand Up @@ -81,15 +112,14 @@ func testGetSupportEntrypointExecute(t *testing.T) {
}
t.Log(res)
}
func testTryPayUserOpExecute(t *testing.T) {
request := getMockTryPayUserOpRequest()
func testTryPayUserOpExecute(t *testing.T, request *model.UserOpRequest) {
result, err := TryPayUserOpExecute(request)
if err != nil {
t.Error(err)
return
}
resultJson, _ := json.Marshal(result)
fmt.Printf("Result: %v", string(resultJson))
t.Logf("Result: %v", string(resultJson))
}

func getMockTryPayUserOpRequest() *model.UserOpRequest {
Expand Down
Loading

0 comments on commit 65198f8

Please sign in to comment.