Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sponsor Fix Expire Lock Release #32

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## build
FROM golang:1.22.4-alpine3.19 AS build-env
FROM golang:1.22.5-alpine3.19 AS build-env

RUN apk add build-base

Expand Down
4 changes: 0 additions & 4 deletions common/price_compoent/price_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,3 @@ func testGetPriceUsd(t *testing.T, tokenType global_const.TokenType) {
}
t.Logf("price:%v", price)
}

func TestGetCoinGeckoPrice(t *testing.T) {
GetConfigTokenPrice()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AAStarCommunity/EthPaymaster_BackService

go 1.22.4
go 1.22.5

require (
github.com/NethermindEth/starknet.go v0.7.0
Expand Down
17 changes: 17 additions & 0 deletions sponsor_manager/sponsor_changelog_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"AAStarCommunity/EthPaymaster_BackService/common/model"
"gorm.io/datatypes"
"math/big"
"time"
)

type UserSponsorBalanceUpdateLogDBModel struct {
Expand All @@ -26,6 +27,13 @@ func (UserSponsorBalanceUpdateLogDBModel) TableName() string {
func AddBalanceChangeLog(changeDbModel *UserSponsorBalanceUpdateLogDBModel) error {
return relayDB.Create(changeDbModel).Error
}
func GetBalanceChangeLogByTimePeriod(startTime, endTime time.Time) (models []*UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(&UserSponsorBalanceUpdateLogDBModel{}).Where("created_at >= ?", startTime).Where("created_at <= ?", endTime).Where("update_type in (?)", []global_const.UpdateType{global_const.UpdateTypeLock, global_const.UpdateTypeRelease}).Find(&models)
if tx.Error != nil {
return nil, tx.Error
}
return models, nil
}

func GetDepositAndWithDrawLog(userId string, IsTestNet bool) (models []*UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(&UserSponsorBalanceUpdateLogDBModel{}).Where("pay_user_id = ?", userId).Where("is_test_net = ?", IsTestNet).Where("update_type in (?)", []global_const.UpdateType{global_const.UpdateTypeDeposit, global_const.UpdateTypeWithdraw}).Find(&models)
Expand Down Expand Up @@ -65,3 +73,12 @@ func GetChangeModel(updateType global_const.UpdateType, payUserId string, txHash
return nil, nil
}
}

func GetChangeModelByUserOpHash(userOpHash string, isTestNet bool) (ChangeModel *UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(ChangeModel).Where("user_op_hash = ?", userOpHash).Where("is_test_net", isTestNet).First(&ChangeModel)
if tx.Error != nil {
return nil, tx.Error
} else {
return ChangeModel, nil
}
}
42 changes: 42 additions & 0 deletions sponsor_manager/sponsor_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"gorm.io/gorm"
"math/big"
"sync"
"time"
)

type Source string
Expand All @@ -38,6 +39,47 @@ func Init() {
}
relayDB = relayDBVar
})
go func() {
for {
time.Sleep(10 * time.Minute)
ReleaseExpireLockBalance()
}
}()
}
func ReleaseExpireLockBalance() {
// Get All Lock Balance
logList, err := GetBalanceChangeLogByTimePeriod(time.Now().Add(-20*time.Minute), time.Now().Add(-5*time.Minute))
if err != nil {
logrus.Errorf("ReleaseExpireLockBalance Error [%v]", err)
}
if len(logList) == 0 {
logrus.Info("[ReleaseExpireLockBalance] No Lock Balance")
return
}
lockMap := make(map[string]*UserSponsorBalanceUpdateLogDBModel)
releaseMap := make(map[string]*UserSponsorBalanceUpdateLogDBModel)
for _, changModel := range logList {
if changModel.UpdateType == global_const.UpdateTypeLock {
lockMap[changModel.UserOpHash] = changModel
} else if changModel.UpdateType == global_const.UpdateTypeRelease {
releaseMap[changModel.UserOpHash] = changModel
}
}

for userOpHash, lockModel := range lockMap {
_, ok := releaseMap[userOpHash]
if !ok {
userOpHashByte, _ := utils.DecodeStringWithPrefix(userOpHash)
_, err := ReleaseUserOpHashLockWhenFail(userOpHashByte, lockModel.IsTestNet)
if err != nil {
logrus.Errorf("ReleaseUserOpHashLockWhenFail Error [%v]", err)
}
logrus.Infof("Release Expire Lock Balance [%s]", userOpHash)
continue
} else {
continue
}
}
}

//----------Functions----------
Expand Down
6 changes: 6 additions & 0 deletions sponsor_manager/sponsor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func TestSponsor(t *testing.T) {
testReleaseBalanceWithActualCost(t, "test", mockUserOpHash2, true, big.NewFloat(0.5))
},
},
{
"TestLockBalanceRelease",
func(t *testing.T) {
ReleaseExpireLockBalance()
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
Expand Down
Loading