@metaplex-foundation/beet-solana
Advanced tools
Comparing version 0.2.0 to 0.3.0
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -16,2 +20,3 @@ if (k2 === undefined) k2 = k; | ||
__exportStar(require("./keys"), exports); | ||
__exportStar(require("./gpa"), exports); | ||
/** | ||
@@ -18,0 +23,0 @@ * Maps solana beet exports to metadata which describes in which package it |
import { keysTypeMap } from './keys'; | ||
export * from './keys'; | ||
export * from './gpa'; | ||
/** | ||
@@ -4,0 +5,0 @@ * Maps solana beet exports to metadata which describes in which package it |
import { SupportedTypeDefinition } from '@metaplex-foundation/beet'; | ||
import { KeysExports, KeysTypeMapKey } from './keys'; | ||
export * from './keys'; | ||
export * from './gpa'; | ||
/** | ||
@@ -5,0 +6,0 @@ * @category TypeDefinition |
{ | ||
"name": "@metaplex-foundation/beet-solana", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Solana specific extension for beet, the borsh compatible de/serializer", | ||
@@ -39,5 +39,8 @@ "sideEffects": false, | ||
"@metaplex-foundation/beet": ">=0.1.0", | ||
"@solana/web3.js": "^1.44.0" | ||
"@solana/web3.js": "^1.44.0", | ||
"bs58": "^5.0.0", | ||
"debug": "^4.3.4" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "^4.1.7", | ||
"@types/node": "^14.18.0", | ||
@@ -48,2 +51,3 @@ "@types/node-fetch": "^2.6.1", | ||
"rimraf": "^3.0.2", | ||
"spok": "^1.4.3", | ||
"tape": "^5.3.2", | ||
@@ -50,0 +54,0 @@ "typedoc": "^0.22.10", |
108
README.md
@@ -9,7 +9,107 @@ # @metaplex-foundation/beet-solana | ||
## Examples | ||
## GPA Builders | ||
### Using PublicKey Directly | ||
solana-beet uses `beet`s knowledge about account layouts to provide `GpaBuilder`s for | ||
them which allow to filter by account data size and content. | ||
1. Create a GPA Builder via `const gpaBuilder = GpaBuilder.fromStruct(programId, accountStruct)` | ||
2. add filters via `gpaBuilder.dataSize`, `gpaBuilder.addFilter` or `gpaBuilder.addInnerFilter` | ||
3. execute `gpaBuilder.run(connection)` which will return all accounts matching the specified | ||
filters | ||
### Examples | ||
#### Simple struct with primitives | ||
```ts | ||
export type ResultsArgs = Pick<Results, 'win' | 'totalWin' | 'losses'> | ||
export class Results { | ||
constructor( | ||
readonly win: number, | ||
readonly totalWin: number, | ||
readonly losses: number | ||
) {} | ||
static readonly struct = new BeetStruct<Results, ResultsArgs>( | ||
[ | ||
['win', u8], | ||
['totalWin', u16], | ||
['losses', i32], | ||
], | ||
(args: ResultsArgs) => new Results(args.win!, args.totalWin!, args.losses!), | ||
'Results' | ||
) | ||
} | ||
const gpaBuilder = GpaBuilder.fromStruct(PROGRAM_ID, Results.struct) | ||
const accounts = await gpaBuilder | ||
.addFilter('totalWin', 8) | ||
.addFilter('losses', -7) | ||
.run() | ||
``` | ||
#### Matching on Complete Nested Struct | ||
_Using `Results` struct from above_ | ||
```ts | ||
export type TraderArgs = Pick<Trader, 'name' | 'results' | 'age'> | ||
export class Trader { | ||
constructor( | ||
readonly name: string, | ||
readonly results: Results, | ||
readonly age: number | ||
) {} | ||
static readonly struct = new BeetStruct<Trader, TraderArgs>( | ||
[ | ||
['name', fixedSizeUtf8String(4)], | ||
['results', Results.struct], | ||
['age', u8], | ||
], | ||
(args) => new Trader(args.name!, args.results!, args.age!), | ||
'Trader' | ||
) | ||
} | ||
const gpaBuilder = GpaBuilder.fromStruct<Trader>( | ||
PROGRAM_ID, | ||
Trader.struct | ||
) | ||
const results = { | ||
win: 3, | ||
totalWin: 4, | ||
losses: -100, | ||
} | ||
const accounts = await gpaBuilder.addFilter('results', results).run() | ||
``` | ||
#### Matching on Part of Nested Struct | ||
_Using `Trader` struct from above_ | ||
```ts | ||
const gpaBuilder = GpaBuilder.fromStruct<Trader>( | ||
PROGRAM_ID, | ||
Trader.struct | ||
) | ||
const account = await gpaBuilder | ||
.addInnerFilter('results.totalWin', 8) | ||
.addInnerFilter('results.win', 2) | ||
.run() | ||
``` | ||
## PublicKey | ||
solana-beet provides a de/serializer for solana public keys. | ||
They can either be used directly or as part of a struct. | ||
### Examples | ||
#### Using PublicKey Directly | ||
```ts | ||
import { publicKey } from '@metaplex-foundation/beet-solana' | ||
@@ -23,3 +123,3 @@ | ||
### PublicKey as part of a Struct Configuration | ||
#### PublicKey as part of a Struct Configuration | ||
@@ -36,3 +136,3 @@ ```ts | ||
const createStruct = new beet.BeetArgsStruct<InstructionArgs>( | ||
[ | ||
[ | ||
['authority', beetSolana.publicKey] | ||
@@ -39,0 +139,0 @@ ], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51372
28
684
144
4
10
+ Addedbs58@^5.0.0
+ Addeddebug@^4.3.4
+ Addedbase-x@4.0.0(transitive)
+ Addedbs58@5.0.0(transitive)