@@ -50,15 +50,15 @@ func SendWithdrawal(ctx context.Context, assetId, destination, tag, amount, trac
50
50
fee = chainFee
51
51
}
52
52
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 )
54
54
}
55
55
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 )
58
58
}
59
59
60
60
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 ) {
62
62
isFeeDifferentAsset := feeAssetId != assetId
63
63
asset := crypto .Sha256Hash ([]byte (assetId ))
64
64
if isFeeDifferentAsset {
@@ -71,16 +71,40 @@ func withdrawalTransaction(ctx context.Context, traceId, feeReceiverId string, f
71
71
Tag : tag ,
72
72
},
73
73
}
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
+ })
77
100
}
78
101
if change .Sign () > 0 {
79
102
recipients = append (recipients , & TransactionRecipient {
80
103
Amount : change .String (),
81
104
MixAddress : NewUUIDMixAddress ([]string {u .UserId }, 1 ),
82
105
})
83
106
}
107
+
84
108
feeRecipients := []* TransactionRecipient {
85
109
{
86
110
Amount : feeAmount ,
0 commit comments