Skip to content

Commit

Permalink
Merge branch 'main' of github.com:blockiosaurus/blob-asset into main
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Mar 12, 2024
2 parents b473d31 + f04b125 commit 605642a
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 95 deletions.
50 changes: 47 additions & 3 deletions clients/js/src/hooked/pluginHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { none, some } from '@metaplex-foundation/umi';

import {
Key,
PluginHeader,
Plugin,
getPluginSerializer,
RegistryRecord,
PluginAuthorityPair,
Authority,
RoyaltiesArgs,
FreezeArgs,
BurnArgs,
TransferArgs,
UpdateDelegateArgs,
PermanentFreezeArgs,
} from '../generated';
import { BaseAuthority, PluginsList } from './types';
import { mapAuthority } from './authorityHelpers';
Expand All @@ -18,20 +28,54 @@ export function formPluginHeader(
};
}

export type PluginAuthorityPairHelperArgs = {
type: 'Royalties'
authority?: Authority
data: RoyaltiesArgs
} | {
type: 'Freeze'
authority?: Authority
data: FreezeArgs
} | {
type: 'Burn'
authority?: Authority
data?: BurnArgs
} | {
type: 'Transfer'
authority?: Authority
data?: TransferArgs
} | {
type: 'UpdateDelegate'
authority?: Authority
data?: UpdateDelegateArgs
} | {
type: 'PermanentFreeze'
authority?: Authority
data: PermanentFreezeArgs
};

export function pluginAuthorityPair( args: PluginAuthorityPairHelperArgs): PluginAuthorityPair {
const { type, authority, data } = args;
return {
plugin: { __kind: type, fields: [data as any || {}] },
authority: authority ? some(authority) : none(),
};
}

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

export function mapPlugin({
plugin,
plugin: plug,
authority,
offset,
}: {
plugin: Exclude<Plugin, { __kind: 'Reserved' }>;
authority: BaseAuthority;
offset: bigint;
}): PluginsList {
const pluginKey = toWords(plugin.__kind)
const pluginKey = toWords(plug.__kind)
.toLowerCase()
.split(' ')
.reduce((s, c) => s + (c.charAt(0).toUpperCase() + c.slice(1)));
Expand All @@ -40,7 +84,7 @@ export function mapPlugin({
[pluginKey]: {
authority,
offset,
...('fields' in plugin ? mapPluginFields(plugin.fields) : {}),
...('fields' in plug ? mapPluginFields(plug.fields) : {}),
},
};
}
Expand Down
7 changes: 5 additions & 2 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@metaplex-foundation/umi';
import test from 'ava';

import { burn, Key, updateAuthority, plugin } from '../src';
import { burn, Key, updateAuthority, pluginAuthorityPair } from '../src';
import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup';

test('it can burn an asset as the owner', async (t) => {
Expand Down Expand Up @@ -65,7 +65,10 @@ test('it cannot burn an asset if it is frozen', async (t) => {

const asset = await createAsset(umi, {
plugins: [
{ plugin: plugin('Freeze', [{ frozen: true }]), authority: null },
pluginAuthorityPair({
type: 'Freeze',
data: { frozen: true }
}),
],
});

Expand Down
7 changes: 5 additions & 2 deletions clients/js/test/collect.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PublicKey, Umi, sol } from '@metaplex-foundation/umi';
import test from 'ava';

import { PluginType, addPlugin, plugin, removePlugin } from '../src';
import { PluginType, addPlugin, plugin, pluginAuthorityPair, removePlugin } from '../src';
import { createAsset, createUmi } from './_setup';

const hasCollectAmount = async (umi: Umi, address: PublicKey) => {
Expand Down Expand Up @@ -47,7 +47,10 @@ test('it can add remove asset plugin with collect amount', async (t) => {
const umi = await createUmi();
const asset = await createAsset(umi, {
plugins: [
{ plugin: plugin('Freeze', [{ frozen: true }]), authority: null },
pluginAuthorityPair({
type: 'Freeze',
data: { frozen: true },
})
],
});

Expand Down
17 changes: 9 additions & 8 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
fetchHashedAsset,
getAssetAccountDataSerializer,
updateAuthority,
plugin,
pluginAuthorityPair,
} from '../src';
import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup';

Expand Down Expand Up @@ -121,12 +121,10 @@ test('it can create a new asset in account state with plugins', async (t) => {
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [
{
plugin: plugin('Freeze', [{ frozen: false }]),
authority: null,
},
],
plugins: [ pluginAuthorityPair({
type: 'Freeze',
data: { frozen: false },
})],
}).sendAndConfirm(umi);

await assertAsset(t, umi, {
Expand Down Expand Up @@ -175,7 +173,10 @@ test('it can create a new asset in account state with plugins with a different u
asset: assetAddress,
updateAuthority: updateAuth.publicKey,
plugins: [
{ plugin: plugin('Freeze', [{ frozen: false }]), authority: null },
pluginAuthorityPair({
type: 'Freeze',
data: { frozen: false },
})
],
});

Expand Down
15 changes: 11 additions & 4 deletions clients/js/test/createCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PluginType,
approveCollectionPluginAuthority,
authority,
plugin,
pluginAuthorityPair,
updateAuthority,
} from '../src';
import {
Expand Down Expand Up @@ -40,7 +40,10 @@ test('it can create a new collection with plugins', async (t) => {

const collection = await createCollection(umi, {
plugins: [
{ plugin: plugin('Freeze', [{ frozen: false }]), authority: null },
pluginAuthorityPair({
type: 'Freeze',
data: { frozen: false },
}),
],
});

Expand All @@ -64,7 +67,7 @@ test('it can create a new asset with a collection', async (t) => {
umi,
{},
{
plugins: [{ plugin: plugin('UpdateDelegate', [{}]), authority: null }],
plugins: [ pluginAuthorityPair({ type: 'UpdateDelegate' })]
}
);

Expand Down Expand Up @@ -93,7 +96,11 @@ test('it can create a new asset with a collection with collection delegate', asy
const delegate = generateSigner(umi);

const collection = await createCollection(umi, {
plugins: [{ plugin: plugin('UpdateDelegate', [{}]), authority: null }],
plugins: [
pluginAuthorityPair({
type: 'UpdateDelegate',
})
]
});

await approveCollectionPluginAuthority(umi, {
Expand Down
5 changes: 3 additions & 2 deletions clients/js/test/plugins/asset/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
updatePlugin,
plugin,
authority,
pluginAuthorityPair,
} from '../../../src';
import {
DEFAULT_ASSET,
Expand All @@ -22,7 +23,7 @@ test('it can delegate a new authority', async (t) => {

const asset = await createAsset(umi, {
plugins: [
{ plugin: plugin('Freeze', [{ frozen: false }]), authority: null },
pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}),
],
});

Expand Down Expand Up @@ -54,7 +55,7 @@ test('a delegate can freeze the token', async (t) => {

const asset = await createAsset(umi, {
plugins: [
{ plugin: plugin('Freeze', [{ frozen: false }]), authority: null },
pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}),
],
});

Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/plugins/asset/delegateTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
approvePluginAuthority,
transfer,
updateAuthority,
plugin,
authority,
pluginAuthorityPair,
} from '../../../src';
import {
DEFAULT_ASSET,
Expand All @@ -22,7 +22,7 @@ test('a delegate can transfer the asset', async (t) => {
const newOwnerAddress = generateSigner(umi);

const asset = await createAsset(umi, {
plugins: [{ plugin: plugin('Transfer', [{}]), authority: null }],
plugins: [pluginAuthorityPair({ type: 'Transfer' }),],
});

await approvePluginAuthority(umi, {
Expand Down
10 changes: 7 additions & 3 deletions clients/js/test/plugins/asset/permanentFreeze.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import { generateSigner } from '@metaplex-foundation/umi';
import { addPlugin, plugin, transfer, updateAuthority, updatePlugin } from '../../../src';
import { addPlugin, plugin, pluginAuthorityPair, transfer, updateAuthority, updatePlugin } from '../../../src';
import {
DEFAULT_ASSET,
assertAsset,
Expand All @@ -15,7 +15,9 @@ test('it can freeze and unfreeze an asset', async (t) => {

const asset = await createAsset(umi, {
owner,
plugins: [{ plugin: plugin('PermanentFreeze', [{ frozen: true }]), authority: null }]
plugins: [
pluginAuthorityPair({ type: 'PermanentFreeze', data: { frozen: true }})
]
});

await assertAsset(t, umi, {
Expand Down Expand Up @@ -58,7 +60,9 @@ test('it cannot be transferred while frozen', async (t) => {

const asset = await createAsset(umi, {
owner,
plugins: [{ plugin: plugin('PermanentFreeze', [{ frozen: true }]), authority: null }]
plugins: [
pluginAuthorityPair({ type: 'PermanentFreeze', data: { frozen: true }}),
]
});

await assertAsset(t, umi, {
Expand Down
Loading

0 comments on commit 605642a

Please sign in to comment.