@solana/accounts
Advanced tools
Comparing version 2.0.0-experimental.96442d1 to 2.0.0-experimental.97527f4
@@ -19,2 +19,17 @@ import { getBase64Encoder, getBase58Encoder } from '@solana/codecs-strings'; | ||
} | ||
function accountExists(account) { | ||
return !("exists" in account) || "exists" in account && account.exists; | ||
} | ||
function assertAccountDecoded(account) { | ||
if (accountExists(account) && account.data instanceof Uint8Array) { | ||
throw new Error(`Expected account [${account.address}] to be decoded.`); | ||
} | ||
} | ||
function assertAccountsDecoded(accounts) { | ||
const encoded = accounts.filter((a) => accountExists(a) && a.data instanceof Uint8Array); | ||
if (encoded.length > 0) { | ||
const encodedAddresses = encoded.map((a) => a.address).join(", "); | ||
throw new Error(`Expected accounts [${encodedAddresses}] to be decoded.`); | ||
} | ||
} | ||
function parseBase64RpcAccount(address, rpcAccount) { | ||
@@ -76,5 +91,12 @@ if (!rpcAccount) | ||
} | ||
function assertAccountsExist(accounts) { | ||
const missingAccounts = accounts.filter((a) => !a.exists); | ||
if (missingAccounts.length > 0) { | ||
const missingAddresses = missingAccounts.map((a) => a.address); | ||
throw new Error(`Expected accounts [${missingAddresses.join(", ")}] to exist.`); | ||
} | ||
} | ||
export { BASE_ACCOUNT_SIZE, assertAccountExists, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
export { BASE_ACCOUNT_SIZE, assertAccountDecoded, assertAccountExists, assertAccountsDecoded, assertAccountsExist, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.browser.js.map |
@@ -19,2 +19,17 @@ import { getBase64Encoder, getBase58Encoder } from '@solana/codecs-strings'; | ||
} | ||
function accountExists(account) { | ||
return !("exists" in account) || "exists" in account && account.exists; | ||
} | ||
function assertAccountDecoded(account) { | ||
if (accountExists(account) && account.data instanceof Uint8Array) { | ||
throw new Error(`Expected account [${account.address}] to be decoded.`); | ||
} | ||
} | ||
function assertAccountsDecoded(accounts) { | ||
const encoded = accounts.filter((a) => accountExists(a) && a.data instanceof Uint8Array); | ||
if (encoded.length > 0) { | ||
const encodedAddresses = encoded.map((a) => a.address).join(", "); | ||
throw new Error(`Expected accounts [${encodedAddresses}] to be decoded.`); | ||
} | ||
} | ||
function parseBase64RpcAccount(address, rpcAccount) { | ||
@@ -76,5 +91,12 @@ if (!rpcAccount) | ||
} | ||
function assertAccountsExist(accounts) { | ||
const missingAccounts = accounts.filter((a) => !a.exists); | ||
if (missingAccounts.length > 0) { | ||
const missingAddresses = missingAccounts.map((a) => a.address); | ||
throw new Error(`Expected accounts [${missingAddresses.join(", ")}] to exist.`); | ||
} | ||
} | ||
export { BASE_ACCOUNT_SIZE, assertAccountExists, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
export { BASE_ACCOUNT_SIZE, assertAccountDecoded, assertAccountExists, assertAccountsDecoded, assertAccountsExist, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.native.js.map |
@@ -19,2 +19,17 @@ import { getBase64Encoder, getBase58Encoder } from '@solana/codecs-strings'; | ||
} | ||
function accountExists(account) { | ||
return !("exists" in account) || "exists" in account && account.exists; | ||
} | ||
function assertAccountDecoded(account) { | ||
if (accountExists(account) && account.data instanceof Uint8Array) { | ||
throw new Error(`Expected account [${account.address}] to be decoded.`); | ||
} | ||
} | ||
function assertAccountsDecoded(accounts) { | ||
const encoded = accounts.filter((a) => accountExists(a) && a.data instanceof Uint8Array); | ||
if (encoded.length > 0) { | ||
const encodedAddresses = encoded.map((a) => a.address).join(", "); | ||
throw new Error(`Expected accounts [${encodedAddresses}] to be decoded.`); | ||
} | ||
} | ||
function parseBase64RpcAccount(address, rpcAccount) { | ||
@@ -76,5 +91,12 @@ if (!rpcAccount) | ||
} | ||
function assertAccountsExist(accounts) { | ||
const missingAccounts = accounts.filter((a) => !a.exists); | ||
if (missingAccounts.length > 0) { | ||
const missingAddresses = missingAccounts.map((a) => a.address); | ||
throw new Error(`Expected accounts [${missingAddresses.join(", ")}] to exist.`); | ||
} | ||
} | ||
export { BASE_ACCOUNT_SIZE, assertAccountExists, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
export { BASE_ACCOUNT_SIZE, assertAccountDecoded, assertAccountExists, assertAccountsDecoded, assertAccountsExist, decodeAccount, fetchEncodedAccount, fetchEncodedAccounts, fetchJsonParsedAccount, fetchJsonParsedAccounts, parseBase58RpcAccount, parseBase64RpcAccount, parseJsonRpcAccount }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.node.js.map |
@@ -7,2 +7,8 @@ import type { Decoder } from '@solana/codecs-core'; | ||
export declare function decodeAccount<TData extends object, TAddress extends string = string>(encodedAccount: MaybeEncodedAccount<TAddress>, decoder: Decoder<TData>): MaybeAccount<TData, TAddress>; | ||
/** Asserts that an account has been decoded. */ | ||
export declare function assertAccountDecoded<TData extends object, TAddress extends string = string>(account: Account<TData | Uint8Array, TAddress>): asserts account is Account<TData, TAddress>; | ||
export declare function assertAccountDecoded<TData extends object, TAddress extends string = string>(account: MaybeAccount<TData | Uint8Array, TAddress>): asserts account is MaybeAccount<TData, TAddress>; | ||
/** Asserts that all accounts have been decoded. */ | ||
export declare function assertAccountsDecoded<TData extends object, TAddress extends string = string>(accounts: Account<TData | Uint8Array, TAddress>[]): asserts accounts is Account<TData, TAddress>[]; | ||
export declare function assertAccountsDecoded<TData extends object, TAddress extends string = string>(accounts: MaybeAccount<TData | Uint8Array, TAddress>[]): asserts accounts is MaybeAccount<TData, TAddress>[]; | ||
//# sourceMappingURL=decode-account.d.ts.map |
import type { Address } from '@solana/addresses'; | ||
import type { Slot } from '@solana/rpc-core/dist/types/rpc-methods/common'; | ||
import type { GetAccountInfoApi } from '@solana/rpc-core/dist/types/rpc-methods/getAccountInfo'; | ||
import type { GetMultipleAccountsApi } from '@solana/rpc-core/dist/types/rpc-methods/getMultipleAccounts'; | ||
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types'; | ||
import type { Commitment } from '@solana/rpc-types'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
import type { Commitment, Slot } from '@solana/rpc-types'; | ||
import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account.js'; | ||
import type { GetAccountInfoApi, GetMultipleAccountsApi } from './rpc-api/index.js'; | ||
/** Optional configuration for fetching a singular account. */ | ||
@@ -9,0 +7,0 @@ export type FetchAccountConfig = { |
@@ -16,2 +16,6 @@ import { Address } from '@solana/addresses'; | ||
}; | ||
/** Asserts that all accounts that may or may not exist, actually all exist. */ | ||
export declare function assertAccountsExist<TData extends object | Uint8Array, TAddress extends string = string>(accounts: MaybeAccount<TData, TAddress>[]): asserts accounts is (Account<TData, TAddress> & { | ||
exists: true; | ||
})[]; | ||
//# sourceMappingURL=maybe-account.d.ts.map |
import type { Address } from '@solana/addresses'; | ||
import type { AccountInfoBase, AccountInfoWithBase58Bytes, AccountInfoWithBase58EncodedData, AccountInfoWithBase64EncodedData } from '@solana/rpc-core/dist/types/rpc-methods/common'; | ||
import type { AccountInfoBase, AccountInfoWithBase58Bytes, AccountInfoWithBase58EncodedData, AccountInfoWithBase64EncodedData } from '@solana/rpc-types'; | ||
import type { Account, EncodedAccount } from './account.js'; | ||
import { MaybeAccount, MaybeEncodedAccount } from './maybe-account.js'; | ||
import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account.js'; | ||
import type { JsonParsedDataResponse } from './rpc-api/index.js'; | ||
type Base64EncodedRpcAccount = AccountInfoBase & AccountInfoWithBase64EncodedData; | ||
@@ -14,9 +15,4 @@ /** Parse an account object received from a base64-encoded RPC call into an EncodedAccount or MaybeEncodedAccount type. */ | ||
type JsonParsedRpcAccount = AccountInfoBase & { | ||
readonly data: JsonParsedData<unknown>; | ||
readonly data: JsonParsedDataResponse<unknown>; | ||
}; | ||
type JsonParsedData<TData> = { | ||
readonly parsed: { | ||
readonly info: TData; | ||
}; | ||
}; | ||
/** Parse an account object received from a json-parsed RPC call into an Account or MaybeAccount type. */ | ||
@@ -23,0 +19,0 @@ export declare function parseJsonRpcAccount<TData extends object, TAddress extends string = string>(address: Address<TAddress>, rpcAccount: JsonParsedRpcAccount): Account<TData, TAddress>; |
{ | ||
"name": "@solana/accounts", | ||
"version": "2.0.0-experimental.96442d1", | ||
"version": "2.0.0-experimental.97527f4", | ||
"description": "Helpers for representing, fetching and decoding Solana accounts", | ||
@@ -49,30 +49,8 @@ "exports": { | ||
"dependencies": { | ||
"@solana/addresses": "2.0.0-experimental.96442d1", | ||
"@solana/codecs-core": "2.0.0-experimental.96442d1", | ||
"@solana/rpc-core": "2.0.0-experimental.96442d1", | ||
"@solana/rpc-transport": "2.0.0-experimental.96442d1", | ||
"@solana/rpc-types": "2.0.0-experimental.96442d1", | ||
"@solana/codecs-strings": "2.0.0-experimental.96442d1" | ||
"@solana/addresses": "2.0.0-experimental.97527f4", | ||
"@solana/codecs-core": "2.0.0-experimental.97527f4", | ||
"@solana/codecs-strings": "2.0.0-experimental.97527f4", | ||
"@solana/rpc-spec": "2.0.0-experimental.97527f4", | ||
"@solana/rpc-types": "2.0.0-experimental.97527f4" | ||
}, | ||
"devDependencies": { | ||
"@solana/eslint-config-solana": "^1.0.2", | ||
"@swc/jest": "^0.2.29", | ||
"@types/jest": "^29.5.11", | ||
"@typescript-eslint/eslint-plugin": "^6.13.2", | ||
"@typescript-eslint/parser": "^6.3.0", | ||
"agadoo": "^3.0.0", | ||
"eslint": "^8.45.0", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
"jest": "^29.7.0", | ||
"jest-runner-eslint": "^2.1.2", | ||
"jest-runner-prettier": "^1.0.0", | ||
"prettier": "^3.1", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.2.2", | ||
"version-from-git": "^1.1.1", | ||
"build-scripts": "0.0.0", | ||
"test-config": "0.0.0", | ||
"test-matchers": "0.0.0", | ||
"tsconfig": "0.0.0" | ||
}, | ||
"bundlewatch": { | ||
@@ -88,8 +66,9 @@ "defaultCompression": "gzip", | ||
"compile:js": "tsup --config build-scripts/tsup.config.package.ts", | ||
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/build-scripts/add-js-extension-to-types.mjs", | ||
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch", | ||
"publish-packages": "pnpm publish --tag experimental --access public --no-git-checks", | ||
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/@solana/build-scripts/add-js-extension-to-types.mjs", | ||
"dev": "jest -c node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch", | ||
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || pnpm publish --tag experimental --access public --no-git-checks", | ||
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl", | ||
"style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json", | ||
"test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent", | ||
"test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent", | ||
"test:lint": "jest -c node_modules/@solana/test-config/jest-lint.config.ts --rootDir . --silent", | ||
"test:prettier": "jest -c node_modules/@solana/test-config/jest-prettier.config.ts --rootDir . --silent", | ||
"test:treeshakability:browser": "agadoo dist/index.browser.js", | ||
@@ -99,5 +78,5 @@ "test:treeshakability:native": "agadoo dist/index.native.js", | ||
"test:typecheck": "tsc --noEmit", | ||
"test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent", | ||
"test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent" | ||
"test:unit:browser": "jest -c node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent", | ||
"test:unit:node": "jest -c node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent" | ||
} | ||
} |
@@ -150,2 +150,17 @@ [![npm][npm-image]][npm-url] | ||
### `assertAccountsExist()` | ||
Given an array of `MaybeAccount`s, this function asserts that all the accounts | ||
exist and allows them to be used as an array of `Account`s going forward. | ||
```ts | ||
const myAccounts: MaybeEncodedAccount<Address>[]; | ||
assertAccountsExist(myAccounts); | ||
// Now we can use them as an array of accounts | ||
for (const a of myAccounts) { | ||
a satisfies EncodedAccount<Address>; | ||
} | ||
``` | ||
### `parseBase64RpcAccount()` | ||
@@ -256,1 +271,45 @@ | ||
``` | ||
### `assertAccountDecoded()` | ||
This function asserts that an account stores decoded data, ie not a Uint8Array. Note that it does not check the shape of the data matches the decoded type, only that it is not a Uint8Array. | ||
```ts | ||
type MyAccountData = { name: string; age: number }; | ||
const myAccount: Account<MyAccountData | Uint8Array, '1234..5678'>; | ||
assertAccountDecoded(myAccount); | ||
// now the account data can be used as MyAccountData | ||
account.data satisfies MyAccountData; | ||
``` | ||
This is particularly useful for narrowing the result of fetching a JSON parsed account. | ||
```ts | ||
const account: MaybeAccount<MockData | Uint8Array> = await fetchJsonParsedAccount<MockData>( | ||
rpc, | ||
'1234..5678' as Address, | ||
) | ||
assertAccountDecoded(account); | ||
// now we have a MaybeAccount<MockData> | ||
account satisfies MaybeAccount<MockData> | ||
``` | ||
### `assertAccountsDecoded` | ||
This function asserts that all input accounts store decoded data, ie not a Uint8Array. As with `assertAccountDecoded` it does not check the shape of the data matches the decoded type, only that it is not a Uint8Array. | ||
```ts | ||
type MyAccountData = { name: string; age: number }; | ||
const myAccounts: Account<MyAccountData | Uint8Array, Address>[]; | ||
assertAccountsDecoded(myAccounts); | ||
// now the account data can be used as MyAccountData | ||
for(const a of account) { | ||
account.data satisfies MyAccountData; | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
159831
5
0
34
827
314
+ Added@solana/addresses@2.0.0-experimental.97527f4(transitive)
+ Added@solana/assertions@2.0.0-experimental.97527f4(transitive)
+ Added@solana/codecs-core@2.0.0-experimental.97527f4(transitive)
+ Added@solana/codecs-numbers@2.0.0-experimental.97527f4(transitive)
+ Added@solana/codecs-strings@2.0.0-experimental.97527f4(transitive)
+ Added@solana/rpc-spec@2.0.0-experimental.97527f4(transitive)
+ Added@solana/rpc-spec-types@2.0.0-experimental.97527f4(transitive)
+ Added@solana/rpc-types@2.0.0-experimental.97527f4(transitive)
- Removed@solana/addresses@2.0.0-experimental.96442d1(transitive)
- Removed@solana/assertions@2.0.0-experimental.96442d1(transitive)
- Removed@solana/codecs-core@2.0.0-experimental.96442d1(transitive)
- Removed@solana/codecs-numbers@2.0.0-experimental.96442d1(transitive)
- Removed@solana/codecs-strings@2.0.0-experimental.96442d1(transitive)
- Removed@solana/rpc-core@2.0.0-experimental.96442d1(transitive)
- Removed@solana/rpc-transport@2.0.0-experimental.96442d1(transitive)
- Removed@solana/rpc-types@2.0.0-experimental.96442d1(transitive)
- Removedws@8.18.0(transitive)