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
Showing
20 changed files
with
560 additions
and
551 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { PublicKey } from '@metaplex-foundation/umi'; | ||
import { Authority, authority as authorityHelper } from '../generated'; | ||
import { BaseAuthority } from './types'; | ||
import { toWords } from './utils'; | ||
|
||
// Authorities data helpers | ||
export function getNoneAuthority() { | ||
return authorityHelper('None'); | ||
} | ||
|
||
export function getOwnerAuthority() { | ||
return authorityHelper('Owner'); | ||
} | ||
|
||
export function getUpdateAuthority() { | ||
return authorityHelper('UpdateAuthority'); | ||
} | ||
|
||
export function getPubkeyAuthority(address: PublicKey) { | ||
return authorityHelper('Pubkey', { address }); | ||
} | ||
|
||
export function getPermanentAuthority(address: PublicKey) { | ||
return authorityHelper('Permanent', { address }); | ||
} | ||
|
||
export function mapAuthority(authority: Authority): BaseAuthority { | ||
const authorityKey = toWords(authority.__kind) | ||
.split(' ')[0] | ||
.toLowerCase() as keyof BaseAuthority; | ||
|
||
if (Object.keys(authority).length > 1) { | ||
return { | ||
[authorityKey]: Object.values(authority).slice(1), | ||
}; | ||
} | ||
|
||
return { [authorityKey]: true }; | ||
} |
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
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
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,15 +1,5 @@ | ||
import { Authority, PluginHeader, PluginRegistry, Plugin } from 'src/generated'; | ||
|
||
export * from './fetchAssetWithPlugins'; | ||
export * from './fetchCollectionWithPlugins'; | ||
|
||
export type PluginWithAuthority = { | ||
plugin: Plugin; | ||
authority: Authority; | ||
}; | ||
|
||
export type PluginList = { | ||
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>; | ||
plugins?: PluginWithAuthority[]; | ||
pluginRegistry?: Omit<PluginRegistry, 'publicKey' | 'header'>; | ||
}; | ||
export * from './authorityHelpers'; | ||
export * from './pluginHelpers'; | ||
export * from './types'; |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
Key, | ||
PluginHeader, | ||
Plugin, | ||
getPluginSerializer, | ||
RegistryRecord, | ||
} from '../generated'; | ||
import { BaseAuthority, PluginsList } from './types'; | ||
import { mapAuthority } from './authorityHelpers'; | ||
import { toWords } from './utils'; | ||
|
||
export function formPluginHeader( | ||
pluginRegistryOffset: bigint | ||
): Omit<PluginHeader, 'publicKey' | 'header'> { | ||
return { | ||
key: Key.PluginHeader, | ||
pluginRegistryOffset, | ||
}; | ||
} | ||
|
||
export function mapPluginFields(fields: Array<Record<string, any>>) { | ||
return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {}); | ||
} | ||
|
||
export function mapPlugin({ | ||
plugin, | ||
authority, | ||
offset, | ||
}: { | ||
plugin: Exclude<Plugin, { __kind: 'Reserved' }>; | ||
authority: BaseAuthority; | ||
offset: bigint; | ||
}): PluginsList { | ||
const pluginKey = toWords(plugin.__kind) | ||
.toLowerCase() | ||
.split(' ') | ||
.reduce((s, c) => s + (c.charAt(0).toUpperCase() + c.slice(1))); | ||
|
||
return { | ||
[pluginKey]: { | ||
authority, | ||
offset, | ||
...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), | ||
}, | ||
}; | ||
} | ||
|
||
export function registryRecordsToPluginsList( | ||
registryRecords: RegistryRecord[], | ||
accountData: Uint8Array | ||
) { | ||
return registryRecords.reduce((acc: PluginsList, record) => { | ||
const mappedAuthority = mapAuthority(record.authority); | ||
const deserializedPlugin = getPluginSerializer().deserialize( | ||
accountData, | ||
Number(record.offset) | ||
)[0]; | ||
|
||
if (deserializedPlugin.__kind === 'Reserved') return acc; | ||
|
||
acc = { | ||
...acc, | ||
...mapPlugin({ | ||
plugin: deserializedPlugin, | ||
authority: mappedAuthority, | ||
offset: record.offset, | ||
}), | ||
}; | ||
|
||
return acc; | ||
}, {}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { PublicKey } from '@metaplex-foundation/umi'; | ||
import { | ||
Asset, | ||
Burn, | ||
Collection, | ||
Freeze, | ||
PluginHeader, | ||
Royalties, | ||
Transfer, | ||
UpdateDelegate, | ||
} from '../generated'; | ||
|
||
export type BaseAuthority = { | ||
none?: boolean; | ||
owner?: boolean; | ||
update?: boolean; | ||
pubkey?: Array<PublicKey>; | ||
permanent?: Array<PublicKey>; | ||
}; | ||
|
||
export type BasePlugin = { | ||
authority: BaseAuthority; | ||
offset?: bigint; | ||
}; | ||
|
||
export type RoyaltiesPlugin = BasePlugin & Royalties; | ||
export type FreezePlugin = BasePlugin & Freeze; | ||
export type BurnPlugin = BasePlugin & Burn; | ||
export type TransferPlugin = BasePlugin & Transfer; | ||
export type UpdateDelegatePlugin = BasePlugin & UpdateDelegate; | ||
|
||
export type PluginsList = { | ||
royalties?: RoyaltiesPlugin; | ||
freeze?: FreezePlugin; | ||
burn?: BurnPlugin; | ||
transfer?: TransferPlugin; | ||
updateDelegate?: UpdateDelegatePlugin; | ||
}; | ||
|
||
export type AssetWithPlugins = Asset & | ||
PluginsList & { | ||
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>; | ||
}; | ||
|
||
export type CollectionWithPlugins = Collection & | ||
PluginsList & { | ||
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function toWords(str: string) { | ||
const camelCaseRegex = /([a-z0-9])([A-Z])/g; | ||
return str.replace(camelCaseRegex, '$1 $2'); | ||
} |
Oops, something went wrong.