Skip to content

Commit

Permalink
feat(chains-ethereum): remove kovan config, make assets dependent on …
Browse files Browse the repository at this point in the history
…network, support previous eth deposits with detectPreviousDeposits option
  • Loading branch information
0x31 committed Oct 28, 2022
1 parent f446af3 commit c6278ab
Show file tree
Hide file tree
Showing 45 changed files with 842 additions and 350 deletions.
5 changes: 1 addition & 4 deletions packages/chains/chains-bitcoin/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,7 @@ export abstract class BitcoinBaseChain
// Ignore error and fallback to getUTXOs.
}

while (true) {
if (listenerCancelled()) {
return;
}
while (!listenerCancelled()) {
try {
const utxos = await this.api.fetchUTXOs(address);
utxos.map((tx) =>
Expand Down
17 changes: 14 additions & 3 deletions packages/chains/chains-bitcoin/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,29 @@ export class Bitcoin extends BitcoinBaseChain {
public static configMap: BitcoinNetworkConfigMap = {
[RenNetwork.Mainnet]: BitcoinMainnet,
[RenNetwork.Testnet]: BitcoinTestnet,
[RenNetwork.Devnet]: BitcoinTestnet,
};
public configMap = Bitcoin.configMap;

public static assets = {
BTC: "BTC",
[RenNetwork.Mainnet]: {
BTC: "BTC",
},
[RenNetwork.Testnet]: {
BTC: "BTC",
},
};
public assets = Bitcoin.assets;

public assets:
| typeof Bitcoin.assets[RenNetwork.Mainnet]
| typeof Bitcoin.assets[RenNetwork.Testnet];

public constructor({ network }: { network: BitcoinNetworkInput }) {
super({
network: resolveBitcoinNetworkConfig(Bitcoin.configMap, network),
});
this.assets =
Bitcoin.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
17 changes: 14 additions & 3 deletions packages/chains/chains-bitcoin/src/bitcoincash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ export class BitcoinCash extends BitcoinBaseChain {
public static configMap: BitcoinNetworkConfigMap = {
[RenNetwork.Mainnet]: BitcoinCashMainnet,
[RenNetwork.Testnet]: BitcoinCashTestnet,
[RenNetwork.Devnet]: BitcoinCashTestnet,
};
public configMap = BitcoinCash.configMap;

public static assets = {
BCH: "BCH",
[RenNetwork.Mainnet]: {
BCH: "BCH",
},
[RenNetwork.Testnet]: {
BCH: "BCH",
},
};
public assets = BitcoinCash.assets;

public assets:
| typeof BitcoinCash.assets[RenNetwork.Mainnet]
| typeof BitcoinCash.assets[RenNetwork.Testnet];

public validateAddress = (address: string): boolean => {
try {
Expand Down Expand Up @@ -106,5 +113,9 @@ export class BitcoinCash extends BitcoinBaseChain {
network,
),
});
this.assets =
BitcoinCash.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
17 changes: 14 additions & 3 deletions packages/chains/chains-bitcoin/src/digibyte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ export class DigiByte extends BitcoinBaseChain {
public static configMap: BitcoinNetworkConfigMap = {
[RenNetwork.Mainnet]: DigiByteMainnet,
[RenNetwork.Testnet]: DigiByteTestnet,
[RenNetwork.Devnet]: DigiByteTestnet,
};
public configMap = DigiByte.configMap;

public static assets = {
DGB: "DGB",
[RenNetwork.Mainnet]: {
DGB: "DGB",
},
[RenNetwork.Testnet]: {
DGB: "DGB",
},
};
public assets = DigiByte.assets;

public assets:
| typeof DigiByte.assets[RenNetwork.Mainnet]
| typeof DigiByte.assets[RenNetwork.Testnet];

public validateAddress = (address: string): boolean => {
try {
Expand All @@ -93,5 +100,9 @@ export class DigiByte extends BitcoinBaseChain {
super({
network: resolveBitcoinNetworkConfig(DigiByte.configMap, network),
});
this.assets =
DigiByte.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
17 changes: 14 additions & 3 deletions packages/chains/chains-bitcoin/src/dogecoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,21 @@ export class Dogecoin extends BitcoinBaseChain {
public static configMap: BitcoinNetworkConfigMap = {
[RenNetwork.Mainnet]: DogecoinMainnet,
[RenNetwork.Testnet]: DogecoinTestnet,
[RenNetwork.Devnet]: DogecoinTestnet,
};
public configMap = Dogecoin.configMap;

public static assets = {
DOGE: "DOGE",
[RenNetwork.Mainnet]: {
DOGE: "DOGE",
},
[RenNetwork.Testnet]: {
DOGE: "DOGE",
},
};
public assets = Dogecoin.assets;

public assets:
| typeof Dogecoin.assets[RenNetwork.Mainnet]
| typeof Dogecoin.assets[RenNetwork.Testnet];

public validateAddress = (address: string): boolean => {
try {
Expand All @@ -86,5 +93,9 @@ export class Dogecoin extends BitcoinBaseChain {
super({
network: resolveBitcoinNetworkConfig(Dogecoin.configMap, network),
});
this.assets =
Dogecoin.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
17 changes: 14 additions & 3 deletions packages/chains/chains-bitcoin/src/zcash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,29 @@ export class Zcash extends BitcoinBaseChain {
public static configMap: BitcoinNetworkConfigMap = {
[RenNetwork.Mainnet]: ZcashMainnet,
[RenNetwork.Testnet]: ZcashTestnet,
[RenNetwork.Devnet]: ZcashTestnet,
};
public configMap = Zcash.configMap;

public static assets = {
ZEC: "ZEC",
[RenNetwork.Mainnet]: {
ZEC: "ZEC",
},
[RenNetwork.Testnet]: {
ZEC: "ZEC",
},
};
public assets = Zcash.assets;

public assets:
| typeof Zcash.assets[RenNetwork.Mainnet]
| typeof Zcash.assets[RenNetwork.Testnet];

public constructor({ network }: { network: BitcoinNetworkInput }) {
super({
network: resolveBitcoinNetworkConfig(Zcash.configMap, network),
});
this.assets =
Zcash.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
8 changes: 4 additions & 4 deletions packages/chains/chains-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
"prepare": "yarn build"
},
"dependencies": {
"@ethersproject/abi": "5.6.4",
"@ethersproject/bytes": "5.6.1",
"@ethersproject/providers": "5.6.8",
"@ethersproject/abi": "5.7.0",
"@ethersproject/bytes": "5.7.0",
"@ethersproject/providers": "5.7.2",
"@renproject/utils": "^3.5.1",
"@types/elliptic": "6.4.14",
"bignumber.js": "9.0.2",
"elliptic": "6.5.4",
"ethers": "5.6.9"
"ethers": "5.7.2"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
Expand Down
11 changes: 9 additions & 2 deletions packages/chains/chains-ethereum/src/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ export class Arbitrum extends EthereumBaseChain {
public static chain = "Arbitrum" as const;
public static configMap = configMap;
public static assets = {
ArbETH: "ArbETH" as const,
[RenNetwork.Mainnet]: { ArbETH: "ArbETH" as const },
[RenNetwork.Testnet]: { ArbETH: "ArbETH" as const },
};

public configMap = configMap;
public assets = Arbitrum.assets;
public assets:
| typeof Arbitrum.assets[RenNetwork.Mainnet]
| typeof Arbitrum.assets[RenNetwork.Testnet];

public constructor({
network,
Expand All @@ -82,5 +85,9 @@ export class Arbitrum extends EthereumBaseChain {
...params,
network: resolveEVMNetworkConfig(configMap, network),
});
this.assets =
Arbitrum.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
11 changes: 9 additions & 2 deletions packages/chains/chains-ethereum/src/avalanche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ export class Avalanche extends EthereumBaseChain {
public static chain = "Avalanche" as const;
public static configMap = configMap;
public static assets = {
AVAX: "AVAX" as const,
[RenNetwork.Mainnet]: { AVAX: "AVAX" as const },
[RenNetwork.Testnet]: { AVAX: "AVAX" as const },
};

public configMap = configMap;
public assets = Avalanche.assets;
public assets:
| typeof Avalanche.assets[RenNetwork.Mainnet]
| typeof Avalanche.assets[RenNetwork.Testnet];

public constructor({
network,
Expand All @@ -78,5 +81,9 @@ export class Avalanche extends EthereumBaseChain {
...params,
network: resolveEVMNetworkConfig(configMap, network),
});
this.assets =
Avalanche.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
88 changes: 88 additions & 0 deletions packages/chains/chains-ethereum/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
findInputByNonce,
findMintBySigHash,
findReleaseBySigHash,
getPastLogs,
mapBurnLogToInputChainTransaction,
mapBurnToChainLogToInputChainTransaction,
mapLockLogToInputChainTransaction,
Expand Down Expand Up @@ -1282,6 +1283,93 @@ export class EthereumBaseChain
});
};

public watchForDeposits = async (
asset: string,
fromPayload: EVMPayloadInterface,
address: string,
onInput: (input: InputChainTransaction) => void,
_removeInput: (input: InputChainTransaction) => void,
listenerCancelled: () => boolean,
): Promise<void> => {
if (fromPayload.chain !== this.chain) {
throw new Error(
`Invalid payload for chain ${fromPayload.chain} instead of ${this.chain}.`,
);
}

if (
!fromPayload.payloadConfig ||
!fromPayload.payloadConfig.detectPreviousDeposits
) {
while (!listenerCancelled()) {
// Nothing more to do.
await utils.sleep(1 * utils.sleep.SECONDS);
}
}

const logTransferredABI = findABIMethod(
TransferWithLogABI,
"LogTransferred",
);

// If the payload is a transaction, submit it to onInput and then loop
// indefinitely.
if (fromPayload.type === "transaction") {
const receipt = await this.provider.getTransactionReceipt(
(fromPayload as EVMTxPayload).params.tx.txHash,
);
const transferEvents = filterLogs<LogTransferredEvent>(
receipt.logs,
logTransferredABI,
).map((e) =>
mapTransferLogToInputChainTransaction(
this.chain,
asset,
e.event,
this.transactionExplorerLink({
txHash: e.log.transactionHash,
}) || "",
),
);
transferEvents.map(onInput);

while (!listenerCancelled()) {
// Nothing more to do.
await utils.sleep(1 * utils.sleep.SECONDS);
}
} else {
const registry = getGatewayRegistryInstance(
this.provider,
this.network.addresses.GatewayRegistry,
);
const transferWithLogAddress = await registry.getTransferContract();

const transferEvents = (
await getPastLogs<LogTransferredEvent>(
this.provider,
transferWithLogAddress,
logTransferredABI,
[
null,
utils.Ox(utils.toNBytes(utils.fromHex(address), 32)),
null,
],
this.network.logRequestLimit,
)
).map((e) =>
mapTransferLogToInputChainTransaction(
this.chain,
asset,
e.event,
this.transactionExplorerLink({
txHash: e.log.transactionHash,
}) || "",
),
);
transferEvents.map(onInput);
}
};

/* ====================================================================== */

public Account = ({
Expand Down
11 changes: 9 additions & 2 deletions packages/chains/chains-ethereum/src/bsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ export class BinanceSmartChain extends EthereumBaseChain {
public static chain = "BinanceSmartChain" as const;
public static configMap = configMap;
public static assets = {
BNB: "BNB" as const,
[RenNetwork.Mainnet]: { BNB: "BNB" as const },
[RenNetwork.Testnet]: { BNB: "BNB" as const },
};

public assets = BinanceSmartChain.assets;
public assets:
| typeof BinanceSmartChain.assets[RenNetwork.Mainnet]
| typeof BinanceSmartChain.assets[RenNetwork.Testnet];
public configMap = configMap;

public constructor({
Expand All @@ -110,5 +113,9 @@ export class BinanceSmartChain extends EthereumBaseChain {
...params,
network: resolveEVMNetworkConfig(configMap, network),
});
this.assets =
BinanceSmartChain.assets[
this.network.isTestnet ? RenNetwork.Testnet : RenNetwork.Mainnet
];
}
}
Loading

0 comments on commit c6278ab

Please sign in to comment.