Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache new asset price points #3408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions background/redux-slices/0x-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,16 @@ export const fetchSwapPrice = createBackgroundAsyncThunk(
quoteRequest.assets.buyAsset,
assets,
quote.buyAmount,
quoteRequest.network
quoteRequest.network,
dispatch
),
sellCurrencyAmount: await checkCurrencyAmount(
Number(quote.sellTokenToEthRate),
quoteRequest.assets.sellAsset,
assets,
quote.sellAmount,
quoteRequest.network
quoteRequest.network,
dispatch
),
}

Expand Down
88 changes: 40 additions & 48 deletions background/redux-slices/utils/0x-swap-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AnyAction, ThunkDispatch } from "@reduxjs/toolkit"
import { PricePoint, SwappableAsset } from "../../assets"
import { USD } from "../../constants"
import {
Expand All @@ -7,11 +8,13 @@ import {
} from "../../lib/fixed-point"
import { getPricePoint, getTokenPrices } from "../../lib/prices"
import { EVMNetwork } from "../../networks"
import { AssetsState, selectAssetPricePoint, SingleAssetState } from "../assets"
import {
AssetMainCurrencyAmount,
enrichAssetAmountWithMainCurrencyValues,
} from "./asset-utils"
AssetsState,
newPricePoint,
selectAssetPricePoint,
SingleAssetState,
} from "../assets"
import { enrichAssetAmountWithMainCurrencyValues } from "./asset-utils"
import { hardcodedMainCurrencySymbol } from "./constants"

type SwapAssets = {
Expand Down Expand Up @@ -69,19 +72,29 @@ export async function getAssetPricePoint(
: getPricePoint(asset, unitPricePoint)
}

export async function getAssetAmount(
assets: AssetsState,
export async function checkCurrencyAmount(
tokenToEthRate: number,
asset: SwappableAsset,
assets: AssetsState,
amount: string,
network: EVMNetwork
): Promise<
| ({
asset: SwappableAsset
amount: bigint
} & AssetMainCurrencyAmount)
| undefined
> {
const fixedPointAmount = parseToFixedPointNumber(amount.toString())
network: EVMNetwork,
dispatch: ThunkDispatch<unknown, unknown, AnyAction>
): Promise<string | undefined> {
/**
* If the tokenToEthRate of a is less than 0.1
* we will probably not get information about the price of the asset.
* The goal is to reduce the number of price requests sent to CoinGecko.
*/
if (tokenToEthRate < 0.1) {
return undefined
}

const fixedPointAmount = parseToFixedPointNumber(
fixedPointNumberToString({
amount: BigInt(amount),
decimals: asset.decimals,
}).toString()
)
if (typeof fixedPointAmount === "undefined") {
return undefined
}
Expand All @@ -90,48 +103,27 @@ export async function getAssetAmount(
asset.decimals
)

const assetPricePoint = selectAssetPricePoint(
let assetPricePoint = selectAssetPricePoint(
assets,
asset,
hardcodedMainCurrencySymbol
)

if (!assetPricePoint) {
const newAssetPricePoint = await getAssetPricePoint(asset, assets, network)

if (newAssetPricePoint) {
dispatch(newPricePoint(newAssetPricePoint))
assetPricePoint = newAssetPricePoint
}
}

return enrichAssetAmountWithMainCurrencyValues(
{
asset,
amount: decimalMatched.amount,
},
assetPricePoint ?? (await getAssetPricePoint(asset, assets, network)),
assetPricePoint,
2
)
}

/**
* If the tokenToEthRate of a is less than 0.1
* we will probably not get information about the price of the asset.
* The goal is to reduce the number of price requests sent to CoinGecko.
*/
export async function checkCurrencyAmount(
tokenToEthRate: number,
asset: SwappableAsset,
assets: AssetsState,
amount: string,
network: EVMNetwork
): Promise<string | undefined> {
const currencyAmount =
tokenToEthRate >= 0.1
? (
await getAssetAmount(
assets,
asset,
fixedPointNumberToString({
amount: BigInt(amount),
decimals: asset.decimals,
}),
network
)
)?.localizedMainCurrencyAmount
: undefined

return currencyAmount
)?.localizedMainCurrencyAmount
}