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 678e75f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
66 changes: 41 additions & 25 deletions core/tokenRegistry/src/foreignAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,38 +131,54 @@ export const getSuggestedUpdates = async (
let suggestedUpdates: TokensConfig = {};
let numUpdates = 0;

const maybeUpdates: [
ChainName,
string,
Promise<ForeignAssetsCache | undefined>,
][] = [];
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,
if (!tokenId) continue;

maybeUpdates.push([
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] || {}
token,
getForeignAssetsData(
wh,
chain as ChainName,
tokenId,
config.foreignAssets,
),
]);
}
}

await Promise.all(maybeUpdates.map((mu) => mu[1]));

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
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 678e75f

Please sign in to comment.