From d3dc376832bf60cefccf5769b94cc2cb038f21ab Mon Sep 17 00:00:00 2001 From: shotgunofdeath Date: Thu, 7 Mar 2024 11:43:50 +0100 Subject: [PATCH] fixup! feat(types): add custom types for authorities, plugins and asset --- clients/js/src/hooked/pluginHelpers.ts | 63 ++++++-------------- clients/js/src/hooked/types.ts | 3 +- clients/js/test/addAuthorityWithTestFetch.ts | 17 +++++- 3 files changed, 35 insertions(+), 48 deletions(-) diff --git a/clients/js/src/hooked/pluginHelpers.ts b/clients/js/src/hooked/pluginHelpers.ts index 47ffc45b..0da5890c 100644 --- a/clients/js/src/hooked/pluginHelpers.ts +++ b/clients/js/src/hooked/pluginHelpers.ts @@ -1,9 +1,6 @@ import { - Authority, Key, PluginHeader, - PluginRegistry, - PluginType, Plugin, getPluginSerializer, RegistryRecord, @@ -21,48 +18,19 @@ export function formPluginHeader( }; } -export function formPluginRegistry({ - pluginType, - offset, - authorities, -}: { - pluginType: PluginType; - offset: bigint; - authorities: Authority[]; -}): Omit { - return { - key: Key.PluginRegistry, - registry: [ - { - pluginType, - offset, - authorities, - }, - ], - }; -} - -export function formPluginWithAuthorities({ - authorities, - plugin, -}: { - authorities: Authority[]; - plugin: Plugin; -}) { - return { - authorities, - plugin, - }; -} - export function mapPluginFields(fields: Array>) { return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {}); } -export function mapPlugin( - plugin: Plugin, - authorities: BaseAuthorities -): PluginsList { +export function mapPlugin({ + plugin, + authorities, + offset, +}: { + plugin: Exclude; + authorities: BaseAuthorities; + offset: bigint; +}): PluginsList { const pluginKey = toWords(plugin.__kind) .toLowerCase() .split(' ') @@ -71,6 +39,7 @@ export function mapPlugin( return { [pluginKey]: { authorities, + offset, ...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), }, }; @@ -79,17 +48,23 @@ export function mapPlugin( export function registryRecordsToPluginsList( registryRecords: RegistryRecord[], accountData: Uint8Array -): PluginsList { - return registryRecords.reduce((acc, record) => { +) { + return registryRecords.reduce((acc: PluginsList, record) => { const mappedAuthorities = mapAuthorities(record.authorities); const deserializedPlugin = getPluginSerializer().deserialize( accountData, Number(record.offset) )[0]; + if (deserializedPlugin.__kind === 'Reserved') return acc; + acc = { ...acc, - ...mapPlugin(deserializedPlugin, mappedAuthorities), + ...mapPlugin({ + plugin: deserializedPlugin, + authorities: mappedAuthorities, + offset: record.offset, + }), }; return acc; diff --git a/clients/js/src/hooked/types.ts b/clients/js/src/hooked/types.ts index 3be1cbd2..6c6753d5 100644 --- a/clients/js/src/hooked/types.ts +++ b/clients/js/src/hooked/types.ts @@ -19,9 +19,9 @@ export type BaseAuthorities = { export type BasePlugin = { authorities: BaseAuthorities; + offset: bigint; }; -export type ReservedPlugin = BasePlugin; export type RoyaltiesPlugin = BasePlugin & Royalties; export type FreezePlugin = BasePlugin & Freeze; export type BurnPlugin = BasePlugin & Burn; @@ -29,7 +29,6 @@ export type TransferPlugin = BasePlugin & Transfer; export type UpdateDelegatePlugin = BasePlugin & UpdateDelegate; export type PluginsList = { - reserved?: ReservedPlugin; royalties?: RoyaltiesPlugin; freeze?: FreezePlugin; burn?: BurnPlugin; diff --git a/clients/js/test/addAuthorityWithTestFetch.ts b/clients/js/test/addAuthorityWithTestFetch.ts index 99dc8d77..46044922 100644 --- a/clients/js/test/addAuthorityWithTestFetch.ts +++ b/clients/js/test/addAuthorityWithTestFetch.ts @@ -1,6 +1,5 @@ import { generateSigner } from '@metaplex-foundation/umi'; import test from 'ava'; -// import { base58 } from '@metaplex-foundation/umi/serializers'; import { Asset, DataState, @@ -15,6 +14,7 @@ import { AssetWithPluginsTest, formPluginHeader, getPubkeyAuthority, + MPL_CORE_PROGRAM_ID, } from '../src'; import { createUmi } from './_setup'; @@ -57,8 +57,20 @@ test('TEST it can add an authority to a plugin TEST', async (t) => { .sendAndConfirm(umi); const asset1 = await fetchAssetWithPluginsTest(umi, assetAddress.publicKey); - t.like(asset1, { + t.deepEqual(asset1, { + key: 1, publicKey: assetAddress.publicKey, + header: { + executable: false, + owner: MPL_CORE_PROGRAM_ID, + lamports: { + basisPoints: BigInt(3622800), + identifier: 'SOL', + decimals: 9, + }, + rentEpoch: 18446744073709552000, + exists: true, + }, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), owner: umi.identity.publicKey, name: 'Test Bread', @@ -70,6 +82,7 @@ test('TEST it can add an authority to a plugin TEST', async (t) => { pubkey: [delegateAddress.publicKey], }, frozen: false, + offset: BigInt(118), }, }); });