Skip to content

Commit

Permalink
refactor: share util method for fetching exchange rates
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Nov 22, 2024
1 parent f1b891e commit 7b21c13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
27 changes: 4 additions & 23 deletions ui/ducks/bridge/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { Hex } from '@metamask/utils';
import { getAddress } from 'ethers/lib/utils';
import { swapsSlice } from '../swaps/swaps';
import { SwapsTokenObject } from '../../../shared/constants/swaps';
import { SwapsEthToken } from '../../selectors';
import { fetchTokenExchangeRates } from '../../helpers/utils/util';
import {
QuoteMetadata,
QuoteResponse,
SortOrder,
} from '../../pages/bridge/types';
import { getTokenExchangeRate } from './utils';

export type BridgeState = {
toChainId: Hex | null;
Expand All @@ -35,30 +34,12 @@ const initialState: BridgeState = {

export const setSrcTokenExchangeRates = createAsyncThunk(
'bridge/setSrcTokenExchangeRates',
async (request: { chainId: Hex; tokenAddress: string; currency: string }) => {
const { chainId, tokenAddress, currency } = request;
const exchangeRates = await fetchTokenExchangeRates(
currency,
[tokenAddress],
chainId,
);
return exchangeRates?.[getAddress(tokenAddress)];
},
getTokenExchangeRate,
);

export const setDestTokenExchangeRates = createAsyncThunk(
'bridge/setDestTokenExchangeRates',
async (request: { chainId: Hex; tokenAddress: string; currency: string }) => {
const { chainId, tokenAddress, currency } = request;
const exchangeRates = await fetchTokenExchangeRates(
currency,
[tokenAddress],
chainId,
);
return {
toTokenExchangeRate: exchangeRates?.[getAddress(tokenAddress)],
};
},
getTokenExchangeRate,
);

const bridgeSlice = createSlice({
Expand Down Expand Up @@ -90,7 +71,7 @@ const bridgeSlice = createSlice({
},
extraReducers: (builder) => {
builder.addCase(setDestTokenExchangeRates.fulfilled, (state, action) => {
state.toTokenExchangeRate = action.payload.toTokenExchangeRate ?? null;
state.toTokenExchangeRate = action.payload ?? null;
});
builder.addCase(setSrcTokenExchangeRates.fulfilled, (state, action) => {
state.fromTokenExchangeRate = action.payload ?? null;
Expand Down
19 changes: 19 additions & 0 deletions ui/ducks/bridge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Hex } from '@metamask/utils';
import { BigNumber } from 'bignumber.js';
import { getAddress } from 'ethers/lib/utils';
import { decGWEIToHexWEI } from '../../../shared/modules/conversion.utils';
import { Numeric } from '../../../shared/modules/Numeric';
import { TxData } from '../../pages/bridge/types';
import { getTransaction1559GasFeeEstimates } from '../../pages/swaps/swaps.util';
import { fetchTokenExchangeRates } from '../../helpers/utils/util';

// We don't need to use gas multipliers here because the gasLimit from Bridge API already included it
export const getHexMaxGasLimit = (gasLimit: number) => {
Expand Down Expand Up @@ -45,3 +47,20 @@ export const getTxGasEstimates = async ({
maxPriorityFeePerGas: undefined,
};
};

export const getTokenExchangeRate = async (request: {
chainId: Hex;
tokenAddress: string;
currency: string;
}) => {
const { chainId, tokenAddress, currency } = request;
const exchangeRates = await fetchTokenExchangeRates(
currency,
[tokenAddress],
chainId,
);
return (
exchangeRates?.[tokenAddress.toLowerCase()] ??
exchangeRates?.[getAddress(tokenAddress)]
);
};

0 comments on commit 7b21c13

Please sign in to comment.