From af71fbf4180c390f1c3aa83199f4d2f12726188d Mon Sep 17 00:00:00 2001 From: seita-uc Date: Wed, 13 Apr 2022 14:50:43 +0900 Subject: [PATCH] :watermelon: Add: check balance before withdraw token --- tokens/jobs.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tokens/jobs.go b/tokens/jobs.go index 2e42808f..e8c47cf7 100644 --- a/tokens/jobs.go +++ b/tokens/jobs.go @@ -3,8 +3,10 @@ package tokens import ( "context" "encoding/json" + "fmt" "github.com/flow-hydraulics/flow-wallet-api/jobs" + log "github.com/sirupsen/logrus" ) const WithdrawalCreateJobType = "withdrawal_create" @@ -26,6 +28,28 @@ func (s *ServiceImpl) executeCreateWithdrawalJob(ctx context.Context, j *jobs.Jo return err } + // check recipient nft balance if executed more than once + if j.ExecCount > 1 && attrs.Request.NftID != 0 { + detail, err := s.Details(ctx, attrs.Request.TokenName, attrs.Request.Recipient) + if err != nil { + return fmt.Errorf("failed to get nft balance: %w", err) + } + val := detail.Balance.CadenceValue.ToGoValue() + balances, ok := val.([]interface{}) + if ok { + for i := range balances { + nftID, ok := balances[i].(uint64) + if !ok { + continue + } + if nftID == attrs.Request.NftID { + log.Warn("recipient already has token", attrs.Request.TokenName, nftID) + return nil + } + } + } + } + transaction, err := s.createWithdrawal(ctx, attrs.Sender, attrs.Request) if err != nil { return err