-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathEIP20CoGateway.js
887 lines (827 loc) · 27.9 KB
/
EIP20CoGateway.js
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/**
* @typedef EIP20CoGatewaySetupConfig
*
* @property {string} valueToken The value token contract address.
* @property {string} utilityToken The utility token address that will be used for
* minting the utility token.
* @property {string} stateRootProvider Address of contract to use for getting the state root of
* the origin chain.
* @property {string} bounty The amount that facilitator will stakes to initiate the
* stake process.
* @property {string} organization Address of an organization contract.
* @property {string} gateway Address of EIP20Gateway on Origin.
* @property {string} burner Address where tokens will be burned.
* @property {string} messageBus Address of MessageBus contract
* to link into the contract bytecode.
* @property {string} gatewayLib Address of GatewayLib contract
* to link into the contract bytecode.
* @property {string} deployer Address to be used to send deployment
* transactions.
*/
'use strict';
const Web3 = require('web3');
const BN = require('bn.js');
const AbiBinProvider = require('../AbiBinProvider');
const Contracts = require('../Contracts');
const Utils = require('../utils/Utils');
const Anchor = require('../ContractInteract/Anchor');
const EIP20Token = require('../ContractInteract/EIP20Token');
const { validateConfigKeyExists } = require('./validation');
const ContractName = 'EIP20CoGateway';
/**
* Contract interact for EIP20CoGateway.
*/
class EIP20CoGateway {
/**
* Constructor for EIP20CoGateway.
*
* @param {Object} web3 Web3 object.
* @param {string} coGatewayAddress EIP20CoGateway contract address.
*/
constructor(web3, coGatewayAddress) {
if (!(web3 instanceof Web3)) {
throw new TypeError("Mandatory Parameter 'web3' is missing or invalid");
}
if (!Web3.utils.isAddress(coGatewayAddress)) {
throw new TypeError(
`Mandatory Parameter 'coGatewayAddress' is missing or invalid: ${coGatewayAddress}`,
);
}
this.web3 = web3;
this.address = coGatewayAddress;
this.contract = Contracts.getEIP20CoGateway(this.web3, this.address);
if (!this.contract) {
const err = new Error(
`Could not load EIP20CoGateway contract for: ${this.address}`,
);
throw err;
}
this.proveGateway = this.proveGateway.bind(this);
this.proveGatewayRawTx = this.proveGatewayRawTx.bind(this);
this.confirmStakeIntent = this.confirmStakeIntent.bind(this);
this.confirmStakeIntentRawTx = this.confirmStakeIntentRawTx.bind(this);
this.progressMint = this.progressMint.bind(this);
this.progressMintRawTx = this.progressMintRawTx.bind(this);
this.getBounty = this.getBounty.bind(this);
this.getNonce = this.getNonce.bind(this);
this.getStateRootProviderAddress = this.getStateRootProviderAddress.bind(
this,
);
this.getInboxMessageStatus = this.getInboxMessageStatus.bind(this);
this.getOutboxMessageStatus = this.getOutboxMessageStatus.bind(this);
this.getAnchor = this.getAnchor.bind(this);
this.getLatestAnchorInfo = this.getLatestAnchorInfo.bind(this);
this.getUtilityTokenContract = this.getUtilityTokenContract.bind(this);
this.getUtilityToken = this.getUtilityToken.bind(this);
this.isRedeemAmountApproved = this.isRedeemAmountApproved.bind(this);
this.redeem = this.redeem.bind(this);
this.redeemRawTx = this.redeemRawTx.bind(this);
this.progressRedeemRawTx = this.progressRedeemRawTx.bind(this);
this.approveRedeemAmount = this.approveRedeemAmount.bind(this);
this.progressRedeem = this.progressRedeem.bind(this);
}
/**
* Setup for EIP20CoGateway. Deploys EIP20CoGateway.
*
* @param {Object} web3 Web3 object.
* @param {EIP20CoGatewaySetupConfig} config EIP20CoGateway setup configuration.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<EIP20CoGateway>} Promise containing the EIP20CoGateway instance that
* has been set up.
*/
static setup(web3, config, txOptions = {}) {
EIP20CoGateway.validateSetupConfig(config);
const deployParams = Object.assign({}, txOptions);
deployParams.from = txOptions.from || config.deployer;
return EIP20CoGateway.deploy(
web3,
config.valueToken,
config.utilityToken,
config.stateRootProvider,
config.bounty,
config.organization,
config.gateway,
config.burner,
config.messageBus,
config.gatewayLib,
deployParams,
);
}
/**
* Validate the setup configuration.
*
* @param {EIP20CoGatewaySetupConfig} config EIP20CoGateway setup configuration.
*
* @throws Will throw an error if setup configuration is incomplete.
*/
static validateSetupConfig(config) {
validateConfigKeyExists(config, 'gateway', 'config');
validateConfigKeyExists(config, 'deployer', 'config');
validateConfigKeyExists(config, 'bounty', 'config');
validateConfigKeyExists(config, 'organization', 'config');
validateConfigKeyExists(config, 'stateRootProvider', 'config');
validateConfigKeyExists(config, 'messageBus', 'config');
validateConfigKeyExists(config, 'gatewayLib', 'config');
validateConfigKeyExists(config, 'valueToken', 'config');
validateConfigKeyExists(config, 'utilityToken', 'config');
validateConfigKeyExists(config, 'burner', 'config');
}
/**
* Deploys an EIP20CoGateway contract.
*
* @param {Object} web3 Web3 object.
* @param {string} valueToken The value token contract address.
* @param {string} utilityToken The utility token address that will be used for
* minting the utility token.
* @param {string} stateRootProvider Address of contract to use for getting the state root of
* the origin chain.
* @param {string} bounty The amount that facilitator will stakes to initiate the
* stake process.
* @param {string} organization Address of an organization contract.
* @param {string} gateway Address of EIP20Gateway on Origin.
* @param {string} burner Address where tokens will be burned.
* @param {string} messageBusAddress Address of MessageBus contract
* to link into the contract bytecode.
* @param {string} gatewayLibAddress Address of GatewayLib contract
* to link into the contract bytecode.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<EIP20CoGateway>} Promise containing the EIP20CoGateway instance that
* has been deployed.
*/
static async deploy(
web3,
valueToken,
utilityToken,
stateRootProvider,
bounty,
organization,
gateway,
burner,
messageBusAddress,
gatewayLibAddress,
txOptions,
) {
if (!txOptions) {
const err = new TypeError('Invalid transaction options.');
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(`Invalid from address: ${txOptions.from}.`);
return Promise.reject(err);
}
const tx = EIP20CoGateway.deployRawTx(
web3,
valueToken,
utilityToken,
stateRootProvider,
bounty,
organization,
gateway,
burner,
messageBusAddress,
gatewayLibAddress,
);
return Utils.sendTransaction(tx, txOptions).then((txReceipt) => {
const address = txReceipt.contractAddress;
return new EIP20CoGateway(web3, address);
});
}
/**
* Raw transaction object for {@link EIP20CoGateway#deploy}
*
* @param {Object} web3 Web3 object.
* @param {string} valueToken The value token contract address.
* @param {string} utilityToken The utility token address that will be used for
* minting the utility token.
* @param {string} stateRootProvider Address of contract to use for getting the state root of
* the origin chain.
* @param {string} bounty The amount that facilitators will stake to initiate the
* stake process.
* @param {string} organization Address of an organization contract.
* @param {string} gateway Address of EIP20Gateway on Origin.
* @param {string} burner Address where tokens will be burned.
* @param {string} messageBusAddress Address of MessageBus contract
* to link into the contract bytecode.
* @param {string} gatewayLibAddress Address of GatewayLib contract
* to link into the contract bytecode.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
static deployRawTx(
web3,
valueToken,
utilityToken,
stateRootProvider,
bounty,
organization,
gateway,
burner,
messageBusAddress,
gatewayLibAddress,
) {
if (!(web3 instanceof Web3)) {
throw new TypeError(
`Mandatory Parameter 'web3' is missing or invalid: ${web3}`,
);
}
if (!Web3.utils.isAddress(valueToken)) {
throw new TypeError(`Invalid valueToken address: ${valueToken}.`);
}
if (!Web3.utils.isAddress(utilityToken)) {
throw new TypeError(`Invalid utilityToken address: ${utilityToken}.`);
}
if (!Web3.utils.isAddress(stateRootProvider)) {
throw new TypeError(
`Invalid stateRootProvider address: ${stateRootProvider}.`,
);
}
if (!(typeof bounty === 'string' || typeof bounty === 'number')) {
throw new TypeError(`Invalid bounty: ${bounty}.`);
}
if (!Web3.utils.isAddress(organization)) {
throw new TypeError(`Invalid organization address: ${organization}.`);
}
if (!Web3.utils.isAddress(gateway)) {
throw new TypeError(`Invalid gateway address: ${gateway}.`);
}
if (!Web3.utils.isAddress(burner)) {
throw new TypeError(`Invalid burner address: ${burner}.`);
}
if (!Web3.utils.isAddress(messageBusAddress)) {
throw new TypeError(
`Invalid messageBusAddress address: ${messageBusAddress}.`,
);
}
if (!Web3.utils.isAddress(gatewayLibAddress)) {
throw new TypeError(
`Invalid gatewayLibAddress address: ${gatewayLibAddress}.`,
);
}
const messageBusLinkInfo = {
address: messageBusAddress,
name: 'MessageBus',
};
const gatewayLibLinkInfo = {
address: gatewayLibAddress,
name: 'GatewayLib',
};
const abiBinProvider = new AbiBinProvider();
const abi = abiBinProvider.getABI(ContractName);
const bin = abiBinProvider.getLinkedBIN(
ContractName,
messageBusLinkInfo,
gatewayLibLinkInfo,
);
const contract = new web3.eth.Contract(abi, null, null);
const args = [
valueToken,
utilityToken,
stateRootProvider,
bounty,
organization,
gateway,
burner,
];
return contract.deploy({
data: bin,
arguments: args,
});
}
/**
* Prove gateway contract account address on auxiliary chain.
*
* @param {string} blockHeight Block number.
* @param {string} encodedAccount Encoded account data.
* @param {string} accountProof Account proof data.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
proveGateway(blockHeight, encodedAccount, accountProof, txOptions) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(
`Invalid from address ${txOptions.from} in transaction options.`,
);
return Promise.reject(err);
}
return this.proveGatewayRawTx(
blockHeight,
encodedAccount,
accountProof,
).then((tx) => Utils.sendTransaction(tx, txOptions));
}
/**
* Get raw transaction object for prove Gateway contract.
*
* @param {string} blockHeight Block number.
* @param {string} encodedAccount Encoded account data.
* @param {string} accountProof Account proof data.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
proveGatewayRawTx(blockHeight, encodedAccount, accountProof) {
if (typeof blockHeight !== 'string') {
const err = new TypeError(`Invalid block height: ${blockHeight}.`);
return Promise.reject(err);
}
if (typeof encodedAccount !== 'string') {
const err = new TypeError(`Invalid account data: ${encodedAccount}.`);
return Promise.reject(err);
}
if (typeof accountProof !== 'string') {
const err = new TypeError(`Invalid account proof: ${accountProof}.`);
return Promise.reject(err);
}
const tx = this.contract.methods.proveGateway(
blockHeight,
encodedAccount,
accountProof,
);
return Promise.resolve(tx);
}
/**
* Performs confirm stake intent.
*
* @param {string} staker Staker address.
* @param {string} nonce Staker nonce.
* @param {string} beneficiary Beneficiary address.
* @param {string} amount Amount to stake.
* @param {string} gasPrice Gas price that staker is willing to pay for the reward.
* @param {string} gasLimit Maximum gas limit for reward calculation.
* @param {string} hashLock Hash lock.
* @param {string} blockHeight Block number.
* @param {string} storageProof Storage proof.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
confirmStakeIntent(
staker,
nonce,
beneficiary,
amount,
gasPrice,
gasLimit,
hashLock,
blockHeight,
storageProof,
txOptions,
) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(
`Invalid from address ${txOptions.from} in transaction options.`,
);
return Promise.reject(err);
}
return this.confirmStakeIntentRawTx(
staker,
nonce,
beneficiary,
amount,
gasPrice,
gasLimit,
hashLock,
blockHeight,
storageProof,
).then((tx) => Utils.sendTransaction(tx, txOptions));
}
/**
* Get the raw transaction for confirm stake intent.
*
* @param {string} staker Staker address.
* @param {string} nonce Staker nonce.
* @param {string} beneficiary Beneficiary address.
* @param {string} amount Amount to stake.
* @param {string} gasPrice Gas price that staker is willing to pay for the reward.
* @param {string} gasLimit Maximum gas limit for reward calculation.
* @param {string} hashLock Hash lock.
* @param {string} blockHeight Block number.
* @param {string} storageProof Storage proof.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
confirmStakeIntentRawTx(
staker,
nonce,
beneficiary,
amount,
gasPrice,
gasLimit,
hashLock,
blockHeight,
storageProof,
) {
if (!Web3.utils.isAddress(staker)) {
const err = new TypeError(`Invalid staker address: ${staker}.`);
return Promise.reject(err);
}
if (typeof nonce !== 'string') {
const err = new TypeError(`Invalid nonce: ${nonce}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(beneficiary)) {
const err = new TypeError(
`Invalid beneficiary address: ${beneficiary}.`,
);
return Promise.reject(err);
}
if (typeof amount !== 'string') {
const err = new TypeError(`Invalid stake amount: ${amount}.`);
return Promise.reject(err);
}
if (typeof gasPrice !== 'string') {
const err = new TypeError(`Invalid gas price: ${gasPrice}.`);
return Promise.reject(err);
}
if (typeof gasLimit !== 'string') {
const err = new TypeError(`Invalid gas limit: ${gasLimit}.`);
return Promise.reject(err);
}
if (typeof blockHeight !== 'string') {
const err = new TypeError(`Invalid block height: ${blockHeight}.`);
return Promise.reject(err);
}
if (typeof hashLock !== 'string') {
const err = new TypeError(`Invalid hash lock: ${hashLock}.`);
return Promise.reject(err);
}
if (typeof storageProof !== 'string') {
const err = new TypeError(
`Invalid storage proof data: ${storageProof}.`,
);
return Promise.reject(err);
}
const tx = this.contract.methods.confirmStakeIntent(
staker,
nonce,
beneficiary,
amount,
gasPrice,
gasLimit,
hashLock,
blockHeight,
storageProof,
);
return Promise.resolve(tx);
}
/**
* Performs progress mint.
*
* @param {string} messageHash Hash to identify mint message.
* @param {string} unlockSecret Unlock secret.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
progressMint(messageHash, unlockSecret, txOptions) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(
`Invalid from address ${txOptions.from} in transaction options.`,
);
return Promise.reject(err);
}
return this.progressMintRawTx(messageHash, unlockSecret).then((tx) =>
Utils.sendTransaction(tx, txOptions),
);
}
/**
* Get the raw transaction for progress mint.
*
* @param {string} messageHash Hash to identify mint message.
* @param {string} unlockSecret Unlock secret.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
progressMintRawTx(messageHash, unlockSecret) {
if (typeof messageHash !== 'string') {
const err = new TypeError(`Invalid message hash: ${messageHash}.`);
return Promise.reject(err);
}
if (typeof unlockSecret !== 'string') {
const err = new TypeError(`Invalid unlock secret: ${unlockSecret}.`);
return Promise.reject(err);
}
const tx = this.contract.methods.progressMint(messageHash, unlockSecret);
return Promise.resolve(tx);
}
/**
* Performs progress redeem.
*
* @param {string} messageHash Hash to identify redeem message.
* @param {string} unlockSecret Unlock secret.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
progressRedeem(messageHash, unlockSecret, txOptions) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(
`Invalid from address ${txOptions.from} in transaction options.`,
);
return Promise.reject(err);
}
return this.progressRedeemRawTx(messageHash, unlockSecret).then((tx) =>
Utils.sendTransaction(tx, txOptions),
);
}
/**
* Get the raw transaction for progress redeem.
*
* @param {string} messageHash Hash to identify redeem message.
* @param {string} unlockSecret Unlock secret.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
progressRedeemRawTx(messageHash, unlockSecret) {
if (typeof messageHash !== 'string') {
const err = new TypeError(`Invalid message hash: ${messageHash}.`);
return Promise.reject(err);
}
if (typeof unlockSecret !== 'string') {
const err = new TypeError(`Invalid unlock secret: ${unlockSecret}.`);
return Promise.reject(err);
}
const tx = this.contract.methods.progressRedeem(messageHash, unlockSecret);
return Promise.resolve(tx);
}
/**
* Return the bounty amount.
*
* @returns {Promise<string>} Promise that resolves to bounty amount.
*/
getBounty() {
if (this._bountyAmount) {
return Promise.resolve(this._bountyAmount);
}
return this.contract.methods
.bounty()
.call()
.then((bounty) => {
this._bountyAmount = bounty;
return bounty;
});
}
/**
* Returns the utility token address.
*
* @returns {Promise<string>} Promise that resolves to utility token contract address.
*/
getUtilityToken() {
if (this._utilityToken) {
return Promise.resolve(this._utilityToken);
}
return this.contract.methods
.utilityToken()
.call()
.then((utilityToken) => {
this._utilityToken = utilityToken;
return utilityToken;
});
}
/**
* Returns the nonce for the given account address.
*
* @param {string} accountAddress Account address for which the nonce is to be fetched.
*
* @returns {Promise<Object>} Promise that resolves to nonce
*/
getNonce(accountAddress) {
if (!Web3.utils.isAddress(accountAddress)) {
throw new TypeError(`Invalid account address: ${accountAddress}.`);
}
return this.contract.methods.getNonce(accountAddress).call();
}
/**
* Returns the state root provider contract address.
*
* @returns {Promise<Object>} Promise that resolves to state root provider contract's address.
*/
async getStateRootProviderAddress() {
if (this._stateRootProviderAddress) {
return Promise.resolve(this._stateRootProviderAddress);
}
return this.contract.methods
.stateRootProvider()
.call()
.then((stateRootProviderAddress) => {
this._stateRootProviderAddress = stateRootProviderAddress;
return stateRootProviderAddress;
});
}
/**
* Returns inbox message status.
*
* @param {string} messageHash Message hash.
*
* @returns {Promise<Object>} Promise that resolves to message status.
*/
getInboxMessageStatus(messageHash) {
if (typeof messageHash !== 'string') {
const err = new TypeError(`Invalid message hash: ${messageHash}.`);
return Promise.reject(err);
}
return this.contract.methods.getInboxMessageStatus(messageHash).call();
}
/**
* Returns outbox message status.
*
* @param {string} messageHash Message hash.
*
* @returns {Promise<Object>} Promise that resolves to message status.
*/
getOutboxMessageStatus(messageHash) {
if (typeof messageHash !== 'string') {
const err = new TypeError(`Invalid message hash: ${messageHash}.`);
return Promise.reject(err);
}
return this.contract.methods.getOutboxMessageStatus(messageHash).call();
}
/**
* Performs redeem.
*
* @param {string} amount Amount to redeem.
* @param {string} beneficiary Beneficiary address.
* @param {string} gasPrice Gas price that staker is willing to pay for the reward.
* @param {string} gasLimit Maximum gas limit for reward calculation.
* @param {string} nonce Redeemer's nonce.
* @param {string} hashLock Hash lock.
* @param {Object} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
redeem(amount, beneficiary, gasPrice, gasLimit, nonce, hashLock, txOptions) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(
`Invalid redeemer address: ${txOptions.from}.`,
);
return Promise.reject(err);
}
return this.redeemRawTx(
amount,
beneficiary,
gasPrice,
gasLimit,
nonce,
hashLock,
).then((tx) => Utils.sendTransaction(tx, txOptions));
}
/**
* Get the raw transaction for redeem.
*
* @param {string} amount Amount to redeem.
* @param {string} beneficiary Beneficiary address.
* @param {string} gasPrice Gas price that staker is willing to pay for the reward.
* @param {string} gasLimit Maximum gas limit for reward calculation.
* @param {string} nonce Redeemer's nonce.
* @param {string} hashLock Hash lock.
*
* @returns {Promise<Object>} Promise that resolves to raw transaction object.
*/
redeemRawTx(amount, beneficiary, gasPrice, gasLimit, nonce, hashLock) {
if (!new BN(amount).gtn(0)) {
const err = new TypeError(
`Redeem amount must be greater than zero: ${amount}.`,
);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(beneficiary)) {
const err = new TypeError(
`Invalid beneficiary address: ${beneficiary}.`,
);
return Promise.reject(err);
}
if (typeof gasPrice !== 'string') {
const err = new TypeError(`Invalid gas price: ${gasPrice}.`);
return Promise.reject(err);
}
if (typeof gasLimit !== 'string') {
const err = new TypeError(`Invalid gas limit: ${gasLimit}.`);
return Promise.reject(err);
}
if (typeof nonce !== 'string') {
const err = new TypeError(`Invalid nonce: ${nonce}.`);
return Promise.reject(err);
}
if (typeof hashLock !== 'string') {
const err = new TypeError(`Invalid hash lock: ${hashLock}.`);
return Promise.reject(err);
}
const tx = this.contract.methods.redeem(
amount,
beneficiary,
gasPrice,
gasLimit,
nonce,
hashLock,
);
return Promise.resolve(tx);
}
/**
* Check if the account has approved CoGateway contract for redeem amount transfer.
*
* @param {string} redmeer Redeemer account address.
* @param {string} amount Approval amount.
*
* @returns {Promise<boolean>} Promise that resolves to `true` if approved.
*/
isRedeemAmountApproved(redeemer, amount) {
if (!Web3.utils.isAddress(redeemer)) {
const err = new TypeError(`Invalid redeemer address: ${redeemer}.`);
return Promise.reject(err);
}
if (typeof amount !== 'string') {
const err = new TypeError(`Invalid redeem amount: ${amount}.`);
return Promise.reject(err);
}
return this.getUtilityTokenContract().then((eip20ValueToken) => {
return eip20ValueToken.isAmountApproved(redeemer, this.address, amount);
});
}
/**
* Approves CoGateway contract address for the amount transfer.
*
* @param {string} amount Approve amount.
* @param {string} txOptions Transaction options.
*
* @returns {Promise<Object>} Promise that resolves to transaction receipt.
*/
approveRedeemAmount(amount, txOptions) {
if (!txOptions) {
const err = new TypeError(`Invalid transaction options: ${txOptions}.`);
return Promise.reject(err);
}
if (!Web3.utils.isAddress(txOptions.from)) {
const err = new TypeError(`Invalid from address: ${txOptions.from}.`);
return Promise.reject(err);
}
if (typeof amount !== 'string') {
const err = new TypeError(`Invalid redeem amount: ${amount}.`);
return Promise.reject(err);
}
return this.getUtilityTokenContract().then((eip20Token) => {
return eip20Token.approve(this.address, amount, txOptions);
});
}
/**
* Returns Anchor object.
*
* @returns {Promise<string>} Promise object that resolves to anchor contract address.
*/
getAnchor() {
if (this._anchor) {
return Promise.resolve(this._anchor);
}
return this.getStateRootProviderAddress().then((anchorAddress) => {
const anchor = new Anchor(this.web3, anchorAddress);
this._anchor = anchor;
return anchor;
});
}
/**
* Get the latest state root and block height.
*
* @returns {Promise<Object>} Promise object that resolves to object containing state root and block height.
*/
async getLatestAnchorInfo() {
return this.getAnchor().then((anchor) => anchor.getLatestInfo());
}
/**
* Returns utility token object.
*
* @returns {Promise<Object>} Promise that resolves to utility token object.
*/
getUtilityTokenContract() {
if (this._eip20UtilityToken) {
return Promise.resolve(this._eip20UtilityToken);
}
return this.getUtilityToken().then((utilityToken) => {
const token = new EIP20Token(this.web3, utilityToken);
this._eip20UtilityToken = token;
return token;
});
}
}
module.exports = EIP20CoGateway;