Skip to content

Commit

Permalink
feat(abstract-eth): add abstractEthLikeNewCoins class
Browse files Browse the repository at this point in the history
  • Loading branch information
gianchandania committed Oct 30, 2023
1 parent 41d9598 commit 29b9e28
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
26 changes: 17 additions & 9 deletions modules/abstract-eth/src/abstractEthLikeNewCoins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {
protected readonly sendMethodName: 'sendMultiSig' | 'sendMultiSigToken';

protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
private static _ethLikeCoin: Readonly<StaticsBaseCoin>;

protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo);
Expand All @@ -363,6 +364,7 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {
}

this._staticsCoin = staticsCoin;
AbstractEthLikeNewCoins._ethLikeCoin = staticsCoin;
this.sendMethodName = 'sendMultiSig';
}

Expand Down Expand Up @@ -458,14 +460,19 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {

// solve this
static getCustomChainName(chainId?: number): string {
switch (chainId) {
case 137:
return 'PolygonMainnet';
case 80001:
return 'PolygonMumbai';
default:
throw new Error(`unsupported chain id ${chainId}`);
let chainName: string | undefined;
if (AbstractEthLikeNewCoins._ethLikeCoin.family === 'polygon') {
if (chainId === 80001) {
chainName = 'PolygonMumbai';
} else {
chainName = 'PolygonMainnet';
}
} else if (AbstractEthLikeNewCoins._ethLikeCoin.family === 'arbeth') {
chainName = 'ArbitrumOne';
} else if (AbstractEthLikeNewCoins._ethLikeCoin.family === 'opeth') {
chainName = 'OptimisticEthereum';
}
return chainName;
}

/**
Expand All @@ -486,6 +493,7 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {
ethLikeCommon = optionalDeps.EthLikeCommon.default.custom(customChain);
ethLikeCommon.setHardfork(replayProtectionOptions.hardfork);
} else {
// const network = AbstractEthLikeNewCoins._ethLikeCoin.network as EthLikeNetwork;
const customChain = optionalDeps.EthLikeCommon.CustomChain[AbstractEthLikeNewCoins.getCustomChainName()];
ethLikeCommon = optionalDeps.EthLikeCommon.default.custom(customChain);
ethLikeCommon.setHardfork(defaultHardfork);
Expand Down Expand Up @@ -670,10 +678,11 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {
* @returns {Array} operation array
*/
getOperation(recipient: Recipient, expireTime: number, contractSequenceId: number): (string | Buffer)[][] {
const network = this.getNetwork() as EthLikeNetwork;
return [
['string', 'address', 'uint256', 'bytes', 'uint256', 'uint256'],
[
'POLYGON',
network.networkIdForNativeCoinTransfer,
new optionalDeps.ethUtil.BN(optionalDeps.ethUtil.stripHexPrefix(recipient.address), 16),
recipient.amount,
Buffer.from(optionalDeps.ethUtil.stripHexPrefix(optionalDeps.ethUtil.padToEven(recipient.data || '')), 'hex'),
Expand Down Expand Up @@ -2323,4 +2332,3 @@ export abstract class AbstractEthLikeNewCoins extends BaseCoin {
}

// buildTransaction related abstraction to be done
// getOperation related abstraction
8 changes: 5 additions & 3 deletions modules/abstract-eth/src/ethLikeToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @prettier
*/
import { bip32 } from '@bitgo/utxo-lib';
import { coins, EthLikeTokenConfig, tokens } from '@bitgo/statics';
import { coins, EthLikeTokenConfig, tokens, EthereumNetwork as EthLikeNetwork } from '@bitgo/statics';

import {
BitGoBase,
Expand All @@ -15,7 +15,6 @@ import {
getIsUnsignedSweep,
checkKrsProvider,
} from '@bitgo/sdk-core';
// import { AbstractEthLikeCoin } from './abstractEthLikeCoin';
import {
AbstractEthLikeNewCoins,
optionalDeps,
Expand Down Expand Up @@ -54,11 +53,13 @@ interface RecoverTokenTransaction {

export class EthLikeToken extends AbstractEthLikeNewCoins {
public readonly tokenConfig: EthLikeTokenConfig;
protected readonly sendMethodName: 'sendMultiSig' | 'sendMultiSigToken';

protected constructor(bitgo: BitGoBase, tokenConfig: EthLikeTokenConfig, coinNames: CoinNames) {
const staticsCoin = coins.get(coinNames[tokenConfig.network]);
super(bitgo, staticsCoin);
this.tokenConfig = tokenConfig;
this.sendMethodName = 'sendMultiSigToken';
}

static createTokenConstructor(config: EthLikeTokenConfig, coinNames: CoinNames): CoinConstructor {
Expand Down Expand Up @@ -488,10 +489,11 @@ export class EthLikeToken extends AbstractEthLikeNewCoins {
}

getOperation(recipient, expireTime, contractSequenceId) {
const network = this.getNetwork() as EthLikeNetwork;
return [
['string', 'address', 'uint256', 'bytes', 'uint256', 'uint256'],
[
'ERC20',
network.networkIdForTokenTransfer,
new optionalDeps.ethUtil.BN(optionalDeps.ethUtil.stripHexPrefix(recipient.address), 16),
recipient.amount,
new optionalDeps.ethUtil.BN(optionalDeps.ethUtil.stripHexPrefix(this.tokenContractAddress), 16),
Expand Down
18 changes: 18 additions & 0 deletions modules/statics/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export interface EthereumNetwork extends AccountNetwork {
readonly forwarderImplementationAddress?: string;
readonly walletFactoryAddress?: string;
readonly walletImplementationAddress?: string;
readonly networkIdForNativeCoinTransfer?: string;
readonly networkIdForTokenTransfer?: string;
}

export interface TronNetwork extends AccountNetwork {
Expand Down Expand Up @@ -147,6 +149,8 @@ class Arbitrum extends Mainnet implements EthereumNetwork {
explorerUrl = 'https://arbiscan.io/tx/';
accountExplorerUrl = 'https://arbiscan.io/address/';
chainId = 42161;
networkIdForNativeCoinTransfer = 'ARBETH';
networkIdForTokenTransfer = 'ARBETH-ERC20';
}

class ArbitrumTestnet extends Testnet implements EthereumNetwork {
Expand All @@ -155,6 +159,8 @@ class ArbitrumTestnet extends Testnet implements EthereumNetwork {
explorerUrl = 'https://sepolia-explorer.arbitrum.io/tx/';
accountExplorerUrl = 'https://sepolia-explorer.arbitrum.io/address/';
chainId = 421614;
networkIdForNativeCoinTransfer = 'ARBETH';
networkIdForTokenTransfer = 'ARBETH-ERC20';
}

class AvalancheC extends Mainnet implements AccountNetwork {
Expand Down Expand Up @@ -404,6 +410,8 @@ class Ethereum extends Mainnet implements EthereumNetwork {
batcherContractAddress = '0x0c9b25dfe02b2c89cce86e1a0bd6c04a7aca01b6';
forwarderFactoryAddress = '0xffa397285ce46fb78c588a9e993286aac68c37cd';
forwarderImplementationAddress = '0x059ffafdc6ef594230de44f824e2bd0a51ca5ded';
networkIdForNativeCoinTransfer = 'ETHER';
networkIdForTokenTransfer = 'ERC20';
}

class Ethereum2 extends Mainnet implements AccountNetwork {
Expand Down Expand Up @@ -454,6 +462,8 @@ class Goerli extends Testnet implements EthereumNetwork {
batcherContractAddress = '0xe8e847cf573fc8ed75621660a36affd18c543d7e';
forwarderFactoryAddress = '0xf5caa5e3e93afbc21bd19ef4f2691a37121f7917';
forwarderImplementationAddress = '0x80d5c91e8cc21df69fc4d64f21dc2d83121c3999';
networkIdForNativeCoinTransfer = 'ETHER';
networkIdForTokenTransfer = 'ERC20';
}

class Holesky extends Testnet implements EthereumNetwork {
Expand All @@ -467,6 +477,8 @@ class Holesky extends Testnet implements EthereumNetwork {
batcherContractAddress = '0xe8e847cf573fc8ed75621660a36affd18c543d7e';
forwarderFactoryAddress = '0xf5caa5e3e93afbc21bd19ef4f2691a37121f7917';
forwarderImplementationAddress = '0x80d5c91e8cc21df69fc4d64f21dc2d83121c3999';
networkIdForNativeCoinTransfer = 'ETHER';
networkIdForTokenTransfer = 'ERC20';
}

class EthereumClassic extends Mainnet implements EthereumNetwork {
Expand Down Expand Up @@ -869,6 +881,8 @@ class Polygon extends Mainnet implements EthereumNetwork {
walletFactoryAddress = '0xa7198f48c58e91f01317e70cd24c5cce475c1555';
walletImplementationAddress = '0xe5dcdc13b628c2df813db1080367e929c1507ca0';
batcherContractAddress = '0x7adc9b3d7521710321bec7dd6897d337e53c2493';
networkIdForNativeCoinTransfer = 'POLYGON';
networkIdForTokenTransfer = 'POLYGON-ERC20';
}

class PolygonTestnet extends Testnet implements EthereumNetwork {
Expand All @@ -890,6 +904,8 @@ class Optimism extends Mainnet implements EthereumNetwork {
explorerUrl = 'https://optimistic.etherscan.io/ tx/';
accountExplorerUrl = 'https://optimistic.etherscan.io/address/';
chainId = 10;
networkIdForNativeCoinTransfer = 'OPETH';
networkIdForTokenTransfer = 'OPETH-ERC20';
}

class OptimismTestnet extends Testnet implements EthereumNetwork {
Expand All @@ -898,6 +914,8 @@ class OptimismTestnet extends Testnet implements EthereumNetwork {
explorerUrl = 'https://optimism-sepolia.blockscout.com/tx/';
accountExplorerUrl = 'https://optimism-sepolia.blockscout.com/address/';
chainId = 11155420;
networkIdForNativeCoinTransfer = 'OPETH';
networkIdForTokenTransfer = 'OPETH-ERC20';
}

export const Networks = {
Expand Down

0 comments on commit 29b9e28

Please sign in to comment.