This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathethService.spec.ts
More file actions
697 lines (612 loc) · 28.6 KB
/
ethService.spec.ts
File metadata and controls
697 lines (612 loc) · 28.6 KB
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
import {
ChainError,
ChainRpcProvider,
FullChannelState,
IChainServiceStore,
IChannelSigner,
MinimalTransaction,
Result,
StoredTransaction,
TransactionReason,
} from "@connext/vector-types";
import {
ChannelSigner,
createTestChannelState,
expect,
getTestLoggers,
MemoryStoreService,
mkAddress,
mkBytes32,
mkHash,
} from "@connext/vector-utils";
import { AddressZero, One, Zero } from "@ethersproject/constants";
import { TransactionReceipt, TransactionResponse } from "@ethersproject/providers";
import { BigNumber } from "ethers";
import { parseUnits } from "ethers/lib/utils";
import { restore, reset, createStubInstance, SinonStubbedInstance, stub, SinonStub } from "sinon";
import { v4 as uuidV4 } from "uuid";
import { BIG_GAS_PRICE, EthereumChainService } from "./ethService";
let storeMock: SinonStubbedInstance<IChainServiceStore>;
let signer: SinonStubbedInstance<IChannelSigner>;
let ethService: EthereumChainService;
let provider1337: SinonStubbedInstance<ChainRpcProvider>;
let provider1338: SinonStubbedInstance<ChainRpcProvider>;
let sendTxWithRetriesMock: SinonStub;
let approveMock: SinonStub;
let getCodeMock: SinonStub;
let getOnchainBalanceMock: SinonStub;
let waitForConfirmation: SinonStub<[chainId: number, responses: TransactionResponse[]], Promise<TransactionReceipt>>;
let getGasPrice: SinonStub<[chainId: number], Promise<Result<BigNumber, ChainError>>>;
let channelState: FullChannelState;
const assertResult = (result: Result<any>, isError: boolean, unwrappedVal?: any) => {
if (isError) {
expect(result.isError).to.be.true;
if (unwrappedVal) {
expect(result.getError()?.message).to.be.eq(unwrappedVal);
}
} else {
expect(result.isError).to.be.false;
if (unwrappedVal) {
expect(result.getValue()).to.deep.eq(unwrappedVal);
}
}
};
const txResponse = {
chainId: 1337,
confirmations: 1,
data: "0x",
from: AddressZero,
gasLimit: One,
gasPrice: One,
hash: mkHash(),
nonce: 1,
value: Zero,
wait: () => Promise.resolve({} as TransactionReceipt),
};
const txReceipt: TransactionReceipt = {
blockHash: mkHash("0xabc"),
blockNumber: 123,
byzantium: true,
confirmations: 1,
contractAddress: mkAddress("0xa"),
cumulativeGasUsed: BigNumber.from(21000),
from: txResponse.from,
gasUsed: BigNumber.from(21000),
logs: [],
logsBloom: "0x",
to: mkAddress("0xbbb"),
transactionHash: txResponse.hash,
transactionIndex: 1,
status: 1,
};
const { log } = getTestLoggers("ethService");
describe("ethService unit test", () => {
beforeEach(() => {
// eth service deps
storeMock = createStubInstance(MemoryStoreService);
signer = createStubInstance(ChannelSigner);
signer.connect.returns(signer as any);
(signer as any)._isSigner = true;
const _provider = createStubInstance(ChainRpcProvider);
_provider.getTransaction.resolves(txResponse);
provider1337 = _provider;
provider1338 = _provider;
(signer as any).provider = provider1337;
// create eth service class
ethService = new EthereumChainService(
storeMock,
{
1337: provider1337,
1338: provider1338,
},
signer,
log,
);
// stubs with default friendly behavior
getCodeMock = stub(ethService, "getCode").resolves(Result.ok("0x"));
approveMock = stub(ethService, "approveTokens").resolves(Result.ok(txReceipt));
getOnchainBalanceMock = stub(ethService, "getOnchainBalance").resolves(Result.ok(BigNumber.from("100")));
getGasPrice = stub(ethService, "getGasPrice").resolves(Result.ok(parseUnits("1", "gwei")));
// channel state
const test = createTestChannelState("create");
channelState = test.channel;
channelState.networkContext.chainId = 1337;
signer.getAddress.resolves(channelState.alice);
});
afterEach(() => {
restore();
reset();
});
describe("sendDeployChannelTx", () => {
beforeEach(() => {
sendTxWithRetriesMock = stub(ethService, "sendTxWithRetries").resolves(Result.ok(txReceipt));
});
it("errors if cannot get a signer", async () => {
channelState.networkContext.chainId = 1234;
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, true, ChainError.reasons.SignerNotFound);
});
it("errors if multisig code cannot be retrieved", async () => {
getCodeMock.resolves(Result.fail(new ChainError("getCode error")));
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, true, "getCode error");
});
it("errors if multisig is already deployed", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, true, ChainError.reasons.MultisigDeployed);
});
it("errors if multisig deployment fails without deposit", async () => {
sendTxWithRetriesMock.resolves(Result.fail(new ChainError(ChainError.reasons.TxReverted)));
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, true, ChainError.reasons.TxReverted);
});
it("errors if multisig deployment returns nothing", async () => {
sendTxWithRetriesMock.resolves(Result.ok(undefined));
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, true, ChainError.reasons.MultisigDeployed);
});
it("errors if deposit and is not alice", async () => {
signer.getAddress.resolves(channelState.bob);
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "1",
assetId: AddressZero,
});
assertResult(result, true, ChainError.reasons.FailedToDeploy);
});
it("errors if deposit and cannot get onchain balance", async () => {
getOnchainBalanceMock.resolves(Result.fail(new ChainError(ChainError.reasons.TxNotFound)));
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "1",
assetId: AddressZero,
});
assertResult(result, true, ChainError.reasons.TxNotFound);
});
it("errors if deposit and not enough onchain balance", async () => {
getOnchainBalanceMock.resolves(Result.ok(BigNumber.from("9")));
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "10",
assetId: AddressZero,
});
assertResult(result, true, ChainError.reasons.NotEnoughFunds);
});
it("errors if error on approve", async () => {
approveMock.resolves(Result.fail(new ChainError(ChainError.reasons.NotEnoughFunds)));
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "1",
assetId: mkAddress("0xa"),
});
assertResult(result, true, ChainError.reasons.NotEnoughFunds);
});
it("happy: alice can deploy channel without deposit", async () => {
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, false, txReceipt);
const call = sendTxWithRetriesMock.getCall(0);
expect(call.args[0]).to.eq(channelState.channelAddress);
expect(call.args[1]).to.eq(channelState.networkContext.chainId);
expect(call.args[2]).to.eq(TransactionReason.deploy);
});
it("happy: bob can deploy channel without deposit", async () => {
signer.getAddress.resolves(channelState.bob);
const result = await ethService.sendDeployChannelTx(channelState);
assertResult(result, false, txReceipt);
const call = sendTxWithRetriesMock.getCall(0);
expect(call.args[0]).to.eq(channelState.channelAddress);
expect(call.args[1]).to.eq(channelState.networkContext.chainId);
expect(call.args[2]).to.eq(TransactionReason.deploy);
});
it("happy: calls createChannelAndDepositAlice with native asset if 0x000... deposit", async () => {
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "1",
assetId: AddressZero,
});
assertResult(result, false, txReceipt);
const call = sendTxWithRetriesMock.getCall(0);
expect(call.args[0]).to.eq(channelState.channelAddress);
expect(call.args[1]).to.eq(channelState.networkContext.chainId);
expect(call.args[2]).to.eq(TransactionReason.deployWithDepositAlice);
});
it("happy: calls createChannelAndDepositAlice with tokens if token deposit", async () => {
const result = await ethService.sendDeployChannelTx(channelState, {
amount: "1",
assetId: mkAddress("0xa"),
});
assertResult(result, false);
const approveCall = approveMock.getCall(0);
expect(approveCall.args[0]).to.eq(channelState.channelAddress);
expect(approveCall.args[1]).to.eq(channelState.networkContext.channelFactoryAddress);
expect(approveCall.args[2]).to.eq(channelState.alice);
expect(approveCall.args[3]).to.eq("1");
expect(approveCall.args[4]).to.eq(mkAddress("0xa"));
expect(approveCall.args[5]).to.eq(channelState.networkContext.chainId);
const call = sendTxWithRetriesMock.getCall(0);
expect(call.args[0]).to.eq(channelState.channelAddress);
expect(call.args[1]).to.eq(channelState.networkContext.chainId);
expect(call.args[2]).to.eq("deployWithDepositAlice");
});
});
describe("sendWithdrawTx", () => {
const minTx: MinimalTransaction = {
data: mkBytes32("0xabc"),
to: AddressZero,
value: 0,
};
let sendDeployChannelTxMock: SinonStub;
beforeEach(() => {
sendDeployChannelTxMock = stub(ethService, "sendDeployChannelTx").resolves(Result.ok(txReceipt));
sendTxWithRetriesMock = stub(ethService, "sendTxWithRetries").resolves(Result.ok(txReceipt));
});
it("errors if cannot get a signer", async () => {
channelState.networkContext.chainId = 1234;
const result = await ethService.sendWithdrawTx(channelState, minTx);
assertResult(result, true, ChainError.reasons.SignerNotFound);
});
it("errors if multisig code cannot be retrieved", async () => {
getCodeMock.resolves(Result.fail(new ChainError("getCode error")));
const result = await ethService.sendWithdrawTx(channelState, minTx);
assertResult(result, true, "getCode error");
});
it("errors if channel deployment fails", async () => {
sendDeployChannelTxMock.resolves(Result.fail(new ChainError(ChainError.reasons.NotEnoughFunds)));
const result = await ethService.sendWithdrawTx(channelState, minTx);
assertResult(result, true, ChainError.reasons.FailedToDeploy);
});
it("errors if deploy tx throws an error", async () => {
sendDeployChannelTxMock.resolves(Result.fail(new ChainError(ChainError.reasons.TxReverted)));
const result = await ethService.sendWithdrawTx(channelState, minTx);
assertResult(result, true, ChainError.reasons.FailedToDeploy);
});
it("happy: if channel is deployed, send withdrawal tx", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
const result = await ethService.sendWithdrawTx(channelState, minTx);
expect(sendDeployChannelTxMock.callCount).to.eq(0);
assertResult(result, false, txReceipt);
});
it("happy: if channel is not deployed, deploy channel then send withdrawal tx", async () => {
const result = await ethService.sendWithdrawTx(channelState, minTx);
expect(sendDeployChannelTxMock.callCount).to.eq(1);
expect(sendDeployChannelTxMock.getCall(0).firstArg).to.deep.eq(channelState);
assertResult(result, false, txReceipt);
});
});
describe("sendDepositTx", () => {
let sendDeployChannelTxMock: SinonStub;
let sendDepositATxMock: SinonStub;
let sendDepositBTxMock: SinonStub;
beforeEach(() => {
sendDeployChannelTxMock = stub(ethService, "sendDeployChannelTx").resolves(Result.ok(txReceipt));
sendDepositATxMock = stub(ethService, "sendDepositATx").resolves(Result.ok(txReceipt));
sendDepositBTxMock = stub(ethService, "sendDepositBTx").resolves(Result.ok(txReceipt));
sendTxWithRetriesMock = stub(ethService, "sendTxWithRetries").resolves(Result.ok(txReceipt));
});
it("errors if cannot get a signer", async () => {
channelState.networkContext.chainId = 1234;
const result = await ethService.sendDepositTx(channelState, channelState.alice, "1", AddressZero);
assertResult(result, true, ChainError.reasons.SignerNotFound);
});
it("errors if sender is not in channel", async () => {
const result = await ethService.sendDepositTx(channelState, mkAddress("0xababab"), "1", AddressZero);
assertResult(result, true, ChainError.reasons.SenderNotInChannel);
});
it("errors if deposit amount is negative", async () => {
const result = await ethService.sendDepositTx(channelState, channelState.alice, "-1", AddressZero);
assertResult(result, true, ChainError.reasons.NegativeDepositAmount);
});
it("errors if multisig code cannot be retrieved", async () => {
getCodeMock.resolves(Result.fail(new ChainError("getCode error")));
const result = await ethService.sendDepositTx(channelState, channelState.alice, "1", AddressZero);
assertResult(result, true, "getCode error");
});
it("errors if onchain balance returns an error", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
getOnchainBalanceMock.resolves(Result.fail(new Error("getOnchainBalance error")));
const result = await ethService.sendDepositTx(channelState, channelState.alice, "1", AddressZero);
assertResult(result, true, "getOnchainBalance error");
});
it("errors if channel is deployed and onchain balance is < send amount", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
getOnchainBalanceMock.resolves(Result.ok(BigNumber.from(5)));
const result = await ethService.sendDepositTx(channelState, channelState.alice, "6", AddressZero);
assertResult(result, true, ChainError.reasons.NotEnoughFunds);
});
it("happy: alice deploys channel with deposit if not deployed", async () => {
const result = await ethService.sendDepositTx(channelState, channelState.alice, "1", AddressZero);
assertResult(result, false, txReceipt);
expect(sendDeployChannelTxMock.callCount).to.eq(1);
const call = sendDeployChannelTxMock.getCall(0);
expect(call.args[0]).to.deep.eq(channelState);
expect(call.args[1]).to.deep.eq({ amount: "1", assetId: AddressZero });
});
it("happy: alice calls sendDepositATx if multisig is deployed", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
const result = await ethService.sendDepositTx(channelState, channelState.alice, "1", AddressZero);
assertResult(result, false, txReceipt);
expect(sendDepositATxMock.callCount).to.eq(1);
const call = sendDepositATxMock.getCall(0);
expect(call.args[0]).to.deep.eq(channelState);
expect(call.args[1]).to.deep.eq("1");
expect(call.args[2]).to.deep.eq(AddressZero);
});
it("happy: bob calls sendDepositBTx if multisig is deployed", async () => {
getCodeMock.resolves(Result.ok(mkHash("0xabc")));
const result = await ethService.sendDepositTx(channelState, channelState.bob, "1", AddressZero);
assertResult(result, false, txReceipt);
expect(sendDepositBTxMock.callCount).to.eq(1);
const call = sendDepositBTxMock.getCall(0);
expect(call.args[0]).to.deep.eq(channelState);
expect(call.args[1]).to.deep.eq("1");
expect(call.args[2]).to.deep.eq(AddressZero);
});
it("happy: bob calls sendDepositBTx if multisig is not deployed", async () => {
const result = await ethService.sendDepositTx(channelState, channelState.bob, "1", AddressZero);
assertResult(result, false, txReceipt);
expect(sendDepositBTxMock.callCount).to.eq(1);
const call = sendDepositBTxMock.getCall(0);
expect(call.args[0]).to.deep.eq(channelState);
expect(call.args[1]).to.deep.eq("1");
expect(call.args[2]).to.deep.eq(AddressZero);
});
});
describe("sendTxWithRetries", () => {
let sendAndConfirmTx: SinonStub;
beforeEach(() => {
sendAndConfirmTx = stub(ethService, "sendAndConfirmTx").resolves(Result.ok(txReceipt));
});
it("errors if sendTxAndParseResponse errors", async () => {
sendAndConfirmTx.resolves(Result.fail(new ChainError(ChainError.reasons.NotEnoughFunds)));
const result = await ethService.sendTxWithRetries(
channelState.channelAddress,
channelState.networkContext.chainId,
"allowance",
() => {
return Promise.resolve(txResponse);
},
);
assertResult(result, true, ChainError.reasons.NotEnoughFunds);
});
it("retries if it's a retryable error", async () => {
sendAndConfirmTx
.onFirstCall()
.resolves(
Result.fail(
new ChainError(
'processing response error (body="{"jsonrpc":"2.0","error":{"code":-32000,"message":"Block information is incomplete while ancient block sync is still in progress, before it\'s finished we can\'t determine the existence of requested item."},"id":14890}\n", error={"code":-32000}, requestBody="{"method":"eth_getTransactionReceipt","params":["0x8731c46fafd569bb65c6c26cd3960ad418d88310a41a03c5c4f4a0dcce15cd8a"],"id":14890,"jsonrpc":"2.0"}", requestMethod="POST", url="https://rpc.xdaichain.com/", code=SERVER_ERROR, version=web/5.1.0)',
),
),
);
sendAndConfirmTx.resolves(Result.ok(txReceipt));
const result = await ethService.sendTxWithRetries(
channelState.channelAddress,
channelState.networkContext.chainId,
"allowance",
() => {
return Promise.resolve(txResponse);
},
);
assertResult(result, false, txReceipt);
});
it("happy: should work when sendTxAndParseResponse works on the first try", async () => {
const result = await ethService.sendTxWithRetries(
channelState.channelAddress,
channelState.networkContext.chainId,
"allowance",
() => {
return Promise.resolve(txResponse);
},
);
assertResult(result, false, txReceipt);
});
});
describe("sendAndConfirmTx", () => {
beforeEach(() => {
waitForConfirmation = stub(ethService, "waitForConfirmation");
});
it("if txFn returns undefined, returns undefined", async () => {
const result = await ethService.sendAndConfirmTx(
AddressZero,
1337,
"allowance",
async () => {
return undefined;
},
BigNumber.from(10_000),
);
assertResult(result, false, undefined);
});
it("if txFn errors, returns error", async () => {
const result = await ethService.sendAndConfirmTx(
AddressZero,
1337,
"allowance",
async () => {
throw new Error("Boooo");
},
BigNumber.from(10_000),
);
assertResult(result, true, "Boooo");
});
it("if txFn errors, with not enough funds, return special error", async () => {
const result = await ethService.sendAndConfirmTx(
AddressZero,
1337,
"allowance",
async () => {
throw new Error("sender doesn't have enough funds");
},
BigNumber.from(10_000),
);
assertResult(result, true, ChainError.reasons.NotEnoughFunds);
});
it("if receipt status == 0, saves response with error", async () => {
const badReceipt = { ...txReceipt, status: 0 };
waitForConfirmation.resolves(badReceipt);
const result = await ethService.sendAndConfirmTx(AddressZero, 1337, "allowance", async () => {
return txResponse;
});
expect(storeMock.saveTransactionAttempt.callCount).eq(1);
const saveTransactionAttemptCall = storeMock.saveTransactionAttempt.getCall(0);
const id = saveTransactionAttemptCall.args[0];
expect(saveTransactionAttemptCall.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall.args[2]).eq("allowance");
expect(saveTransactionAttemptCall.args[3]).deep.eq(txResponse);
expect(storeMock.saveTransactionFailure.callCount).eq(1);
const saveTransactionFailureCall = storeMock.saveTransactionFailure.getCall(0);
expect(saveTransactionFailureCall.args[0]).eq(id);
expect(saveTransactionFailureCall.args[1]).eq(ChainError.reasons.TxReverted);
expect(saveTransactionFailureCall.args[2]).eq(badReceipt);
assertResult(result, true, ChainError.reasons.TxReverted);
});
it("if receipt wait fn errors, saves response with error", async () => {
const ERROR_MSG = "Booooo";
const error = new Error(ERROR_MSG);
waitForConfirmation.rejects(error);
const result = await ethService.sendAndConfirmTx(AddressZero, 1337, "allowance", async () => {
return txResponse;
});
expect(storeMock.saveTransactionAttempt.callCount).eq(1);
const saveTransactionAttemptCall = storeMock.saveTransactionAttempt.getCall(0);
const id = saveTransactionAttemptCall.args[0];
expect(saveTransactionAttemptCall.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall.args[2]).eq("allowance");
expect(saveTransactionAttemptCall.args[3]).deep.eq(txResponse);
expect(storeMock.saveTransactionFailure.callCount).eq(1);
const saveTransactionFailureCall = storeMock.saveTransactionFailure.getCall(0);
expect(saveTransactionFailureCall.args[0]).eq(id);
expect(saveTransactionFailureCall.args[1]).eq(ERROR_MSG);
assertResult(result, true, ERROR_MSG);
});
it("retries transaction with higher gas price", async () => {
const newTx = { ...txResponse, hash: mkHash("0xddd") }; // change hash to simulate higher gas and new hash
const newReceipt = { ...txReceipt, transactionHash: newTx.hash };
waitForConfirmation.onFirstCall().rejects(new ChainError(ChainError.reasons.ConfirmationTimeout));
waitForConfirmation.onSecondCall().resolves(newReceipt);
let receivedNonce: number = -1;
let firstGasPrice: BigNumber = BigNumber.from(-1);
let secondGasPrice: BigNumber = BigNumber.from(-1);
const result = await ethService.sendAndConfirmTx(
AddressZero,
1337,
"allowance",
async (gasPrice: BigNumber, nonce?: number) => {
if (nonce) {
// If the nonce was passed in, we are on the second call of this callback.
receivedNonce = nonce;
secondGasPrice = gasPrice;
return newTx;
}
firstGasPrice = gasPrice;
return txResponse;
},
);
expect(receivedNonce === txResponse.nonce, "nonce passed into callback was not the same as original tx nonce");
expect(secondGasPrice > firstGasPrice, "second gas price should be larger than first");
expect(storeMock.saveTransactionAttempt.callCount).eq(2);
const saveTransactionAttemptCall = storeMock.saveTransactionAttempt.getCall(0);
const id = saveTransactionAttemptCall.args[0];
expect(saveTransactionAttemptCall.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall.args[2]).eq("allowance");
expect(saveTransactionAttemptCall.args[3]).deep.eq(txResponse);
const saveTransactionAttemptCall2 = storeMock.saveTransactionAttempt.getCall(1);
expect(saveTransactionAttemptCall2.args[0]).eq(id);
expect(saveTransactionAttemptCall2.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall2.args[2]).eq("allowance");
expect(saveTransactionAttemptCall2.args[3]).deep.eq(newTx);
expect(storeMock.saveTransactionReceipt.callCount).eq(1);
const saveTransactionReceiptCall = storeMock.saveTransactionReceipt.getCall(0);
expect(saveTransactionReceiptCall.args[0]).eq(id);
expect(saveTransactionReceiptCall.args[1]).eq(newReceipt);
assertResult(result, false, newReceipt);
});
it("stops trying to send if at max gas price", async () => {
getGasPrice.resolves(Result.ok(BIG_GAS_PRICE.sub(1)));
waitForConfirmation.onFirstCall().rejects(new ChainError(ChainError.reasons.ConfirmationTimeout));
const result = await ethService.sendAndConfirmTx(AddressZero, 1337, "allowance", async () => {
return txResponse;
});
expect(storeMock.saveTransactionAttempt.callCount).eq(1);
const saveTransactionAttemptCall = storeMock.saveTransactionAttempt.getCall(0);
const id = saveTransactionAttemptCall.args[0];
expect(saveTransactionAttemptCall.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall.args[2]).eq("allowance");
expect(saveTransactionAttemptCall.args[3]).deep.eq(txResponse);
expect(storeMock.saveTransactionFailure.callCount).eq(1);
const saveTransactionFailureCall = storeMock.saveTransactionFailure.getCall(0);
expect(saveTransactionFailureCall.args[0]).eq(id);
expect(saveTransactionFailureCall.args[1]).eq(ChainError.reasons.MaxGasPriceReached);
assertResult(result, true, ChainError.reasons.MaxGasPriceReached);
});
it("happy: saves responses if confirmation happens on first loop", async () => {
waitForConfirmation.resolves(txReceipt);
const result = await ethService.sendAndConfirmTx(AddressZero, 1337, "allowance", async () => {
return txResponse;
});
expect(storeMock.saveTransactionAttempt.callCount).eq(1);
const saveTransactionAttemptCall = storeMock.saveTransactionAttempt.getCall(0);
const id = saveTransactionAttemptCall.args[0];
expect(saveTransactionAttemptCall.args[1]).eq(AddressZero);
expect(saveTransactionAttemptCall.args[2]).eq("allowance");
expect(saveTransactionAttemptCall.args[3]).deep.eq(txResponse);
expect(storeMock.saveTransactionReceipt.callCount).eq(1);
const saveTransactionReceiptCall = storeMock.saveTransactionReceipt.getCall(0);
expect(saveTransactionReceiptCall.args[0]).eq(id);
expect(saveTransactionReceiptCall.args[1]).eq(txReceipt);
assertResult(result, false, txReceipt);
});
});
describe("waitForConfirmation", () => {
it("should return an error if there is no provider for chain", async () => {
await expect(ethService.waitForConfirmation(111, [txResponse])).to.eventually.be.rejectedWith(
ChainError.reasons.ProviderNotFound,
);
});
it("should wait for the required amount of confirmations", async () => {
provider1337.getTransactionReceipt.onFirstCall().resolves({ ...txReceipt, confirmations: 0 });
provider1337.getTransactionReceipt.onSecondCall().resolves({ ...txReceipt, confirmations: 0 });
provider1337.getTransactionReceipt.onThirdCall().resolves(txReceipt);
const res = await ethService.waitForConfirmation(1337, [txResponse]);
expect(res).to.deep.eq(txReceipt);
expect(provider1337.getTransactionReceipt.callCount).to.eq(3);
});
it("should error with a timeout error if it is past the confirmation time", async () => {
provider1337.getTransactionReceipt.onThirdCall().resolves(undefined);
await expect(ethService.waitForConfirmation(1337, [txResponse])).to.eventually.be.rejectedWith(
ChainError.reasons.ConfirmationTimeout,
);
});
});
describe("revitalizeTxs", () => {
it("should resubmit and monitor active txs", async () => {
const storedTx: StoredTransaction = {
channelAddress: mkAddress("0xa"),
reason: "allowance",
status: "submitted",
to: mkAddress(),
chainId: txResponse.chainId,
data: txResponse.data,
from: txResponse.from,
id: uuidV4(),
nonce: txResponse.nonce,
attempts: [
{
transactionHash: txResponse.hash,
gasLimit: txResponse.gasLimit.toString(),
gasPrice: txResponse.gasPrice.toString(),
createdAt: new Date(),
},
],
value: txResponse.value.toString(),
};
const storedTxs = [storedTx, storedTx, storedTx, storedTx];
storeMock.getActiveTransactions.resolves(storedTxs);
const getTxResponseFromHash = stub(ethService, "getTxResponseFromHash");
getTxResponseFromHash.onFirstCall().resolves(Result.fail(new ChainError(ChainError.reasons.TxNotFound))); // first one errors, should be skipped
getTxResponseFromHash.onSecondCall().resolves(Result.ok({ response: txResponse, receipt: txReceipt })); // second one has a receipt, needs to be saved
getTxResponseFromHash.onThirdCall().resolves(Result.ok({ response: txResponse })); // 3rd and 4th need to be resubmitted
getTxResponseFromHash.resolves(Result.ok({ response: txResponse }));
const sendTxWithRetries = stub(ethService, "sendTxWithRetries").resolves(Result.ok(txReceipt));
await ethService.revitalizeTxs();
expect(storeMock.saveTransactionReceipt.callCount).to.eq(1);
expect(sendTxWithRetries.callCount).to.eq(2);
});
});
});