Skip to content

Commit

Permalink
feat: bump ethers to v6 (#6224)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored Feb 27, 2024
1 parent 3f2bfe8 commit 4c92cde
Show file tree
Hide file tree
Showing 80 changed files with 431 additions and 379 deletions.
114 changes: 48 additions & 66 deletions __mocks__/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,54 @@
import {
BigNumber,
Bytes,
BytesLike,
ethers,
providers,
Signature,
Signer,
UnsignedTransaction,
utils,
} from 'ethers'
import { ethers } from 'ethers'
import { vi } from 'vitest'

const Contract = vi.fn().mockImplementation(address => ({
decimals: () => {
switch (address as string) {
case '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48':
return 6
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 18
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 18
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 18
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
name: () => {
switch (address as string) {
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 'FOX'
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 'Uniswap V2'
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 'Wrapped Ether'
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
symbol: () => {
switch (address as string) {
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 'FOX'
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 'UNI-V2'
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 'WETH'
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
}))

const JsonRpcProvider = vi.fn()

const ethersMock = {
...ethers,
providers: {
JsonRpcProvider: vi.fn(),
StaticJsonRpcProvider: vi.fn(),
},
Contract: vi.fn().mockImplementation(address => ({
decimals: () => {
switch (address as string) {
case '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48':
return 6
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 18
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 18
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 18
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
name: () => {
switch (address as string) {
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 'FOX'
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 'Uniswap V2'
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 'Wrapped Ether'
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
symbol: () => {
switch (address as string) {
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d':
return 'FOX'
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c':
return 'UNI-V2'
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
return 'WETH'
default:
throw new Error(`no decimals mock for address: ${address}`)
}
},
})),
JsonRpcProvider,
Contract,
}

export {
ethersMock as ethers,
BigNumber,
Bytes,
BytesLike,
Signature,
Signer,
UnsignedTransaction,
providers,
utils,
}
export * from 'ethers'
export { ethersMock as ethers, Contract, JsonRpcProvider }
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
"envalid": "^7.3.1",
"eslint-plugin-react-memo": "^0.0.3",
"eth-url-parser": "^1.0.4",
"ethers": "^5.7.2",
"ethers": "^6.11.1",
"ethers5": "npm:[email protected]",
"framer-motion": "^11.0.3",
"friendly-challenge": "0.9.2",
"grapheme-splitter": "^1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/caip/src/adapters/yearn/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Token, Vault } from '@yfi/sdk'
import { Yearn } from '@yfi/sdk'
import { ethers } from 'ethers'
import { ethers } from 'ethers5'
import fs from 'fs'
import toLower from 'lodash/toLower'
import uniqBy from 'lodash/uniqBy'
Expand Down
6 changes: 3 additions & 3 deletions packages/chain-adapters/src/evm/EvmBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { BIP44Params } from '@shapeshiftoss/types'
import { KnownChainIds } from '@shapeshiftoss/types'
import type * as unchained from '@shapeshiftoss/unchained-client'
import BigNumber from 'bignumber.js'
import { utils } from 'ethers'
import { isAddress, toHex } from 'viem'
import { numberToHex } from 'web3-utils'

import type { ChainAdapter as IChainAdapter } from '../api'
Expand Down Expand Up @@ -247,7 +247,7 @@ export abstract class EvmBaseAdapter<T extends EvmChainId> implements IChainAdap
}[this.chainId]

await wallet.ethSwitchChain({
chainId: utils.hexValue(adapterChainReference),
chainId: toHex(adapterChainReference),
chainName: this.getDisplayName(),
nativeCurrency: {
name: targetNetwork.name,
Expand Down Expand Up @@ -538,7 +538,7 @@ export abstract class EvmBaseAdapter<T extends EvmChainId> implements IChainAdap

// eslint-disable-next-line require-await
async validateAddress(address: string): Promise<ValidAddressResult> {
const isValidAddress = utils.isAddress(address)
const isValidAddress = isAddress(address)
if (isValidAddress) return { valid: true, result: ValidAddressResultType.Valid }
return { valid: false, result: ValidAddressResultType.Invalid }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Test GnosisChainAdapter
* @group unit
*/
import { AddressZero } from '@ethersproject/constants'
import { ASSET_REFERENCE, fromChainId, gnosisAssetId, gnosisChainId } from '@shapeshiftoss/caip'
import type { ETHSignMessage, ETHSignTx, ETHWallet } from '@shapeshiftoss/hdwallet-core'
import type { NativeAdapterArgs } from '@shapeshiftoss/hdwallet-native'
Expand All @@ -13,6 +12,7 @@ import type { BIP44Params } from '@shapeshiftoss/types'
import { KnownChainIds } from '@shapeshiftoss/types'
import type * as unchained from '@shapeshiftoss/unchained-client'
import { merge } from 'lodash'
import { zeroAddress } from 'viem'
import { describe, expect, it, vi } from 'vitest'
import { numberToHex } from 'web3-utils'

Expand Down Expand Up @@ -134,7 +134,7 @@ describe('GnosisChainAdapter', () => {
to: '0x642F4Bda144C63f6DC47EE0fDfbac0a193e2eDb7',
value: '123',
chainSpecific: {
from: AddressZero,
from: zeroAddress,
data: '0x',
},
}
Expand Down Expand Up @@ -460,7 +460,7 @@ describe('GnosisChainAdapter', () => {
const adapter = new gnosis.ChainAdapter(args)

const wallet = await getWallet()
wallet.ethGetAddress = async () => await Promise.resolve(AddressZero)
wallet.ethGetAddress = async () => await Promise.resolve(zeroAddress)

const tx = {
wallet,
Expand Down Expand Up @@ -563,7 +563,7 @@ describe('GnosisChainAdapter', () => {
const tx = {
wallet: await getWallet(),
accountNumber,
to: AddressZero,
to: zeroAddress,
value,
chainSpecific: makeChainSpecific({ contractAddress }),
} as unknown as BuildSendTxInput<KnownChainIds.GnosisMainnet>
Expand Down
3 changes: 0 additions & 3 deletions packages/chain-adapters/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AssetNamespace, ChainId } from '@shapeshiftoss/caip'
import { CHAIN_NAMESPACE, CHAIN_REFERENCE, fromChainId } from '@shapeshiftoss/caip'
import { BigNumber } from 'ethers'

export * from './bignumber'
export * from './bip44'
Expand Down Expand Up @@ -70,5 +69,3 @@ export const chainIdToChainLabel = (chainId: ChainId): string => {
throw new Error(`chainNamespace ${chainNamespace} not supported.`)
}
}

export const convertNumberToHex = (value: string): string => BigNumber.from(value).toHexString()
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

const bep20: JsonFragment[] = [
const bep20: InterfaceAbi = [
{
anonymous: false,
inputs: [
Expand Down
13 changes: 6 additions & 7 deletions packages/unchained-client/src/evm/bnbsmartchain/parser/bep20.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ChainId } from '@shapeshiftoss/caip'
import { toAssetId } from '@shapeshiftoss/caip'
import type { BigNumber } from 'ethers'
import { ethers } from 'ethers'

import type { BaseTxMetadata } from '../../../types'
Expand All @@ -16,17 +15,17 @@ export interface TxMetadata extends BaseTxMetadata {

interface ParserArgs {
chainId: ChainId
provider: ethers.providers.StaticJsonRpcProvider
provider: ethers.JsonRpcProvider
}

export class Parser<T extends Tx> implements SubParser<T> {
provider: ethers.providers.StaticJsonRpcProvider
provider: ethers.JsonRpcProvider

readonly chainId: ChainId
readonly abiInterface = new ethers.utils.Interface(bep20)
readonly abiInterface = new ethers.Interface(bep20)

readonly supportedFunctions = {
approveSigHash: this.abiInterface.getSighash('approve'),
approveSigHash: this.abiInterface.getFunction('approve')!.selector,
}

constructor(args: ParserArgs) {
Expand Down Expand Up @@ -58,9 +57,9 @@ export class Parser<T extends Tx> implements SubParser<T> {

switch (txSigHash) {
case this.supportedFunctions.approveSigHash: {
const amount = decoded.args.amount as BigNumber
const amount = decoded.args.amount as BigInt
const value = amount.toString()
if (amount.isZero()) {
if (amount === 0n) {
return await Promise.resolve({ data: { ...data, method: 'revoke', value } })
}
return await Promise.resolve({ data: { ...data, value } })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const FOXY_STAKING_ABI: JsonFragment[] = [
export const FOXY_STAKING_ABI: InterfaceAbi = [
{
inputs: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const SHAPESHIFT_ROUTER_ABI: JsonFragment[] = [
export const SHAPESHIFT_ROUTER_ABI: InterfaceAbi = [
{
inputs: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const THOR_AVALANCHE_ABI: JsonFragment[] = [
export const THOR_AVALANCHE_ABI: InterfaceAbi = [
{ inputs: [], stateMutability: 'nonpayable', type: 'constructor' },
{
anonymous: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const THOR_ETHEREUM_ABI: JsonFragment[] = [
export const THOR_ETHEREUM_ABI: InterfaceAbi = [
{ inputs: [], stateMutability: 'nonpayable', type: 'constructor' },
{
anonymous: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const UNIV2_ABI: JsonFragment[] = [
export const UNIV2_ABI: InterfaceAbi = [
{
inputs: [
{ internalType: 'address', name: '_factory', type: 'address' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const UNIV2_STAKING_REWARDS_ABI: JsonFragment[] = [
export const UNIV2_STAKING_REWARDS_ABI: InterfaceAbi = [
{
inputs: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/unchained-client/src/evm/ethereum/parser/abi/weth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const WETH_ABI: JsonFragment[] = [
export const WETH_ABI: InterfaceAbi = [
{
constant: true,
inputs: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonFragment } from '@ethersproject/abi/lib/fragments'
import type { InterfaceAbi } from 'ethers'

export const YEARN_VAULT_ABI: JsonFragment[] = [
export const YEARN_VAULT_ABI: InterfaceAbi = [
{
name: 'Transfer',
inputs: [
Expand Down
10 changes: 5 additions & 5 deletions packages/unchained-client/src/evm/ethereum/parser/foxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export interface TxMetadata extends BaseTxMetadata {
}

export class Parser implements SubParser<Tx> {
readonly abiInterface = new ethers.utils.Interface(FOXY_STAKING_ABI)
readonly abiInterface = new ethers.Interface(FOXY_STAKING_ABI)

readonly supportedFunctions = {
stakeSigHash: this.abiInterface.getSighash('stake(uint256,address)'),
unstakeSigHash: this.abiInterface.getSighash('unstake'),
instantUnstakeSigHash: this.abiInterface.getSighash('instantUnstake'),
claimWithdrawSigHash: this.abiInterface.getSighash('claimWithdraw'),
stakeSigHash: this.abiInterface.getFunction('stake(uint256,address)')!.selector,
unstakeSigHash: this.abiInterface.getFunction('unstake')!.selector,
instantUnstakeSigHash: this.abiInterface.getFunction('instantUnstake')!.selector,
claimWithdrawSigHash: this.abiInterface.getFunction('claimWithdraw')!.selector,
}

async parse(tx: Tx): Promise<TxSpecific | undefined> {
Expand Down
Loading

0 comments on commit 4c92cde

Please sign in to comment.