Skip to content

Commit

Permalink
Merging in main.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Mar 9, 2024
2 parents 3dd2d60 + 39233b6 commit 6c614e2
Show file tree
Hide file tree
Showing 20 changed files with 560 additions and 551 deletions.
39 changes: 39 additions & 0 deletions clients/js/src/hooked/authorityHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PublicKey } from '@metaplex-foundation/umi';
import { Authority, authority as authorityHelper } from '../generated';
import { BaseAuthority } from './types';
import { toWords } from './utils';

// Authorities data helpers
export function getNoneAuthority() {
return authorityHelper('None');
}

export function getOwnerAuthority() {
return authorityHelper('Owner');
}

export function getUpdateAuthority() {
return authorityHelper('UpdateAuthority');
}

export function getPubkeyAuthority(address: PublicKey) {
return authorityHelper('Pubkey', { address });
}

export function getPermanentAuthority(address: PublicKey) {
return authorityHelper('Permanent', { address });
}

export function mapAuthority(authority: Authority): BaseAuthority {
const authorityKey = toWords(authority.__kind)
.split(' ')[0]
.toLowerCase() as keyof BaseAuthority;

if (Object.keys(authority).length > 1) {
return {
[authorityKey]: Object.values(authority).slice(1),
};
}

return { [authorityKey]: true };
}
30 changes: 12 additions & 18 deletions clients/js/src/hooked/fetchAssetWithPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import {
publicKey as toPublicKey,
} from '@metaplex-foundation/umi';
import {
Asset,
PluginHeaderAccountData,
PluginRegistryAccountData,
deserializeAsset,
getAssetAccountDataSerializer,
getPluginHeaderAccountDataSerializer,
getPluginRegistryAccountDataSerializer,
getPluginSerializer,
} from '../generated';
import { PluginList, PluginWithAuthority } from '.';

export type AssetWithPlugins = Asset & PluginList;
import { AssetWithPlugins, PluginsList } from './types';
import { registryRecordsToPluginsList } from './pluginHelpers';

export async function fetchAssetWithPlugins(
context: Pick<Context, 'rpc'>,
Expand All @@ -35,31 +32,28 @@ export async function fetchAssetWithPlugins(

let pluginHeader: PluginHeaderAccountData | undefined;
let pluginRegistry: PluginRegistryAccountData | undefined;
let plugins: PluginWithAuthority[] | undefined;
let pluginsList: PluginsList | undefined;

if (maybeAccount.data.length !== assetData.length) {
[pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize(
maybeAccount.data,
assetData.length
);

[pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize(
maybeAccount.data,
Number(pluginHeader.pluginRegistryOffset)
);
plugins = pluginRegistry.registry.map((record) => ({
plugin: getPluginSerializer().deserialize(
maybeAccount.data,
Number(record.offset)
)[0],
authority: record.authority,
}));

pluginsList = registryRecordsToPluginsList(
pluginRegistry.registry,
maybeAccount.data
);
}

const assetWithPlugins: AssetWithPlugins = {
return {
pluginHeader,
plugins,
pluginRegistry,
...pluginsList,
...asset,
};

return assetWithPlugins;
}
33 changes: 15 additions & 18 deletions clients/js/src/hooked/fetchCollectionWithPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import {
publicKey as toPublicKey,
} from '@metaplex-foundation/umi';
import {
Collection,
PluginHeaderAccountData,
PluginRegistryAccountData,
deserializeCollection,
getCollectionAccountDataSerializer,
getPluginHeaderAccountDataSerializer,
getPluginRegistryAccountDataSerializer,
getPluginSerializer,
} from '../generated';
import { PluginList, PluginWithAuthority } from '.';

export type CollectionWithPlugins = Collection & PluginList;
import {
CollectionWithPlugins,
PluginsList,
registryRecordsToPluginsList,
} from '.';

export async function fetchCollectionWithPlugins(
context: Pick<Context, 'rpc'>,
Expand All @@ -36,31 +36,28 @@ export async function fetchCollectionWithPlugins(

let pluginHeader: PluginHeaderAccountData | undefined;
let pluginRegistry: PluginRegistryAccountData | undefined;
let plugins: PluginWithAuthority[] | undefined;
let pluginsList: PluginsList | undefined;

if (maybeAccount.data.length !== collectionData.length) {
[pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize(
maybeAccount.data,
collectionData.length
);

[pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize(
maybeAccount.data,
Number(pluginHeader.pluginRegistryOffset)
);
plugins = pluginRegistry.registry.map((record) => ({
plugin: getPluginSerializer().deserialize(
maybeAccount.data,
Number(record.offset)
)[0],
authority: record.authority,
}));

pluginsList = registryRecordsToPluginsList(
pluginRegistry.registry,
maybeAccount.data
);
}

const collectionWithPlugins: CollectionWithPlugins = {
return {
pluginHeader,
plugins,
pluginRegistry,
...pluginsList,
...collection,
};

return collectionWithPlugins;
}
16 changes: 3 additions & 13 deletions clients/js/src/hooked/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { Authority, PluginHeader, PluginRegistry, Plugin } from 'src/generated';

export * from './fetchAssetWithPlugins';
export * from './fetchCollectionWithPlugins';

export type PluginWithAuthority = {
plugin: Plugin;
authority: Authority;
};

export type PluginList = {
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>;
plugins?: PluginWithAuthority[];
pluginRegistry?: Omit<PluginRegistry, 'publicKey' | 'header'>;
};
export * from './authorityHelpers';
export * from './pluginHelpers';
export * from './types';
72 changes: 72 additions & 0 deletions clients/js/src/hooked/pluginHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
Key,
PluginHeader,
Plugin,
getPluginSerializer,
RegistryRecord,
} from '../generated';
import { BaseAuthority, PluginsList } from './types';
import { mapAuthority } from './authorityHelpers';
import { toWords } from './utils';

export function formPluginHeader(
pluginRegistryOffset: bigint
): Omit<PluginHeader, 'publicKey' | 'header'> {
return {
key: Key.PluginHeader,
pluginRegistryOffset,
};
}

export function mapPluginFields(fields: Array<Record<string, any>>) {
return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {});
}

export function mapPlugin({
plugin,
authority,
offset,
}: {
plugin: Exclude<Plugin, { __kind: 'Reserved' }>;
authority: BaseAuthority;
offset: bigint;
}): PluginsList {
const pluginKey = toWords(plugin.__kind)
.toLowerCase()
.split(' ')
.reduce((s, c) => s + (c.charAt(0).toUpperCase() + c.slice(1)));

return {
[pluginKey]: {
authority,
offset,
...('fields' in plugin ? mapPluginFields(plugin.fields) : {}),
},
};
}

export function registryRecordsToPluginsList(
registryRecords: RegistryRecord[],
accountData: Uint8Array
) {
return registryRecords.reduce((acc: PluginsList, record) => {
const mappedAuthority = mapAuthority(record.authority);
const deserializedPlugin = getPluginSerializer().deserialize(
accountData,
Number(record.offset)
)[0];

if (deserializedPlugin.__kind === 'Reserved') return acc;

acc = {
...acc,
...mapPlugin({
plugin: deserializedPlugin,
authority: mappedAuthority,
offset: record.offset,
}),
};

return acc;
}, {});
}
48 changes: 48 additions & 0 deletions clients/js/src/hooked/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PublicKey } from '@metaplex-foundation/umi';
import {
Asset,
Burn,
Collection,
Freeze,
PluginHeader,
Royalties,
Transfer,
UpdateDelegate,
} from '../generated';

export type BaseAuthority = {
none?: boolean;
owner?: boolean;
update?: boolean;
pubkey?: Array<PublicKey>;
permanent?: Array<PublicKey>;
};

export type BasePlugin = {
authority: BaseAuthority;
offset?: bigint;
};

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 = {
royalties?: RoyaltiesPlugin;
freeze?: FreezePlugin;
burn?: BurnPlugin;
transfer?: TransferPlugin;
updateDelegate?: UpdateDelegatePlugin;
};

export type AssetWithPlugins = Asset &
PluginsList & {
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>;
};

export type CollectionWithPlugins = Collection &
PluginsList & {
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>;
};
4 changes: 4 additions & 0 deletions clients/js/src/hooked/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function toWords(str: string) {
const camelCaseRegex = /([a-z0-9])([A-Z])/g;
return str.replace(camelCaseRegex, '$1 $2');
}
Loading

0 comments on commit 6c614e2

Please sign in to comment.