@@ -9,13 +9,12 @@ import {
9
9
OwnedObjectRef ,
10
10
Signature ,
11
11
} from "@iota/iota-sdk/client" ;
12
- import { messageWithIntent , PublicKey , toSerializedSignature } from "@iota/iota-sdk/cryptography" ;
13
- import { Ed25519PublicKey } from "@iota/iota-sdk/keypairs/ed25519" ;
14
12
import { GasData , TransactionDataBuilder } from "@iota/iota-sdk/transactions" ;
15
- import { blake2b } from "@noble/hashes/blake2b" ;
16
13
17
14
export type Signer = { sign ( data : Uint8Array ) : Promise < Signature > } ;
18
15
16
+ const MINIMUM_BALANCE_FOR_COIN = BigInt ( 1_000_000_000 ) ;
17
+
19
18
export class WasmIotaTransactionBlockResponseWrapper {
20
19
response : IotaTransactionBlockResponse ;
21
20
@@ -54,13 +53,37 @@ export class WasmIotaTransactionBlockResponseWrapper {
54
53
}
55
54
}
56
55
57
- async function getCoinForTransaction ( iotaClient : IotaClient , senderAddress : string ) : Promise < CoinStruct > {
58
- const coins = await iotaClient . getCoins ( { owner : senderAddress } ) ;
59
- if ( coins . data . length === 0 ) {
60
- throw new Error ( `could not find coins for transaction with sender ${ senderAddress } ` ) ;
56
+ function byHighestBalance < T extends { balance : BigInt } > ( { balance : a } : T , { balance : b } : T ) {
57
+ if ( a > b ) {
58
+ return - 1 ;
59
+ }
60
+ if ( a < b ) {
61
+ return 1 ;
61
62
}
63
+ return 0 ;
64
+ }
65
+
66
+ async function getCoinForTransaction ( iotaClient : IotaClient , senderAddress : string ) : Promise < CoinStruct > {
67
+ let cursor : string | null | undefined = undefined ;
68
+ do {
69
+ const response = await iotaClient . getCoins ( { owner : senderAddress , cursor } ) ;
70
+ if ( response . data . length === 0 ) {
71
+ throw new Error ( `no coin found with minimum required balance of ${ MINIMUM_BALANCE_FOR_COIN } for address ${ senderAddress } "` ) ;
72
+ }
73
+
74
+ let sortedValidCoins = response . data
75
+ . map ( ( coin ) => ( { coin, balance : BigInt ( coin . balance ) } ) )
76
+ . filter ( ( { balance } ) => balance >= MINIMUM_BALANCE_FOR_COIN )
77
+ . sort ( byHighestBalance ) ;
78
+
79
+ if ( sortedValidCoins . length >= 1 ) {
80
+ return sortedValidCoins [ 0 ] . coin ;
81
+ }
82
+
83
+ cursor = response . nextCursor ;
84
+ } while ( cursor )
62
85
63
- return coins . data [ 1 ] ;
86
+ throw new Error ( `no coin found with minimum required balance of ${ MINIMUM_BALANCE_FOR_COIN } for address ${ senderAddress } "` ) ;
64
87
}
65
88
66
89
/**
0 commit comments