@@ -62,7 +62,7 @@ var UI = (function(UI, $, undefined) {
62
62
63
63
UI . isDoingPOW = true ;
64
64
65
- getUnspentInputs ( connection . seed , 0 , 10 , amount , function ( error , inputs ) {
65
+ getUnspentInputs ( connection . seed , 0 , amount , function ( error , inputs ) {
66
66
if ( error ) {
67
67
UI . isDoingPOW = false ;
68
68
UI . formError ( "transfer" , error , { "initial" : "send_it_now" } ) ;
@@ -259,15 +259,12 @@ function filterSpentAddresses(inputs) {
259
259
} )
260
260
}
261
261
262
- function getUnspentInputs ( seed , start , step , threshold , inputs , cb ) {
263
- // TODO: define limit in settings to stop scanning for balance
264
- var limit = 500 //connection.maxIndex
265
- end = start + step
266
- if ( arguments . length === 5 ) {
267
- cb = arguments [ 4 ]
262
+ function getUnspentInputs ( seed , start , threshold , inputs , cb ) {
263
+ if ( arguments . length === 4 ) {
264
+ cb = arguments [ 3 ]
268
265
inputs = { inputs : [ ] , totalBalance : 0 , allBalance : 0 }
269
266
}
270
- getInputs ( seed , { start : start , end : end , threshold : threshold } , ( err , res ) => {
267
+ iota . api . getInputs ( seed , { start : start , threshold : threshold } , ( err , res ) => {
271
268
if ( err ) {
272
269
cb ( err )
273
270
return
@@ -277,13 +274,9 @@ function getUnspentInputs(seed, start, step, threshold, inputs, cb) {
277
274
var collected = filtered . reduce ( ( sum , input ) => sum + input . balance , 0 )
278
275
var diff = threshold - collected
279
276
if ( diff > 0 ) {
280
- start = end + 1
281
- end += step
282
- if ( end > limit ) {
283
- cb ( 'Not enough balance' )
284
- return
285
- }
286
- getUnspentInputs ( seed , start , step , diff , { inputs : inputs . inputs . concat ( filtered ) , totalBalance : inputs . totalBalance + collected , allBalance : inputs . allBalance } , cb )
277
+ var ordered = res . inputs . sort ( ( a , b ) => a . keyIndex - b . keyIndex ) . reverse ( )
278
+ var end = ordered [ 0 ] . keyIndex
279
+ getUnspentInputs ( seed , end + 1 , diff , { inputs : inputs . inputs . concat ( filtered ) , totalBalance : inputs . totalBalance + collected , allBalance : inputs . allBalance } , cb )
287
280
}
288
281
else {
289
282
cb ( null , { inputs : inputs . inputs . concat ( filtered ) , totalBalance : inputs . totalBalance + collected , allBalance : inputs . allBalance } )
@@ -292,40 +285,3 @@ function getUnspentInputs(seed, start, step, threshold, inputs, cb) {
292
285
} )
293
286
}
294
287
295
- function getInputs ( seed , options , cb ) {
296
- if ( ! options ) {
297
- options = { }
298
- }
299
- var start = options . start || 0
300
- var end = options . end || 10
301
- var security = options . security || 2
302
- var threshold = options . threshold || null
303
- addresses = [ ]
304
- for ( var i = start ; i <= end ; i ++ ) {
305
- addresses . push ( iota . api . _newAddress ( seed , i , security , false ) )
306
- }
307
- iota . api . getBalances ( addresses , 100 , ( err , res ) => {
308
- if ( err ) {
309
- cb ( err )
310
- return
311
- }
312
- var inputs = [ ]
313
- var sum = 0
314
- for ( var i = 0 ; i < addresses . length ; i ++ ) {
315
- var balance = parseInt ( res . balances [ i ] )
316
- if ( balance > 0 ) {
317
- sum += balance
318
- inputs . push ( {
319
- address : addresses [ i ] ,
320
- balance : balance ,
321
- keyIndex : start + i ,
322
- security : security
323
- } )
324
- if ( threshold && sum >= threshold ) {
325
- break
326
- }
327
- }
328
- }
329
- cb ( null , { inputs : inputs , totalBalance : sum } )
330
- } )
331
- }
0 commit comments