Skip to content

Commit

Permalink
TP-1781: update gas type to native (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
feenaIMX authored Nov 22, 2023
1 parent b3a1d9e commit 5ecbd09
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/internal/dex/sdk/src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
CoinAmount,
ERC20,
ExchangeModuleConfiguration,
Native,
Quote,
SecondaryFee,
TransactionResponse,
Expand Down Expand Up @@ -45,7 +46,7 @@ export class Exchange {

private chainId: number;

private nativeToken: Coin;
private nativeToken: Native;

private wrappedNativeToken: ERC20;

Expand Down
14 changes: 0 additions & 14 deletions packages/internal/dex/sdk/src/lib/nativeTokenService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/internal/dex/sdk/src/lib/nativeTokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Coin>): CoinAmount<ERC20> {
if (!canUnwrapToken(amount.token)) {
throw new Error(`cannot wrap non-native token: ${amount.token.address}`);
}
wrapAmount(amount: CoinAmount<Native>): CoinAmount<ERC20> {
return newAmount(amount.value, this.wrappedToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -137,7 +137,7 @@ export const getApproval = async (
provider: JsonRpcProvider,
ownerAddress: string,
preparedApproval: PreparedApproval,
gasPrice: CoinAmount<Coin> | null,
gasPrice: CoinAmount<Native> | null,
): Promise<TransactionDetails | null> => {
const approveTransaction = await getApproveTransaction(
provider,
Expand Down
9 changes: 5 additions & 4 deletions packages/internal/dex/sdk/src/lib/transactionUtils/gas.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CoinAmount<Coin> | null> => {
export const fetchGasPrice = async (provider: JsonRpcProvider, nativeToken: Native)
: Promise<CoinAmount<Native> | null> => {
const feeData = await provider.getFeeData().catch(() => null);
if (!feeData) return null;

Expand All @@ -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<Coin>, gasEstimate: BigNumber): CoinAmount<Coin> =>
export const calculateGasFee = (gasPrice: CoinAmount<Native>, gasEstimate: BigNumber): CoinAmount<Native> =>
// eslint-disable-next-line implicit-arrow-linebreak
newAmount(gasEstimate.mul(gasPrice.value), gasPrice.token);
4 changes: 2 additions & 2 deletions packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -446,7 +446,7 @@ export function getSwap(
deadline: number,
routerContractAddress: string,
secondaryFeesContractAddress: string,
gasPrice: CoinAmount<Coin> | null,
gasPrice: CoinAmount<Native> | null,
secondaryFees: SecondaryFee[],
): TransactionDetails {
const swapRecipient = getSwapRecipient(
Expand Down
8 changes: 4 additions & 4 deletions packages/internal/dex/sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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 = {
chainId: number;
rpcUrl: string;
contracts: ExchangeContracts;
commonRoutingTokens: ERC20[];
nativeToken: Coin;
nativeToken: Native;
wrappedNativeToken: ERC20;
};

Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit 5ecbd09

Please sign in to comment.