From 106b0409770acec1c24bc44517eb1f9f44afa3b9 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Mon, 8 May 2023 14:40:31 -0700 Subject: [PATCH] chore(utils): remove ethers deps from ui-toolkit (#3627) --- .../src/hooks/use-get-user-balances.ts | 8 ++--- .../hooks/use-refresh-associated-balances.ts | 10 ++++-- .../src/hooks/use-refresh-balances.ts | 18 ++++++---- .../governance/src/routes/redemption/hooks.ts | 5 +-- .../staking/associate/wallet-associate.tsx | 2 +- libs/react-helpers/.eslintrc.json | 4 ++- .../src/contracts/staking-bridge.ts | 3 +- .../src/contracts/token-vesting.ts | 9 +++-- libs/utils/.eslintrc.json | 4 ++- libs/utils/src/lib/format/number.ts | 9 ++--- libs/utils/src/lib/validate/common.spec.ts | 33 +++++++++++++++++++ libs/utils/src/lib/validate/common.ts | 5 ++- 12 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 libs/utils/src/lib/validate/common.spec.ts diff --git a/apps/governance/src/hooks/use-get-user-balances.ts b/apps/governance/src/hooks/use-get-user-balances.ts index d6456ab8f..6fc5c4f87 100644 --- a/apps/governance/src/hooks/use-get-user-balances.ts +++ b/apps/governance/src/hooks/use-get-user-balances.ts @@ -21,10 +21,10 @@ export const useGetUserBalances = (account: string | undefined) => { token.allowance(account, config.staking_bridge_contract.address), ]); - const balance = toBigNum(b, decimals); - const walletBalance = toBigNum(w, decimals); - const lien = toBigNum(stats.lien, decimals); - const allowance = toBigNum(a, decimals); + const balance = toBigNum(b.toString(), decimals); + const walletBalance = toBigNum(w.toString(), decimals); + const lien = toBigNum(stats.lien.toString(), decimals); + const allowance = toBigNum(a.toString(), decimals); return { balanceFormatted: balance, diff --git a/apps/governance/src/hooks/use-refresh-associated-balances.ts b/apps/governance/src/hooks/use-refresh-associated-balances.ts index f6ca9dfaf..f99441e1b 100644 --- a/apps/governance/src/hooks/use-refresh-associated-balances.ts +++ b/apps/governance/src/hooks/use-refresh-associated-balances.ts @@ -21,8 +21,14 @@ export function useRefreshAssociatedBalances() { ]); updateBalances({ - walletAssociatedBalance: toBigNum(walletAssociatedBalance, decimals), - vestingAssociatedBalance: toBigNum(vestingAssociatedBalance, decimals), + walletAssociatedBalance: toBigNum( + walletAssociatedBalance.toString(), + decimals + ), + vestingAssociatedBalance: toBigNum( + vestingAssociatedBalance.toString(), + decimals + ), }); }, [staking, vesting, updateBalances, decimals] diff --git a/apps/governance/src/hooks/use-refresh-balances.ts b/apps/governance/src/hooks/use-refresh-balances.ts index 24cfd9e7d..dd5031238 100644 --- a/apps/governance/src/hooks/use-refresh-balances.ts +++ b/apps/governance/src/hooks/use-refresh-balances.ts @@ -31,12 +31,18 @@ export const useRefreshBalances = (address: string) => { pubKey ? vesting.stake_balance(address, pubKey) : null, ]); - const balance = toBigNum(b, decimals); - const walletBalance = toBigNum(w, decimals); - const lien = toBigNum(stats.lien, decimals); - const allowance = toBigNum(a, decimals); - const walletAssociatedBalance = toBigNum(walletStakeBalance, decimals); - const vestingAssociatedBalance = toBigNum(vestingStakeBalance, decimals); + const balance = toBigNum(b.toString(), decimals); + const walletBalance = toBigNum(w.toString(), decimals); + const lien = toBigNum(stats.lien.toString(), decimals); + const allowance = toBigNum(a.toString(), decimals); + const walletAssociatedBalance = toBigNum( + walletStakeBalance ? walletStakeBalance.toString() : 0, + decimals + ); + const vestingAssociatedBalance = toBigNum( + vestingStakeBalance ? vestingStakeBalance.toString() : 0, + decimals + ); updateBalances({ balanceFormatted: balance, diff --git a/apps/governance/src/routes/redemption/hooks.ts b/apps/governance/src/routes/redemption/hooks.ts index 6d7638d7b..53deab81e 100644 --- a/apps/governance/src/routes/redemption/hooks.ts +++ b/apps/governance/src/routes/redemption/hooks.ts @@ -34,8 +34,9 @@ export const useUserTrancheBalances = (address: string | undefined) => { vesting.get_vested_for_tranche(address, tId), ]); - const total = toBigNum(t, decimals); - const vested = toBigNum(v, decimals); + // Convert t and v EthersBigNumbers to regular BigNumbers + const total = toBigNum(t.toString(), decimals); + const vested = toBigNum(v.toString(), decimals); return { id: tId, diff --git a/apps/governance/src/routes/staking/associate/wallet-associate.tsx b/apps/governance/src/routes/staking/associate/wallet-associate.tsx index 67c815620..b38909f3e 100644 --- a/apps/governance/src/routes/staking/associate/wallet-associate.tsx +++ b/apps/governance/src/routes/staking/associate/wallet-associate.tsx @@ -54,7 +54,7 @@ export const WalletAssociate = ({ address, ethereumConfig.staking_bridge_contract.address ); - const allowance = toBigNum(a, decimals); + const allowance = toBigNum(a.toString(), decimals); setAllowance(allowance); } }; diff --git a/libs/react-helpers/.eslintrc.json b/libs/react-helpers/.eslintrc.json index 95a79ce34..043434607 100644 --- a/libs/react-helpers/.eslintrc.json +++ b/libs/react-helpers/.eslintrc.json @@ -11,7 +11,9 @@ "@vegaprotocol/data-provider", "graphql", "graphql-tag", - "graphql-ws" + "graphql-ws", + "ethers", + "@ethersproject" ] } }, diff --git a/libs/smart-contracts/src/contracts/staking-bridge.ts b/libs/smart-contracts/src/contracts/staking-bridge.ts index cfd190768..a98c8c170 100644 --- a/libs/smart-contracts/src/contracts/staking-bridge.ts +++ b/libs/smart-contracts/src/contracts/staking-bridge.ts @@ -1,6 +1,7 @@ import { ethers } from 'ethers'; import abi from '../abis/staking_abi.json'; import { calcGasBuffer, prepend0x } from '../utils'; +import type { BigNumber as EthersBigNum } from 'ethers'; export class StakingBridge { public contract: ethers.Contract; @@ -51,7 +52,7 @@ export class StakingBridge { staking_token() { return this.contract.staking_token(); } - stake_balance(target: string, vegaPublicKey: string) { + stake_balance(target: string, vegaPublicKey: string): Promise { return this.contract.stake_balance(target, prepend0x(vegaPublicKey)); } total_staked() { diff --git a/libs/smart-contracts/src/contracts/token-vesting.ts b/libs/smart-contracts/src/contracts/token-vesting.ts index 43ad9d43c..b85afbbad 100644 --- a/libs/smart-contracts/src/contracts/token-vesting.ts +++ b/libs/smart-contracts/src/contracts/token-vesting.ts @@ -1,6 +1,7 @@ import { ethers } from 'ethers'; import abi from '../abis/vesting_abi.json'; import { calcGasBuffer, prepend0x } from '../utils'; +import type { BigNumber as EthersBigNum } from 'ethers'; export class TokenVesting { public contract: ethers.Contract; @@ -29,13 +30,15 @@ export class TokenVesting { gasLimit, }); } - stake_balance(address: string, vegaPublicKey: string) { + stake_balance(address: string, vegaPublicKey: string): Promise { return this.contract.stake_balance(address, prepend0x(vegaPublicKey)); } total_staked() { return this.contract.total_staked(); } - user_stats(address: string) { + user_stats( + address: string + ): Promise<{ lien: EthersBigNum; total_in_all_tranches: EthersBigNum }> { return this.contract.user_stats(address); } get_tranche_balance(address: string, trancheId: number) { @@ -44,7 +47,7 @@ export class TokenVesting { get_vested_for_tranche(address: string, trancheId: number) { return this.contract.get_vested_for_tranche(address, trancheId); } - user_total_all_tranches(address: string) { + user_total_all_tranches(address: string): Promise { return this.contract.user_total_all_tranches(address); } async withdraw_from_tranche(trancheId: number) { diff --git a/libs/utils/.eslintrc.json b/libs/utils/.eslintrc.json index 340d6c422..1e8f0f5f3 100644 --- a/libs/utils/.eslintrc.json +++ b/libs/utils/.eslintrc.json @@ -11,7 +11,9 @@ "@vegaprotocol/data-provider", "graphql", "graphql-tag", - "graphql-ws" + "graphql-ws", + "ethers", + "@ethersproject" ] } }, diff --git a/libs/utils/src/lib/format/number.ts b/libs/utils/src/lib/format/number.ts index ddcf0b9b8..06b920e84 100644 --- a/libs/utils/src/lib/format/number.ts +++ b/libs/utils/src/lib/format/number.ts @@ -1,5 +1,4 @@ import { BigNumber } from 'bignumber.js'; -import { BigNumber as EthersBigNumber } from 'ethers'; import isNil from 'lodash/isNil'; import memoize from 'lodash/memoize'; @@ -15,16 +14,14 @@ export function toDecimal(numberOfDecimals: number) { } export function toBigNum( - rawValue: string | number | EthersBigNumber, + rawValue: string | number, decimals: number ): BigNumber { - return new BigNumber( - rawValue instanceof EthersBigNumber ? rawValue.toString() : rawValue || 0 - ).dividedBy(Math.pow(10, decimals)); + return new BigNumber(rawValue || 0).dividedBy(Math.pow(10, decimals)); } export function addDecimal( - value: string | number | EthersBigNumber, + value: string | number, decimals: number, decimalPrecision = decimals ): string { diff --git a/libs/utils/src/lib/validate/common.spec.ts b/libs/utils/src/lib/validate/common.spec.ts new file mode 100644 index 000000000..87a6bc5c8 --- /dev/null +++ b/libs/utils/src/lib/validate/common.spec.ts @@ -0,0 +1,33 @@ +import { ethereumAddress, vegaPublicKey } from './common'; + +it('ethereumAddress', () => { + const errorMessage = 'Invalid Ethereum address'; + + const validAddress = '0x72c22822A19D20DE7e426fB84aa047399Ddd8853'; + expect(ethereumAddress(validAddress)).toEqual(true); + + const invalidChars = '0xzzc22822A19D20DE7e426fB84aa047399Ddd8853'; + expect(ethereumAddress(invalidChars)).toEqual(errorMessage); + + const tooManyChars = '0x72c22822A19D20DE7e426fB84aa047399Ddd88531111111'; + expect(ethereumAddress(tooManyChars)).toEqual(errorMessage); + + const no0x = '1x72c22822A19D20DE7e426fB84aa047399Ddd8853'; + expect(ethereumAddress(no0x)).toEqual(errorMessage); +}); + +it('vegaPublicKey', () => { + const errorMessage = 'Invalid Vega key'; + + const validKey = + '70d14a321e02e71992fd115563df765000ccc4775cbe71a0e2f9ff5a3b9dc680'; + expect(vegaPublicKey(validKey)).toEqual(true); + + const invalidChars = + 'zzz14a321e02e71992fd115563df765000ccc4775cbe71a0e2f9ff5a3b9dc680'; + expect(vegaPublicKey(invalidChars)).toEqual(errorMessage); + + const tooManyChars = + '70d14a321e02e71992fd115563df765000ccc4775cbe71a0e2f9ff5a3b9dc680111111'; + expect(vegaPublicKey(tooManyChars)).toEqual(errorMessage); +}); diff --git a/libs/utils/src/lib/validate/common.ts b/libs/utils/src/lib/validate/common.ts index 434fd5e37..9590f4cb3 100644 --- a/libs/utils/src/lib/validate/common.ts +++ b/libs/utils/src/lib/validate/common.ts @@ -1,5 +1,4 @@ import BigNumber from 'bignumber.js'; -import { ethers } from 'ethers'; import { t } from '@vegaprotocol/i18n'; export const required = (value: string) => { @@ -10,14 +9,14 @@ export const required = (value: string) => { }; export const ethereumAddress = (value: string) => { - if (!ethers.utils.isAddress(value)) { + if (!/^0x[0-9a-fA-F]{40}$/i.test(value)) { return t('Invalid Ethereum address'); } return true; }; export const vegaPublicKey = (value: string) => { - if (value.length !== 64 || !/^[A-Fa-f0-9]*$/i.test(value)) { + if (!/^[A-Fa-f0-9]{64}$/i.test(value)) { return t('Invalid Vega key'); } return true;