-
Notifications
You must be signed in to change notification settings - Fork 47
/
transfer-gateway.ts
621 lines (554 loc) · 23.6 KB
/
transfer-gateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import BN from 'bn.js'
import { Client } from '../client'
import { Contract } from '../contract'
import { Address } from '../address'
import {
TransferGatewayWithdrawTokenRequest,
TransferGatewayWithdrawETHRequest,
TransferGatewayWithdrawalReceipt,
TransferGatewayWithdrawalReceiptRequest,
TransferGatewayWithdrawalReceiptResponse,
TransferGatewayTokenKind,
TransferGatewayAddContractMappingRequest,
TransferGatewayTokenWithdrawalSigned,
TransferGatewayContractMappingConfirmed,
TransferGatewayReclaimContractTokensRequest,
TransferGatewayReclaimDepositorTokensRequest,
TransferGatewayGetUnclaimedTokensRequest,
TransferGatewayGetUnclaimedTokensResponse,
TransferGatewayListContractMappingRequest,
TransferGatewayListContractMappingResponse,
TransferGatewayGetLocalAccountInfoRequest,
TransferGatewayGetLocalAccountInfoResponse,
TransferGatewayGetContractMappingRequest,
TransferGatewayGetContractMappingResponse,
TransferGatewayTxStatus,
TransferGatewayStateRequest,
TransferGatewayStateResponse
} from '../proto/transfer_gateway_pb'
import { marshalBigUIntPB, unmarshalBigUIntPB } from '../big-uint'
import { B64ToUint8Array } from '../crypto-utils'
import { IAddressMapping } from './address-mapper'
export interface IUnclaimedToken {
tokenContract: Address
tokenKind: TransferGatewayTokenKind
tokenIds?: Array<BN>
tokenAmounts?: Array<BN>
}
export interface ITransferGatewayState {
maxTotalDailyWithdrawalAmount: BN
maxPerAccountDailyWithdrawalAmount: BN
lastWithdrawalLimitResetTime: Date
totalWithdrawalAmount: BN
}
export interface IWithdrawalReceipt {
tokenOwner: Address
// Mainnet address of token contract (NOTE: not set when withdrawing LOOM via Binance Gateway)
tokenContract?: Address
tokenKind: TransferGatewayTokenKind
// ERC721/X token ID
tokenId?: BN
// ERC721X/ERC20/ETH amount
tokenAmount?: BN
withdrawalNonce: BN
// Signature generated by the Oracle that confirmed the withdrawal
oracleSignature: Uint8Array
// Withdrawal tx hash on the foreign chain (currently only used by Binance Gateway)
txHash: Uint8Array
// Withdrawal tx status on the foreign chain (currently only used by Binance Gateway)
txStatus: TransferGatewayTxStatus
// Deprecated, use tokenId and tokenAmount instead.
// This is the ERC721 token ID, or ERC721X/ERC20/ETH amount.
value: BN
}
export interface ITokenWithdrawalEventArgs {
tokenOwner: Address
// Mainnet address of token contract, blank if ETH
tokenContract: Address
tokenKind: TransferGatewayTokenKind
// ERC721/X token ID
tokenId?: BN
// ERC721X/ERC20/ETH amount
tokenAmount?: BN
// Oracle signature
sig: Uint8Array
// Deprecated, use tokenId and tokenAmount instead.
// This is the ERC721 token ID, or ERC721X/ERC20/ETH amount.
value: BN
}
export interface IContractMappingConfirmedEventArgs {
// Address of a contract on a foreign blockchain
foreignContract: Address
// Address of corresponding contract on the local blockchain
localContract: Address
}
export interface ILocalAccountInfo {
withdrawalReceipt: IWithdrawalReceipt | null
totalWithdrawalAmount: BN
lastWithdrawalLimitResetTime: number
}
export class TransferGateway extends Contract {
// LoomJS user events
static readonly EVENT_TOKEN_WITHDRAWAL = 'event_token_withdrawal'
static readonly EVENT_CONTRACT_MAPPING_CONFIRMED = 'event_contract_mapping_confirmed'
// Events from Loomchain
static readonly tokenWithdrawalSignedEventTopic: String = 'event:TokenWithdrawalSigned'
static readonly contractMappingConfirmedEventTopic: String = 'event:ContractMappingConfirmed'
static async createAsync(client: Client, callerAddr: Address): Promise<TransferGateway> {
const contractAddr = await client.getContractAddressAsync('gateway')
if (!contractAddr) {
throw Error('Failed to resolve contract address for TransferGateway')
}
return new TransferGateway({ contractAddr, callerAddr, client })
}
constructor(params: { contractAddr: Address; callerAddr: Address; client: Client }) {
super(params)
this.on(Contract.EVENT, event => {
if (!event.topics || event.topics.length === 0) {
return
}
if (event.topics[0] === TransferGateway.tokenWithdrawalSignedEventTopic) {
const eventData = TransferGatewayTokenWithdrawalSigned.deserializeBinary(
B64ToUint8Array(event.data)
)
let tokenId: BN | undefined
let tokenAmount: BN | undefined
let value: BN
const tokenKind = eventData.getTokenKind()
switch (tokenKind) {
case TransferGatewayTokenKind.ERC721:
tokenId = unmarshalBigUIntPB(eventData.getTokenId()!)
value = tokenId
break
case TransferGatewayTokenKind.ERC721X:
tokenId = unmarshalBigUIntPB(eventData.getTokenId()!)
// fallthrough
// tslint:disable-next-line: no-switch-case-fall-through
default:
tokenAmount = unmarshalBigUIntPB(eventData.getTokenAmount()!)
value = tokenAmount
break
}
this.emit(TransferGateway.EVENT_TOKEN_WITHDRAWAL, {
tokenOwner: Address.UnmarshalPB(eventData.getTokenOwner()!),
tokenContract: Address.UnmarshalPB(eventData.getTokenContract()!),
tokenKind,
tokenId,
tokenAmount,
sig: eventData.getSig_asU8(),
value
} as ITokenWithdrawalEventArgs)
} else if (event.topics[0] === TransferGateway.contractMappingConfirmedEventTopic) {
const contractMappingConfirmed = TransferGatewayContractMappingConfirmed.deserializeBinary(
B64ToUint8Array(event.data)
)
this.emit(TransferGateway.EVENT_CONTRACT_MAPPING_CONFIRMED, {
foreignContract: Address.UnmarshalPB(contractMappingConfirmed.getForeignContract()!),
localContract: Address.UnmarshalPB(contractMappingConfirmed.getLocalContract()!)
} as IContractMappingConfirmedEventArgs)
}
})
}
/**
* Adds a contract mapping to the DAppChain Gateway using gatway owner signature.
* A contract mapping associates a token contract on the DAppChain with it's counterpart on Ethereum.
*/
addAuthorizedContractMappingAsync(params: {
foreignContract: Address
localContract: Address
}): Promise<void> {
const { foreignContract, localContract } = params
const mappingContractRequest = new TransferGatewayAddContractMappingRequest()
mappingContractRequest.setForeignContract(foreignContract.MarshalPB())
mappingContractRequest.setLocalContract(localContract.MarshalPB())
return this.callAsync<void>('AddAuthorizedContractMapping', mappingContractRequest)
}
/**
* Adds a contract mapping to the DAppChain Gateway.
* A contract mapping associates a token contract on the DAppChain with it's counterpart on Ethereum.
*/
addContractMappingAsync(params: {
foreignContract: Address
localContract: Address
foreignContractCreatorSig: Uint8Array
foreignContractCreatorTxHash: Uint8Array
}): Promise<void> {
const {
foreignContract,
localContract,
foreignContractCreatorSig,
foreignContractCreatorTxHash
} = params
const mappingContractRequest = new TransferGatewayAddContractMappingRequest()
mappingContractRequest.setForeignContract(foreignContract.MarshalPB())
mappingContractRequest.setLocalContract(localContract.MarshalPB())
mappingContractRequest.setForeignContractCreatorSig(foreignContractCreatorSig)
mappingContractRequest.setForeignContractTxHash(foreignContractCreatorTxHash)
return this.callAsync<void>('AddContractMapping', mappingContractRequest)
}
/**
* Fetches contract mappings from the DAppChain Gateway.
*
* @returns An object containing both confirmed & pending contract mappings.
*/
async listContractMappingsAsync(): Promise<{
confirmed: IAddressMapping[]
pending: IAddressMapping[]
}> {
const response = await this.staticCallAsync<TransferGatewayListContractMappingResponse>(
'ListContractMapping',
new TransferGatewayListContractMappingRequest(),
new TransferGatewayListContractMappingResponse()
)
const confirmed = response.getConfimedMappingsList().map(mapping => ({
from: Address.UnmarshalPB(mapping.getFrom()!),
to: Address.UnmarshalPB(mapping.getTo()!)
}))
const pending = response.getConfimedMappingsList().map(mapping => ({
from: Address.UnmarshalPB(mapping.getFrom()!),
to: Address.UnmarshalPB(mapping.getTo()!)
}))
return {
confirmed,
pending
}
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal of an ERC721 token from the
* current DAppChain account to an Ethereum account.
* @param tokenId ERC721 token ID.
* @param tokenContract DAppChain address of ERC721 contract.
* @param recipient Ethereum address of the account the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Ethereum account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawERC721Async(tokenId: BN, tokenContract: Address, recipient?: Address): Promise<void> {
const req = new TransferGatewayWithdrawTokenRequest()
req.setTokenKind(TransferGatewayTokenKind.ERC721)
req.setTokenId(marshalBigUIntPB(tokenId))
req.setTokenContract(tokenContract.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawToken', req)
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal of ERC721X tokens from the current
* DAppChain account to an Ethereum account.
* @param tokenId ERC721X token ID.
* @param amount Amount of tokenId to withdraw.
* @param tokenContract DAppChain address of ERC721X contract.
* @param recipient Ethereum address of the account the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Ethereum account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawERC721XAsync(
tokenId: BN,
amount: BN,
tokenContract: Address,
recipient?: Address
): Promise<void> {
const req = new TransferGatewayWithdrawTokenRequest()
req.setTokenKind(TransferGatewayTokenKind.ERC721X)
req.setTokenId(marshalBigUIntPB(tokenId))
req.setTokenAmount(marshalBigUIntPB(amount))
req.setTokenContract(tokenContract.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawToken', req)
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal ERC20 tokens from the current
* DAppChain account to an Ethereum account.
* @param amount Amount to withdraw.
* @param tokenContract DAppChain address of ERC20 contract.
* @param recipient Ethereum address of the account the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Ethereum account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawERC20Async(amount: BN, tokenContract: Address, recipient?: Address): Promise<void> {
const req = new TransferGatewayWithdrawTokenRequest()
req.setTokenKind(TransferGatewayTokenKind.ERC20)
req.setTokenAmount(marshalBigUIntPB(amount))
req.setTokenContract(tokenContract.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawToken', req)
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal of ETH from the current
* DAppChain account to an Ethereum account.
* @param amount Amount to withdraw.
* @param ethereumGateway Ethereum address of Ethereum Gateway.
* @param recipient Ethereum address of the account the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Ethereum account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawETHAsync(amount: BN, ethereumGateway: Address, recipient?: Address): Promise<void> {
const req = new TransferGatewayWithdrawETHRequest()
req.setAmount(marshalBigUIntPB(amount))
req.setMainnetGateway(ethereumGateway.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawETH', req)
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal TRX tokens from the current
* DAppChain account to an Tron account.
* @param amount Amount to withdraw.
* @param tokenContract DAppChain address of Tron coin contract.
* @param recipient Tron address of the account in hex string where the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Tron account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawTRXAsync(amount: BN, tokenContract: Address, recipient?: Address): Promise<void> {
const req = new TransferGatewayWithdrawTokenRequest()
req.setTokenKind(TransferGatewayTokenKind.TRX)
req.setTokenAmount(marshalBigUIntPB(amount))
req.setTokenContract(tokenContract.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawToken', req)
}
/**
* Sends a request to the DAppChain Gateway to begin withdrawal TRC20 tokens from the current
* DAppChain account to an Tron account.
* @param amount Amount to withdraw.
* @param tokenContract DAppChain address of Tron TRC20 contract.
* @param recipient Tron address of the account in hex string where the token should be withdrawn to, if this is
* omitted the Gateway will attempt to use the Address Mapper to retrieve the
* address of the Tron account mapped to the current DAppChain account.
* @returns A promise that will be resolved when the DAppChain Gateway has accepted the withdrawal
* request.
*/
withdrawTRC20Async(amount: BN, tokenContract: Address, recipient?: Address): Promise<void> {
const req = new TransferGatewayWithdrawTokenRequest()
req.setTokenKind(TransferGatewayTokenKind.TRC20)
req.setTokenAmount(marshalBigUIntPB(amount))
req.setTokenContract(tokenContract.MarshalPB())
if (recipient) {
req.setRecipient(recipient.MarshalPB())
}
return this.callAsync<void>('WithdrawToken', req)
}
/**
* Retrieves the current withdrawal receipt (if any) for the given DAppChain account.
* Withdrawal receipts are created by calling one of the withdraw methods.
* @param owner DAppChain address of a user account.
* @returns A promise that will be resolved with the withdrawal receipt, or null if no withdrawal
* receipt exists (this indicates there's no withdrawal from the specified account
* currently in progress).
*/
async withdrawalReceiptAsync(owner: Address): Promise<IWithdrawalReceipt | null> {
const tgWithdrawReceiptReq = new TransferGatewayWithdrawalReceiptRequest()
tgWithdrawReceiptReq.setOwner(owner.MarshalPB())
const result = await this.staticCallAsync(
'WithdrawalReceipt',
tgWithdrawReceiptReq,
new TransferGatewayWithdrawalReceiptResponse()
)
const receipt = result.getReceipt()
return receipt ? unmarshalWithdrawalReceipt(receipt) : null
}
/**
* Attempt to transfer tokens that originated from the specified Ethereum contract, and that have
* been deposited to the Ethereum Gateway, but haven't yet been received by the depositors on the
* DAppChain because of a missing identity or contract mapping. This method can only be called by
* the creator of the specified token contract, or the Gateway owner.
*
* @param tokenContract token contract to reclaim the tokens
*/
async reclaimContractTokensAsync(tokenContract: Address): Promise<void> {
const req = new TransferGatewayReclaimContractTokensRequest()
req.setTokenContract(tokenContract.MarshalPB())
return this.callAsync<void>('ReclaimContractTokens', req)
}
async getUnclaimedTokensAsync(owner: Address): Promise<Array<IUnclaimedToken>> {
const req = new TransferGatewayGetUnclaimedTokensRequest()
req.setOwner(owner.MarshalPB())
const result = await this.staticCallAsync(
'GetUnclaimedTokens',
req,
new TransferGatewayGetUnclaimedTokensResponse()
)
const unclaimedTokens = result.getUnclaimedTokensList()
let tokens: Array<IUnclaimedToken> = []
for (let token of unclaimedTokens) {
const tokenKind = token.getTokenKind()
let tokenIds: Array<BN> = []
let tokenAmounts: Array<BN> = []
if (tokenKind === TransferGatewayTokenKind.ERC721) {
// Set only the tokenId for ERC721
for (let amt of token.getAmountsList()) {
tokenIds.push(unmarshalBigUIntPB(amt.getTokenId()!))
}
} else if (tokenKind === TransferGatewayTokenKind.ERC721X) {
// Set both the tokenId and the tokenAmounts for ERC721x
for (let amt of token.getAmountsList()) {
tokenIds.push(unmarshalBigUIntPB(amt.getTokenId()!))
tokenAmounts.push(unmarshalBigUIntPB(amt.getTokenAmount()!))
}
} else {
// Set only amount for all other cases
for (let amt of token.getAmountsList()) {
tokenAmounts.push(unmarshalBigUIntPB(amt.getTokenAmount()!))
}
}
tokens.push({
tokenContract: Address.UnmarshalPB(token.getTokenContract()!),
tokenKind: tokenKind,
tokenAmounts: tokenAmounts,
tokenIds: tokenIds
})
}
return tokens
}
/**
* Attempt to transfer any tokens that the caller may have deposited into the Ethereum Gateway
* but hasn't yet received from the DAppChain Gateway because of a missing identity or contract
* mapping.
*
* @param depositors Optional list of DAppChain accounts to reclaim tokens for, when set tokens
* will be reclaimed for the specified accounts instead of the caller's account.
* NOTE: Only the Gateway owner is authorized to provide a list of accounts.
*/
async reclaimDepositorTokensAsync(depositors?: Array<Address>): Promise<void> {
const req = new TransferGatewayReclaimDepositorTokensRequest()
if (depositors && depositors.length > 0) {
req.setDepositorsList(depositors.map((address: Address) => address.MarshalPB()))
}
return this.callAsync<void>('ReclaimDepositorTokens', req)
}
/**
* Retrieves the information stored by the Gateway for the given DAppChain account.
* @param owner DAppChain address of a user account.
* @returns A promise that will be resolved with the local account info.
*/
async getLocalAccountInfoAsync(owner: Address): Promise<ILocalAccountInfo> {
const req = new TransferGatewayGetLocalAccountInfoRequest()
req.setOwner(owner.MarshalPB())
const resp = await this.staticCallAsync(
'GetLocalAccountInfo',
req,
new TransferGatewayGetLocalAccountInfoResponse()
)
const receipt = resp.getWithdrawalReceipt()
const amount = resp.getTotalWithdrawalAmount()
return {
withdrawalReceipt: receipt ? unmarshalWithdrawalReceipt(receipt) : null,
totalWithdrawalAmount: amount ? unmarshalBigUIntPB(amount) : new BN(0),
lastWithdrawalLimitResetTime: resp.getLastWithdrawalLimitResetTime()
}
}
/**
* Looks up the contract mapping for the given contract address.
* @param from Contract address.
* @returns null if no mapping was found for the given contract, otherwise an object that contains
* the address of the contract the `from` contract is mapped to, and the current status of
* the mapping (pending or confirmed).
*/
async getContractMappingAsync(from: Address): Promise<{ to: Address; pending: boolean } | null> {
const request = new TransferGatewayGetContractMappingRequest()
request.setFrom(from.MarshalPB())
const response = await this.staticCallAsync<TransferGatewayGetContractMappingResponse>(
'GetContractMapping',
request,
new TransferGatewayGetContractMappingResponse()
)
if (!response.getFound()) {
return null
}
return {
to: Address.UnmarshalPB(response.getMappedAddress()!),
pending: response.getIsPending()
}
}
/**
* Retrieves the current transfer gateway state.
* @returns A promise that will be resolved with the state info.
*/
async getStateAsync(): Promise<ITransferGatewayState> {
const response = await this.staticCallAsync<TransferGatewayStateResponse>(
'GetState',
new TransferGatewayStateRequest(),
new TransferGatewayStateResponse()
)
const state = response.getState()
if (!state) {
return {
maxTotalDailyWithdrawalAmount: new BN(0),
maxPerAccountDailyWithdrawalAmount: new BN(0),
lastWithdrawalLimitResetTime: new Date(0),
totalWithdrawalAmount: new BN(0)
}
}
const maxTotalDailyWithdrawalAmount = state.getMaxTotalDailyWithdrawalAmount()
const maxPerAccountDailyWithdrawalAmount = state.getMaxPerAccountDailyWithdrawalAmount()
const lastWithdrawalLimitResetTime = state.getLastWithdrawalLimitResetTime()
const totalWithdrawalAmount = state.getTotalWithdrawalAmount()
return {
maxTotalDailyWithdrawalAmount: maxTotalDailyWithdrawalAmount
? unmarshalBigUIntPB(maxTotalDailyWithdrawalAmount)
: new BN(0),
maxPerAccountDailyWithdrawalAmount: maxPerAccountDailyWithdrawalAmount
? unmarshalBigUIntPB(maxPerAccountDailyWithdrawalAmount)
: new BN(0),
lastWithdrawalLimitResetTime: lastWithdrawalLimitResetTime
? new Date(lastWithdrawalLimitResetTime * 1000)
: new Date(0),
totalWithdrawalAmount: totalWithdrawalAmount
? unmarshalBigUIntPB(totalWithdrawalAmount)
: new BN(0)
}
}
}
function unmarshalWithdrawalReceipt(
receipt: TransferGatewayWithdrawalReceipt
): IWithdrawalReceipt {
let tokenId: BN | undefined
let tokenAmount: BN | undefined
let value: BN
const tokenKind = receipt.getTokenKind()
switch (tokenKind) {
case TransferGatewayTokenKind.ERC721:
tokenId = unmarshalBigUIntPB(receipt.getTokenId()!)
value = tokenId
break
case TransferGatewayTokenKind.ERC721X:
tokenId = unmarshalBigUIntPB(receipt.getTokenId()!)
// fallthrough
// tslint:disable-next-line: no-switch-case-fall-through
default:
tokenAmount = unmarshalBigUIntPB(receipt.getTokenAmount()!)
value = tokenAmount
break
}
return {
tokenOwner: Address.UnmarshalPB(receipt.getTokenOwner()!),
tokenContract: receipt.getTokenContract()
? Address.UnmarshalPB(receipt.getTokenContract()!)
: undefined,
tokenKind,
tokenId,
tokenAmount,
withdrawalNonce: new BN(receipt.getWithdrawalNonce()!),
oracleSignature: receipt.getOracleSignature_asU8(),
txHash: receipt.getTxHash_asU8(),
txStatus: receipt.getTxStatus(),
value
}
}