Skip to content

Commit

Permalink
add DB
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed May 23, 2024
1 parent 0b9f00f commit 2d8fcb5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 38 deletions.
2 changes: 1 addition & 1 deletion common/model/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (strategy *Strategy) GetNewWork() global_const.Network {
return strategy.NetWorkInfo.NetWork
}

func (strategy *Strategy) GetUseToken() global_const.TokenType {
func (strategy *Strategy) GetGasToken() global_const.TokenType {
return strategy.NetWorkInfo.GasToken
}
func (strategy *Strategy) GetPayType() global_const.PayType {
Expand Down
7 changes: 0 additions & 7 deletions service/dashboard_service/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,19 @@ import (

var (
configDB *gorm.DB
relayDB *gorm.DB
onlyOnce = sync.Once{}
)

func Init() {
onlyOnce.Do(func() {
configDBDsn := config.GetConfigDBDSN()
relayDBDsn := config.GetRelayDBDSN()

configDBVar, err := gorm.Open(postgres.Open(configDBDsn), &gorm.Config{})
if err != nil {
panic(err)
}
configDB = configDBVar

relayDBVar, err := gorm.Open(postgres.Open(relayDBDsn), &gorm.Config{})
if err != nil {
panic(err)
}
relayDB = relayDBVar
})

}
Expand Down
12 changes: 9 additions & 3 deletions service/operator/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ func executePay(input *ExecutePayInput) (*model.PayReceipt, error) {
return nil, nil
}
if config.IsSponsorWhitelist(input.UserOpSender) {
logrus.Debugf("Not Need ExecutePay In SponsorWhitelist")
logrus.Debugf("Not Need ExecutePay In SponsorWhitelist [%s]", input.UserOpSender)
return nil, nil
}
//if config.IsTestNet(input.Network) {
// logrus.Debugf("Not Need ExecutePay In TestNet [%s]", input.Network)
// return nil, nil
//}
// Get Deposit Balance
var payUserKey string
if input.ProjectSponsor == true {
Expand All @@ -112,15 +116,15 @@ func executePay(input *ExecutePayInput) (*model.PayReceipt, error) {
if err != nil {
return nil, err
}
gasUsdCost, err := price_compoent.GetTokenCostInUsd(global_const.TokenTypeETH, input.MaxTxGasCostInEther)
gasUsdCost, err := price_compoent.GetTokenCostInUsd(input.GasToken, input.MaxTxGasCostInEther)
if err != nil {
return nil, err
}
if depositBalance.Cmp(gasUsdCost) < 0 {
return nil, xerrors.Errorf("Insufficient balance [%s] not Enough to Pay Cost [%s]", depositBalance.String(), gasUsdCost.String())
}
//Lock Deposit Balance
err = sponsor_manager.LockBalance(payUserKey, input.UserOpHash, input.Network,
err = sponsor_manager.LockUserBalance(payUserKey, input.UserOpHash, input.Network,
gasUsdCost)
if err != nil {
return nil, err
Expand All @@ -140,6 +144,7 @@ type ExecutePayInput struct {
MaxTxGasCostInEther *big.Float
UserOpHash []byte
Network global_const.Network
GasToken global_const.TokenType
}

func postExecute(apiKeyModel *model.ApiKeyModel, userOp *user_op.UserOpInput, strategy *model.Strategy, gasResponse *model.ComputeGasResponse, paymasterDataInput *paymaster_data.PaymasterDataInput) (*model.TryPayUserOpResponse, error) {
Expand All @@ -159,6 +164,7 @@ func postExecute(apiKeyModel *model.ApiKeyModel, userOp *user_op.UserOpInput, st
MaxTxGasCostInEther: gasResponse.TotalGasDetail.MaxTxGasCostInEther,
UserOpHash: userOpHash,
Network: strategy.GetNewWork(),
GasToken: strategy.GetGasToken(),
})
if err != nil {
return nil, xerrors.Errorf("postExecute executePay Error: [%w]", err)
Expand Down
27 changes: 0 additions & 27 deletions sponsor_manager/sponsor_repo.go

This file was deleted.

136 changes: 136 additions & 0 deletions sponsor_manager/sponsor_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package sponsor_manager

import (
"AAStarCommunity/EthPaymaster_BackService/common/global_const"
"AAStarCommunity/EthPaymaster_BackService/common/model"
"AAStarCommunity/EthPaymaster_BackService/config"
"errors"
"golang.org/x/xerrors"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"math/big"
"sync"
)

var (
relayDB *gorm.DB
onlyOnce = sync.Once{}
)

func Init() {
onlyOnce.Do(func() {
relayDBDsn := config.GetRelayDBDSN()

relayDBVar, err := gorm.Open(postgres.Open(relayDBDsn), &gorm.Config{})
if err != nil {
panic(err)
}
relayDB = relayDBVar
})
}

type UpdateType string
type BalanceType string

const (
Deposit UpdateType = "deposit"
Lock UpdateType = "lock"
Withdraw UpdateType = "withdraw"
Release UpdateType = "release"

AvailableBalance BalanceType = "available_balance"
LockBalance BalanceType = "lock_balance"
)

type UserSponsorBalanceDBModel struct {
model.BaseData
PayUserId string `gorm:"type:varchar(255);index" json:"pay_user_id"`
AvailableBalance *big.Float `gorm:"type:numeric(30,18)" json:"available_balance"`
LockBalance *big.Float `gorm:"type:numeric(30,18)" json:"lock_balance"`
}

type DepositBalanceInput struct {
Source string
Signature string
Amount *big.Float
TxReceipt string
}

func (UserSponsorBalanceDBModel) TableName() string {
return config.GetStrategyConfigTableName()
}

type UserSponsorBalanceUpdateLogDBModel struct {
model.BaseData
Amount *big.Float `gorm:"type:numeric(30,18)" json:"amount"`
UpdateType UpdateType `gorm:"type:varchar(20)" json:"update_type"`
}

func (UserSponsorBalanceUpdateLogDBModel) TableName() string {
return config.GetStrategyConfigTableName()
}

//----------Functions----------

func GetAvailableBalance(userId string) (balance *big.Float, err error) {
balanceModel, err := getUserSponsorBalance(userId)

if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, xerrors.Errorf("No Balance Deposit Here ")
}
return nil, err
}
return balanceModel.AvailableBalance, nil
}

func LockUserBalance(userId string, userOpHash []byte, network global_const.Network,
lockAmount *big.Float) (err error) {
balanceModel, err := getUserSponsorBalance(userId)
if errors.Is(err, gorm.ErrRecordNotFound) {
return xerrors.Errorf("No Balance Deposit Here ")
}
if err != nil {
return err
}
lockBalance := new(big.Float).Add(balanceModel.LockBalance, lockAmount)
availableBalance := new(big.Float).Sub(balanceModel.AvailableBalance, lockAmount)
balanceModel.LockBalance = lockBalance
balanceModel.AvailableBalance = availableBalance
tx := relayDB.Model(&UserSponsorBalanceDBModel{}).Where("pay_user_id = ?", userId).Updates(balanceModel)
if tx.Error != nil {
return tx.Error
}

return nil
}

func ReleaseBalanceWithActualCost(userId string, userOpHash []byte, network global_const.Network,
actualGasCost *big.Float) (err error) {
//TODO
return nil
}
func ReleaseUserOpHashLock(userOpHash []byte) (err error) {
//TODO
return nil
}

func DepositSponsorBalance(input *DepositBalanceInput) (err error) {
//TODO
return nil

}

func LogBalanceChange(balanceType BalanceType, data interface{}, amount *big.Float) (err error) {
//TODO
return nil
}

func getUserSponsorBalance(userId string) (balanceModel *UserSponsorBalanceDBModel, err error) {
relayDB.Model(&UserSponsorBalanceDBModel{}).Where("pay_user_id = ?", userId).First(&balanceModel)
return balanceModel, nil
}
func CreateSponsorBalance(userId string) (err error) {
//TODO
return nil
}

0 comments on commit 2d8fcb5

Please sign in to comment.