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