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

feat: Upgrade to @solana/web3.js version 2.0.0 #15535

Closed
Closed
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
2 changes: 1 addition & 1 deletion packages/blockchain-link-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prepublish": "yarn tsx ../../scripts/prepublish.js"
},
"dependencies": {
"@solana/web3.js": "^1.95.4",
"@solana/web3.js": "^2.0.0",
"@trezor/type-utils": "workspace:*",
"@trezor/utxo-lib": "workspace:*"
},
Expand Down
75 changes: 62 additions & 13 deletions packages/blockchain-link-types/src/solana.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,73 @@
import type { ParsedTransactionWithMeta } from '@solana/web3.js';
import type {
AccountInfoBase,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
AccountInfoWithBase64EncodedZStdCompressedData,
AccountInfoWithJsonData,
GetTransactionApi,
Signature,
} from '@solana/web3.js';

import type {
GetObjectWithKey,
GetObjectWithoutKey,
ObjectsOnly,
Overloads,
} from '@trezor/type-utils';

export type SolanaValidParsedTxWithMeta = ParsedTransactionWithMeta & {
meta: Required<NonNullable<ParsedTransactionWithMeta['meta']>>;
transaction: Required<ParsedTransactionWithMeta['transaction']>;
blockTime: Required<NonNullable<ParsedTransactionWithMeta['blockTime']>>;
type GetTransactionApiOverloads = Overloads<GetTransactionApi['getTransaction']>;
type GetJsonParsedTransactionApiOverloads = {
[P in keyof GetTransactionApiOverloads]: GetTransactionApiOverloads[P] extends (
...args: [
signature: Signature,
config: Readonly<{
encoding: 'jsonParsed';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intense, but basically boils down to: choose the overload that has encoding: 'jsonParsed', infer its return type, and use that.

}>,
]
) => infer R
? R
: never;
};

export type ParsedTransactionWithMeta = Pick<
SolanaValidParsedTxWithMeta,
'slot' | 'transaction' | 'meta'
> &
Readonly<{
blockTime?: SolanaValidParsedTxWithMeta['blockTime'];
version?: SolanaValidParsedTxWithMeta['version'];
}>;

export type PartiallyDecodedInstruction = GetObjectWithoutKey<
SolanaValidParsedTxWithMeta['transaction']['message']['instructions'][number],
'parsed'
>;

export type ParsedAccountData = ObjectsOnly<AccountInfoWithJsonData['data']>;

export type ParsedInstruction = GetObjectWithKey<
SolanaValidParsedTxWithMeta['transaction']['message']['instructions'][number],
'parsed'
>;

export type SolanaValidParsedTxWithMeta = NonNullable<
ObjectsOnly<GetJsonParsedTransactionApiOverloads[keyof GetTransactionApiOverloads]>
>;

export type SolanaTokenAccountInfo = {
address: string;
mint: string | undefined;
decimals: number | undefined;
};

export type {
ParsedInstruction,
ParsedTransactionWithMeta,
PartiallyDecodedInstruction,
AccountInfo,
ParsedAccountData,
PublicKey,
} from '@solana/web3.js';
export type AccountInfo<
TData extends
| AccountInfoWithBase58EncodedData['data']
| AccountInfoWithBase64EncodedData['data']
| AccountInfoWithBase64EncodedZStdCompressedData['data']
| AccountInfoWithJsonData['data'],
> = AccountInfoBase & Readonly<{ data: TData }>;

export type { Address } from '@solana/web3.js';

export type TokenDetailByMint = { [mint: string]: { name: string; symbol: string } };
2 changes: 1 addition & 1 deletion packages/blockchain-link-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {
"@mobily/ts-belt": "^3.13.1",
"@solana/web3.js": "^1.95.4",
"@solana/web3.js": "^2.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"@solana/web3.js": "^2.0.0",

Yarn depcheck is telling me this is unused

"@trezor/env-utils": "workspace:*",
"@trezor/utils": "workspace:*"
},
Expand Down
67 changes: 46 additions & 21 deletions packages/blockchain-link-utils/src/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import { Target, TokenTransfer, Transaction } from '@trezor/blockchain-link-type
import { arrayPartition } from '@trezor/utils';
import type {
AccountInfo,
Address,
ParsedAccountData,
ParsedInstruction,
ParsedTransactionWithMeta,
SolanaValidParsedTxWithMeta,
SolanaTokenAccountInfo,
PartiallyDecodedInstruction,
SolanaTokenAccountInfo,
SolanaValidParsedTxWithMeta,
TokenDetailByMint,
PublicKey,
} from '@trezor/blockchain-link-types/src/solana';
import type { TokenInfo } from '@trezor/blockchain-link-types/src';
import { isCodesignBuild } from '@trezor/env-utils';

import { formatTokenSymbol } from './utils';

export type ApiTokenAccount = { account: AccountInfo<ParsedAccountData>; pubkey: PublicKey };
export type ApiTokenAccount = {
account: AccountInfo<ParsedAccountData>;
pubkey: Address;
};

// Docs regarding solana programs: https://spl.solana.com/
// Token program docs: https://spl.solana.com/token
Expand Down Expand Up @@ -74,29 +77,35 @@ type SplTokenAccountData = {
decimals: number;
};
};
type: string;
};
/** Space used by account data */
space: number;
space: bigint;
};

type SplTokenAccount = { account: AccountInfo<SplTokenAccountData>; pubkey: PublicKey };
type SplTokenAccount = { account: AccountInfo<SplTokenAccountData>; pubkey: Address };

const isSplTokenAccount = (tokenAccount: ApiTokenAccount): tokenAccount is SplTokenAccount => {
const { parsed } = tokenAccount.account.data;

return (
tokenAccount.account.data.program === 'spl-token' &&
'info' in parsed &&
!!parsed.info &&
'mint' in parsed.info &&
typeof parsed.info.mint === 'string' &&
'tokenAmount' in parsed.info &&
!!parsed.info.tokenAmount &&
typeof parsed.info.tokenAmount === 'object' &&
'amount' in parsed.info.tokenAmount &&
typeof parsed.info.tokenAmount.amount === 'string' &&
'decimals' in parsed.info.tokenAmount &&
typeof parsed.info.tokenAmount.decimals === 'number'
);
};

export const transformTokenInfo = (
tokenAccounts: ApiTokenAccount[],
tokenAccounts: readonly ApiTokenAccount[],
tokenDetailByMint: TokenDetailByMint,
) => {
const tokens: TokenInfo[] = F.toMutable(
Expand All @@ -113,7 +122,7 @@ export const transformTokenInfo = (
balance: info.tokenAmount.amount,
decimals: info.tokenAmount.decimals,
...getTokenNameAndSymbol(info.mint, tokenDetailByMint),
address: tokenAccount.pubkey.toString(),
address: tokenAccount.pubkey,
};
}),
A.reduce(
Expand Down Expand Up @@ -162,7 +171,7 @@ export const extractAccountBalanceDiff = (
postBalance: BigNumber;
} | null => {
const pubKeyIndex = transaction.transaction.message.accountKeys.findIndex(
ak => ak.pubkey.toString() === address,
ak => ak.pubkey === address,
);

if (pubKeyIndex === -1) {
Expand All @@ -188,16 +197,22 @@ export const extractAccountBalanceDiff = (
const postBalance = transaction.meta?.postBalances[pubKeyIndex];

return {
preBalance: new BigNumber(preBalance ?? 0),
postBalance: new BigNumber(postBalance ?? 0),
preBalance: new BigNumber(preBalance?.toString() ?? 0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should also be changed above in if (isTokenDiff)... (L190-191)

postBalance: new BigNumber(postBalance?.toString() ?? 0),
};
};

const isWSolTransfer = (ixs: (ParsedInstruction | PartiallyDecodedInstruction)[]) =>
ixs.find(ix => 'parsed' in ix && ix.parsed.info?.mint === WSOL_MINT);
const isWSolTransfer = (ixs: readonly (ParsedInstruction | PartiallyDecodedInstruction)[]) =>
ixs.find(
ix =>
'parsed' in ix &&
!!ix.parsed.info &&
'mint' in ix.parsed.info &&
ix.parsed.info.mint === WSOL_MINT,
);

type TransactionEffect = {
address: string;
address: Address;
amount: BigNumber;
};

Expand All @@ -208,15 +223,19 @@ export function getNativeEffects(transaction: ParsedTransactionWithMeta): Transa

return transaction.transaction.message.accountKeys
.map(ak => {
const targetAddress = ak.pubkey.toString();
const targetAddress = ak.pubkey;
const balanceDiff = extractAccountBalanceDiff(transaction, targetAddress);

// WSOL Transfers are counted as SOL transfers in the transaction effects, leading to duplicate
// entries in the tx history. This serves to filter out the WSOL transfers from the native effects.
if (wSolTransferInstruction && 'parsed' in wSolTransferInstruction) {
if (
wSolTransferInstruction.parsed.info.destination === targetAddress ||
wSolTransferInstruction.parsed.info.source === targetAddress
(!!wSolTransferInstruction.parsed.info &&
'destination' in wSolTransferInstruction.parsed.info &&
wSolTransferInstruction.parsed.info.destination === targetAddress) ||
(!!wSolTransferInstruction.parsed.info &&
'source' in wSolTransferInstruction.parsed.info &&
wSolTransferInstruction.parsed.info.source === targetAddress)
) {
return null;
}
Expand Down Expand Up @@ -296,7 +315,7 @@ const getNativeTransferTxType = (
if (
effects.length === 1 &&
effects[0]?.address === accountAddress &&
effects[0]?.amount.abs().isEqualTo(new BigNumber(transaction.meta?.fee || 0))
effects[0]?.amount.abs().isEqualTo(new BigNumber(transaction.meta?.fee.toString() || 0))
) {
return 'self';
}
Expand Down Expand Up @@ -391,7 +410,10 @@ export const getDetails = (
}

return {
size: transaction.meta?.computeUnitsConsumed || 0,
size:
transaction.meta?.computeUnitsConsumed != null
? Number(transaction.meta?.computeUnitsConsumed)
: 0,
totalInput: senders
.reduce((acc, curr) => acc.plus(curr.amount.abs()), new BigNumber(0))
.toString(),
Expand Down Expand Up @@ -420,7 +442,7 @@ export const getAmount = (

type TokenTransferInstruction = {
program: 'spl-token';
programId: PublicKey;
programId: Address;
parsed: {
type: 'transferChecked' | 'transfer';
info: {
Expand Down Expand Up @@ -464,6 +486,7 @@ const isTokenTransferInstruction = (
'destination' in parsed.info &&
typeof parsed.info.destination === 'string' &&
(('tokenAmount' in parsed.info &&
!!parsed.info.tokenAmount &&
typeof parsed.info.tokenAmount === 'object' &&
'amount' in parsed.info.tokenAmount &&
typeof parsed.info.tokenAmount.amount === 'string') ||
Expand Down Expand Up @@ -573,8 +596,10 @@ export const transformTransaction = (
return {
type,
txid: tx.transaction.signatures[0].toString(),
blockTime: tx.blockTime,
blockTime: tx.blockTime == null ? undefined : Number(tx.blockTime),
amount,
// FIXME: It is possible for `meta` to be null for some older transactions.
// @ts-expect-error
fee: tx.meta.fee.toString(),
targets,
tokens,
Expand Down
Loading