Skip to content

Commit

Permalink
Prettier fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 26, 2024
1 parent 7bb3d6c commit 8c6b107
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 87 deletions.
115 changes: 62 additions & 53 deletions clients/js/src/hooked/fetchAssetWithPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,78 @@
import {
Context,
Pda,
PublicKey,
RpcGetAccountOptions,
assertAccountExists,
publicKey as toPublicKey
} from "@metaplex-foundation/umi";
Context,
Pda,
PublicKey,
RpcGetAccountOptions,
assertAccountExists,
publicKey as toPublicKey,
} from '@metaplex-foundation/umi';
import {
Asset,
Authority,
Plugin,
PluginHeader,
PluginHeaderAccountData,
PluginRegistry,
PluginRegistryAccountData,
deserializeAsset,
getAssetAccountDataSerializer,
getPluginHeaderAccountDataSerializer,
getPluginRegistryAccountDataSerializer,
getPluginSerializer
} from "../generated";
Asset,
Authority,
Plugin,
PluginHeader,
PluginHeaderAccountData,
PluginRegistry,
PluginRegistryAccountData,
deserializeAsset,
getAssetAccountDataSerializer,
getPluginHeaderAccountDataSerializer,
getPluginRegistryAccountDataSerializer,
getPluginSerializer,
} from '../generated';

export type PluginWithAuthorities = {
plugin: Plugin;
authorities: Authority[];
plugin: Plugin;
authorities: Authority[];
};

export type PluginList = {
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>,
plugins?: PluginWithAuthorities[],
pluginRegistry?: Omit<PluginRegistry, 'publicKey' | 'header'>,
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>;
plugins?: PluginWithAuthorities[];
pluginRegistry?: Omit<PluginRegistry, 'publicKey' | 'header'>;
};
export type AssetWithPlugins = Asset & PluginList;

export async function fetchAssetWithPlugins(
context: Pick<Context, 'rpc'>,
publicKey: PublicKey | Pda,
options?: RpcGetAccountOptions
context: Pick<Context, 'rpc'>,
publicKey: PublicKey | Pda,
options?: RpcGetAccountOptions
): Promise<AssetWithPlugins> {
const maybeAccount = await context.rpc.getAccount(
toPublicKey(publicKey, false),
options
);
assertAccountExists(maybeAccount, 'Asset');
const asset = deserializeAsset(maybeAccount);
const assetData = getAssetAccountDataSerializer().serialize(asset);
const maybeAccount = await context.rpc.getAccount(
toPublicKey(publicKey, false),
options
);
assertAccountExists(maybeAccount, 'Asset');
const asset = deserializeAsset(maybeAccount);
const assetData = getAssetAccountDataSerializer().serialize(asset);

let pluginHeader: PluginHeaderAccountData | undefined;
let pluginRegistry: PluginRegistryAccountData | undefined;
let plugins: PluginWithAuthorities[] | 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.data.offset))[0],
authorities: record.data.authorities,
}));
}
let pluginHeader: PluginHeaderAccountData | undefined;
let pluginRegistry: PluginRegistryAccountData | undefined;
let plugins: PluginWithAuthorities[] | 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.data.offset)
)[0],
authorities: record.data.authorities,
}));
}

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

return assetWithPlugins;
return assetWithPlugins;
}
84 changes: 50 additions & 34 deletions clients/js/test/update.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { AssetWithPlugins, DataState, Key, addPlugin, create, fetchAssetWithPlugins, update } from '../src';
import {
AssetWithPlugins,
DataState,
Key,
addPlugin,
create,
fetchAssetWithPlugins,
update,
} from '../src';
import { createUmi } from './_setup';

test('it can update an asset to be larger', async (t) => {
Expand Down Expand Up @@ -29,8 +37,8 @@ test('it can update an asset to be larger', async (t) => {
key: Key.Asset,
updateAuthority: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: "Test Bread 2",
uri: "https://example.com/bread2"
name: 'Test Bread 2',
uri: 'https://example.com/bread2',
});
});

Expand Down Expand Up @@ -59,8 +67,8 @@ test('it can update an asset to be smaller', async (t) => {
key: Key.Asset,
updateAuthority: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: "",
uri: ""
name: '',
uri: '',
});
});

Expand All @@ -82,7 +90,7 @@ test('it can update an asset with plugins to be larger', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
}).sendAndConfirm(umi);

await update(umi, {
Expand All @@ -106,21 +114,25 @@ test('it can update an asset with plugins to be larger', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(120),
authorities: [{ __kind: "Owner" }]
}
}],
registry: [
{
pluginType: 2,
data: {
offset: BigInt(120),
authorities: [{ __kind: 'Owner' }],
},
},
],
},
plugins: [{
authorities: [{ __kind: "Owner" }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
plugins: [
{
authorities: [{ __kind: 'Owner' }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});

Expand All @@ -142,7 +154,7 @@ test('it can update an asset with plugins to be smaller', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
}).sendAndConfirm(umi);

await update(umi, {
Expand All @@ -165,20 +177,24 @@ test('it can update an asset with plugins to be smaller', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(82),
authorities: [{ __kind: "Owner" }]
}
}],
registry: [
{
pluginType: 2,
data: {
offset: BigInt(82),
authorities: [{ __kind: 'Owner' }],
},
},
],
},
plugins: [{
authorities: [{ __kind: "Owner" }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
plugins: [
{
authorities: [{ __kind: 'Owner' }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});
});

0 comments on commit 8c6b107

Please sign in to comment.