Skip to content

Commit

Permalink
V1.0 main (#22)
Browse files Browse the repository at this point in the history
* optimize get API

* add GetSupportStrategy

* change response

* add Support gas

* add userop convert

* add userop gasCompute

* add paymaster_generator impl
change swagger

* update

* update

* support get token price

* fix debug

* fix debug

* optimize gas compute

* update API

* update API

* update API

* update API

* fix bug

* fix bug

* fix bug

* fix bug

* generatePaymasterData

* add GetCoinPrice

* update PackUserOp

* update PackUserOp

* update PackUserOp

* update PackUserOp

* update PackUserOp

* update PackUserOp

* update PackUserOp

* update PackUserOp

* feat: change call methods function

* feat: split string

* update PackUserOp

* fix packOp bug

* fix packOp bug

* fix packOp bug

* fix packOp bug

* fix sign bug

* fix sign bug

* fix sign bug

* fix sign bug

* fix sign bug

* fix sign bug

* change paymasterAddress

* change paymasterAddress

* change paymasterAddress

* v1.0 init

* v1.0 init

* init

* v1.0 add abi

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* add verifying paymaster

* fix start error

* optimize

* optimize catalog

* optimize catalog

* add Starknet demo

* optimize

* optimize

* optimize

* optimize

* optimize

* optimize

* add Estimate userOp Gas API

* add Estimate userOp Gas API

* add Estimate userOp Gas API

* optimize

* optimize

* optimize

* optimize Gas Compute

* optimize Gas Compute

* optimize Gas Compute

* optimize Gas Compute
getBasicPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* optimize Gas Compute
getPreVerificationGas

* go mod tidy

* fix init

* fix init

* fix init

* optimize gas

* optimize gas

* optimize gas

* optimize gas

* update

* update

* update

* update

* update

* update string

* update string

* update

* update

* update

* update

* add test

* add test

* add gas Price

* optimize  gas

* optimize  gas

* fix test

* fix test

* fix test

* fix init error

* fix dep

* fix version issue

* fix bug

* optimize test

* optimize test

* optimize test

* optimize test

* optimize test

* optimize test

* optimize test

* optimize code

* optimize code

* optimize code

* skip test in Github action

* fix test

* fix test

* fix test

* fix test

* fix test

* fix test

* config files cp to image

* upgrade base image

* fix comment

* fix comment

* fix comment

* fix comment

* fix comment

* Update coverage.yml

* fix comment

* fix comment

* fix comment

* fix comment

---------

Co-authored-by: 0xRory <[email protected]>
Co-authored-by: devops <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 4991f0d commit cedeb46
Show file tree
Hide file tree
Showing 118 changed files with 21,908 additions and 1,168 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
conf/appsettings.yaml
config/appsettings.yaml
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.0'
go-version: '1.22.3'
- name: Build
run: go build -v ./...
- name: Audit
run: go install golang.org/x/vuln/cmd/govulncheck@latest && govulncheck ./...
- name: Test
run: go test -v ./...
run: go test -v ./... -test.short

name: Building And Testing
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
# Go workspace file
go.work

.env

.idea
.vscode/
build/
config/*.json

vendor

conf/appsettings.*.yaml
config/secret_*.json
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## build
FROM golang:1.22.1-alpine3.19 AS build-env
FROM golang:1.22.3-alpine3.19 AS build-env

RUN apk add build-base

Expand All @@ -20,6 +20,7 @@ RUN mkdir -p /ep && mkdir -p /ep/log
WORKDIR /ep

COPY --from=build-env /go/src/app /ep/
COPY --from=build-env /go/src/app/config/*.json /ep/config/

ENV PATH $PATH:/aa

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##generate-verifyingPaymaster-v06-pkg:
# abigen --abi=./common/ethereum_contract/paymaster_abi/simulate_entrypoint_abi.json --pkg=contract --out=./common/ethereum_contract/contract/simulate_entrypoint/simulate_entrypoint.go
##
##

86 changes: 86 additions & 0 deletions cmd/server/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
"net/http"
"net/http/httptest"
"testing"
)

func APITestCall(engine *gin.Engine, method, url string, body any, response any, apiToken string) (*http.Response, error) {
bodyBytes, err := json.Marshal(body)
logrus.Debug("bodyBytes: ", string(bodyBytes))
if err != nil {
return nil, xerrors.Errorf("ERROR Marshal ", err)
}
w := httptest.NewRecorder()
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Authorization", "Bearer "+apiToken)
w.Header().Set("Accept", "application/json")
req, _ := http.NewRequest(method, url, bytes.NewReader(bodyBytes))
engine.ServeHTTP(w, req)

logrus.Debug(req)
if w.Code != 200 {
return w.Result(), xerrors.Errorf("ERROR Code ", w.Result().Status)
}
if w.Body == nil {
return w.Result(), xerrors.Errorf("ERROR Body is nil")
}
err = json.Unmarshal(w.Body.Bytes(), response)
if err != nil {
return w.Result(), xerrors.Errorf("ERROR Unmarshal ", err)
}
//logrus.Debugf("Response: %s", w.Body)
return w.Result(), nil
}

func TestAPI(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}

initEngine("../../config/basic_strategy_config.json", "../../config/basic_config.json", "../../config/secret_config.json")
tests := []struct {
name string
test func(t *testing.T)
}{
{
"TestHealthz",
func(t *testing.T) {
var rssponse map[string]any
_, err := APITestCall(Engine, "GET", "/api/healthz", nil, &rssponse, "")
if err != nil {
t.Error(err)
return
}
t.Logf("Response: %v", rssponse)
},
},

//TODO fix this test
//
//{
// name: "TestAuth",
// test: func(t *testing.T) {
// request := model.ClientCredential{
// ApiKey: "String",
// }
// var response map[string]any
// _, err := APITestCall(Engine, "POST", "/api/auth", &request, &response, "")
// if err != nil {
// t.Error(err)
// return
// }
// },
//},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
}

}
33 changes: 32 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package main

import (
"AAStarCommunity/EthPaymaster_BackService/config"
"AAStarCommunity/EthPaymaster_BackService/envirment"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/routers"
"flag"
"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 All @@ -29,6 +38,8 @@ func runMode() string {
return *aPort
}

var Engine *gin.Engine

// @contact.name AAStar Support
// @contact.url https://aastar.xyz
// @securityDefinitions.apikey JWT
Expand All @@ -37,6 +48,26 @@ func runMode() string {
// @description Type 'Bearer \<TOKEN\>' to correctly set the AccessToken
// @BasePath /api
func main() {
secretPath := os.Getenv("secret_config_path")
if secretPath == "" {
secretPath = "./config/secret_config.json"
}
initEngine(strategyPath, basicConfigPath, secretPath)
port := runMode()
_ = routers.SetRouters().Run(port)
os.Getenv("secret_config_path")
_ = Engine.Run(port)
}

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)
Engine = routers.SetRouters()
}
41 changes: 41 additions & 0 deletions common/data_utils/data_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package data_utils

import (
"AAStarCommunity/EthPaymaster_BackService/common/global_const"
"AAStarCommunity/EthPaymaster_BackService/common/model"
"AAStarCommunity/EthPaymaster_BackService/common/network"
"AAStarCommunity/EthPaymaster_BackService/common/paymaster_data"
"AAStarCommunity/EthPaymaster_BackService/common/user_op"
"github.com/sirupsen/logrus"
)

func GetUserOpWithPaymasterAndDataForSimulate(op user_op.UserOpInput, strategy *model.Strategy, paymasterDataInput *paymaster_data.PaymasterDataInput, gasPriceResult *model.GasPrice) (*user_op.UserOpInput, error) {
executor := network.GetEthereumExecutor(strategy.GetNewWork())
op.MaxFeePerGas = gasPriceResult.MaxFeePerGas
op.MaxPriorityFeePerGas = gasPriceResult.MaxPriorityFeePerGas
logrus.Debug("MaxFeePerGas", op.MaxFeePerGas)
logrus.Debug("MaxPriorityFeePerGas", op.MaxPriorityFeePerGas)
paymasterDataInput.PaymasterPostOpGasLimit = global_const.DummyPaymasterPostoperativelyBigint
paymasterDataInput.PaymasterVerificationGasLimit = global_const.DummyPaymasterOversimplificationBigint
op.AccountGasLimits = user_op.DummyAccountGasLimits
op.GasFees = user_op.DummyGasFees
if op.PreVerificationGas == nil {
op.PreVerificationGas = global_const.DummyReverificationsBigint
}
if op.VerificationGasLimit == nil {
op.VerificationGasLimit = global_const.DummyVerificationGasLimit
}
if op.Signature == nil {
op.Signature = global_const.DummySignatureByte
}
if op.CallGasLimit == nil {
op.CallGasLimit = global_const.DummyCallGasLimit
}

paymasterData, err := executor.GetPaymasterData(&op, strategy, paymasterDataInput)
if err != nil {
return nil, err
}
op.PaymasterAndData = paymasterData
return &op, nil
}
2,262 changes: 2,262 additions & 0 deletions common/ethereum_contract/contract/contract_entrypoint_v06/eth_entrypoint_v06.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package contract_entrypoint_v06

import (
"AAStarCommunity/EthPaymaster_BackService/common/ethereum_contract/paymaster_abi"
"AAStarCommunity/EthPaymaster_BackService/common/utils"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
"math/big"
)

type ExecutionResultRevert struct {
PreOpGas *big.Int
Paid *big.Int
ValidAfter *big.Int
ValidUntil *big.Int
TargetSuccess bool
TargetResult []byte
}

func ExecutionResult() abi.Error {
return abi.NewError("ExecutionResult", abi.Arguments{
{Name: "preOpGas", Type: paymaster_abi.Uint256Type},
{Name: "paid", Type: paymaster_abi.Uint256Type},
{Name: "validAfter", Type: paymaster_abi.Uint48Type},
{Name: "validUntil", Type: paymaster_abi.Uint48Type},
{Name: "targetSuccess", Type: paymaster_abi.BooleanType},
{Name: "targetResult", Type: paymaster_abi.BytesType},
})
}

func NewExecutionResult(inputError error, abi *abi.ABI) (*ExecutionResultRevert, error) {

var rpcErr rpc.DataError
ok := errors.As(inputError, &rpcErr)
if !ok {
return nil, xerrors.Errorf("ExecutionResult: cannot assert type: error is not of type rpc.DataError")
}

data, ok := rpcErr.ErrorData().(string)

logrus.Debug("data: ", data)
if !ok {
return nil, xerrors.Errorf("ExecutionResult: cannot assert type: data is not of type string")
}

sim := ExecutionResult()
revert, upPackerr := sim.Unpack(common.Hex2Bytes(data[2:]))
if upPackerr != nil {
// is another ERROR
errstr, parseErr := utils.ParseCallError(inputError, abi)
return nil, fmt.Errorf("executionResult err: [%s] parErr [%s]", errstr, parseErr)
}

args, ok := revert.([]any)
if !ok {
return nil, xerrors.Errorf("executionResult: cannot assert type: args is not of type []any")
}
if len(args) != 6 {
return nil, xerrors.Errorf("executionResult: invalid args length: expected 6, got %d", len(args))
}

return &ExecutionResultRevert{
PreOpGas: args[0].(*big.Int),
Paid: args[1].(*big.Int),
ValidAfter: args[2].(*big.Int),
ValidUntil: args[3].(*big.Int),
TargetSuccess: args[4].(bool),
TargetResult: args[5].([]byte),
}, nil
}
Loading

0 comments on commit cedeb46

Please sign in to comment.