Skip to content

Commit

Permalink
chore(blockchain-link): Solana getAccountInfo - optimize for basic de…
Browse files Browse the repository at this point in the history
…tails
  • Loading branch information
martykan committed Nov 19, 2024
1 parent b438c73 commit f31a3ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
58 changes: 30 additions & 28 deletions packages/blockchain-link/src/workers/solana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ type SignatureWithSlot = {
const getAllSignatures = async (
api: SolanaAPI,
descriptor: MessageTypes.GetAccountInfo['payload']['descriptor'],
fullHistory = true,
) => {
let lastSignature: SignatureWithSlot | undefined;
let keepFetching = true;
let keepFetching = fullHistory;
let allSignatures: SignatureWithSlot[] = [];

const limit = 100;
Expand Down Expand Up @@ -144,28 +145,7 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>

const publicKey = new PublicKey(payload.descriptor);

const accountInfo = await api.getAccountInfo(publicKey);

const getTransactionPage = async (
txIds: string[],
tokenAccountsInfos: SolanaTokenAccountInfo[],
) => {
const transactionsPage = await fetchTransactionPage(api, txIds);

const tokenMetadata = await request.getTokenMetadata();

return transactionsPage
.filter(isValidTransaction)
.map(tx =>
solanaUtils.transformTransaction(
tx,
payload.descriptor,
tokenAccountsInfos,
tokenMetadata,
),
)
.filter((tx): tx is Transaction => !!tx);
};
const accountInfo = details === 'basic' ? null : await api.getAccountInfo(publicKey);

const tokenAccounts = await api.getParsedTokenAccountsByOwner(publicKey, {
programId: new PublicKey(TOKEN_PROGRAM_PUBLIC_KEY),
Expand All @@ -179,7 +159,9 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>
new Set(
(
await Promise.all(
allAccounts.map(account => getAllSignatures(api, account)),
allAccounts.map(account =>
getAllSignatures(api, account, details !== 'basic'),
),
)
)
.flat()
Expand All @@ -204,6 +186,26 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>
decimals: a.account.data.parsed?.info?.tokenAmount?.decimals as number | undefined,
}));

const getTransactionPage = async (
txIds: string[],
tokenAccountsInfos: SolanaTokenAccountInfo[],
) => {
const transactionsPage = await fetchTransactionPage(api, txIds);

const tokenMetadata = await request.getTokenMetadata();

return transactionsPage
.filter(isValidTransaction)
.map(tx =>
solanaUtils.transformTransaction(
tx,
payload.descriptor,
tokenAccountsInfos,
tokenMetadata,
),
)
.filter((tx): tx is Transaction => !!tx);
};
const transactionPage =
details === 'txs' ? await getTransactionPage(txIdPage, tokenAccountsInfos) : undefined;

Expand All @@ -217,9 +219,6 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>

const balance = await api.getBalance(publicKey);

// https://solana.stackexchange.com/a/13102
const rent = await api.getMinimumBalanceForRentExemption(accountInfo?.data.byteLength || 0);

const account: AccountInfo = {
descriptor: payload.descriptor,
balance: balance.toString(),
Expand All @@ -243,7 +242,10 @@ const getAccountInfo = async (request: Request<MessageTypes.GetAccountInfo>) =>
? {
misc: {
owner: accountInfo.owner.toString(),
rent,
// https://solana.stackexchange.com/a/13102
rent: await api.getMinimumBalanceForRentExemption(
accountInfo?.data.byteLength || 0,
),
},
}
: {}),
Expand Down
3 changes: 3 additions & 0 deletions suite-common/wallet-utils/src/accountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ export const isAccountOutdated = (account: Account, freshInfo: AccountInfo) => {
// changed stake pool
freshInfo.misc!.staking?.poolId !== account.misc.staking.poolId
);
case 'solana':
// compare last transaction signature since the total number of txs may not be fetched fully
return freshInfo.history.txids?.[0] !== account.history.txids?.[0];
default:
return false;
}
Expand Down

0 comments on commit f31a3ec

Please sign in to comment.