Skip to content
Open
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
39 changes: 22 additions & 17 deletions examples/swap-tokens/config/assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { chains } from 'chain-registry';
import { Asset } from '@chain-registry/types';
import { CoinDenom } from '@chain-registry/utils';
import { CoinDenom } from '@osmonauts/math/dist/types';
import { assets, asset_list } from '@chain-registry/osmosis';
import { CoinGeckoId } from '@/hooks';
import { defaultChainName } from '@/config';
Expand All @@ -10,24 +10,29 @@ export type Assets = Asset[] & {
CoinGeckoIds: CoinGeckoId[];
CoinDenomToAsset: Record<CoinDenom, Asset>;
CoinGeckoIdToAsset: Record<CoinGeckoId, Asset>;
}
};

export const OsmosisAssets = [
...assets.assets,
...asset_list.assets
].filter(({ type_asset }) => type_asset !== 'ics20') as Assets;
export const OsmosisAssets = [...assets.assets, ...asset_list.assets].filter(
({ type_asset }) => type_asset !== 'ics20'
) as Assets;

OsmosisAssets.WithCoinGeckoId = OsmosisAssets
.filter(({ coingecko_id }) => Boolean(coingecko_id));
OsmosisAssets.WithCoinGeckoId = OsmosisAssets.filter(({ coingecko_id }) =>
Boolean(coingecko_id)
);

OsmosisAssets.CoinGeckoIds = OsmosisAssets.WithCoinGeckoId
.map(({ coingecko_id }) => coingecko_id) as CoinGeckoId[];
OsmosisAssets.CoinGeckoIds = OsmosisAssets.WithCoinGeckoId.map(
({ coingecko_id }) => coingecko_id
) as CoinGeckoId[];

OsmosisAssets.CoinGeckoIdToAsset = OsmosisAssets.WithCoinGeckoId
.reduce((cache, asset) => ({ ...cache, [asset.coingecko_id!]: asset }), {})
OsmosisAssets.CoinGeckoIdToAsset = OsmosisAssets.WithCoinGeckoId.reduce(
(cache, asset) => ({ ...cache, [asset.coingecko_id!]: asset }),
{}
);

OsmosisAssets.CoinDenomToAsset = OsmosisAssets
.reduce((cache, asset) => ({ ...cache, [asset.base]: asset }), {});
OsmosisAssets.CoinDenomToAsset = OsmosisAssets.reduce(
(cache, asset) => ({ ...cache, [asset.base]: asset }),
{}
);

function getChainByDenom(denom: CoinDenom) {
let chainName = '';
Expand All @@ -39,10 +44,10 @@ function getChainByDenom(denom: CoinDenom) {
}
return chainName
? chains.find(({ chain_name }) => chain_name === chainName)
: null
: null;
}

export const Osmosis = {
getChainByDenom,
Assets: OsmosisAssets
}
Assets: OsmosisAssets,
};
29 changes: 18 additions & 11 deletions examples/swap-tokens/hooks/usePrices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery, UseQueryResult } from "@tanstack/react-query";
import { PriceHash } from '@chain-registry/utils';
import { Osmosis } from "@/config";
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import { PriceHash } from '@osmonauts/math/dist/types';
import { Osmosis } from '@/config';

export type CoinGeckoId = string;
export type CoinGeckoPrice = { usd: number };
Expand All @@ -18,26 +18,33 @@ export type CoinGeckoResponse = Record<CoinGeckoId, CoinGeckoPrice>;
// }
// }

async function fetchPrices(coinGeckoIds: CoinGeckoId[]): Promise<CoinGeckoResponse> {
async function fetchPrices(
coinGeckoIds: CoinGeckoId[]
): Promise<CoinGeckoResponse> {
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoIds.join()}&vs_currencies=usd`;
return await fetch(url).then((res) => res.json());
}

function toPriceHash(prices: CoinGeckoResponse = {}): PriceHash {
return Object.entries(prices).reduce((result, [id, price]) => ({
...result,
[Osmosis.Assets.CoinGeckoIdToAsset[id]!.base]: price.usd,
}), {});
return Object.entries(prices).reduce(
(result, [id, price]) => ({
...result,
[Osmosis.Assets.CoinGeckoIdToAsset[id]!.base]: price.usd,
}),
{}
);
}

export function usePrices() {
const query = useQuery({
queryKey: ['prices'],
queryFn: () => fetchPrices(Osmosis.Assets.CoinGeckoIds),
staleTime: Infinity
staleTime: Infinity,
});

return { query, ...toPriceHash(query.data) } as PriceHash & { query: UseQueryResult<CoinGeckoResponse> };
return { query, ...toPriceHash(query.data) } as PriceHash & {
query: UseQueryResult<CoinGeckoResponse>;
};
}

// Waht prices: PriceHash looks like:
Expand All @@ -47,4 +54,4 @@ export function usePrices() {
// ...
// "uion": 75.55,
// "uosmo": 0.228555
// }
// }
Loading