Skip to content

Commit

Permalink
Format ts code
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Feb 23, 2024
1 parent 952b2c4 commit 2348653
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 243 deletions.
90 changes: 60 additions & 30 deletions clients/js/src/hooked/fetchAssetWithPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,70 @@
import { Context, Pda, PublicKey, RpcGetAccountOptions, assertAccountExists, publicKey as toPublicKey } from "@metaplex-foundation/umi";
import { Asset, Authority, Plugin, PluginHeader, PluginRegistry, deserializeAsset, getAssetAccountDataSerializer, getPluginHeaderAccountDataSerializer, getPluginRegistryAccountDataSerializer, getPluginSerializer } from "../generated";
import {
Context,
Pda,
PublicKey,
RpcGetAccountOptions,
assertAccountExists,
publicKey as toPublicKey,
} from '@metaplex-foundation/umi';
import {
Asset,
Authority,
Plugin,
PluginHeader,
PluginRegistry,
deserializeAsset,
getAssetAccountDataSerializer,
getPluginHeaderAccountDataSerializer,
getPluginRegistryAccountDataSerializer,
getPluginSerializer,
} from '../generated';

export type PluginWithAuthorities = { plugin: Plugin, authorities: Authority[] };
export type PluginWithAuthorities = {
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 pluginHeader = getPluginHeaderAccountDataSerializer().deserialize(maybeAccount.data, assetData.length)[0];
const pluginRegistry = getPluginRegistryAccountDataSerializer().deserialize(maybeAccount.data, Number(pluginHeader.pluginRegistryOffset))[0];
const plugins = pluginRegistry.registry.map((record) => ({
plugin: getPluginSerializer().deserialize(maybeAccount.data, Number(record.data.offset))[0],
authorities: record.data.authorities,
}));
const maybeAccount = await context.rpc.getAccount(
toPublicKey(publicKey, false),
options
);
assertAccountExists(maybeAccount, 'Asset');
const asset = deserializeAsset(maybeAccount);
const assetData = getAssetAccountDataSerializer().serialize(asset);
const pluginHeader = getPluginHeaderAccountDataSerializer().deserialize(
maybeAccount.data,
assetData.length
)[0];
const pluginRegistry = getPluginRegistryAccountDataSerializer().deserialize(
maybeAccount.data,
Number(pluginHeader.pluginRegistryOffset)
)[0];
const 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;
}
2 changes: 1 addition & 1 deletion clients/js/src/hooked/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './fetchAssetWithPlugins';
export * from './fetchAssetWithPlugins';
3 changes: 1 addition & 2 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-tests';
import { mplAsset } from '../src';

export const createUmi = async () =>
(await basecreateUmi()).use(mplAsset());
export const createUmi = async () => (await basecreateUmi()).use(mplAsset());
78 changes: 47 additions & 31 deletions clients/js/test/addAuthority.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, AssetWithPlugins, DataState, PluginType, addAuthority, addPlugin, create, fetchAsset, fetchAssetWithPlugins } from '../src';
import {
Asset,
AssetWithPlugins,
DataState,
PluginType,
addAuthority,
addPlugin,
create,
fetchAsset,
fetchAssetWithPlugins,
} from '../src';
import { createUmi } from './_setup';

test('it can add an authority to a plugin', async (t) => {
Expand Down Expand Up @@ -34,17 +44,19 @@ test('it can add an authority to a plugin', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
}).append(
addAuthority(umi, {
assetAddress: assetAddress.publicKey,
pluginType: PluginType.Freeze,
newAuthority: {
__kind: 'Pubkey',
address: delegateAddress.publicKey,
}
})
).sendAndConfirm(umi);
},
})
.append(
addAuthority(umi, {
assetAddress: assetAddress.publicKey,
pluginType: PluginType.Freeze,
newAuthority: {
__kind: 'Pubkey',
address: delegateAddress.publicKey,
},
})
)
.sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log(JSON.stringify(asset1, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
Expand All @@ -60,26 +72,30 @@ test('it can add an authority to a plugin', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [
{ __kind: "Owner" },
{ __kind: "Pubkey", address: delegateAddress.publicKey }
]
}
}],
},
plugins: [{
authorities: [
{ __kind: "Owner" },
{ __kind: "Pubkey", address: delegateAddress.publicKey }
registry: [
{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [
{ __kind: 'Owner' },
{ __kind: 'Pubkey', address: delegateAddress.publicKey },
],
},
},
],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
plugins: [
{
authorities: [
{ __kind: 'Owner' },
{ __kind: 'Pubkey', address: delegateAddress.publicKey },
],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});
42 changes: 27 additions & 15 deletions clients/js/test/addPlugin.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 { Asset, AssetWithPlugins, DataState, addPlugin, create, fetchAsset, fetchAssetWithPlugins } from '../src';
import {
Asset,
AssetWithPlugins,
DataState,
addPlugin,
create,
fetchAsset,
fetchAssetWithPlugins,
} from '../src';
import { createUmi } from './_setup';

test('it can add a plugin to an asset', async (t) => {
Expand Down Expand Up @@ -33,7 +41,7 @@ test('it can add a plugin to an asset', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
Expand All @@ -50,20 +58,24 @@ test('it can add a plugin to an asset', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [{ __kind: "Owner" }]
}
}],
registry: [
{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [{ __kind: 'Owner' }],
},
},
],
},
plugins: [{
authorities: [{ __kind: "Owner" }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
plugins: [
{
authorities: [{ __kind: 'Owner' }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});
10 changes: 7 additions & 3 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertAccountExists, generateSigner, sol} from '@metaplex-foundation/umi';
import {
assertAccountExists,
generateSigner,
sol,
} from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, DataState, create, fetchAsset, burn, Key } from '../src';
Expand Down Expand Up @@ -30,7 +34,7 @@ test('it can burn an asset as the owner', async (t) => {

await burn(umi, {
assetAddress: assetAddress.publicKey,
compressionProof: null
compressionProof: null,
}).sendAndConfirm(umi);

// And the asset address still exists but was resized to 1.
Expand Down Expand Up @@ -73,7 +77,7 @@ test('it cannot burn an asset if not the owner', async (t) => {
authority: attacker,
}).sendAndConfirm(umi);

await t.throwsAsync(result, { name: 'InvalidAuthority' })
await t.throwsAsync(result, { name: 'InvalidAuthority' });

const afterAsset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", afterAsset);
Expand Down
12 changes: 9 additions & 3 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { generateSigner, publicKey } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, DataState, create, fetchAsset, fetchHashedAsset, getAssetAccountDataSerializer } from '../src';
import {
Asset,
DataState,
create,
fetchAsset,
fetchHashedAsset,
getAssetAccountDataSerializer,
} from '../src';
import { createUmi } from './_setup';

test('it can create a new asset in account state', async (t) => {
Expand Down Expand Up @@ -40,7 +47,7 @@ test('it can create a new asset in ledger state', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
logWrapper: publicKey("noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV")
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand All @@ -64,5 +71,4 @@ test('it can create a new asset in ledger state', async (t) => {
uri: 'https://example.com/bread',
});
}

});
Loading

0 comments on commit 2348653

Please sign in to comment.