Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ETH Holesky and Matic Amoy networks #71

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions apps/recovery-relay/lib/wallets/ERC20/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ConnectedWallet } from '../ConnectedWallet';
import { Ethereum } from '../EVM/ETH';
import { erc20Abi } from './erc20.abi';
import { transferAbi } from './transfer.abi';
import BigNumber from 'bignumber.js';

export class ERC20 extends Ethereum implements ConnectedWallet {
private contract: Contract;
Expand All @@ -17,13 +18,17 @@ export class ERC20 extends Ethereum implements ConnectedWallet {
}

public async getBalance(): Promise<number> {
const amountInWei = await this.contract.balanceOf(this.address);
return parseFloat(parseFloat(ethers.formatEther(amountInWei)).toFixed(2));
this.weiBalance = await this.contract.balanceOf(this.address);
return parseFloat(parseFloat(ethers.formatEther(this.weiBalance)).toFixed(2));
}

public async prepare(): Promise<AccountData> {
const displayBalance = await this.getBalance();
const extraParams = new Map();
extraParams.set(this.KEY_EVM_WEI_BALANCE, new BigNumber(this.weiBalance.toString()).toString(16));
const preparedData = {
balance: await this.getBalance(),
balance: displayBalance,
extraParams,
};
this.relayLogger.logPreparedData('ERC20', preparedData);
return preparedData;
Expand Down
8 changes: 7 additions & 1 deletion apps/recovery-relay/lib/wallets/EVM/ETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EVM } from '.';
export class Ethereum extends EVM implements ConnectedWallet {
constructor(input: Input) {
let cluster: string;
let rpcEndpoint: string = '';

switch (input.assetId) {
case 'ETH':
Expand All @@ -13,14 +14,19 @@ export class Ethereum extends EVM implements ConnectedWallet {
case 'ETH_TEST3':
cluster = 'goerli';
break;
case 'ETH_TEST6':
cluster = 'holesky';
rpcEndpoint = 'https://ethereum-holesky-rpc.publicnode.com';
break;
case 'ETH_TEST5':
cluster = 'sepolia';
rpcEndpoint = 'https://ethereum-sepolia-rpc.publicnode.com';
break;
default:
throw new Error('Unsupported asset');
}

const rpcEndpoint = `https://${cluster}.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161`;
rpcEndpoint = rpcEndpoint === '' ? `https://${cluster}.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161` : rpcEndpoint;

super(input, rpcEndpoint);
}
Expand Down
20 changes: 16 additions & 4 deletions apps/recovery-relay/lib/wallets/EVM/MATIC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import { EVM } from '.';

export class Matic extends EVM implements ConnectedWallet {
constructor(input: Input) {
super(
input,
`${input.isTestnet ? 'https://polygon-mumbai.infura.io/v3/4458cf4d1689497b9a38b1d6bbf05e78' : 'https://polygon-rpc.com/'}`,
);
let rpcEndpoint: string;

switch (input.assetId) {
case 'MATIC_POLYGON_MUMBAI':
rpcEndpoint = 'https://polygon-mumbai-bor-rpc.publicnode.com';
break;
case 'AMOY_POLYGON_TEST':
rpcEndpoint = 'https://polygon-amoy-bor-rpc.publicnode.com';
break;
case 'MATIC_POLYGON':
rpcEndpoint = 'https://polygon-bor-rpc.publicnode.com';
break;
default:
throw new Error('Unsupported asset');
}
super(input, rpcEndpoint);
}
}
24 changes: 16 additions & 8 deletions apps/recovery-relay/lib/wallets/EVM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { JsonRpcProvider, formatEther, parseEther } from 'ethers';
import { EVMWallet as EVMBase, Input } from '@fireblocks/wallet-derivation';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';
import BigNumber from 'bignumber.js';

export class EVM extends EVMBase implements ConnectedWallet {
protected readonly provider: JsonRpcProvider;

protected weiBalance: bigint = BigInt(0);

constructor(input: Input, rpcEndpoint: string, chainId?: number) {
super(input);

Expand All @@ -15,8 +18,8 @@ export class EVM extends EVMBase implements ConnectedWallet {
}

public async getBalance() {
const wei = await this.provider.getBalance(this.address);
const balance = formatEther(wei);
this.weiBalance = await this.provider.getBalance(this.address);
const balance = formatEther(this.weiBalance);
const ethBalance = Number(balance);

console.info('Eth balance info', { ethBalance });
Expand All @@ -25,11 +28,11 @@ export class EVM extends EVMBase implements ConnectedWallet {
}

public async prepare(): Promise<AccountData> {
const balance = await this.getBalance();
const displayBalance = await this.getBalance();

if (balance === 0) {
if (displayBalance === 0) {
return {
balance,
balance: 0,
insufficientBalance: true,
};
}
Expand All @@ -44,20 +47,25 @@ export class EVM extends EVMBase implements ConnectedWallet {
}

const gas = gasPrice * 21000n;
const balance = new BigNumber(this.weiBalance.toString());

const adjustedBalance = parseEther(String(balance)) - gas;
const adjustedBalance = balance.minus(new BigNumber(gas.toString()));

if (adjustedBalance < 0) {
if (adjustedBalance.lt(new BigNumber(0))) {
this.relayLogger.error('Insufficient balance');
}

const chainId = (await this.provider.getNetwork()).chainId;

const extraParams = new Map();
extraParams.set(this.KEY_EVM_WEI_BALANCE, adjustedBalance.toString(16));

const preparedData = {
balance: Number(formatEther(adjustedBalance)),
balance: displayBalance,
nonce,
gasPrice,
chainId: parseInt(chainId.toString()),
extraParams,
};

this.relayLogger.logPreparedData('EVM', preparedData);
Expand Down
2 changes: 2 additions & 0 deletions apps/recovery-relay/lib/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const WalletClasses = {
ETH: Ethereum,
ETH_TEST3: Ethereum,
ETH_TEST5: Ethereum,
ETH_TEST6: Ethereum,
ETHW: EthereumPoW,
EVMOS,
GLMR: Moonbeam,
Expand All @@ -86,6 +87,7 @@ export const WalletClasses = {
ADA_TEST: Cardano,
MATIC_POLYGON: Matic,
MATIC_POLYGON_MUMBAI: Matic,
AMOY_POLYGON_TEST: Matic,
'ETH-OPT': Optimism,
'ETH-OPT_KOV': Optimism,
'ETH-AETH': Arbitrum,
Expand Down
1 change: 1 addition & 0 deletions apps/recovery-utility/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@terra-money/terra.js": "^3.1.10",
"algosdk": "2.5.0",
"archiver": "^5.3.1",
"bignumber.js": "^9.1.2",
"bitcoinjs-lib": "^6.1.3",
"bitcore-lib-doge": "^10.0.21",
"bitcore-lib-ltc": "^10.0.21",
Expand Down
9 changes: 6 additions & 3 deletions apps/recovery-utility/renderer/lib/wallets/EVM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ export class EVM extends EVMBase implements SigningWallet {

public async generateTx({
to,
amount,
// amount,
nonce,
gasPrice, // Should we use maxGasPrice? i.e. EIP1559.
chainId,
extraParams,
}: GenerateTxInput): Promise<TxPayload> {
if (!this.privateKey) {
throw new Error('No private key found');
}

const balanceHex = extraParams?.get(this.KEY_EVM_WEI_BALANCE);

this.utilityLogger.logSigningTx('EVM', {
from: this.address,
to,
nonce,
gasLimit: 21000,
gasPrice,
value: parseEther(`${amount}`),
value: BigInt(`0x${balanceHex}`),
chainId: chainId ? chainId : this.path.coinType === 1 ? 5 : 1,
});

Expand All @@ -35,7 +38,7 @@ export class EVM extends EVMBase implements SigningWallet {
nonce,
gasLimit: 21000,
gasPrice,
value: parseEther(`${amount}`),
value: BigInt(`0x${balanceHex}`),
chainId: chainId ? chainId : this.path.coinType === 1 ? 5 : 1,
});

Expand Down
1 change: 1 addition & 0 deletions apps/recovery-utility/renderer/lib/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const WalletClasses = {
ETH: EVM,
ETH_TEST3: EVM,
ETH_TEST5: EVM,
ETH_TEST6: EVM,
EOS,
EOS_TEST: EOS,
ATOM_COS: Cosmos,
Expand Down
2 changes: 2 additions & 0 deletions packages/asset-config/config/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const nativeAssetPatches: NativeAssetPatches = {
ETH: evm('etherscan.io', 'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'),
ETH_TEST3: evm('goerli.etherscan.io', 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'),
ETH_TEST5: evm('sepolia.etherscan.io', 'https://sepolia.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'),
ETH_TEST6: evm('holesky.etherscan.io', 'https://ethereum-holesky-rpc.publicnode.com'),
'ETH-AETH': evm('arbiscan.io'),
'ETH-AETH_RIN': evm('testnet.arbiscan.io'),
'ETH-OPT': evm('optimistic.etherscan.io'),
Expand Down Expand Up @@ -169,6 +170,7 @@ export const nativeAssetPatches: NativeAssetPatches = {
},
MATIC_POLYGON: evm('polygonscan.com', 'https://polygon-rpc.com/'),
MATIC_POLYGON_MUMBAI: evm('mumbai.polygonscan.com', 'https://polygon-mumbai.infura.io/v3/4458cf4d1689497b9a38b1d6bbf05e78'),
AMOY_POLYGON_TEST: evm('amoy.polygonscan.com', 'https://polygon-amoy-bor-rpc.publicnode.com'),
MOVR_MOVR: evm('moonriver.moonscan.io'),
NEAR: {
derive: true,
Expand Down
18 changes: 18 additions & 0 deletions packages/asset-config/data/globalAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11223,6 +11223,15 @@ export const globalAssets = [
protocol: 'ETH',
testnet: true,
},
{
id: 'ETH_TEST6',
symbol: 'ETH_TEST6',
name: 'Ethereum Test (Holesky)',
decimals: 18,
nativeAsset: 'ETH_TEST6',
protocol: 'ETH',
testnet: true,
},
{
id: 'ETH-AETH_RIN',
symbol: 'ETH_TEST2',
Expand Down Expand Up @@ -11455,6 +11464,15 @@ export const globalAssets = [
protocol: 'ETH',
testnet: true,
},
{
id: 'AMOY_POLYGON_TEST',
symbol: 'AMOY_POLYGON_TEST',
name: 'Matic Gas Token (Polygon Test Amoy)',
decimals: 18,
nativeAsset: 'AMOY_POLYGON_TEST',
protocol: 'ETH',
testnet: true,
},
{
id: 'MATIC_TEST',
symbol: 'MATIC_TEST',
Expand Down
2 changes: 2 additions & 0 deletions packages/wallet-derivation/wallets/chains/EVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export class EVMWallet extends ECDSAWallet {
protected getAddress(evmAddress?: string) {
return evmAddress as string;
}

protected readonly KEY_EVM_WEI_BALANCE = 'b';
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5620,7 +5620,7 @@ bignumber.js@^4.0.0:
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
integrity sha512-eJzYkFYy9L4JzXsbymsFn3p54D+llV27oTQ+ziJG7WFRheJcNZilgVXMG0LoZtlQSKBsJdWtLFqOD0u+U0jZKA==

bignumber.js@^9.0.0, bignumber.js@^9.0.1, bignumber.js@^9.1.0, bignumber.js@^9.1.1:
bignumber.js@^9.0.0, bignumber.js@^9.0.1, bignumber.js@^9.1.0, bignumber.js@^9.1.1, bignumber.js@^9.1.2:
version "9.1.2"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==
Expand Down