Skip to content

Commit

Permalink
fixup! feat(types): add custom types for authorities, plugins and asset
Browse files Browse the repository at this point in the history
  • Loading branch information
shotgunofdeath committed Mar 7, 2024
1 parent 26460de commit d3dc376
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
63 changes: 19 additions & 44 deletions clients/js/src/hooked/pluginHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
Authority,
Key,
PluginHeader,
PluginRegistry,
PluginType,
Plugin,
getPluginSerializer,
RegistryRecord,
Expand All @@ -21,48 +18,19 @@ export function formPluginHeader(
};
}

export function formPluginRegistry({
pluginType,
offset,
authorities,
}: {
pluginType: PluginType;
offset: bigint;
authorities: Authority[];
}): Omit<PluginRegistry, 'publicKey' | 'header' | 'externalPlugins'> {
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<Record<string, any>>) {
return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {});
}

export function mapPlugin(
plugin: Plugin,
authorities: BaseAuthorities
): PluginsList {
export function mapPlugin({
plugin,
authorities,
offset,
}: {
plugin: Exclude<Plugin, { __kind: 'Reserved' }>;
authorities: BaseAuthorities;
offset: bigint;
}): PluginsList {
const pluginKey = toWords(plugin.__kind)
.toLowerCase()
.split(' ')
Expand All @@ -71,6 +39,7 @@ export function mapPlugin(
return {
[pluginKey]: {
authorities,
offset,
...('fields' in plugin ? mapPluginFields(plugin.fields) : {}),
},
};
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions clients/js/src/hooked/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ 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;
export type TransferPlugin = BasePlugin & Transfer;
export type UpdateDelegatePlugin = BasePlugin & UpdateDelegate;

export type PluginsList = {
reserved?: ReservedPlugin;
royalties?: RoyaltiesPlugin;
freeze?: FreezePlugin;
burn?: BurnPlugin;
Expand Down
17 changes: 15 additions & 2 deletions clients/js/test/addAuthorityWithTestFetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import {
Asset,
DataState,
Expand All @@ -15,6 +14,7 @@ import {
AssetWithPluginsTest,
formPluginHeader,
getPubkeyAuthority,
MPL_CORE_PROGRAM_ID,
} from '../src';
import { createUmi } from './_setup';

Expand Down Expand Up @@ -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, <AssetWithPluginsTest>{
t.deepEqual(asset1, <AssetWithPluginsTest>{
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',
Expand All @@ -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),
},
});
});

0 comments on commit d3dc376

Please sign in to comment.