diff --git a/Dockerfile b/Dockerfile index e285f123..9c2dc95c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/common/price_compoent/price_util_test.go b/common/price_compoent/price_util_test.go index 7211d590..cd8b5467 100644 --- a/common/price_compoent/price_util_test.go +++ b/common/price_compoent/price_util_test.go @@ -40,7 +40,3 @@ func testGetPriceUsd(t *testing.T, tokenType global_const.TokenType) { } t.Logf("price:%v", price) } - -func TestGetCoinGeckoPrice(t *testing.T) { - GetConfigTokenPrice() -} diff --git a/go.mod b/go.mod index e0df251a..2dd0efb8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/sponsor_manager/sponsor_changelog_repository.go b/sponsor_manager/sponsor_changelog_repository.go index 882b6116..c6185765 100644 --- a/sponsor_manager/sponsor_changelog_repository.go +++ b/sponsor_manager/sponsor_changelog_repository.go @@ -5,6 +5,7 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/model" "gorm.io/datatypes" "math/big" + "time" ) type UserSponsorBalanceUpdateLogDBModel struct { @@ -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) @@ -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 + } +} diff --git a/sponsor_manager/sponsor_service.go b/sponsor_manager/sponsor_service.go index 59f46a01..d5e960bd 100644 --- a/sponsor_manager/sponsor_service.go +++ b/sponsor_manager/sponsor_service.go @@ -13,6 +13,7 @@ import ( "gorm.io/gorm" "math/big" "sync" + "time" ) type Source string @@ -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---------- diff --git a/sponsor_manager/sponsor_test.go b/sponsor_manager/sponsor_test.go index 05c96aa7..c9001476 100644 --- a/sponsor_manager/sponsor_test.go +++ b/sponsor_manager/sponsor_test.go @@ -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)