Skip to content

Commit

Permalink
Merge branch 'main' into test_optimize
Browse files Browse the repository at this point in the history
# Conflicts:
#	config/secret_config.go
  • Loading branch information
cherry-yl-sh committed Jun 17, 2024
2 parents d3b413f + 327f1f3 commit 216eb98
Show file tree
Hide file tree
Showing 26 changed files with 835 additions and 539 deletions.
5 changes: 0 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ var Engine *gin.Engine

// @contact.name AAStar Support
// @contact.url https://aastar.xyz
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @description Type 'Bearer \<TOKEN\>' to correctly set the AccessToken
// @BasePath /api
func main() {
secretPath := os.Getenv("secret_config_path")
Expand All @@ -61,7 +57,6 @@ func main() {
}

func initEngine(strategyPath string, basicConfigPath string, secretPath string) {

logrus.Infof("secretPath: %s", secretPath)
config.InitConfig(strategyPath, basicConfigPath, secretPath)
if envirment.Environment.IsDevelopment() {
Expand Down
20 changes: 15 additions & 5 deletions common/model/secret_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ type SecretConfig struct {

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"`
FreeSponsorWhitelist []string `json:"free_sponsor_whitelist"`
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"`
FreeSponsorWhitelist []string `json:"free_sponsor_whitelist"`
SponsorConfig SponsorConfig `json:"sponsor_config"`
}
type SponsorConfig struct {
SponsorDepositAddress string `json:"sponsor_deposit_address"`
SponsorDepositPrivateKey string `json:"sponsor_deposit_private_key"`
DashBoardSignerAddress string `json:"dashboard_signer_address"`
DepositTestNetUrl string `json:"deposit_test_net_url"`
DepositMainNetUrl string `json:"deposit_main_net_url"`
SponsorTestClientUrl string `json:"sponsor_client_rpc_test_net"`
SponsorMainClientUrl string `json:"sponsor_client_rpc_main_net"`
}

type NetWorkSecretConfig struct {
Expand Down
35 changes: 13 additions & 22 deletions common/model/sponsor.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package model

import "math/big"

type DepositSponsorRequest struct {
Source string `json:"source"`
Amount *big.Float `json:"amount"`
TxHash string `json:"tx_hash"`

TxInfo map[string]string `json:"tx_info"`
PayUserId string `json:"pay_user_id"`
IsTestNet bool `json:"is_test_net"`
TimeStamp int64 `json:"time_stamp"`
DepositAddress string `json:"deposit_address"`
TxHash string `json:"tx_hash"`
IsTestNet bool `json:"is_test_net"`
PayUserId string `json:"pay_user_id"`
DepositSource string `json:"deposit_source"`
}
type WithdrawSponsorRequest struct {
Amount *big.Float

PayUserId string
IsTestNet bool
TxInfo map[string]string
TxHash string
}
type GetSponsorTransactionsRequest struct {
}
type GetSponsorMetaDataRequest struct {
}

type Transaction struct {
Amount float64 `json:"amount"`
TimeStamp int64 `json:"time_stamp"`
PayUserId string `json:"pay_user_id"`
IsTestNet bool `json:"is_test_net"`
WithdrawSource string `json:"withdraw_source"`
RefundAddress string `json:"refund_address"`
DepositSource string `json:"deposit_source"`
}
64 changes: 59 additions & 5 deletions common/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package utils
import (
"AAStarCommunity/EthPaymaster_BackService/common/global_const"
"bytes"
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
"gorm.io/gorm"
"log"
"math/big"
"regexp"
"runtime"
Expand All @@ -24,7 +28,7 @@ import (

var HexPattern = regexp.MustCompile(`^0x[a-fA-F\d]*$`)

const defaultStackSize = 4096
const defaultStackSize = 10000

type EthCallReq struct {
From common.Address `json:"from"`
Expand Down Expand Up @@ -164,6 +168,11 @@ func ConvertBalanceToEther(balance *big.Int) *big.Float {
balanceFloat = new(big.Float).Quo(balanceFloat, global_const.EthWeiFactor)
return balanceFloat
}
func CounverEtherToWei(ether *big.Float) *big.Int {
afterEther := ether.Mul(ether, global_const.EthWeiFactor)
afterEtherInt, _ := afterEther.Int(nil)
return afterEtherInt
}
func ConvertStringToSet(input string, split string) mapset.Set[string] {
set := mapset.NewSet[string]()
arr := strings.Split(input, split)
Expand Down Expand Up @@ -202,18 +211,63 @@ func GetCurrentGoroutineStack() string {
n := runtime.Stack(buf[:], false)
return string(buf[:n])
}
func DBTransactional(db *gorm.DB, handle func() error) (err error) {
func DBTransactional(db *gorm.DB, handle func(*gorm.DB) error) (err error) {
tx := db.Begin()
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p)
logrus.Errorf("TX ERROR [%s] ", GetCurrentGoroutineStack())
err = xerrors.Errorf("TX ERROR [%v]", p)
//panic(p)
} else if err != nil {
tx.Rollback()
} else {
err = tx.Commit().Error
}
}()
err = handle()
return
err = handle(tx)
return err
}

func TransEth(from *ecdsa.PrivateKey, toAddress *common.Address, client *ethclient.Client, amount *big.Int, chainID *big.Int) (*types.Transaction, error) {
fromPrivateKey := from.Public()
fromPublicKeyECDSA, ok := fromPrivateKey.(*ecdsa.PublicKey)
if !ok {
logrus.Error("error casting public key to ECDSA")
return nil, xerrors.Errorf("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*fromPublicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err

}
gasLimit := uint64(21000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}

var data []byte
tx := types.NewTx(&types.DynamicFeeTx{

Nonce: nonce,
Data: data,
Gas: gasLimit,
GasFeeCap: gasPrice,
GasTipCap: gasPrice,
Value: amount,
To: toAddress,
})

signTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), from)

if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signTx)
if err != nil {
log.Fatal(err)
}
return signTx, nil
}
4 changes: 0 additions & 4 deletions config/appsettings.yaml

This file was deleted.

60 changes: 60 additions & 0 deletions config/secret_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@ package config
import (
"AAStarCommunity/EthPaymaster_BackService/common/global_const"
"AAStarCommunity/EthPaymaster_BackService/common/model"
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
mapset "github.com/deckarep/golang-set/v2"
"os"
"sync"
)

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

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

var sponsorTestNetClient *ethclient.Client
var sponsorTestNetClientChainId *big.Int
var sponsorMainNetClient *ethclient.Client
var sponsorMainNetClientChainId *big.Int
var onlyOnce = sync.Once{}

func GetPaymasterSponsorClient(isTestNet bool) *ethclient.Client {
if isTestNet {
return sponsorTestNetClient

}
return sponsorMainNetClient
}
func GetPaymasterSponsorChainId(isTestNet bool) *big.Int {
if isTestNet {
return sponsorTestNetClientChainId
}
return sponsorMainNetClientChainId
}
var sponsorWhitelist = mapset.NewSet[string]()

type SignerConfigMap map[global_const.Network]*global_const.EOA

func GetDepositer() *global_const.EOA {
return depositer
}
func secretConfigInit(secretConfigPath string) {
if secretConfigPath == "" {
panic("secretConfigPath is empty")
Expand All @@ -41,10 +69,38 @@ func secretConfigInit(secretConfigPath string) {

signerConfig[global_const.Network(network)] = eoa
}
depositer, err = global_const.NewEoa(secretConfig.SponsorConfig.SponsorDepositPrivateKey)
if err != nil {
panic(fmt.Sprintf("signer key error: %s", err))
}
onlyOnce.Do(func() {
paymasterSponsorMainNetClient, err := ethclient.Dial(secretConfig.SponsorConfig.SponsorMainClientUrl)
if err != nil {
panic(fmt.Sprintf("paymaster inner client error: %s", err))
}
paymasterInnerClientChainId, err := paymasterSponsorMainNetClient.ChainID(context.Background())
if err != nil {
panic(fmt.Sprintf("paymaster inner client chain id error: %s", err))
}
sponsorMainNetClient = paymasterSponsorMainNetClient
sponsorMainNetClientChainId = paymasterInnerClientChainId

paymasterSponsorTestNetClient, err := ethclient.Dial(secretConfig.SponsorConfig.SponsorTestClientUrl)
if err != nil {
panic(fmt.Sprintf("paymaster inner client error: %s", err))
}
paymasterInnerClientChainId, err = paymasterSponsorTestNetClient.ChainID(context.Background())
if err != nil {
panic(fmt.Sprintf("paymaster inner client chain id error: %s", err))
}
sponsorTestNetClient = paymasterSponsorTestNetClient
sponsorTestNetClientChainId = paymasterInnerClientChainId
})
if secretConfig.FreeSponsorWhitelist != nil {
sponsorWhitelist.Append(secretConfig.FreeSponsorWhitelist...)
}
}

func IsSponsorWhitelist(senderAddress string) bool {
//TODO
return true
Expand Down Expand Up @@ -72,6 +128,10 @@ func GetSigner(network global_const.Network) *global_const.EOA {
func GetAPIKeyTableName() string {
return secretConfig.ApiKeyTableName
}
func GetSponsorConfig() *model.SponsorConfig {
//TODO
return &secretConfig.SponsorConfig
}
func GetStrategyConfigTableName() string {
return secretConfig.StrategyConfigTableName
}
Expand Down
Loading

0 comments on commit 216eb98

Please sign in to comment.