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

chore(blockchain-link): Solana getAccountInfo - optimize for basic details #15445

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
62 changes: 33 additions & 29 deletions packages/blockchain-link/src/workers/solana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type SignatureWithSlot = {
const getAllSignatures = async (
api: SolanaAPI,
descriptor: MessageTypes.GetAccountInfo['payload']['descriptor'],
fullHistory = true,
) => {
let lastSignature: SignatureWithSlot | undefined;
let keepFetching = true;
Expand All @@ -61,7 +62,7 @@ const getAllSignatures = async (
slot: info.slot,
}));
lastSignature = signatures[signatures.length - 1];
keepFetching = signatures.length === limit;
keepFetching = signatures.length === limit && fullHistory;
allSignatures = [...allSignatures, ...signatures];
}

Expand Down Expand Up @@ -144,42 +145,25 @@ 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),
});

const allAccounts = [payload.descriptor, ...tokenAccounts.value.map(a => a.pubkey.toString())];
const sortedTokenAccountPubkeys = tokenAccounts.value.map(a => a.pubkey.toString()).sort();

const allAccounts = [payload.descriptor, ...sortedTokenAccountPubkeys];

const allTxIds =
details === 'basic' || details === 'txs' || details === 'txids'
? Array.from(
new Set(
(
await Promise.all(
allAccounts.map(account => getAllSignatures(api, account)),
allAccounts.map(account =>
getAllSignatures(api, account, details !== 'basic'),
),
)
)
.flat()
Expand All @@ -204,6 +188,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 +221,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 +244,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
Loading