Skip to content

Commit dbd194a

Browse files
authored
improve withdrawal with utxos (#223)
1 parent 28ce723 commit dbd194a

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

withdrawal.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func SendWithdrawal(ctx context.Context, assetId, destination, tag, amount, trac
5050
fee = chainFee
5151
}
5252

53-
return withdrawalTransaction(ctx, traceId, MixinFeeUserId, fee.AssetID, fee.Amount, assetId, destination, tag, memo, amount, nil, u)
53+
return withdrawalTransaction(ctx, traceId, MixinFeeUserId, fee.AssetID, fee.Amount, assetId, destination, tag, memo, amount, nil, nil, u)
5454
}
5555

56-
func WithdrawalWithFeeUtxos(ctx context.Context, traceId, feeAssetId, feeAmount, assetId, destination, tag, memo, amount string, feeUtxos []*Output, u *SafeUser) ([]*SequencerTransactionRequest, error) {
57-
return withdrawalTransaction(ctx, traceId, MixinFeeUserId, feeAssetId, feeAmount, assetId, destination, tag, memo, amount, feeUtxos, u)
56+
func WithdrawalWithUtxos(ctx context.Context, traceId, feeAssetId, feeAmount, assetId, destination, tag, memo, amount string, utxos, feeUtxos []*Output, u *SafeUser) ([]*SequencerTransactionRequest, error) {
57+
return withdrawalTransaction(ctx, traceId, MixinFeeUserId, feeAssetId, feeAmount, assetId, destination, tag, memo, amount, utxos, feeUtxos, u)
5858
}
5959

6060
func withdrawalTransaction(ctx context.Context, traceId, feeReceiverId string, feeAssetId string, feeAmount,
61-
assetId, destination, tag, memo, amount string, feeUtxos []*Output, u *SafeUser) ([]*SequencerTransactionRequest, error) {
61+
assetId, destination, tag, memo, amount string, utxos, feeUtxos []*Output, u *SafeUser) ([]*SequencerTransactionRequest, error) {
6262
isFeeDifferentAsset := feeAssetId != assetId
6363
asset := crypto.Sha256Hash([]byte(assetId))
6464
if isFeeDifferentAsset {
@@ -71,16 +71,40 @@ func withdrawalTransaction(ctx context.Context, traceId, feeReceiverId string, f
7171
Tag: tag,
7272
},
7373
}
74-
utxos, change, err := requestUnspentOutputsForRecipients(ctx, assetId, recipients, u)
75-
if err != nil {
76-
return nil, err
74+
var err error
75+
var change common.Integer
76+
if len(utxos) > 0 {
77+
totalOutput := common.NewIntegerFromString(amount)
78+
var totalInput common.Integer
79+
for _, o := range utxos {
80+
amt := common.NewIntegerFromString(o.Amount)
81+
totalInput = totalInput.Add(amt)
82+
}
83+
if totalInput.Cmp(totalOutput) < 0 {
84+
return nil, &UtxoInsufficientError{
85+
TotalInput: totalInput,
86+
TotalOutput: totalOutput,
87+
OutputSize: len(utxos),
88+
}
89+
}
90+
change = totalInput.Sub(totalOutput)
91+
} else {
92+
utxos, change, err = requestUnspentOutputsForRecipients(ctx, assetId, recipients, u)
93+
if err != nil {
94+
return nil, err
95+
}
96+
recipients = append(recipients, &TransactionRecipient{
97+
Amount: amount,
98+
MixAddress: NewUUIDMixAddress([]string{u.UserId}, 1),
99+
})
77100
}
78101
if change.Sign() > 0 {
79102
recipients = append(recipients, &TransactionRecipient{
80103
Amount: change.String(),
81104
MixAddress: NewUUIDMixAddress([]string{u.UserId}, 1),
82105
})
83106
}
107+
84108
feeRecipients := []*TransactionRecipient{
85109
{
86110
Amount: feeAmount,

0 commit comments

Comments
 (0)