From 2498654e00751eac29e125bbc010da519f0163ef Mon Sep 17 00:00:00 2001 From: dylanyang Date: Thu, 23 May 2024 17:39:37 +0800 Subject: [PATCH] add DB --- sponsor_manager/sponsor_balance_repository.go | 1 + .../sponsor_changelog_repository.go | 1 + sponsor_manager/sponsor_service.go | 42 +++++++++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 sponsor_manager/sponsor_balance_repository.go create mode 100644 sponsor_manager/sponsor_changelog_repository.go diff --git a/sponsor_manager/sponsor_balance_repository.go b/sponsor_manager/sponsor_balance_repository.go new file mode 100644 index 00000000..60e225ee --- /dev/null +++ b/sponsor_manager/sponsor_balance_repository.go @@ -0,0 +1 @@ +package sponsor_manager diff --git a/sponsor_manager/sponsor_changelog_repository.go b/sponsor_manager/sponsor_changelog_repository.go new file mode 100644 index 00000000..60e225ee --- /dev/null +++ b/sponsor_manager/sponsor_changelog_repository.go @@ -0,0 +1 @@ +package sponsor_manager diff --git a/sponsor_manager/sponsor_service.go b/sponsor_manager/sponsor_service.go index 4458c41d..3acfeb19 100644 --- a/sponsor_manager/sponsor_service.go +++ b/sponsor_manager/sponsor_service.go @@ -54,6 +54,7 @@ type DepositBalanceInput struct { Signature string Amount *big.Float TxReceipt string + PayUserId string } func (UserSponsorBalanceDBModel) TableName() string { @@ -64,6 +65,7 @@ type UserSponsorBalanceUpdateLogDBModel struct { model.BaseData Amount *big.Float `gorm:"type:numeric(30,18)" json:"amount"` UpdateType UpdateType `gorm:"type:varchar(20)" json:"update_type"` + UserOpHash []byte `gorm:"type:bytea" json:"user_op_hash"` } func (UserSponsorBalanceUpdateLogDBModel) TableName() string { @@ -101,6 +103,7 @@ func LockUserBalance(userId string, userOpHash []byte, network global_const.Netw if tx.Error != nil { return tx.Error } + LogBalanceChange(LockBalance, userOpHash, lockAmount) return nil } @@ -110,27 +113,48 @@ func ReleaseBalanceWithActualCost(userId string, userOpHash []byte, network glob //TODO return nil } + +type ReleaseUserOpHashLockInput struct { + UserOpHash []byte +} + func ReleaseUserOpHashLock(userOpHash []byte) (err error) { + // Get ChangeLog By UserOpHash //TODO return nil } func DepositSponsorBalance(input *DepositBalanceInput) (err error) { //TODO + balanceModel, err := getUserSponsorBalance(input.PayUserId) + if errors.Is(err, gorm.ErrRecordNotFound) { + balanceModel.AvailableBalance = input.Amount + balanceModel.PayUserId = input.PayUserId + balanceModel.LockBalance = big.NewFloat(0) + tx := relayDB.Create(&balanceModel) + if tx.Error != nil { + return tx.Error + } + } + if err != nil { + return err + } + newAvaileBalnce := new(big.Float).Add(balanceModel.AvailableBalance, input.Amount) + balanceModel.AvailableBalance = newAvaileBalnce + tx := relayDB.Model(&UserSponsorBalanceDBModel{}).Where("pay_user_id = ?", input.PayUserId).Updates(balanceModel) + if tx.Error != nil { + return tx.Error + } + LogBalanceChange(AvailableBalance, input, input.Amount) return nil - } -func LogBalanceChange(balanceType BalanceType, data interface{}, amount *big.Float) (err error) { +func LogBalanceChange(balanceType BalanceType, data interface{}, amount *big.Float) { //TODO - return nil + return } 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 + tx := relayDB.Model(&UserSponsorBalanceDBModel{}).Where("pay_user_id = ?", userId).First(&balanceModel) + return balanceModel, tx.Error }