Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Jun 11, 2024
1 parent e8d173f commit 16c50a3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 68 deletions.
18 changes: 13 additions & 5 deletions src/das.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
DasApiAssetInterface,
SearchAssetsRpcInput,
} from '@metaplex-foundation/digital-asset-standard-api';
import { AssetV1, CollectionV1, deriveAssetPluginsWithFetch } from '@metaplex-foundation/mpl-core';
import {
AssetV1,
CollectionV1,
deriveAssetPluginsWithFetch,
} from '@metaplex-foundation/mpl-core';
import { MPL_CORE_ASSET, MPL_CORE_COLLECTION } from './constants';
import { AssetOptions, Pagination } from './types';
import { dasAssetToCoreAssetOrCollection } from './helpers';
Expand Down Expand Up @@ -54,7 +58,8 @@ function getAssetsByOwner(
context: Umi,
input: {
owner: PublicKey;
} & Pagination & AssetOptions
} & Pagination &
AssetOptions
) {
return searchAssets(context, {
...input,
Expand All @@ -66,7 +71,8 @@ function getAssetsByAuthority(
context: Umi,
input: {
authority: PublicKey;
} & Pagination & AssetOptions
} & Pagination &
AssetOptions
) {
return searchAssets(context, {
...input,
Expand All @@ -78,7 +84,8 @@ function getAssetsByCollection(
context: Umi,
input: {
collection: PublicKey;
} & Pagination & AssetOptions
} & Pagination &
AssetOptions
) {
return searchAssets(context, {
...input,
Expand All @@ -90,7 +97,8 @@ function getCollectionsByUpdateAuthority(
context: Umi,
input: {
updateAuthority: PublicKey;
} & Pagination & AssetOptions
} & Pagination &
AssetOptions
) {
return searchCollections(context, {
...input,
Expand Down
124 changes: 76 additions & 48 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ function getRuleSet(dasRuleSet: string | Record<string, any>): RuleSet {
const ruleSetKind = isRuleSetString ? dasRuleSet : Object.keys(dasRuleSet)[0];
const ruleSetData: PublicKey[] =
!isRuleSetString && ruleSetKind
? dasRuleSet[ruleSetKind].map((bytes: any) => publicKey(typeof bytes === 'string' ? bytes : new Uint8Array(bytes)))
? dasRuleSet[ruleSetKind].map((bytes: any) =>
publicKey(typeof bytes === 'string' ? bytes : new Uint8Array(bytes))
)
: [];

// RuleSet has both __kind and type for backwards compatibility
Expand All @@ -134,7 +136,7 @@ function getRuleSet(dasRuleSet: string | Record<string, any>): RuleSet {
}

function parseExtraAccount(data: any): ExtraAccount | undefined {
let result: ExtraAccount | undefined
let result: ExtraAccount | undefined;
Object.keys(data).forEach((key) => {
const acc = data[key];
const type = convertSnakeCase(key, 'pascal');
Expand All @@ -148,15 +150,15 @@ function parseExtraAccount(data: any): ExtraAccount | undefined {
type,
isSigner: acc.is_signer,
isWritable: acc.is_writable,
}
};
break;
case 'Address':
result = {
type,
isSigner: acc.is_signer,
isWritable: acc.is_writable,
address: publicKey(acc.address),
}
};
break;
case 'CustomPda':
result = {
Expand All @@ -167,34 +169,37 @@ function parseExtraAccount(data: any): ExtraAccount | undefined {
if (typeof seed === 'string') {
return {
type: seed,
}
};
}
if (seed.address) {
return {
type: 'Address',
pubkey: publicKey(seed.address),
}
};
}
if (seed.bytes) {
return {
type: 'Bytes',
bytes: new Uint8Array(seed.bytes),
}
};
}
return null
return null;
}),
customProgramId: acc.custom_program_id ? publicKey(acc.custom_program_id) : undefined,
}
customProgramId: acc.custom_program_id
? publicKey(acc.custom_program_id)
: undefined,
};
break;
default:
}
})
}
});

return result
return result;
}

function parseLifecycleChecks(data: any): LifecycleChecks {
return Object.keys(data).reduce((acc: LifecycleChecks, key) => ({
return Object.keys(data).reduce(
(acc: LifecycleChecks, key) => ({
...acc,
[key]: data[key].map((check: string) => {
switch (check) {
Expand All @@ -206,43 +211,65 @@ function parseLifecycleChecks(data: any): LifecycleChecks {
default:
return CheckResult.CAN_REJECT;
}
})
}), {})
}),
}),
{}
);
}

function dasExternalPluginsToCoreExternalPlugins(
externalPlugins: Record<string, any>[]
) {
return externalPlugins.reduce((acc: ExternalPluginAdaptersList, externalPlugin) => {
const { authority, offset, type, adapter_config: adapterConfig } = externalPlugin;
const authorityAddress = authority?.address;
return externalPlugins.reduce(
(acc: ExternalPluginAdaptersList, externalPlugin) => {
const {
authority,
offset,
type,
adapter_config: adapterConfig,
} = externalPlugin;
const authorityAddress = authority?.address;

if (type === 'Oracle') {
if (!acc.oracles) {
acc.oracles = [];
}
if (type === 'Oracle') {
if (!acc.oracles) {
acc.oracles = [];
}

acc.oracles.push({
type: 'Oracle',
authority: {
type: authority.type,
...(authorityAddress ? { address: publicKey(authorityAddress) } : {}),
},
baseAddress: adapterConfig.base_address,
resultsOffset: typeof adapterConfig.results_offset === 'string' ? {
type: convertSnakeCase(adapterConfig.results_offset, 'pascal') as any
} : {
type: 'Custom',
offset: BigInt(adapterConfig.results_offset.custom)
},
baseAddressConfig: adapterConfig.base_address_config ? parseExtraAccount(adapterConfig.base_address_config) : undefined,
lifecycleChecks: externalPlugin.lifecycle_checks ? parseLifecycleChecks(externalPlugin.lifecycle_checks): undefined,
offset: BigInt(offset),
});
}
acc.oracles.push({
type: 'Oracle',
authority: {
type: authority.type,
...(authorityAddress
? { address: publicKey(authorityAddress) }
: {}),
},
baseAddress: adapterConfig.base_address,
resultsOffset:
typeof adapterConfig.results_offset === 'string'
? {
type: convertSnakeCase(
adapterConfig.results_offset,
'pascal'
) as any,
}
: {
type: 'Custom',
offset: BigInt(adapterConfig.results_offset.custom),
},
baseAddressConfig: adapterConfig.base_address_config
? parseExtraAccount(adapterConfig.base_address_config)
: undefined,
lifecycleChecks: externalPlugin.lifecycle_checks
? parseLifecycleChecks(externalPlugin.lifecycle_checks)
: undefined,
offset: BigInt(offset),
});
}

return acc;
}, {});
return acc;
},
{}
);
}

function dasPluginDataToCorePluginData(
Expand All @@ -262,8 +289,7 @@ function dasPluginDataToCorePluginData(
| AddBlocker
| ImmutableMetadata
| Autograph
| VerifiedCreators
{
| VerifiedCreators {
// TODO: Refactor when DAS types are defined
return (({
basis_points,
Expand All @@ -276,7 +302,7 @@ function dasPluginDataToCorePluginData(
max_supply,
name,
uri,
signatures
signatures,
}) => ({
...(basis_points !== undefined ? { basisPoints: basis_points } : {}),
...(creators !== undefined
Expand All @@ -297,7 +323,7 @@ function dasPluginDataToCorePluginData(
...(max_supply !== undefined ? { maxSupply: max_supply } : {}),
...(name !== undefined ? { name } : {}),
...(uri !== undefined ? { uri } : {}),
...(signatures !== undefined ? { signatures } : {})
...(signatures !== undefined ? { signatures } : {}),
}))(dasPluginData);
}

Expand Down Expand Up @@ -397,7 +423,9 @@ export function dasAssetToCoreAssetOrCollection(
name,
...getAccountHeader(executable, lamps, rentEpoch),
...dasPluginsToCorePlugins(plugins),
...(externalPlugins !== undefined ? dasExternalPluginsToCoreExternalPlugins(externalPlugins) : {}),
...(externalPlugins !== undefined
? dasExternalPluginsToCoreExternalPlugins(externalPlugins)
: {}),
...handleUnknownPlugins(unknownPlugins),
// pluginHeader: // TODO: Reconstruct
};
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export type Pagination = Pick<

export type AssetOptions = {
skipDerivePlugins?: boolean;
}
};
31 changes: 17 additions & 14 deletions test/das.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import { publicKey } from '@metaplex-foundation/umi';
import { publicKey } from '@metaplex-foundation/umi';
import {
AssetV1,
CollectionV1,
Expand Down Expand Up @@ -187,35 +187,38 @@ test('das: it can convert a DAS collection with the master edition plugin to the
t.like(collectionDas, collectionMplCore);
});


test('das: it can fetch asset with oracle', async (t) => {
const umi = createUmiWithDas(DAS_API_ENDPOINT);
const assets = await das.searchAssets(umi, {
owner: publicKey('APrZTeVysBJqAznfLXS71NAzjr2fCVTSF1A66MeErzM7')
})
owner: publicKey('APrZTeVysBJqAznfLXS71NAzjr2fCVTSF1A66MeErzM7'),
});

const asset = assets.find((a) => a.publicKey === "AFENffFzHQaT1c9GzLfJjUZPY4ZYbHBj7NKA9UgMv6RT")!
prepareAssetForComparison(asset, false)
const asset = assets.find(
(a) => a.publicKey === 'AFENffFzHQaT1c9GzLfJjUZPY4ZYbHBj7NKA9UgMv6RT'
)!;
prepareAssetForComparison(asset, false);

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

t.like(asset, mplCoreAsset)
t.like(asset, mplCoreAsset);
});

test('das: it can fetch derived asset', async (t) => {
const umi = createUmiWithDas(DAS_API_ENDPOINT);
const assets = await das.getAssetsByCollection(umi, {
collection: publicKey('4waGHv3uwEvvZ35VNh8xZrVAkuDr6tEx8YnCvxYN4A7A')
})
collection: publicKey('4waGHv3uwEvvZ35VNh8xZrVAkuDr6tEx8YnCvxYN4A7A'),
});

const asset = assets.find((a) => a.publicKey === "9KvAqZVYJbXZzNvaV1HhxvybD6xfguztQwnqhkmzxWV3")!
prepareAssetForComparison(asset, false)
const asset = assets.find(
(a) => a.publicKey === '9KvAqZVYJbXZzNvaV1HhxvybD6xfguztQwnqhkmzxWV3'
)!;
prepareAssetForComparison(asset, false);

const mplCoreAsset = await fetchAsset(umi, asset.publicKey);
prepareAssetForComparison(mplCoreAsset)
prepareAssetForComparison(mplCoreAsset);

t.like(asset, mplCoreAsset)
t.like(asset, mplCoreAsset);
});

// TODO
Expand Down

0 comments on commit 16c50a3

Please sign in to comment.