Skip to content

Commit e7b7244

Browse files
committed
feat: improve detecting reverted txs, code tidying
1 parent 0a87758 commit e7b7244

11 files changed

+196
-106
lines changed

packages/ren/src/gateway.ts

-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ export class Gateway<
568568

569569
await transaction.initialize();
570570

571-
// Check if deposit has already been submitted.
572571
this.eventEmitter.emit("transaction", transaction);
573572
// this.deposits.set(deposit);
574573
this.config.logger.debug("new deposit:", inputTx);

packages/ren/src/utils/defaultTransactionHandler.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,29 @@ const chainTransactionHandler = async (
3939
}, retries);
4040
break;
4141
case ChainTransactionStatus.Confirming:
42-
await utils.tryNTimes(async (i: number) => {
43-
try {
44-
await tx.wait();
45-
} catch (error: unknown) {
46-
// Log error every 10 attempts.
47-
if ((i + 1) % 10 === 0) {
48-
console.error(error);
49-
}
50-
throw error;
51-
}
52-
}, retries);
42+
await new Promise((resolve, reject) =>
43+
resolve(
44+
utils.tryNTimes(async (i: number) => {
45+
try {
46+
await tx.wait();
47+
} catch (error: unknown) {
48+
if (
49+
ErrorWithCode.isErrorWithCode(error) &&
50+
error.code ===
51+
RenJSError.CHAIN_TRANSACTION_REVERTED
52+
) {
53+
reject(error);
54+
throw error;
55+
}
56+
// Log error every 10 attempts.
57+
if ((i + 1) % 10 === 0) {
58+
console.error(error);
59+
}
60+
throw error;
61+
}
62+
}, retries),
63+
),
64+
);
5365
break;
5466
case ChainTransactionStatus.Reverted:
5567
throw new ErrorWithCode(

packages/utils/src/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const decodeRenVMSelector = (
4242
};
4343
};
4444

45-
const EMPTY_SIGNATURE =
45+
export const EMPTY_SIGNATURE =
4646
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
4747
export const isEmptySignature = (sig: Uint8Array): boolean =>
4848
utils.toURLBase64(sig) === EMPTY_SIGNATURE;

packages/utils/src/txSubmitter.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ErrorWithCode, RenJSError } from "./errors";
12
import { isDefined, newPromiEvent } from "./internal/common";
23
import { sleep } from "./internal/sleep";
34
import { Chain, ChainTransaction, SyncOrPromise } from "./types/chain";
@@ -358,6 +359,16 @@ export class DefaultTxWaiter implements TxWaiter {
358359
}
359360
}
360361
} catch (error: unknown) {
362+
this.updateProgress({
363+
...this.progress,
364+
status: ChainTransactionStatus.Reverted,
365+
});
366+
if (
367+
ErrorWithCode.isErrorWithCode(error) &&
368+
error.code === RenJSError.CHAIN_TRANSACTION_REVERTED
369+
) {
370+
throw error;
371+
}
361372
console.error(error);
362373
}
363374
await sleep(15 * sleep.SECONDS);

packages/utils/src/types/chain.ts

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ export interface ChainCommon {
9696
/** Override the chain's provider. */
9797
provider?: any;
9898
withProvider?: (...providers: any[]) => SyncOrPromise<this>;
99+
checkProviderNetwork?: () => SyncOrPromise<{
100+
result: boolean;
101+
actualNetworkId: string | number;
102+
expectedNetworkId: string | number;
103+
expectedNetworkLabel: string;
104+
}>;
99105

100106
/** Override the chain's signer. */
101107
signer?: any;

packages/utils/test/common.spec.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import BigNumber from "bignumber.js";
2-
import chai, { expect } from "chai";
3-
import chaiAsPromised from "chai-as-promised";
2+
import { expect } from "chai";
43

54
import { utils } from "../src/internal";
65

7-
chai.use(chaiAsPromised);
8-
96
describe("common utils", () => {
107
context("sleep", () => {
118
it("correct amount of time passes", async () => {

test/gateway.spec.ts

+127-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Solana } from "@renproject/chains";
12
import { getERC20Instance } from "@renproject/chains-ethereum/src/contracts";
23
import BigNumber from "bignumber.js";
34
import chai from "chai";
@@ -7,12 +8,14 @@ import {
78
Arbitrum,
89
Avalanche,
910
BinanceSmartChain,
11+
Catalog,
1012
Ethereum,
13+
EthereumBaseChain,
1114
EVMParam,
15+
Goerli,
1216
Polygon,
1317
} from "packages/chains/chains-ethereum/src";
1418
import { Filecoin } from "packages/chains/chains-filecoin/src";
15-
import { Solana } from "packages/chains/chains-solana/src";
1619
import { Terra } from "packages/chains/chains-terra/src";
1720
import RenJS from "packages/ren/src";
1821
import { GatewayParams } from "packages/ren/src/params";
@@ -436,36 +439,143 @@ describe("Gateway", () => {
436439
await defaultGatewayHandler(await renJS.gateway(gatewayParams));
437440
}).timeout(100000000000);
438441

439-
it("DAI/fromBinanceSmartChain", async () => {
442+
it("USDT/toCatalog", async () => {
440443
const network = RenNetwork.Testnet;
441444

442-
const asset = Ethereum.assets.DAI;
443-
const from = initializeChain(BinanceSmartChain, network);
444-
const to = initializeChain(Ethereum, network);
445+
const from = initializeChain(Goerli, network);
446+
const catalog = initializeChain(Catalog, network);
447+
const bsc = initializeChain(BinanceSmartChain, network);
448+
const polygon = initializeChain(Polygon, network);
449+
const renJS = new RenJS(network).withChains(
450+
from,
451+
catalog,
452+
bsc,
453+
polygon,
454+
);
455+
console.log(await from.signer!.getAddress());
445456

446-
const renJS = new RenJS(network).withChains(from, to);
457+
console.log(
458+
Goerli.assets.USDT,
459+
(await from.getBalance(Goerli.assets.USDT))
460+
.shiftedBy(-(await from.assetDecimals(Goerli.assets.USDT)))
461+
.toFixed(),
462+
);
463+
console.log(
464+
Goerli.assets.DAI,
465+
(await from.getBalance(Goerli.assets.DAI))
466+
.shiftedBy(-(await from.assetDecimals(Goerli.assets.DAI)))
467+
.toFixed(),
468+
);
469+
console.log(
470+
Goerli.assets.USDC,
471+
(await from.getBalance(Goerli.assets.USDC))
472+
.shiftedBy(-(await from.assetDecimals(Goerli.assets.USDC)))
473+
.toFixed(),
474+
);
447475

448-
const gatewayParams = {
449-
asset,
450-
from: from.Account({ amount: 0.5, convertUnit: true }),
451-
to: to.Address("0x797522fb74d42bb9fbf6b76dea24d01a538d5d66"),
452-
};
476+
const options: Array<[string, EthereumBaseChain]> = [
477+
// [Goerli.assets.USDT, catalog],
478+
// [Goerli.assets.USDC, catalog],
479+
// [Goerli.assets.DAI, catalog],
480+
[Goerli.assets.USDT, bsc],
481+
[Goerli.assets.USDC, bsc],
482+
[Goerli.assets.DAI, bsc],
483+
[Goerli.assets.USDT, polygon],
484+
[Goerli.assets.USDC, polygon],
485+
[Goerli.assets.DAI, polygon],
486+
];
487+
488+
for (const [asset, to] of options) {
489+
// const asset = Ethereum.assets.USDT;
490+
const decimals = await from.assetDecimals(asset);
491+
492+
const amount = 100.2;
493+
494+
const gatewayParams = {
495+
asset,
496+
from: from.Account({ amount, convertUnit: true }),
497+
to: to.Address("0x5eb99e19183728404AaeBc8eEF47C085dBE86F54"),
498+
};
453499

454-
await defaultGatewayHandler(await renJS.gateway(gatewayParams));
500+
await defaultGatewayHandler(
501+
await renJS.gateway(gatewayParams),
502+
new BigNumber(amount).shiftedBy(decimals),
503+
);
504+
505+
console.log(
506+
asset,
507+
"on",
508+
to.chain,
509+
(
510+
await to.getBalance(
511+
asset,
512+
"0x5eb99e19183728404AaeBc8eEF47C085dBE86F54",
513+
)
514+
)
515+
.shiftedBy(-(await to.assetDecimals(asset)))
516+
.toFixed(),
517+
);
518+
}
455519
}).timeout(100000000000);
456520

457-
it("BTC/fromEthereum", async () => {
521+
it("BTC/to catalog chains", async () => {
458522
const network = RenNetwork.Testnet;
459523

460-
const asset = Bitcoin.assets.BTC;
461-
const from = initializeChain(Ethereum, network);
462-
const to = initializeChain(Solana, network);
524+
const from = initializeChain(Bitcoin, network);
525+
const asset = from.assets.BTC;
526+
const catalog = initializeChain(Catalog, network);
527+
const bsc = initializeChain(BinanceSmartChain, network);
528+
const polygon = initializeChain(Polygon, network);
529+
const ethereum = initializeChain(Ethereum, network);
530+
const renJS = new RenJS(network).withChains(
531+
from,
532+
catalog,
533+
bsc,
534+
polygon,
535+
ethereum,
536+
);
537+
538+
const options: EthereumBaseChain[] = [catalog, bsc, polygon, ethereum];
539+
540+
for (const to of options) {
541+
// const asset = Ethereum.assets.USDT;
542+
543+
const gatewayParams = {
544+
asset,
545+
from: from.GatewayAddress(),
546+
to: to.Address("0x5eb99e19183728404AaeBc8eEF47C085dBE86F54"),
547+
};
548+
549+
await defaultGatewayHandler(await renJS.gateway(gatewayParams));
550+
551+
console.log(
552+
asset,
553+
"on",
554+
to.chain,
555+
(
556+
await to.getBalance(
557+
asset,
558+
"0x5eb99e19183728404AaeBc8eEF47C085dBE86F54",
559+
)
560+
)
561+
.shiftedBy(-(await to.assetDecimals(asset)))
562+
.toFixed(),
563+
);
564+
}
565+
}).timeout(100000000000);
566+
567+
it.only("REN/toSolana", async () => {
568+
const network = RenNetwork.Testnet;
569+
570+
const asset = Ethereum.assets.REN;
571+
const from = initializeChain(Solana, network);
572+
const to = initializeChain(Ethereum, network);
463573

464574
const renJS = new RenJS(network).withChains(from, to);
465575

466576
const gatewayParams: GatewayParams = {
467577
asset,
468-
from: from.Account({ amount: 1, convertUnit: true }),
578+
from: from.Account({ amount: 95, convertUnit: true }),
469579
to: to.Account(),
470580
};
471581

test/index.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
/* eslint-disable no-console */
22

3-
import { AbiCoder } from "ethers/lib/utils";
43
import { Ethereum } from "packages/chains/chains-ethereum/src";
54
import { BinanceSmartChain } from "packages/chains/chains/src";
65
import RenJS from "packages/ren/src";
76
import { RenNetwork } from "packages/utils";
87

9-
import { getEVMProvider } from "./utils/testUtils";
8+
import { initializeChain } from "./utils/testUtils";
109

1110
const network = RenNetwork.Testnet;
1211

1312
const main = async () => {
1413
// Initialize Ethereum and BSC chains.
15-
const ethereum = new Ethereum({
16-
network,
17-
...getEVMProvider(Ethereum, network),
18-
});
19-
const bsc = new BinanceSmartChain({
20-
network,
21-
...getEVMProvider(BinanceSmartChain, network),
22-
});
14+
const ethereum = initializeChain(Ethereum, network);
15+
const bsc = initializeChain(BinanceSmartChain, network);
2316

2417
// Create RenJS instance. NOTE - chains must now be linked to RenJS using
2518
// `withChains`.

test/simple.spec.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk from "chalk";
55
import { BinanceSmartChain } from "packages/chains/chains/src";
66
import RenJS from "packages/ren/src";
77

8-
import { getEVMProvider } from "./utils/testUtils";
8+
import { initializeChain } from "./utils/testUtils";
99

1010
const network = RenNetwork.Testnet;
1111

@@ -18,14 +18,8 @@ const network = RenNetwork.Testnet;
1818

1919
describe("DAI/toBinanceSmartChain - simpler", () => {
2020
it("DAI/toBinanceSmartChain - simpler", async () => {
21-
const ethereum = new Ethereum({
22-
network,
23-
...getEVMProvider(Ethereum, network),
24-
});
25-
const bsc = new BinanceSmartChain({
26-
network,
27-
...getEVMProvider(BinanceSmartChain, network),
28-
});
21+
const ethereum = initializeChain(Ethereum, network);
22+
const bsc = initializeChain(BinanceSmartChain, network);
2923

3024
const renJS = new RenJS(network).withChains(ethereum, bsc);
3125

test/utils/defaultGatewayHandler.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { printChain, sendFunds } from "./testUtils";
66

77
export const defaultGatewayHandler = async (
88
gateway: Gateway,
9+
amount?: BigNumber | number | string,
910
logger: Logger = console,
1011
): Promise<void> => {
1112
const asset = gateway.params.asset;
@@ -29,7 +30,7 @@ export const defaultGatewayHandler = async (
2930
-decimalsOnFromChain,
3031
);
3132
const receivedAmount = gateway.fees
32-
.estimateOutput(gateway.fees.minimumAmount)
33+
.estimateOutput(amount || gateway.fees.minimumAmount)
3334
.shiftedBy(-decimalsOnFromChain);
3435

3536
try {
@@ -117,7 +118,7 @@ export const defaultGatewayHandler = async (
117118
gateway.gatewayAddress
118119
} (to receive at least ${receivedAmount.toFixed()})`,
119120
);
120-
const SEND_FUNDS = false;
121+
const SEND_FUNDS = true;
121122
if (SEND_FUNDS) {
122123
try {
123124
await sendFunds(

0 commit comments

Comments
 (0)