-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add accounts package
- Loading branch information
1 parent
3bc22e7
commit 9482bee
Showing
23 changed files
with
1,348 additions
and
137 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2023 Solana Labs, Inc | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,237 @@ | ||
[![npm][npm-image]][npm-url] | ||
[![npm-downloads][npm-downloads-image]][npm-url] | ||
[![semantic-release][semantic-release-image]][semantic-release-url] | ||
<br /> | ||
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url] | ||
|
||
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square | ||
[code-style-prettier-url]: https://github.com/prettier/prettier | ||
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/accounts/experimental.svg?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/@solana/accounts/experimental.svg?style=flat | ||
[npm-url]: https://www.npmjs.com/package/@solana/accounts/v/experimental | ||
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg | ||
[semantic-release-url]: https://github.com/semantic-release/semantic-release | ||
|
||
# @solana/accounts | ||
|
||
This package contains types and helper methods for representing, fetching and decoding Solana accounts. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library). | ||
|
||
It provides a unified definition of a Solana 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. | ||
|
||
Helper functions are provided for fetching, parsing and decoding accounts as well as asserting that an account exists. | ||
|
||
```ts | ||
// Fetch. | ||
const myAddress = address('1234..5678'); | ||
const myAccount = fetchAccount(rpc, myAddress); | ||
myAccount satisfies MaybeEncodedAccount<'1234..5678'>; | ||
|
||
// Assert. | ||
assertAccountExists(myAccount); | ||
myAccount satisfies EncodedAccount<'1234..5678'>; | ||
|
||
// Decode. | ||
type MyAccountData = { name: string; age: number }; | ||
const myDecoder: Decoder<MyAccountData> = getStructDecoder([ | ||
['name', getStringDecoder({ size: getU32Decoder() })], | ||
['age', getU32Decoder()], | ||
]); | ||
const myDecodedAccount = decodeAccount(myAccount, myDecoder); | ||
myDecodedAccount satisfies Account<MyAccountData, '1234..5678'>; | ||
``` | ||
|
||
## Types | ||
|
||
### `BaseAccount` | ||
|
||
The `BaseAccount` type defines the attributes common to all Solana accounts. Namely, it contains everything stored on-chain except the account data itself. | ||
|
||
```ts | ||
const BaseAccount: BaseAccount = { | ||
executable: false, | ||
lamports: lamports(1_000_000_000n), | ||
programAddress: address('1111..1111'), | ||
}; | ||
``` | ||
|
||
This package also exports a `BASE_ACCOUNT_SIZE` constant representing the size of the `BaseAccount` attributes in bytes. | ||
|
||
```ts | ||
const myTotalAccountSize = myAccountDataSize + BASE_ACCOUNT_SIZE; | ||
``` | ||
|
||
### `Account` and `EncodedAccount` | ||
|
||
The `Account` type contains all the information relevant to a Solana account. It contains the `BaseAccount` described above as well as the account data and the address of the account. | ||
|
||
The account data can be represented as either a `Uint8Array` — meaning the account is encoded — or a custom data type — meaning the account is decoded. | ||
|
||
```ts | ||
// Encoded. | ||
const myEncodedAccount: Account<Uint8Array, '1234..5678'> = { | ||
address: address('1234..5678'), | ||
data: new Uint8Array([1, 2, 3]), | ||
executable: false, | ||
lamports: lamports(1_000_000_000n), | ||
programAddress: address('1111..1111'), | ||
}; | ||
|
||
// Decoded. | ||
type MyAccountData = { name: string; age: number }; | ||
const myDecodedAccount: Account<MyAccountData, '1234..5678'> = { | ||
address: address('1234..5678'), | ||
data: { name: 'Alice', age: 30 }, | ||
executable: false, | ||
lamports: lamports(1_000_000_000n), | ||
programAddress: address('1111..1111'), | ||
}; | ||
``` | ||
|
||
The `EncodedAccount` type can also be used to represent an encoded account and is equivalent to an `Account` with a `Uint8Array` account data. | ||
|
||
```ts | ||
myEncodedAccount satisfies EncodedAccount<'1234..5678'>; | ||
``` | ||
|
||
### `MaybeAccount` and `MaybeEncodedAccount` | ||
|
||
The `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`. | ||
|
||
```ts | ||
// Account exists. | ||
const myExistingAccount: MaybeAccount<MyAccountData, '1234..5678'> = { | ||
exists: true, | ||
address: address('1234..5678'), | ||
data: { name: 'Alice', age: 30 }, | ||
executable: false, | ||
lamports: lamports(1_000_000_000n), | ||
programAddress: address('1111..1111'), | ||
}; | ||
|
||
// Account does not exist. | ||
const myMissingAccount: MaybeAccount<MyAccountData, '8765..4321'> = { | ||
exists: false, | ||
address: address('8765..4321'), | ||
}; | ||
``` | ||
|
||
Similarly 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. | ||
|
||
```ts | ||
// Encoded account exists. | ||
const myExistingAccount: MaybeEncodedAccount<'1234..5678'> = { | ||
exists: true, | ||
address: address('1234..5678'), | ||
data: new Uint8Array([1, 2, 3]), | ||
// ... | ||
}; | ||
|
||
// Encoded account does not exist. | ||
const myMissingAccount: MaybeEncodedAccount<'8765..4321'> = { | ||
exists: false, | ||
address: address('8765..4321'), | ||
}; | ||
``` | ||
|
||
## Functions | ||
|
||
### `assertAccountExists()` | ||
|
||
Given a `MaybeAccount`, this function asserts that the account exists and allows it to be used as an `Account` type going forward. | ||
|
||
```ts | ||
const myAccount: MaybeEncodedAccount<'1234..5678'>; | ||
assertAccountExists(myAccount); | ||
|
||
// Now we can use myAccount as an Account. | ||
myAccount satisfies EncodedAccount<'1234..5678'>; | ||
``` | ||
|
||
### `parseRpcAccount()` | ||
|
||
This function parses the raw data provided by the RPC client into an `EncodedAccount` type or a `MaybeEncodedAccount` type if the raw data can be set to `null`. | ||
|
||
```ts | ||
const myAddress = address('1234..5678'); | ||
const myRpcAccount = await rpc.getAccountInfo(myAddress).send(); | ||
const myAccount: MaybeEncodedAccount<'1234..5678'> = parseRpcAccount(myRpcAccount); | ||
``` | ||
|
||
It can also handle `jsonParsed` accounts returned by the RPC client. In this case, the data type expected to be returned by the RPC client should be explicitly provided as the first type parameter. | ||
|
||
```ts | ||
const myAddress = address('1234..5678'); | ||
const myRpcAccount = await rpc.getAccountInfo(myAddress, { encoding: 'jsonParsed' }).send(); | ||
const myAccount: MaybeAccount<MyData, '1234..5678'> = parseRpcAccount<MyData, '1234..5678'>(myRpcAccount); | ||
``` | ||
|
||
### `fetchAccount()` | ||
|
||
This function fetches a `MaybeEncodedAccount` from the provided RPC client and address. It uses the `getAccountInfo` RPC method under the hood and an additional configuration object can be provided to customize the behavior of the RPC call. | ||
|
||
```ts | ||
const myAddress = address('1234..5678'); | ||
const myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchAccount(rpc, myAddress); | ||
|
||
// With custom configuration. | ||
const myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchAccount(rpc, myAddress, { | ||
abortSignal: myAbortController.signal, | ||
commitment: 'confirmed', | ||
}); | ||
``` | ||
|
||
This function also supports the `jsonParsed` encoding as long as the data type expected to be returned by the RPC client is explicitly provided as the first type parameter. Note that the return value may still be a `MaybeEncodedAccount` as the RPC client may return a base64 encoded string instead of a JSON object if it doesn't know how to decode the requested account. | ||
|
||
```ts | ||
type TokenData = { mint: Address; owner: Address }; | ||
const myAccount: MaybeAccount<TokenData> | MaybeEncodedAccount = await fetchAccount<TokenData>(rpc, myAddress, { | ||
encoding: 'jsonParsed', | ||
}); | ||
``` | ||
|
||
### `fetchAccounts()` | ||
|
||
This 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 and an additional configuration object can be provided to customize the behavior of the RPC call. | ||
|
||
```ts | ||
const myAddressA = address('1234..5678'); | ||
const myAddressB = address('8765..4321'); | ||
const [myAccountA, myAccountB] = await fetchAccounts(rpc, [myAddressA, myAddressB]); | ||
myAccountA satisfies MaybeEncodedAccount<'1234..5678'>; | ||
myAccountB satisfies MaybeEncodedAccount<'8765..4321'>; | ||
|
||
// With custom configuration. | ||
const [myAccountA, myAccountB] = await fetchAccounts(rpc, [myAddressA, myAddressB], { | ||
abortSignal: myAbortController.signal, | ||
commitment: 'confirmed', | ||
}); | ||
``` | ||
|
||
Similarly to the `fetchAccount` method, you may provide a `jsonParsed` encoding and an explicit data type array as the first type parameter. Here as well, you may still receive a `MaybeEncodedAccount` if the RPC doesn't know how to decode the requested account. | ||
|
||
```ts | ||
type TokenData = { mint: Address; owner: Address }; | ||
type MintData = { supply: bigint }; | ||
const [myAccountA, myAccountB] = await fetchAccounts<[TokenData, MintData]>(rpc, [myAddressA, myAddressB], { | ||
encoding: 'jsonParsed', | ||
}); | ||
myAccountA satisfies MaybeAccount<TokenData> | MaybeEncodedAccount; | ||
myAccountB satisfies MaybeAccount<MintData> | MaybeEncodedAccount; | ||
``` | ||
|
||
### `decodeAccount()` | ||
|
||
This function transforms an `EncodedAccount` into an `Account` (or a `MaybeEncodedAccount` into a `MaybeAccount`) by decoding the account data using the provided `Decoder` instance. | ||
|
||
```ts | ||
type MyAccountData = { name: string; age: number }; | ||
|
||
const myAccount: EncodedAccount<'1234..5678'>; | ||
const myDecoder: Decoder<MyAccountData> = getStructDecoder([ | ||
['name', getStringDecoder({ size: getU32Decoder() })], | ||
['age', getU32Decoder()], | ||
]); | ||
|
||
const myDecodedAccount = decodeAccount(myAccount, myDecoder); | ||
myDecodedAccount satisfies Account<MyAccountData, '1234..5678'>; | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{ | ||
"name": "@solana/accounts", | ||
"version": "2.0.0-development", | ||
"description": "Helpers for representing, fetching and decoding Solana accounts", | ||
"exports": { | ||
"browser": { | ||
"import": "./dist/index.browser.js", | ||
"require": "./dist/index.browser.cjs" | ||
}, | ||
"node": { | ||
"import": "./dist/index.node.js", | ||
"require": "./dist/index.node.cjs" | ||
}, | ||
"react-native": "./dist/index.native.js", | ||
"types": "./dist/types/index.d.ts" | ||
}, | ||
"browser": { | ||
"./dist/index.node.cjs": "./dist/index.browser.cjs", | ||
"./dist/index.node.js": "./dist/index.browser.js" | ||
}, | ||
"main": "./dist/index.node.cjs", | ||
"module": "./dist/index.node.js", | ||
"react-native": "./dist/index.native.js", | ||
"types": "./dist/types/index.d.ts", | ||
"type": "module", | ||
"files": [ | ||
"./dist/" | ||
], | ||
"sideEffects": false, | ||
"keywords": [ | ||
"blockchain", | ||
"solana", | ||
"web3" | ||
], | ||
"scripts": { | ||
"compile:js": "tsup --config build-scripts/tsup.config.library.ts", | ||
"compile:typedefs": "tsc -p ./tsconfig.declarations.json", | ||
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch", | ||
"prepublishOnly": "version-from-git --no-git-tag-version --template experimental.short", | ||
"publish-packages": "pnpm publish --tag experimental --access public --no-git-checks", | ||
"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:treeshakability:browser": "agadoo dist/index.browser.js", | ||
"test:treeshakability:native": "agadoo dist/index.native.js", | ||
"test:treeshakability:node": "agadoo dist/index.node.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" | ||
}, | ||
"author": "Solana Labs Maintainers <maintainers@solanalabs.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/solana-labs/solana-web3.js" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/solana-labs/solana-web3.js/issues" | ||
}, | ||
"browserslist": [ | ||
"supports bigint and not dead", | ||
"maintained node versions" | ||
], | ||
"dependencies": { | ||
"@solana/addresses": "workspace:*", | ||
"@solana/codecs-core": "workspace:*", | ||
"@solana/codecs-strings": "workspace:*", | ||
"@solana/rpc-core": "workspace:*", | ||
"@solana/rpc-transport": "workspace:*", | ||
"@solana/rpc-types": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@solana/eslint-config-solana": "^1.0.2", | ||
"@swc/jest": "^0.2.29", | ||
"@types/jest": "^29.5.6", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
"@typescript-eslint/parser": "^6.3.0", | ||
"agadoo": "^3.0.0", | ||
"build-scripts": "workspace:*", | ||
"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", | ||
"test-config": "workspace:*", | ||
"test-matchers": "workspace:*", | ||
"tsconfig": "workspace:*", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.2.2", | ||
"version-from-git": "^1.1.1" | ||
}, | ||
"bundlewatch": { | ||
"defaultCompression": "gzip", | ||
"files": [ | ||
{ | ||
"path": "./dist/index*.js" | ||
} | ||
] | ||
} | ||
} |
Oops, something went wrong.