-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
steveluscher
wants to merge
6
commits into
trezor:develop
from
steveluscher:solana-web3-js-two-dot-oh
+2,005
−626
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc6f2e3
Update `@solana/web3.js` to 2.0.0
steveluscher 50559fa
Port `getBaseFee` and `getPriorityFee` to the 2.0 API
steveluscher 5f66fbb
Update the Solana types in `@trezor/blockchain-link-types`
steveluscher 928ac0a
Ported `@trezor/blockchain-link-utils` to `@solana/web3.js` version 2
steveluscher 9d410dd
Port `@trezor/blockchain-link` to `@solana/web3.js` version 2
steveluscher 4b67a19
Port `@suite-common/wallet-core` and `@suite-common/wallet-utils` to …
steveluscher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}>, | ||
] | ||
) => 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 } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -21,7 +21,7 @@ | |||
}, | ||||
"dependencies": { | ||||
"@mobily/ts-belt": "^3.13.1", | ||||
"@solana/web3.js": "^1.95.4", | ||||
"@solana/web3.js": "^2.0.0", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Yarn depcheck is telling me this is unused |
||||
"@trezor/env-utils": "workspace:*", | ||||
"@trezor/utils": "workspace:*" | ||||
}, | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
@@ -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) { | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this should also be changed above in |
||
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; | ||
}; | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -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'; | ||
} | ||
|
@@ -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(), | ||
|
@@ -420,7 +442,7 @@ export const getAmount = ( | |
|
||
type TokenTransferInstruction = { | ||
program: 'spl-token'; | ||
programId: PublicKey; | ||
programId: Address; | ||
parsed: { | ||
type: 'transferChecked' | 'transfer'; | ||
info: { | ||
|
@@ -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') || | ||
|
@@ -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, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.