generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Umi] Add merkle tree serializers and hash helpers (#1)
- Loading branch information
1 parent
f02dda4
commit f445ce3
Showing
21 changed files
with
755 additions
and
46 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 |
---|---|---|
|
@@ -25,7 +25,8 @@ | |
"author": "Metaplex Maintainers <[email protected]>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@metaplex-foundation/mpl-toolbox": "^0.7.0" | ||
"@metaplex-foundation/mpl-toolbox": "^0.7.0", | ||
"@noble/hashes": "^1.3.1" | ||
}, | ||
"peerDependencies": { | ||
"@metaplex-foundation/umi": "^0.8.0" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,124 @@ | ||
/** | ||
* This code was AUTOGENERATED using the kinobi library. | ||
* Please DO NOT EDIT THIS FILE, instead use visitors | ||
* to add features, then rerun kinobi to update it. | ||
* | ||
* @see https://github.com/metaplex-foundation/kinobi | ||
*/ | ||
|
||
import { | ||
Account, | ||
Context, | ||
Pda, | ||
PublicKey, | ||
RpcAccount, | ||
RpcGetAccountOptions, | ||
RpcGetAccountsOptions, | ||
assertAccountExists, | ||
deserializeAccount, | ||
gpaBuilder, | ||
publicKey as toPublicKey, | ||
} from '@metaplex-foundation/umi'; | ||
import { | ||
MerkleTreeAccountData, | ||
getMerkleTreeAccountDataSerializer, | ||
} from '../../hooked'; | ||
import { | ||
CompressionAccountTypeArgs, | ||
ConcurrentMerkleTreeHeaderDataArgs, | ||
getCompressionAccountTypeSerializer, | ||
getConcurrentMerkleTreeHeaderDataSerializer, | ||
} from '../types'; | ||
|
||
export type MerkleTree = Account<MerkleTreeAccountData>; | ||
|
||
export function deserializeMerkleTree( | ||
context: Pick<Context, 'serializer'>, | ||
rawAccount: RpcAccount | ||
): MerkleTree { | ||
return deserializeAccount( | ||
rawAccount, | ||
getMerkleTreeAccountDataSerializer(context) | ||
); | ||
} | ||
|
||
export async function fetchMerkleTree( | ||
context: Pick<Context, 'rpc' | 'serializer'>, | ||
publicKey: PublicKey | Pda, | ||
options?: RpcGetAccountOptions | ||
): Promise<MerkleTree> { | ||
const maybeAccount = await context.rpc.getAccount( | ||
toPublicKey(publicKey, false), | ||
options | ||
); | ||
assertAccountExists(maybeAccount, 'MerkleTree'); | ||
return deserializeMerkleTree(context, maybeAccount); | ||
} | ||
|
||
export async function safeFetchMerkleTree( | ||
context: Pick<Context, 'rpc' | 'serializer'>, | ||
publicKey: PublicKey | Pda, | ||
options?: RpcGetAccountOptions | ||
): Promise<MerkleTree | null> { | ||
const maybeAccount = await context.rpc.getAccount( | ||
toPublicKey(publicKey, false), | ||
options | ||
); | ||
return maybeAccount.exists | ||
? deserializeMerkleTree(context, maybeAccount) | ||
: null; | ||
} | ||
|
||
export async function fetchAllMerkleTree( | ||
context: Pick<Context, 'rpc' | 'serializer'>, | ||
publicKeys: Array<PublicKey | Pda>, | ||
options?: RpcGetAccountsOptions | ||
): Promise<MerkleTree[]> { | ||
const maybeAccounts = await context.rpc.getAccounts( | ||
publicKeys.map((key) => toPublicKey(key, false)), | ||
options | ||
); | ||
return maybeAccounts.map((maybeAccount) => { | ||
assertAccountExists(maybeAccount, 'MerkleTree'); | ||
return deserializeMerkleTree(context, maybeAccount); | ||
}); | ||
} | ||
|
||
export async function safeFetchAllMerkleTree( | ||
context: Pick<Context, 'rpc' | 'serializer'>, | ||
publicKeys: Array<PublicKey | Pda>, | ||
options?: RpcGetAccountsOptions | ||
): Promise<MerkleTree[]> { | ||
const maybeAccounts = await context.rpc.getAccounts( | ||
publicKeys.map((key) => toPublicKey(key, false)), | ||
options | ||
); | ||
return maybeAccounts | ||
.filter((maybeAccount) => maybeAccount.exists) | ||
.map((maybeAccount) => | ||
deserializeMerkleTree(context, maybeAccount as RpcAccount) | ||
); | ||
} | ||
|
||
export function getMerkleTreeGpaBuilder( | ||
context: Pick<Context, 'rpc' | 'serializer' | 'programs'> | ||
) { | ||
const s = context.serializer; | ||
const programId = context.programs.getPublicKey( | ||
'splAccountCompression', | ||
'cmtDvXumGCrqC1Age74AVPhSRVXJMd8PJS91L8KbNCK' | ||
); | ||
return gpaBuilder(context, programId) | ||
.registerFields<{ | ||
discriminator: CompressionAccountTypeArgs; | ||
treeHeader: ConcurrentMerkleTreeHeaderDataArgs; | ||
serializedTree: Uint8Array; | ||
}>({ | ||
discriminator: [0, getCompressionAccountTypeSerializer(context)], | ||
treeHeader: [1, getConcurrentMerkleTreeHeaderDataSerializer(context)], | ||
serializedTree: [56, s.bytes()], | ||
}) | ||
.deserializeUsing<MerkleTree>((account) => | ||
deserializeMerkleTree(context, account) | ||
); | ||
} |
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,25 @@ | ||
/** | ||
* This code was AUTOGENERATED using the kinobi library. | ||
* Please DO NOT EDIT THIS FILE, instead use visitors | ||
* to add features, then rerun kinobi to update it. | ||
* | ||
* @see https://github.com/metaplex-foundation/kinobi | ||
*/ | ||
|
||
import { Context, Serializer } from '@metaplex-foundation/umi'; | ||
|
||
export enum CompressionAccountType { | ||
Uninitialized, | ||
ConcurrentMerkleTree, | ||
} | ||
|
||
export type CompressionAccountTypeArgs = CompressionAccountType; | ||
|
||
export function getCompressionAccountTypeSerializer( | ||
context: Pick<Context, 'serializer'> | ||
): Serializer<CompressionAccountTypeArgs, CompressionAccountType> { | ||
const s = context.serializer; | ||
return s.enum<CompressionAccountType>(CompressionAccountType, { | ||
description: 'CompressionAccountType', | ||
}) as Serializer<CompressionAccountTypeArgs, CompressionAccountType>; | ||
} |
59 changes: 59 additions & 0 deletions
59
clients/js/src/generated/types/concurrentMerkleTreeHeader.ts
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,59 @@ | ||
/** | ||
* This code was AUTOGENERATED using the kinobi library. | ||
* Please DO NOT EDIT THIS FILE, instead use visitors | ||
* to add features, then rerun kinobi to update it. | ||
* | ||
* @see https://github.com/metaplex-foundation/kinobi | ||
*/ | ||
|
||
import { Context, Serializer } from '@metaplex-foundation/umi'; | ||
import { | ||
CompressionAccountType, | ||
CompressionAccountTypeArgs, | ||
ConcurrentMerkleTreeHeaderData, | ||
ConcurrentMerkleTreeHeaderDataArgs, | ||
getCompressionAccountTypeSerializer, | ||
getConcurrentMerkleTreeHeaderDataSerializer, | ||
} from '.'; | ||
|
||
/** | ||
* Initialization parameters for an SPL ConcurrentMerkleTree. | ||
* | ||
* Only the following permutations are valid: | ||
* | ||
* | max_depth | max_buffer_size | | ||
* | --------- | --------------------- | | ||
* | 14 | (64, 256, 1024, 2048) | | ||
* | 20 | (64, 256, 1024, 2048) | | ||
* | 24 | (64, 256, 512, 1024, 2048) | | ||
* | 26 | (64, 256, 512, 1024, 2048) | | ||
* | 30 | (512, 1024, 2048) | | ||
* | ||
*/ | ||
|
||
export type ConcurrentMerkleTreeHeader = { | ||
/** Account type */ | ||
accountType: CompressionAccountType; | ||
/** Versioned header */ | ||
header: ConcurrentMerkleTreeHeaderData; | ||
}; | ||
|
||
export type ConcurrentMerkleTreeHeaderArgs = { | ||
/** Account type */ | ||
accountType: CompressionAccountTypeArgs; | ||
/** Versioned header */ | ||
header: ConcurrentMerkleTreeHeaderDataArgs; | ||
}; | ||
|
||
export function getConcurrentMerkleTreeHeaderSerializer( | ||
context: Pick<Context, 'serializer'> | ||
): Serializer<ConcurrentMerkleTreeHeaderArgs, ConcurrentMerkleTreeHeader> { | ||
const s = context.serializer; | ||
return s.struct<ConcurrentMerkleTreeHeader>( | ||
[ | ||
['accountType', getCompressionAccountTypeSerializer(context)], | ||
['header', getConcurrentMerkleTreeHeaderDataSerializer(context)], | ||
], | ||
{ description: 'ConcurrentMerkleTreeHeader' } | ||
) as Serializer<ConcurrentMerkleTreeHeaderArgs, ConcurrentMerkleTreeHeader>; | ||
} |
Oops, something went wrong.