@@ -20,6 +20,8 @@ import {
20
20
} from 'redux-saga/effects' ;
21
21
import { t } from 'ttag' ;
22
22
import { NanoRequest404Error } from '@hathor/wallet-lib/lib/errors' ;
23
+ import { getRegisteredNanoContracts , safeEffect } from './helpers' ;
24
+ import { isWalletServiceEnabled } from './wallet' ;
23
25
import {
24
26
nanoContractBlueprintInfoFailure ,
25
27
nanoContractBlueprintInfoSuccess ,
@@ -35,9 +37,7 @@ import {
35
37
} from '../actions' ;
36
38
import { logger } from '../logger' ;
37
39
import { NANO_CONTRACT_TX_HISTORY_SIZE } from '../constants' ;
38
- import { consumeGenerator , getNanoContractFeatureToggle } from '../utils' ;
39
- import { getRegisteredNanoContracts , safeEffect } from './helpers' ;
40
- import { isWalletServiceEnabled } from './wallet' ;
40
+ import { getNanoContractFeatureToggle } from '../utils' ;
41
41
42
42
const log = logger ( 'nano-contract-saga' ) ;
43
43
@@ -54,6 +54,25 @@ export const failureMessage = {
54
54
nanoContractHistoryFailure : t `Error while trying to download Nano Contract transactions history.` ,
55
55
} ;
56
56
57
+ /**
58
+ * Call the async wallet method `isAddressMine` considering the type of wallet.
59
+ *
60
+ * @param {Object } wallet A wallet instance
61
+ * @param {string } address A wallet address to check
62
+ * @param {boolean } useWalletService A flag that determines if wallet service is in use
63
+ */
64
+ export const isAddressMine = async ( wallet , address , useWalletService ) => {
65
+ // XXX: Wallet Service doesn't implement isAddressMine.
66
+ // See issue: https://github.com/HathorNetwork/hathor-wallet-lib/issues/732
67
+ // Default to `false` if using Wallet Service.
68
+ if ( useWalletService ) {
69
+ return false ;
70
+ }
71
+
72
+ const isMine = await wallet . isAddressMine ( address ) ;
73
+ return isMine ;
74
+ } ;
75
+
57
76
export function * init ( ) {
58
77
const isEnabled = yield select ( getNanoContractFeatureToggle ) ;
59
78
if ( ! isEnabled ) {
@@ -101,13 +120,13 @@ export function* registerNanoContract({ payload }) {
101
120
// XXX: Wallet Service doesn't implement isAddressMine.
102
121
// See issue: https://github.com/HathorNetwork/hathor-wallet-lib/issues/732
103
122
// Default to `false` if using Wallet Service.
104
- let isAddressMine = false ;
123
+ let isAddrMine = false ;
105
124
const useWalletService = yield call ( isWalletServiceEnabled ) ;
106
125
if ( ! useWalletService ) {
107
- isAddressMine = yield call ( [ wallet , wallet . isAddressMine ] , address ) ;
126
+ isAddrMine = yield call ( [ wallet , wallet . isAddressMine ] , address ) ;
108
127
}
109
128
110
- if ( ! isAddressMine ) {
129
+ if ( ! isAddrMine ) {
111
130
log . debug ( 'Fail registering Nano Contract because address do not belongs to this wallet.' ) ;
112
131
yield put ( nanoContractRegisterFailure ( failureMessage . addressNotMine ) ) ;
113
132
return ;
@@ -158,6 +177,7 @@ export function* registerNanoContract({ payload }) {
158
177
// emit action NANOCONTRACT_REGISTER_SUCCESS with feedback to user
159
178
yield put ( nanoContractRegisterSuccess ( { entryKey : ncId , entryValue : nc , hasFeedback : true } ) ) ;
160
179
}
180
+
161
181
/**
162
182
* Effect invoked by safeEffect if an unexpected error occurs.
163
183
*
@@ -215,16 +235,18 @@ function* registerNanoContractOnError(error) {
215
235
* @param {string } req.before Transaction hash to start to get newer items
216
236
* @param {string } req.after Transaction hash to start to get older items
217
237
* @param {Object } req.wallet Wallet instance from redux state
238
+ * @param {boolean } req.useWalletService A flag that determines if wallet service is in use
218
239
*
219
240
* @returns {{
220
- * history: {} ;
241
+ * history: NcTxHistory ;
221
242
* }}
222
243
*
223
244
* @throws {Error } when request code is greater then 399 or when response's success is false
224
245
*/
225
246
export async function fetchHistory ( req ) {
226
247
const {
227
248
wallet,
249
+ useWalletService,
228
250
ncId,
229
251
count,
230
252
after,
@@ -245,28 +267,17 @@ export async function fetchHistory(req) {
245
267
throw new Error ( 'Failed to fetch nano contract history' ) ;
246
268
}
247
269
248
- /* TODO: Make it run concurrently while guaranting the order.
249
- /* see https://github.com/HathorNetwork/hathor-wallet-mobile/issues/514
250
- */
251
- const historyNewestToOldest = new Array ( rawHistory . length )
252
- for ( let idx = 0 ; idx < rawHistory . length ; idx += 1 ) {
253
- const rawTx = rawHistory [ idx ] ;
254
- const network = wallet . getNetworkObject ( ) ;
270
+ const network = wallet . getNetworkObject ( ) ;
271
+ // Translate rawNcTxHistory to NcTxHistory
272
+ // Prouce a list ordered from newest to oldest
273
+ const transformedTxHistory = rawHistory . map ( async ( rawTx ) => {
255
274
const caller = addressUtils . getAddressFromPubkey ( rawTx . nc_pubkey , network ) . base58 ;
256
- // XXX: Wallet Service doesn't implement isAddressMine.
257
- // See issue: https://github.com/HathorNetwork/hathor-wallet-lib/issues/732
258
- // Default to `false` if using Wallet Service.
259
- let isMine = false ;
260
- const useWalletService = consumeGenerator ( isWalletServiceEnabled ( ) ) ;
261
- if ( ! useWalletService ) {
262
- // eslint-disable-next-line no-await-in-loop
263
- isMine = await wallet . isAddressMine ( caller ) ;
264
- }
265
275
const actions = rawTx . nc_context . actions . map ( ( each ) => ( {
266
276
type : each . type , // 'deposit' or 'withdrawal'
267
277
uid : each . token_uid ,
268
278
amount : each . amount ,
269
279
} ) ) ;
280
+ const isMine = await isAddressMine ( wallet , caller , useWalletService ) ;
270
281
271
282
const tx = {
272
283
txId : rawTx . hash ,
@@ -278,13 +289,13 @@ export async function fetchHistory(req) {
278
289
blueprintId : rawTx . nc_blueprint_id ,
279
290
firstBlock : rawTx . first_block ,
280
291
caller,
281
- isMine,
282
292
actions,
293
+ isMine,
283
294
} ;
284
- historyNewestToOldest [ idx ] = tx ;
285
- }
295
+ return tx ;
296
+ } ) ;
286
297
287
- return { history : historyNewestToOldest } ;
298
+ return { history : await Promise . all ( transformedTxHistory ) } ;
288
299
}
289
300
290
301
/**
@@ -332,9 +343,11 @@ export function* requestHistoryNanoContract({ payload }) {
332
343
yield put ( nanoContractHistoryClean ( { ncId } ) ) ;
333
344
}
334
345
346
+ const useWalletService = yield select ( ( state ) => state . useWalletService ) ;
335
347
try {
336
348
const req = {
337
349
wallet,
350
+ useWalletService,
338
351
ncId,
339
352
before,
340
353
after,
0 commit comments