Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine plugin updating into single utility function #113

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clients/js/src/instructions/addPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import {
pluginAuthorityPairV2,
} from '../plugins';

export type AddPluginArgsPlugin =
| AddablePluginAuthorityPairArgsV2
| ExternalPluginInitInfoArgs;

export type AddPluginArgs = Omit<
Parameters<typeof addPluginV1>[1],
'plugin'
> & {
plugin: AddablePluginAuthorityPairArgsV2 | ExternalPluginInitInfoArgs;
plugin: AddPluginArgsPlugin;
};

export const addPlugin = (
Expand Down
30 changes: 8 additions & 22 deletions clients/js/src/instructions/approvePluginAuthority.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
import { Context } from '@metaplex-foundation/umi';
import { approvePluginAuthorityV1, PluginType } from '../generated';
import {
isExternalPluginType,
PluginAuthority,
pluginAuthorityToBase,
} from '../plugins';
import { ExternalPluginKey } from '../plugins/externalPluginKey';
import { PluginAuthority, pluginAuthorityToBase } from '../plugins';

export type ApprovePluginAuthorityArgsPlugin = {
type: keyof typeof PluginType;
};

export type ApprovePluginAuthorityArgs = Omit<
Parameters<typeof approvePluginAuthorityV1>[1],
'pluginType' | 'newAuthority'
> & {
plugin:
| {
type: keyof typeof PluginType;
}
| ExternalPluginKey;
plugin: ApprovePluginAuthorityArgsPlugin;
newAuthority: PluginAuthority;
};

export const approvePluginAuthority = (
context: Pick<Context, 'payer' | 'programs' | 'identity'>,
{ plugin, newAuthority, ...args }: ApprovePluginAuthorityArgs
) => {
if (isExternalPluginType(plugin)) {
// TODO implement this
// return approveExternalPluginAuthorityV1(context, {
// ...args,
// key: externalPluginKeyToBase(plugin as ExternalPluginKey),
// });
}

return approvePluginAuthorityV1(context, {
) =>
approvePluginAuthorityV1(context, {
...args,
pluginType: PluginType[plugin.type as keyof typeof PluginType],
newAuthority: pluginAuthorityToBase(newAuthority),
});
};
4 changes: 2 additions & 2 deletions clients/js/src/instructions/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export type BurnArgs = Omit<
Parameters<typeof burnV1>[1],
'asset' | 'collection'
> & {
asset: AssetV1;
collection?: CollectionV1;
asset: Pick<AssetV1, 'publicKey' | 'owner' | 'oracles' | 'lifecycleHooks'>;
collection?: Pick<CollectionV1, 'publicKey' | 'oracles' | 'lifecycleHooks'>;
};

export const burn = (
Expand Down
14 changes: 9 additions & 5 deletions clients/js/src/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CollectionV1, createV2, ExternalPluginSchema } from '../generated';
import {
createExternalPluginInitInfo,
findExtraAccounts,
PluginArgsV2,
PluginAuthorityPairArgsV2,
pluginAuthorityPairV2,
} from '../plugins';
import { deriveExternalPlugins } from '../helpers';
Expand All @@ -13,12 +13,16 @@ import {
isExternalPluginType,
} from '../plugins/externalPlugins';

export type CreateArgsPlugin =
| PluginAuthorityPairArgsV2
| ExternalPluginInitInfoArgs;

export type CreateArgs = Omit<
Parameters<typeof createV2>[1],
'plugins' | 'externalPlugins' | 'collection'
> & {
collection?: CollectionV1;
plugins?: (PluginArgsV2 | ExternalPluginInitInfoArgs)[];
collection?: Pick<CollectionV1, 'publicKey' | 'oracles' | 'lifecycleHooks'>;
plugins?: CreateArgsPlugin[];
};

export const create = (
Expand All @@ -33,7 +37,7 @@ export const create = (
};

const externalPlugins: ExternalPluginInitInfoArgs[] = [];
const firstPartyPlugins: PluginArgsV2[] = [];
const firstPartyPlugins: PluginAuthorityPairArgsV2[] = [];

// Create dummy external plugins to resuse findExtraAccounts method
plugins?.forEach((plugin) => {
Expand Down Expand Up @@ -69,7 +73,7 @@ export const create = (
// Do nothing
}
} else {
firstPartyPlugins.push(plugin as PluginArgsV2);
firstPartyPlugins.push(plugin as PluginAuthorityPairArgsV2);
}
});

Expand Down
12 changes: 7 additions & 5 deletions clients/js/src/instructions/removePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
externalPluginKeyToBase,
} from '../plugins/externalPluginKey';

export type RemovePluginArgsPlugin =
| {
type: Exclude<keyof typeof PluginType, 'Edition'>;
}
| ExternalPluginKey;

export type RemovePluginArgs = Omit<
Parameters<typeof removePluginV1>[1],
'pluginType'
> & {
plugin:
| {
type: Exclude<keyof typeof PluginType, 'Edition'>;
}
| ExternalPluginKey;
plugin: RemovePluginArgsPlugin;
};

export const removePlugin = (
Expand Down
25 changes: 7 additions & 18 deletions clients/js/src/instructions/revokePluginAuthority.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { Context } from '@metaplex-foundation/umi';
import { revokePluginAuthorityV1, PluginType } from '../generated';
import { isExternalPluginType } from '../plugins';
import { ExternalPluginKey } from '../plugins/externalPluginKey';

export type RevokePluginAuthorityArgsPlugin = {
type: keyof typeof PluginType;
};

export type RevokePluginAuthorityArgs = Omit<
Parameters<typeof revokePluginAuthorityV1>[1],
'pluginType'
> & {
plugin:
| {
type: keyof typeof PluginType;
}
| ExternalPluginKey;
plugin: RevokePluginAuthorityArgsPlugin;
};

export const revokePluginAuthority = (
context: Pick<Context, 'payer' | 'programs' | 'identity'>,
{ plugin, ...args }: RevokePluginAuthorityArgs
) => {
if (isExternalPluginType(plugin)) {
// TODO implement this
// return revokeExternalPluginAuthorityV1(context, {
// ...args,
// key: externalPluginKeyToBase(plugin as ExternalPluginKey),
// });
}

return revokePluginAuthorityV1(context, {
) =>
revokePluginAuthorityV1(context, {
...args,
pluginType: PluginType[plugin.type as keyof typeof PluginType],
});
};
4 changes: 2 additions & 2 deletions clients/js/src/instructions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export type TransferArgs = Omit<
Parameters<typeof transferV1>[1],
'asset' | 'collection'
> & {
asset: AssetV1;
collection?: CollectionV1;
asset: Pick<AssetV1, 'publicKey' | 'owner' | 'oracles' | 'lifecycleHooks'>;
collection?: Pick<CollectionV1, 'publicKey' | 'oracles' | 'lifecycleHooks'>;
};

export const transfer = (
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/instructions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export type UpdateArgs = Omit<
Parameters<typeof updateV1>[1],
'asset' | 'collection' | 'newName' | 'newUri'
> & {
asset: AssetV1;
collection?: CollectionV1;
asset: Pick<AssetV1, 'publicKey' | 'owner' | 'oracles' | 'lifecycleHooks'>;
collection?: Pick<CollectionV1, 'publicKey' | 'oracles' | 'lifecycleHooks'>;
name?: UpdateV1InstructionDataArgs['newName'];
uri?: UpdateV1InstructionDataArgs['newUri'];
};
Expand Down
6 changes: 5 additions & 1 deletion clients/js/src/instructions/updatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
} from '../plugins';
import { ExternalPluginUpdateInfoArgs } from '../plugins/externalPlugins';

export type UpdatePluginArgsPlugin =
| PluginArgsV2
| ExternalPluginUpdateInfoArgs;

export type UpdatePluginArgs = Omit<
Parameters<typeof updatePluginV1>[1],
'plugin'
> & {
plugin: PluginArgsV2 | ExternalPluginUpdateInfoArgs;
plugin: UpdatePluginArgsPlugin;
};

export const updatePlugin = (
Expand Down
132 changes: 132 additions & 0 deletions clients/js/test/externalPlugins/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
update,
addPlugin,
updatePlugin,
fetchAssetV1,
} from '../../src';

const createUmi = async () =>
Expand Down Expand Up @@ -1788,3 +1789,134 @@ test('it can use one fixed address oracle to deny transfer when a second oracle
owner: newOwner.publicKey,
});
});

test('it can update with oracle', async (t) => {
const umi = await createUmi();
const oracleSigner = generateSigner(umi);
await fixedAccountInit(umi, {
signer: umi.identity,
account: oracleSigner,
args: {
oracleData: {
__kind: 'V1',
create: ExternalValidationResult.Pass,
transfer: ExternalValidationResult.Pass,
burn: ExternalValidationResult.Pass,
update: ExternalValidationResult.Rejected,
},
},
}).sendAndConfirm(umi);

const asset = generateSigner(umi);
await create(umi, {
asset,
name: 'Test name',
uri: 'https://example.com',
plugins: [
{
type: 'Oracle',
resultsOffset: {
type: 'Anchor',
},
lifecycleChecks: {
update: [CheckResult.CAN_REJECT],
},
baseAddress: oracleSigner.publicKey,
},
],
}).sendAndConfirm(umi);

await fixedAccountSet(umi, {
signer: umi.identity,
account: oracleSigner.publicKey,
args: {
oracleData: {
__kind: 'V1',
create: ExternalValidationResult.Pass,
transfer: ExternalValidationResult.Pass,
burn: ExternalValidationResult.Pass,
update: ExternalValidationResult.Pass,
},
},
}).sendAndConfirm(umi);

const a = await fetchAssetV1(umi, asset.publicKey);

await update(umi, {
asset: a,
name: 'name 2',
}).sendAndConfirm(umi);

await assertAsset(t, umi, {
uri: 'https://example.com',
name: 'name 2',
owner: umi.identity.publicKey,
asset: asset.publicKey,
});
});

test('it can update oracle to different size external plugin', async (t) => {
const umi = await createUmi();
const oracleSigner = generateSigner(umi);
await fixedAccountInit(umi, {
signer: umi.identity,
account: oracleSigner,
args: {
oracleData: {
__kind: 'V1',
create: ExternalValidationResult.Pass,
transfer: ExternalValidationResult.Pass,
burn: ExternalValidationResult.Pass,
update: ExternalValidationResult.Rejected,
},
},
}).sendAndConfirm(umi);

const asset = generateSigner(umi);
await create(umi, {
asset,
name: 'Test name',
uri: 'https://example.com',
plugins: [
{
type: 'Oracle',
resultsOffset: {
type: 'Anchor',
},
lifecycleChecks: {
create: [CheckResult.CAN_REJECT],
update: [CheckResult.CAN_REJECT],
transfer: [CheckResult.CAN_REJECT],
burn: [CheckResult.CAN_REJECT],
},
baseAddress: oracleSigner.publicKey,
},
],
}).sendAndConfirm(umi);

await updatePlugin(umi, {
asset: asset.publicKey,

plugin: {
key: {
type: 'Oracle',
baseAddress: oracleSigner.publicKey,
},
type: 'Oracle',
resultsOffset: {
type: 'Anchor',
},
lifecycleChecks: {
transfer: [CheckResult.CAN_REJECT],
},
},
}).sendAndConfirm(umi);

// TODO: Validate external plugins.
await assertAsset(t, umi, {
uri: 'https://example.com',
name: 'Test name',
owner: umi.identity.publicKey,
asset: asset.publicKey,
});
});
Loading