Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from GincoInc/fix-transfer
Browse files Browse the repository at this point in the history
🍉 Add: check balance before withdraw token
  • Loading branch information
seitau authored Apr 13, 2022
2 parents 6c07b84 + af71fbf commit c2a5a58
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tokens/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit c2a5a58

Please sign in to comment.