Skip to content

Commit

Permalink
try to check foreign asset cache concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 15, 2023
1 parent 6b0dde3 commit be30b43
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
85 changes: 56 additions & 29 deletions core/tokenRegistry/src/foreignAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ console.warn = function (x: any, ...rest: any) {
};

import { ChainName, chains } from "@wormhole-foundation/sdk-base";
import { ForeignAssetsCache, TokensConfig } from "./types";
import { ForeignAssetsCache, TokenEntries, TokensConfig } from "./types";
import { TokenId, Wormhole, toNative } from "@wormhole-foundation/connect-sdk";

// TODO: Question: How do we handle if a user tries to perform an action for a chain/platform which isn't installed??
Expand Down Expand Up @@ -124,45 +124,72 @@ export const getForeignAssetsData = async (
return updates;
};

type MaybeUpdate = [ChainName, string, ForeignAssetsCache | undefined];

export const getForeignAssetsDataForChain = async (
wh: Wormhole,
chain: ChainName,
chainTokensConfig: TokenEntries,
) => {
const maybeUpdates: MaybeUpdate[] = [];
for (const [token, config] of Object.entries(chainTokensConfig)) {
const tokenId = createTokenId(chain as ChainName, token);
if (!tokenId) continue;

maybeUpdates.push([
chain as ChainName,
token,
await getForeignAssetsData(
wh,
chain as ChainName,
tokenId,
config.foreignAssets,
),
]);
}
return maybeUpdates;
};

export const getSuggestedUpdates = async (
wh: Wormhole,
tokensConfig: TokensConfig,
) => {
let suggestedUpdates: TokensConfig = {};
let numUpdates = 0;

const maybeUpdatePromises: Promise<MaybeUpdate[]>[] = [];

for (const [chain, chainTokensConfig] of Object.entries(tokensConfig)) {
for (const [token, config] of Object.entries(chainTokensConfig)) {
const tokenId = createTokenId(chain as ChainName, token);
const updates = await getForeignAssetsData(
wh,
chain as ChainName,
tokenId,
config.foreignAssets,
);
if (updates && Object.values(updates).length > 0) {
numUpdates += Object.values(updates).length;
suggestedUpdates = {
...suggestedUpdates,
[chain]: {
...(suggestedUpdates[chain as ChainName] || {}),
[token]: {
...(suggestedUpdates[chain as ChainName]
? suggestedUpdates[chain as ChainName]![token] || {}
maybeUpdatePromises.push(
getForeignAssetsDataForChain(wh, chain as ChainName, chainTokensConfig),
);
}

const maybeUpdates = (await Promise.all(maybeUpdatePromises)).flat();

for (const [chain, token, updates] of maybeUpdates) {
if (updates && Object.values(updates).length > 0) {
numUpdates += Object.values(updates).length;

suggestedUpdates = {
...suggestedUpdates,
[chain]: {
...(suggestedUpdates[chain] || {}),
[token]: {
...(suggestedUpdates[chain]
? suggestedUpdates[chain]![token] || {}
: {}),
foreignAssets: {
...(suggestedUpdates[chain]
? suggestedUpdates[chain]![token]
? suggestedUpdates[chain]![token]!.foreignAssets
: {}
: {}),
foreignAssets: {
...(suggestedUpdates[chain as ChainName]
? suggestedUpdates[chain as ChainName]![token]
? suggestedUpdates[chain as ChainName]![token]!
.foreignAssets
: {}
: {}),
...updates,
},
...updates,
},
},
};
}
},
};
}
}
// console.log(`${numUpdates} updates available`);
Expand Down
7 changes: 6 additions & 1 deletion core/tokenRegistry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type TokenConfig = {
decimals: number;
foreignAssets?: ForeignAssetsCache;
};

export type TokenEntries = {
[key: string]: TokenConfig;
};

export type TokensConfig = {
[chain in ChainName]?: { [key: string]: TokenConfig };
[chain in ChainName]?: TokenEntries;
};
8 changes: 5 additions & 3 deletions platforms/solana/src/platformUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
chainToPlatform,
UniversalOrNative,
} from '@wormhole-foundation/connect-sdk';
import { Connection, PublicKey } from '@solana/web3.js';
import { Connection, ParsedAccountData, PublicKey } from '@solana/web3.js';
import { solGenesisHashToNetworkChainPair } from './constants';
import { SolanaPlatform } from './platform';
import { SolanaAddress, SolanaZeroAddress } from './address';
Expand Down Expand Up @@ -53,8 +53,10 @@ export module SolanaUtils {
let mint = await rpc.getParsedAccountInfo(
new PublicKey(token.toUint8Array()),
);
if (!mint) throw new Error('could not fetch token details');
const { decimals } = (mint as any).value.data.parsed.info;

if (!mint || !mint.value) throw new Error('could not fetch token details');

const { decimals } = (mint.value.data as ParsedAccountData).parsed.info;
return decimals;
}

Expand Down

0 comments on commit be30b43

Please sign in to comment.