Skip to content

Commit

Permalink
fix(@fireblocks/recovery-utility): 🐛 fixed incorrect floating point d…
Browse files Browse the repository at this point in the history
…ata transfer for evm txs
  • Loading branch information
a0ngo committed Apr 24, 2024
1 parent e30e433 commit e884f76
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
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
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
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
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

0 comments on commit e884f76

Please sign in to comment.