generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bb3d6c
commit 8c6b107
Showing
2 changed files
with
112 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters