Skip to content

Commit

Permalink
Updating JS hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Apr 25, 2024
1 parent 4ea1b2b commit 2fbc51e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
5 changes: 5 additions & 0 deletions clients/js/src/hooked/pluginRegistryV1Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,18 @@ export function getExternalRegistryRecordSerializer(): Serializer<
buffer,
lifecycleChecksOffset
);

const [dataOffset, dataOffsetOffset] = option(u64()).deserialize(buffer, pluginOffsetOffset);
const [dataLen] = option(u64()).deserialize(buffer, dataOffsetOffset);
return [
{
pluginType,
authority,
lifecycleChecks,
offset: pluginOffset,
isUnknown,
dataOffset,
dataLen,
},
pluginOffsetOffset,
];
Expand Down
2 changes: 0 additions & 2 deletions clients/js/src/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export const create = (
authority: plugin.initPluginAuthority || {
type: 'UpdateAuthority',
},
dataLen: 0n,
dataOffset: 0n,
type: 'LifecycleHook',
schema: plugin.schema || ExternalPluginSchema.Binary,
});
Expand Down
6 changes: 3 additions & 3 deletions clients/js/src/plugins/dataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BaseDataStoreInitInfoArgs,
BaseDataStoreUpdateInfoArgs,
ExternalPluginSchema,
ExternalRegistryRecord,
} from '../generated';
import { ExternalPluginKey } from './externalPluginKey';
import { ExternalPluginManifest } from './externalPluginManifest';
Expand Down Expand Up @@ -66,14 +67,13 @@ export function dataStoreUpdateInfoArgsToBase(

export function dataStoreFromBase(
s: BaseDataStore,
r: ExternalRegistryRecord,
account: Uint8Array
): DataStore {
return {
...s,
dataAuthority: pluginAuthorityFromBase(s.dataAuthority),
dataOffset: s.dataOffset,
dataLen: s.dataLen,
data: parseExternalPluginData(s, account),
data: parseExternalPluginData(s, r, account),
};
}

Expand Down
3 changes: 2 additions & 1 deletion clients/js/src/plugins/externalPluginManifest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ExternalRegistryRecord } from 'src/generated';
import { ExternalPluginTypeString } from './externalPlugins';

export type ExternalPluginManifest<
Expand All @@ -9,7 +10,7 @@ export type ExternalPluginManifest<
UpdateBase extends Object
> = {
type: ExternalPluginTypeString;
fromBase: (input: Base, account: Uint8Array) => T;
fromBase: (input: Base, record: ExternalRegistryRecord, account: Uint8Array) => T;
initToBase: (input: Init) => InitBase;
updateToBase: (input: Update) => UpdateBase;
};
42 changes: 22 additions & 20 deletions clients/js/src/plugins/externalPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountMeta, Context, PublicKey } from '@metaplex-foundation/umi';
import { AccountMeta, Context, PublicKey, Option } from '@metaplex-foundation/umi';
import {
lifecycleHookFromBase,
LifecycleHookInitInfoArgs,
Expand Down Expand Up @@ -50,25 +50,25 @@ export type ExternalPluginsList = {

export type ExternalPluginInitInfoArgs =
| ({
type: 'Oracle';
} & OracleInitInfoArgs)
type: 'Oracle';
} & OracleInitInfoArgs)
| ({
type: 'LifecycleHook';
} & LifecycleHookInitInfoArgs)
type: 'LifecycleHook';
} & LifecycleHookInitInfoArgs)
| ({
type: 'DataStore';
} & DataStoreInitInfoArgs);
type: 'DataStore';
} & DataStoreInitInfoArgs);

export type ExternalPluginUpdateInfoArgs =
| ({
type: 'Oracle';
} & OracleUpdateInfoArgs)
type: 'Oracle';
} & OracleUpdateInfoArgs)
| ({
type: 'LifecycleHook';
} & LifecycleHookUpdateInfoArgs)
type: 'LifecycleHook';
} & LifecycleHookUpdateInfoArgs)
| ({
type: 'DataStore';
} & DataStoreUpdateInfoArgs);
type: 'DataStore';
} & DataStoreUpdateInfoArgs);

export const externalPluginManifests = {
Oracle: oracleManifest,
Expand Down Expand Up @@ -110,7 +110,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.oracles.push({
type: 'Oracle',
...mappedPlugin,
...oracleFromBase(deserializedPlugin.fields[0], accountData),
...oracleFromBase(deserializedPlugin.fields[0], record, accountData),
});
} else if (deserializedPlugin.__kind === 'DataStore') {
if (!result.dataStores) {
Expand All @@ -119,7 +119,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.dataStores.push({
type: 'DataStore',
...mappedPlugin,
...dataStoreFromBase(deserializedPlugin.fields[0], accountData),
...dataStoreFromBase(deserializedPlugin.fields[0], record, accountData),
});
} else if (deserializedPlugin.__kind === 'LifecycleHook') {
if (!result.lifecycleHooks) {
Expand All @@ -128,7 +128,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.lifecycleHooks.push({
type: 'LifecycleHook',
...mappedPlugin,
...lifecycleHookFromBase(deserializedPlugin.fields[0], accountData),
...lifecycleHookFromBase(deserializedPlugin.fields[0], record, accountData),
});
}
});
Expand Down Expand Up @@ -232,15 +232,17 @@ export const findExtraAccounts = (
export function parseExternalPluginData(
plugin: {
schema: ExternalPluginSchema;
dataLen: bigint | number;
dataOffset: bigint | number;
},
record: {
dataLen: Option<bigint | number>;
dataOffset: Option<bigint | number>;
},
account: Uint8Array
): any {
let data;
const dataSlice = account.slice(
Number(plugin.dataOffset),
Number(plugin.dataOffset) + Number(plugin.dataLen)
Number(record.dataOffset),
Number(record.dataOffset) + Number(record.dataLen)
);

if (plugin.schema === ExternalPluginSchema.Binary) {
Expand Down
6 changes: 3 additions & 3 deletions clients/js/src/plugins/lifecycleHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BaseLifecycleHookInitInfoArgs,
BaseLifecycleHookUpdateInfoArgs,
ExternalPluginSchema,
ExternalRegistryRecord,
} from '../generated';
import { LifecycleChecks, lifecycleChecksToBase } from './lifecycleChecks';
import {
Expand Down Expand Up @@ -95,17 +96,16 @@ export function lifecycleHookUpdateInfoArgsToBase(

export function lifecycleHookFromBase(
s: BaseLifecycleHook,
r: ExternalRegistryRecord,
account: Uint8Array
): LifecycleHook {
return {
...s,
dataOffset: s.dataOffset,
dataLen: s.dataLen,
extraAccounts:
s.extraAccounts.__option === 'Some'
? s.extraAccounts.value.map(extraAccountFromBase)
: undefined,
data: parseExternalPluginData(s, account),
data: parseExternalPluginData(s, r, account),
dataAuthority:
s.dataAuthority.__option === 'Some'
? pluginAuthorityFromBase(s.dataAuthority.value)
Expand Down
3 changes: 2 additions & 1 deletion clients/js/src/plugins/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BaseOracle,
BaseOracleInitInfoArgs,
BaseOracleUpdateInfoArgs,
ExternalRegistryRecord,
} from '../generated';
import { LifecycleChecks, lifecycleChecksToBase } from './lifecycleChecks';
import { PluginAuthority, pluginAuthorityToBase } from './pluginAuthority';
Expand Down Expand Up @@ -82,7 +83,7 @@ export function oracleUpdateInfoArgsToBase(
};
}

export function oracleFromBase(s: BaseOracle, account: Uint8Array): Oracle {
export function oracleFromBase(s: BaseOracle, r: ExternalRegistryRecord, account: Uint8Array): Oracle {
return {
...s,
pda:
Expand Down

0 comments on commit 2fbc51e

Please sign in to comment.