{"_id":"@atlaschain/accounts","name":"@atlaschain/accounts","dist-tags":{"latest":"3.0.0"},"versions":{"3.0.0":{"name":"@atlaschain/accounts","version":"3.0.0","description":"Helpers for representing, fetching and decoding Atlas accounts","exports":{"edge-light":{"import":"./dist/index.node.mjs","require":"./dist/index.node.cjs"},"workerd":{"import":"./dist/index.node.mjs","require":"./dist/index.node.cjs"},"browser":{"import":"./dist/index.browser.mjs","require":"./dist/index.browser.cjs"},"node":{"import":"./dist/index.node.mjs","require":"./dist/index.node.cjs"},"react-native":"./dist/index.native.mjs","types":"./dist/types/index.d.ts"},"browser":{"./dist/index.node.cjs":"./dist/index.browser.cjs","./dist/index.node.mjs":"./dist/index.browser.mjs"},"main":"./dist/index.node.cjs","module":"./dist/index.node.mjs","react-native":"./dist/index.native.mjs","types":"./dist/types/index.d.ts","type":"commonjs","sideEffects":false,"keywords":["blockchain","atlas","web3"],"scripts":{"compile:docs":"typedoc","compile:js":"tsup --config build-scripts/tsup.config.package.ts","compile:typedefs":"tsc -p ./tsconfig.declarations.json","dev":"jest -c ../../node_modules/@atlas/test-config/jest-dev.config.ts --rootDir . --watch","prepublishOnly":"pnpm pkg delete devDependencies","publish-impl":"npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || (pnpm publish --tag ${PUBLISH_TAG:-canary} --access public --no-git-checks && (([ -n \"${GITHUB_OUTPUT:-}\" ] && echo 'published=true' >> \"$GITHUB_OUTPUT\") || true) && (([ \"$PUBLISH_TAG\" != \"canary\" ] && ../build-scripts/maybe-tag-latest.ts $npm_package_name@$npm_package_version) || true))","publish-packages":"pnpm prepublishOnly && pnpm publish-impl","style:fix":"pnpm eslint --fix src && pnpm prettier --log-level warn --ignore-unknown --write ./*","test:lint":"TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@atlas/test-config/jest-lint.config.ts --rootDir . --silent","test:prettier":"TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@atlas/test-config/jest-prettier.config.ts --rootDir . --silent","test:treeshakability:browser":"agadoo dist/index.browser.mjs","test:treeshakability:native":"agadoo dist/index.native.mjs","test:treeshakability:node":"agadoo dist/index.node.mjs","test:typecheck":"tsc --noEmit","test:unit:browser":"TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@atlas/test-config/jest-unit.config.browser.ts --rootDir . --silent","test:unit:node":"TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@atlas/test-config/jest-unit.config.node.ts --rootDir . --silent"},"author":{"name":"Atlas Chain Foundation"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/anza-xyz/kit.git"},"bugs":{"url":"https://github.com/anza-xyz/kit/issues"},"browserslist":["supports bigint and not dead","maintained node versions"],"dependencies":{"@atlaschain/addresses":"workspace:*","@atlaschain/codecs-core":"workspace:*","@atlaschain/codecs-strings":"workspace:*","@atlaschain/errors":"workspace:*","@atlaschain/rpc-spec":"workspace:*","@atlaschain/rpc-types":"workspace:*"},"peerDependencies":{"typescript":">=5.3.3"},"engines":{"node":">=20.18.0"},"_id":"@atlaschain/accounts@3.0.0","homepage":"https://github.com/anza-xyz/kit#readme","_nodeVersion":"22.12.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-sME8CnlTvbYN6Uw3Wp5wxYnZGWz3cJ5jomwM6lLhILZOPbAA4E951fuSZHVWt+LOJUcMAx04qdRf9Io7w5KLmA==","shasum":"3ab861c2eb3bf10bf10c8446205d4863813e8c78","tarball":"https://registry.npmjs.org/@atlaschain/accounts/-/accounts-3.0.0.tgz","fileCount":5,"unpackedSize":30246,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDxB4ReJiGIebuo8nH7WRN+hQvHVegf0zIUBw8M9B9aXwIhAP4IUqcvzWBqiWtuwa3qGhaMS3+QdwIRJjsIgKhJ9ujO"}]},"_npmUser":{"name":"atlaschain","email":"jq@atlaschain.org"},"directories":{},"maintainers":[{"name":"atlaschain","email":"jq@atlaschain.org"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/accounts_3.0.0_1756885293146_0.3792152784107108"},"_hasShrinkwrap":false}},"time":{"created":"2025-09-03T07:41:32.997Z","3.0.0":"2025-09-03T07:41:33.319Z","modified":"2025-09-03T07:41:33.610Z"},"maintainers":[{"name":"atlaschain","email":"jq@atlaschain.org"}],"description":"Helpers for representing, fetching and decoding Atlas accounts","homepage":"https://github.com/anza-xyz/kit#readme","keywords":["blockchain","atlas","web3"],"repository":{"type":"git","url":"git+https://github.com/anza-xyz/kit.git"},"author":{"name":"Atlas Chain Foundation"},"bugs":{"url":"https://github.com/anza-xyz/kit/issues"},"license":"MIT","readme":"[![npm][npm-image]][npm-url]\n[![npm-downloads][npm-downloads-image]][npm-url]\n<br />\n[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]\n\n[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square\n[code-style-prettier-url]: https://github.com/prettier/prettier\n[npm-downloads-image]: https://img.shields.io/npm/dm/@atlas/accounts?style=flat\n[npm-image]: https://img.shields.io/npm/v/@atlas/accounts?style=flat\n[npm-url]: https://www.npmjs.com/package/@atlas/accounts\n\n# @atlas/accounts\n\nThis package contains types and helper methods for representing, fetching and decoding Atlas accounts. It can be used standalone, but it is also exported as part of Kit [`@atlas/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).\n\nIt provides a unified definition of a Atlas account regardless of how it was retrieved and can represent both encoded and decoded accounts. It also introduces the concept of a `MaybeAccount` which represents a fetched account that may or may not exist on-chain whilst keeping track of its address in both cases.\n\nHelper functions are provided for fetching, parsing and decoding accounts as well as asserting that an account exists.\n\n```ts\n// Fetch.\nconst myAddress = address('1234..5678');\nconst myAccount = await fetchEncodedAccount(rpc, myAddress);\nmyAccount satisfies MaybeEncodedAccount<'1234..5678'>;\n\n// Assert.\nassertAccountExists(myAccount);\nmyAccount satisfies EncodedAccount<'1234..5678'>;\n\n// Decode.\ntype MyAccountData = { name: string; age: number };\nconst myDecoder: Decoder<MyAccountData> = getStructDecoder([\n    ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],\n    ['age', getU32Decoder()],\n]);\nconst myDecodedAccount = decodeAccount(myAccount, myDecoder);\nmyDecodedAccount satisfies Account<MyAccountData, '1234..5678'>;\n```\n\n## Types\n\n### `BaseAccount`\n\nThe `BaseAccount` type defines the attributes common to all Atlas accounts. Namely, it contains everything stored on-chain except the account data itself.\n\n```ts\nconst BaseAccount: BaseAccount = {\n    executable: false,\n    lamports: lamports(1_000_000_000n),\n    programAddress: address('1111..1111'),\n    space: 42n,\n};\n```\n\nThis package also exports a `BASE_ACCOUNT_SIZE` constant representing the size of the `BaseAccount` attributes in bytes.\n\n```ts\nconst myTotalAccountSize = myAccountDataSize + BASE_ACCOUNT_SIZE;\n```\n\n### `Account` and `EncodedAccount`\n\nThe `Account` type contains all the information relevant to a Atlas account. It contains the `BaseAccount` described above as well as the account data and the address of the account.\n\nThe account data can be represented as either a `Uint8Array` — meaning the account is encoded — or a custom data type — meaning the account is decoded.\n\n```ts\n// Encoded.\nconst myEncodedAccount: Account<Uint8Array, '1234..5678'> = {\n    address: address('1234..5678'),\n    data: new Uint8Array([1, 2, 3]),\n    executable: false,\n    lamports: lamports(1_000_000_000n),\n    programAddress: address('1111..1111'),\n    space: 42n,\n};\n\n// Decoded.\ntype MyAccountData = { name: string; age: number };\nconst myDecodedAccount: Account<MyAccountData, '1234..5678'> = {\n    address: address('1234..5678'),\n    data: { name: 'Alice', age: 30 },\n    executable: false,\n    lamports: lamports(1_000_000_000n),\n    programAddress: address('1111..1111'),\n    space: 42n,\n};\n```\n\nThe `EncodedAccount` type can also be used to represent an encoded account and is equivalent to an `Account` with a `Uint8Array` account data.\n\n```ts\nmyEncodedAccount satisfies EncodedAccount<'1234..5678'>;\n```\n\n### `MaybeAccount` and `MaybeEncodedAccount`\n\nThe `MaybeAccount` type is a union type representing an account that may or may not exist on-chain. When the account exists, it is represented as an `Account` type with an additional `exists` attribute set to `true`. When it does not exist, it is represented by an object containing only the address of the account and an `exists` attribute set to `false`.\n\n```ts\n// Account exists.\nconst myExistingAccount: MaybeAccount<MyAccountData, '1234..5678'> = {\n    exists: true,\n    address: address('1234..5678'),\n    data: { name: 'Alice', age: 30 },\n    // ...\n};\n\n// Account does not exist.\nconst myMissingAccount: MaybeAccount<MyAccountData, '8765..4321'> = {\n    exists: false,\n    address: address('8765..4321'),\n};\n```\n\nSimilarly to the `Account` type, the `MaybeAccount` type can be used to represent an encoded account by using the `Uint8Array` data type or by using the `MaybeEncodedAccount` helper type.\n\n```ts\n// Encoded account exists.\nconst myExistingAccount: MaybeEncodedAccount<'1234..5678'> = {\n    exists: true,\n    address: address('1234..5678'),\n    data: new Uint8Array([1, 2, 3]),\n    // ...\n};\n\n// Encoded account does not exist.\nconst myMissingAccount: MaybeEncodedAccount<'8765..4321'> = {\n    exists: false,\n    address: address('8765..4321'),\n};\n```\n\n## Functions\n\n### `assertAccountExists()`\n\nGiven a `MaybeAccount`, this function asserts that the account exists and allows it to be used as an `Account` type going forward.\n\n```ts\nconst myAccount: MaybeEncodedAccount<'1234..5678'>;\nassertAccountExists(myAccount);\n\n// Now we can use myAccount as an Account.\nmyAccount satisfies EncodedAccount<'1234..5678'>;\n```\n\n### `assertAccountsExist()`\n\nGiven an array of `MaybeAccount`s, this function asserts that all the accounts\nexist and allows them to be used as an array of `Account`s going forward.\n\n```ts\nconst myAccounts: MaybeEncodedAccount<Address>[];\nassertAccountsExist(myAccounts);\n\n// Now we can use them as an array of accounts\nfor (const a of myAccounts) {\n    a satisfies EncodedAccount<Address>;\n}\n```\n\n### `parseBase64RpcAccount()`\n\nThis function parses a base64-encoded account provided by the RPC client into an `EncodedAccount` type or a `MaybeEncodedAccount` type if the raw data can be set to `null`.\n\n```ts\nconst myAddress = address('1234..5678');\nconst myRpcAccount = await rpc.getAccountInfo(myAddress, { encoding: 'base64' }).send();\nconst myAccount: MaybeEncodedAccount<'1234..5678'> = parseBase64RpcAccount(myRpcAccount);\n```\n\n### `parseBase58RpcAccount()`\n\nThis function parses a base58-encoded account provided by the RPC client into an `EncodedAccount` type or a `MaybeEncodedAccount` type if the raw data can be set to `null`.\n\n```ts\nconst myAddress = address('1234..5678');\nconst myRpcAccount = await rpc.getAccountInfo(myAddress, { encoding: 'base58' }).send();\nconst myAccount: MaybeEncodedAccount<'1234..5678'> = parseBase58RpcAccount(myRpcAccount);\n```\n\n### `parseJsonRpcAccount()`\n\nThis function parses an arbitrary `jsonParsed` account provided by the RPC client into an `Account` type or a `MaybeAccount` type if the raw data can be set to `null`. The expected data type should be explicitly provided as the first type parameter.\n\n```ts\nconst myAccount: Account<MyData> = parseJsonRpcAccount<MyData>(myJsonRpcAccount);\n```\n\n### `fetchEncodedAccount()`\n\nThis function fetches a `MaybeEncodedAccount` from the provided RPC client and address. It uses the `getAccountInfo` RPC method under the hood with base64 encoding and an additional configuration object can be provided to customize the behavior of the RPC call.\n\n```ts\nconst myAddress = address('1234..5678');\nconst myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchEncodedAccount(rpc, myAddress);\n\n// With custom configuration.\nconst myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchEncodedAccount(rpc, myAddress, {\n    abortSignal: myAbortController.signal,\n    commitment: 'confirmed',\n});\n```\n\n### `fetchEncodedAccounts()`\n\nThis function fetches an array of `MaybeEncodedAccount` from the provided RPC client and an array of addresses. It uses the `getMultipleAccounts` RPC method under the hood with base64 encodings and an additional configuration object can be provided to customize the behavior of the RPC call.\n\n```ts\nconst myAddressA = address('1234..5678');\nconst myAddressB = address('8765..4321');\nconst [myAccountA, myAccountB] = await fetchEncodedAccounts(rpc, [myAddressA, myAddressB]);\nmyAccountA satisfies MaybeEncodedAccount<'1234..5678'>;\nmyAccountB satisfies MaybeEncodedAccount<'8765..4321'>;\n\n// With custom configuration.\nconst [myAccountA, myAccountB] = await fetchEncodedAccounts(rpc, [myAddressA, myAddressB], {\n    abortSignal: myAbortController.signal,\n    commitment: 'confirmed',\n});\n```\n\n### `fetchJsonParsedAccount()`\n\nThis function fetches a `MaybeAccount` from the provided RPC client and address by using `getAccountInfo` under the hood with the `jsonParsed` encoding. It may also return a `MaybeEncodedAccount` if the RPC client does not know how to parse the account at the requested address. In any case, the expected data type should be explicitly provided as the first type parameter.\n\n```ts\ntype TokenData = { mint: Address; owner: Address };\nconst myAccount = await fetchJsonParsedAccount<TokenData>(rpc, myAddress);\nmyAccount satisfies MaybeAccount<TokenData> | MaybeEncodedAccount;\n\n// With custom configuration.\nconst myAccount = await fetchJsonParsedAccount<TokenData>(rpc, myAddress, {\n    abortSignal: myAbortController.signal,\n    commitment: 'confirmed',\n});\n```\n\n### `fetchJsonParsedAccounts()`\n\nSimilarly to the `fetchJsonParsedAccount` method, this method fetches an array of `MaybeAccount` from a provided RPC client and an array of addresses. It uses the `getMultipleAccounts` RPC method under the hood with the `jsonParsed` encoding. It may also return a `MaybeEncodedAccount` instead of the expected `MaybeAccount` if the RPC client does not know how to parse some of the requested accounts. In any case, the array of expected data types should be explicitly provided as the first type parameter.\n\n```ts\ntype TokenData = { mint: Address; owner: Address };\ntype MintData = { supply: bigint };\nconst [myAccountA, myAccountB] = await fetchJsonParsedAccounts<[TokenData, MintData]>(rpc, [myAddressA, myAddressB]);\nmyAccountA satisfies MaybeAccount<TokenData> | MaybeEncodedAccount;\nmyAccountB satisfies MaybeAccount<MintData> | MaybeEncodedAccount;\n```\n\n### `decodeAccount()`\n\nThis function transforms an `EncodedAccount` into an `Account` (or a `MaybeEncodedAccount` into a `MaybeAccount`) by decoding the account data using the provided `Decoder` instance.\n\n```ts\ntype MyAccountData = { name: string; age: number };\n\nconst myAccount: EncodedAccount<'1234..5678'>;\nconst myDecoder: Decoder<MyAccountData> = getStructDecoder([\n    ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],\n    ['age', getU32Decoder()],\n]);\n\nconst myDecodedAccount = decodeAccount(myAccount, myDecoder);\nmyDecodedAccount satisfies Account<MyAccountData, '1234..5678'>;\n```\n\n### `assertAccountDecoded()`\n\nThis 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.\n\n```ts\ntype MyAccountData = { name: string; age: number };\n\nconst myAccount: Account<MyAccountData | Uint8Array, '1234..5678'>;\nassertAccountDecoded(myAccount);\n\n// now the account data can be used as MyAccountData\naccount.data satisfies MyAccountData;\n```\n\nThis is particularly useful for narrowing the result of fetching a JSON parsed account.\n\n```ts\nconst account: MaybeAccount<MockData | Uint8Array> = await fetchJsonParsedAccount<MockData>(\n    rpc,\n    '1234..5678' as Address,\n);\n\nassertAccountDecoded(account);\n// now we have a MaybeAccount<MockData>\naccount satisfies MaybeAccount<MockData>;\n```\n\n### `assertAccountsDecoded`\n\nThis 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.\n\n```ts\ntype MyAccountData = { name: string; age: number };\n\nconst myAccounts: Account<MyAccountData | Uint8Array, Address>[];\nassertAccountsDecoded(myAccounts);\n\n// now the account data can be used as MyAccountData\nfor (const a of account) {\n    account.data satisfies MyAccountData;\n}\n```\n","readmeFilename":"README.md","_rev":"1-5118ef025b4ebde3ce461797b137acc5"}