From 5ecbd092e2bb18e042e5faff72850db0b3f9b938 Mon Sep 17 00:00:00 2001 From: Feena Tian <107083573+feenaIMX@users.noreply.github.com> Date: Thu, 23 Nov 2023 10:02:01 +1100 Subject: [PATCH] TP-1781: update gas type to native (#1198) --- packages/internal/dex/sdk/src/exchange.ts | 3 ++- .../dex/sdk/src/lib/nativeTokenService.test.ts | 14 -------------- .../internal/dex/sdk/src/lib/nativeTokenService.ts | 7 ++----- .../dex/sdk/src/lib/transactionUtils/approval.ts | 4 ++-- .../dex/sdk/src/lib/transactionUtils/gas.ts | 9 +++++---- .../dex/sdk/src/lib/transactionUtils/swap.ts | 4 ++-- packages/internal/dex/sdk/src/types/index.ts | 8 ++++---- 7 files changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/internal/dex/sdk/src/exchange.ts b/packages/internal/dex/sdk/src/exchange.ts index 6632f78ed9..321c463478 100644 --- a/packages/internal/dex/sdk/src/exchange.ts +++ b/packages/internal/dex/sdk/src/exchange.ts @@ -16,6 +16,7 @@ import { CoinAmount, ERC20, ExchangeModuleConfiguration, + Native, Quote, SecondaryFee, TransactionResponse, @@ -45,7 +46,7 @@ export class Exchange { private chainId: number; - private nativeToken: Coin; + private nativeToken: Native; private wrappedNativeToken: ERC20; diff --git a/packages/internal/dex/sdk/src/lib/nativeTokenService.test.ts b/packages/internal/dex/sdk/src/lib/nativeTokenService.test.ts index 04e4cca2e8..bfa7f11090 100644 --- a/packages/internal/dex/sdk/src/lib/nativeTokenService.test.ts +++ b/packages/internal/dex/sdk/src/lib/nativeTokenService.test.ts @@ -16,20 +16,6 @@ describe('NativeTokenService', () => { expectERC20(wrappedEquivalent.token, nativeTokenService.wrappedToken.address); expect(formatAmount(wrappedEquivalent)).toEqual('1.0'); }); - - it('throws an error if the token is not a native amount', () => { - const erc20Amount = newAmountFromString('1', FUN_TEST_TOKEN); - expect(() => nativeTokenService.wrapAmount(erc20Amount)).toThrowError( - 'cannot wrap non-native token: 0xCc7bb2D219A0FC08033E130629C2B854b7bA9195', - ); - }); - - it('throws an error if the token is a wrapped native amount', () => { - const erc20Amount = newAmountFromString('1', nativeTokenService.wrappedToken); - expect(() => nativeTokenService.wrapAmount(erc20Amount)).toThrowError( - 'cannot wrap non-native token: 0xAf7cf5D4Af0BFAa85d384d42b8D410762Ccbce69', - ); - }); }); describe('unwrapAmount', () => { diff --git a/packages/internal/dex/sdk/src/lib/nativeTokenService.ts b/packages/internal/dex/sdk/src/lib/nativeTokenService.ts index f58eb7606e..92f9e69946 100644 --- a/packages/internal/dex/sdk/src/lib/nativeTokenService.ts +++ b/packages/internal/dex/sdk/src/lib/nativeTokenService.ts @@ -4,12 +4,9 @@ import { newAmount } from './utils'; export const canUnwrapToken = (token: Coin): token is Native => token.type === 'native'; export class NativeTokenService { - constructor(readonly nativeToken: Coin, readonly wrappedToken: ERC20) {} + constructor(readonly nativeToken: Native, readonly wrappedToken: ERC20) {} - wrapAmount(amount: CoinAmount): CoinAmount { - if (!canUnwrapToken(amount.token)) { - throw new Error(`cannot wrap non-native token: ${amount.token.address}`); - } + wrapAmount(amount: CoinAmount): CoinAmount { return newAmount(amount.value, this.wrappedToken); } diff --git a/packages/internal/dex/sdk/src/lib/transactionUtils/approval.ts b/packages/internal/dex/sdk/src/lib/transactionUtils/approval.ts index 75eaa55a76..779fd721d2 100644 --- a/packages/internal/dex/sdk/src/lib/transactionUtils/approval.ts +++ b/packages/internal/dex/sdk/src/lib/transactionUtils/approval.ts @@ -5,7 +5,7 @@ import { ApproveError } from 'errors'; import { ethers } from 'ethers'; import { TradeType } from '@uniswap/sdk-core'; import { isERC20Amount, toPublicAmount } from 'lib/utils'; -import { CoinAmount, Coin, ERC20 } from 'types'; +import { CoinAmount, Coin, ERC20, Native } from 'types'; import { SecondaryFee, TransactionDetails } from '../../types'; import { calculateGasFee } from './gas'; @@ -137,7 +137,7 @@ export const getApproval = async ( provider: JsonRpcProvider, ownerAddress: string, preparedApproval: PreparedApproval, - gasPrice: CoinAmount | null, + gasPrice: CoinAmount | null, ): Promise => { const approveTransaction = await getApproveTransaction( provider, diff --git a/packages/internal/dex/sdk/src/lib/transactionUtils/gas.ts b/packages/internal/dex/sdk/src/lib/transactionUtils/gas.ts index 51ae87ee80..6df643d0aa 100644 --- a/packages/internal/dex/sdk/src/lib/transactionUtils/gas.ts +++ b/packages/internal/dex/sdk/src/lib/transactionUtils/gas.ts @@ -1,7 +1,7 @@ import { BigNumber } from 'ethers'; import { JsonRpcProvider, FeeData } from '@ethersproject/providers'; import { newAmount } from 'lib'; -import { CoinAmount, Coin } from 'types'; +import { CoinAmount, Native } from 'types'; type EIP1559FeeData = { maxFeePerGas: BigNumber; @@ -24,11 +24,12 @@ export const doesChainSupportEIP1559 = (fee: FeeData): fee is EIP1559FeeData => /** * Fetch the current gas price estimate. Supports both EIP-1559 and non-EIP1559 chains * @param {JsonRpcProvider} provider - The JSON RPC provider used to fetch fee data - * @param {Coin} nativeToken - The native token of the chain. Gas prices will be denominated in this token + * @param {Native} nativeToken - The native token of the chain. Gas prices will be denominated in this token * @returns {Amount | null} - The gas price in the smallest denomination of the chain's currency, * or null if no gas price is available */ -export const fetchGasPrice = async (provider: JsonRpcProvider, nativeToken: Coin): Promise | null> => { +export const fetchGasPrice = async (provider: JsonRpcProvider, nativeToken: Native) +: Promise | null> => { const feeData = await provider.getFeeData().catch(() => null); if (!feeData) return null; @@ -46,6 +47,6 @@ export const fetchGasPrice = async (provider: JsonRpcProvider, nativeToken: Coin * @param {BigNumber} gasEstimate - The total gas units that will be used for the transaction * @returns - The cost of the transaction in the gas token's smallest denomination (e.g. WEI) */ -export const calculateGasFee = (gasPrice: CoinAmount, gasEstimate: BigNumber): CoinAmount => +export const calculateGasFee = (gasPrice: CoinAmount, gasEstimate: BigNumber): CoinAmount => // eslint-disable-next-line implicit-arrow-linebreak newAmount(gasEstimate.mul(gasPrice.value), gasPrice.token); diff --git a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts index c64e80a9d9..bcece56934 100644 --- a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts +++ b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts @@ -7,7 +7,7 @@ import { Fees } from 'lib/fees'; import { isNative, toCurrencyAmount, toPublicAmount } from 'lib/utils'; import { QuoteResult } from 'lib/getQuotesForRoutes'; import { NativeTokenService, canUnwrapToken } from 'lib/nativeTokenService'; -import { Coin, CoinAmount } from 'types'; +import { Coin, CoinAmount, Native } from 'types'; import { Interface } from 'ethers/lib/utils'; import { SecondaryFee, TransactionDetails } from '../../types'; import { calculateGasFee } from './gas'; @@ -446,7 +446,7 @@ export function getSwap( deadline: number, routerContractAddress: string, secondaryFeesContractAddress: string, - gasPrice: CoinAmount | null, + gasPrice: CoinAmount | null, secondaryFees: SecondaryFee[], ): TransactionDetails { const swapRecipient = getSwapRecipient( diff --git a/packages/internal/dex/sdk/src/types/index.ts b/packages/internal/dex/sdk/src/types/index.ts index 8717a5ed3d..497bd88574 100644 --- a/packages/internal/dex/sdk/src/types/index.ts +++ b/packages/internal/dex/sdk/src/types/index.ts @@ -15,7 +15,7 @@ export type ExchangeContracts = { * @property {string} rpcUrl - The RPC URL for the chain * @property {@link ExchangeContracts} contracts - The DEX contract addresses * @property {@link ERC20[]} commonRoutingTokens - The tokens used to find available pools for a swap - * @property {@link Coin} nativeToken - The native token of the chain + * @property {@link Native} nativeToken - The native token of the chain * @property {@link ERC20} wrappedNativeToken - The wrapped native token of the chain */ export type Chain = { @@ -23,7 +23,7 @@ export type Chain = { rpcUrl: string; contracts: ExchangeContracts; commonRoutingTokens: ERC20[]; - nativeToken: Coin; + nativeToken: Native; wrappedNativeToken: ERC20; }; @@ -167,14 +167,14 @@ export type Amount = { * @property {string} rpcURL - The RPC URL for the chain * @property {ExchangeContracts} exchangeContracts - The DEX contract addresses * @property {ERC20[]} commonRoutingTokens - The tokens used to find available pools for a swap - * @property {Coin} nativeToken - The native token of the chain + * @property {Native} nativeToken - The native token of the chain * @property {ERC20} wrappedNativeToken - The wrapped native token of the chain */ export type ExchangeOverrides = { rpcURL: string; exchangeContracts: ExchangeContracts; commonRoutingTokens: ERC20[]; - nativeToken: Coin; + nativeToken: Native; wrappedNativeToken: ERC20; };