@@ -189,24 +189,28 @@ const calculateAmount = ({
189
189
const amounts : Amount [ ] = [ ] ;
190
190
191
191
while ( inputs . length > 0 ) {
192
- const input = inputs . pop ( ) ! ;
193
- const outputIndex = outputs . findIndex ( amount => amount . unit === input . unit ) ;
194
- let qty ;
195
-
196
- if ( outputIndex > - 1 ) {
197
- qty =
198
- ( BigInt ( input . quantity ) - BigInt ( outputs [ outputIndex ] . quantity ) ) *
199
- BigInt ( - 1 ) ;
200
- outputs . splice ( outputIndex , 1 ) ;
201
- } else {
202
- qty = BigInt ( input . quantity ) * BigInt ( - 1 ) ;
192
+ const input = inputs . pop ( ) ;
193
+ if ( input ) {
194
+ const outputIndex = outputs . findIndex (
195
+ amount => amount . unit === input . unit ,
196
+ ) ;
197
+ let qty ;
198
+
199
+ if ( outputIndex > - 1 ) {
200
+ qty =
201
+ ( BigInt ( input . quantity ) - BigInt ( outputs [ outputIndex ] . quantity ) ) *
202
+ BigInt ( - 1 ) ;
203
+ outputs . splice ( outputIndex , 1 ) ;
204
+ } else {
205
+ qty = BigInt ( input . quantity ) * BigInt ( - 1 ) ;
206
+ }
207
+
208
+ if ( qty !== BigInt ( 0 ) || input . unit === 'lovelace' )
209
+ amounts . push ( {
210
+ unit : input . unit ,
211
+ quantity : qty ,
212
+ } ) ;
203
213
}
204
-
205
- if ( qty !== BigInt ( 0 ) || input . unit === 'lovelace' )
206
- amounts . push ( {
207
- unit : input . unit ,
208
- quantity : qty ,
209
- } ) ;
210
214
}
211
215
212
216
return amounts . concat ( outputs ) ;
@@ -325,6 +329,24 @@ export const encodeToCbor = (args: EncodeToCborArgs): Serialization.TxCBOR => {
325
329
return transaction . toCbor ( ) ;
326
330
} ;
327
331
332
+ const getTotalWithdrawlAmount = ( {
333
+ rewardAccountsAddresses,
334
+ withdrawals = [ ] ,
335
+ } : {
336
+ rewardAccountsAddresses : Set < Wallet . Cardano . RewardAccount > ;
337
+ withdrawals : Wallet . Cardano . Withdrawal [ ] ;
338
+ } ) : bigint => {
339
+ let total = BigInt ( 0 ) ;
340
+
341
+ for ( const withdrawal of withdrawals ) {
342
+ if ( rewardAccountsAddresses . has ( withdrawal . stakeAddress ) ) {
343
+ total = total + BigInt ( withdrawal . quantity . toString ( ) ) ;
344
+ }
345
+ }
346
+
347
+ return total ;
348
+ } ;
349
+
328
350
export const getTxInfo = async ( {
329
351
tx,
330
352
getTxInputsValueAndAddress,
@@ -380,6 +402,10 @@ export const getTxInfo = async ({
380
402
Number . parseInt ( implicitCoin . deposit ?. toString ( ) ?? '' ) > 0
381
403
? BigInt ( implicitCoin . deposit ?. toString ( ) ?? 0 )
382
404
: BigInt ( 0 ) ;
405
+ const totalWithdrawals = getTotalWithdrawlAmount ( {
406
+ rewardAccountsAddresses,
407
+ withdrawals : tx . body . withdrawals ?? [ ] ,
408
+ } ) ;
383
409
384
410
const info : TxInfo = {
385
411
txHash : tx . id . toString ( ) ,
@@ -402,9 +428,12 @@ export const getTxInfo = async ({
402
428
rewardAccountsAddresses,
403
429
} ) ,
404
430
amounts : amounts ,
405
- lovelace : [ 'internalIn' , 'externalIn' , 'multisig' ] . includes ( type )
406
- ? BigInt ( lovelace . toString ( ) )
407
- : BigInt ( lovelace . toString ( ) ) + BigInt ( tx . body . fee . toString ( ) ) + deposit ,
431
+ lovelace :
432
+ ( [ 'internalIn' , 'externalIn' , 'multisig' ] . includes ( type )
433
+ ? BigInt ( lovelace . toString ( ) )
434
+ : BigInt ( lovelace . toString ( ) ) +
435
+ BigInt ( tx . body . fee . toString ( ) ) +
436
+ deposit ) - BigInt ( totalWithdrawals . toString ( ) ) ,
408
437
assets : assets
409
438
. map ( asset => {
410
439
const info = assetInfo ?. get ( Wallet . Cardano . AssetId ( asset . unit ) ) ;
0 commit comments