Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a treasury plugin #187

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions clients/js/src/generated/errors/mplCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,69 @@ export class CannotAddDataSectionError extends ProgramError {
codeToErrorMap.set(0x2f, CannotAddDataSectionError);
nameToErrorMap.set('CannotAddDataSection', CannotAddDataSectionError);

/** PluginNotAllowedOnAsset: Plugin is not allowed to be added to an Asset */
export class PluginNotAllowedOnAssetError extends ProgramError {
override readonly name: string = 'PluginNotAllowedOnAsset';

readonly code: number = 0x30; // 48

constructor(program: Program, cause?: Error) {
super('Plugin is not allowed to be added to an Asset', program, cause);
}
}
codeToErrorMap.set(0x30, PluginNotAllowedOnAssetError);
nameToErrorMap.set('PluginNotAllowedOnAsset', PluginNotAllowedOnAssetError);

/** PluginNotAllowedOnCollection: Plugin is not allowed to be added to a Collection */
export class PluginNotAllowedOnCollectionError extends ProgramError {
override readonly name: string = 'PluginNotAllowedOnCollection';

readonly code: number = 0x31; // 49

constructor(program: Program, cause?: Error) {
super('Plugin is not allowed to be added to a Collection', program, cause);
}
}
codeToErrorMap.set(0x31, PluginNotAllowedOnCollectionError);
nameToErrorMap.set(
'PluginNotAllowedOnCollection',
PluginNotAllowedOnCollectionError
);

/** InvalidTreasuryWithdrawn: Cannot add a treasury plugin with nonzero withdrawn amount */
export class InvalidTreasuryWithdrawnError extends ProgramError {
override readonly name: string = 'InvalidTreasuryWithdrawn';

readonly code: number = 0x32; // 50

constructor(program: Program, cause?: Error) {
super(
'Cannot add a treasury plugin with nonzero withdrawn amount',
program,
cause
);
}
}
codeToErrorMap.set(0x32, InvalidTreasuryWithdrawnError);
nameToErrorMap.set('InvalidTreasuryWithdrawn', InvalidTreasuryWithdrawnError);

/** CannotOverdraw: Cannot withdraw more than excess rent from treasury */
export class CannotOverdrawError extends ProgramError {
override readonly name: string = 'CannotOverdraw';

readonly code: number = 0x33; // 51

constructor(program: Program, cause?: Error) {
super(
'Cannot withdraw more than excess rent from treasury',
program,
cause
);
}
}
codeToErrorMap.set(0x33, CannotOverdrawError);
nameToErrorMap.set('CannotOverdraw', CannotOverdrawError);

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
Expand Down
22 changes: 22 additions & 0 deletions clients/js/src/generated/types/baseTreasury.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Serializer, struct, u64 } from '@metaplex-foundation/umi/serializers';

export type BaseTreasury = { withdrawn: bigint };

export type BaseTreasuryArgs = { withdrawn: number | bigint };

export function getBaseTreasurySerializer(): Serializer<
BaseTreasuryArgs,
BaseTreasury
> {
return struct<BaseTreasury>([['withdrawn', u64()]], {
description: 'BaseTreasury',
}) as Serializer<BaseTreasuryArgs, BaseTreasury>;
}
1 change: 1 addition & 0 deletions clients/js/src/generated/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './basePluginAuthority';
export * from './baseRoyalties';
export * from './baseRuleSet';
export * from './baseSeed';
export * from './baseTreasury';
export * from './baseUpdateAuthority';
export * from './baseValidationResultsOffset';
export * from './burnDelegate';
Expand Down
19 changes: 17 additions & 2 deletions clients/js/src/generated/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
BaseMasterEditionArgs,
BaseRoyalties,
BaseRoyaltiesArgs,
BaseTreasury,
BaseTreasuryArgs,
BurnDelegate,
BurnDelegateArgs,
Edition,
Expand All @@ -50,6 +52,7 @@ import {
getAutographSerializer,
getBaseMasterEditionSerializer,
getBaseRoyaltiesSerializer,
getBaseTreasurySerializer,
getBurnDelegateSerializer,
getEditionSerializer,
getFreezeDelegateSerializer,
Expand Down Expand Up @@ -77,7 +80,8 @@ export type Plugin =
| { __kind: 'AddBlocker'; fields: [AddBlocker] }
| { __kind: 'ImmutableMetadata'; fields: [ImmutableMetadata] }
| { __kind: 'VerifiedCreators'; fields: [VerifiedCreators] }
| { __kind: 'Autograph'; fields: [Autograph] };
| { __kind: 'Autograph'; fields: [Autograph] }
| { __kind: 'Treasury'; fields: [BaseTreasury] };

export type PluginArgs =
| { __kind: 'Royalties'; fields: [BaseRoyaltiesArgs] }
Expand All @@ -97,7 +101,8 @@ export type PluginArgs =
| { __kind: 'AddBlocker'; fields: [AddBlockerArgs] }
| { __kind: 'ImmutableMetadata'; fields: [ImmutableMetadataArgs] }
| { __kind: 'VerifiedCreators'; fields: [VerifiedCreatorsArgs] }
| { __kind: 'Autograph'; fields: [AutographArgs] };
| { __kind: 'Autograph'; fields: [AutographArgs] }
| { __kind: 'Treasury'; fields: [BaseTreasuryArgs] };

export function getPluginSerializer(): Serializer<PluginArgs, Plugin> {
return dataEnum<Plugin>(
Expand Down Expand Up @@ -192,6 +197,12 @@ export function getPluginSerializer(): Serializer<PluginArgs, Plugin> {
['fields', tuple([getAutographSerializer()])],
]),
],
[
'Treasury',
struct<GetDataEnumKindContent<Plugin, 'Treasury'>>([
['fields', tuple([getBaseTreasurySerializer()])],
]),
],
],
{ description: 'Plugin' }
) as Serializer<PluginArgs, Plugin>;
Expand Down Expand Up @@ -261,6 +272,10 @@ export function plugin(
kind: 'Autograph',
data: GetDataEnumKindContent<PluginArgs, 'Autograph'>['fields']
): GetDataEnumKind<PluginArgs, 'Autograph'>;
export function plugin(
kind: 'Treasury',
data: GetDataEnumKindContent<PluginArgs, 'Treasury'>['fields']
): GetDataEnumKind<PluginArgs, 'Treasury'>;
export function plugin<K extends PluginArgs['__kind']>(
kind: K,
data?: any
Expand Down
1 change: 1 addition & 0 deletions clients/js/src/generated/types/pluginType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum PluginType {
ImmutableMetadata,
VerifiedCreators,
Autograph,
Treasury,
}

export type PluginTypeArgs = PluginType;
Expand Down
17 changes: 17 additions & 0 deletions clients/js/src/plugins/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from './pluginAuthority';
import { royaltiesFromBase, royaltiesToBase } from './royalties';
import { masterEditionFromBase, masterEditionToBase } from './masterEdition';
import { treasuryFromBase, treasuryToBase } from './treasury';

export function formPluginHeaderV1(
pluginRegistryOffset: bigint
Expand Down Expand Up @@ -103,6 +104,12 @@ export function createPluginV2(args: AssetAllPluginArgsV2): BasePlugin {
fields: [masterEditionToBase(args)],
};
}
if (type === 'Treasury') {
return {
__kind: type,
fields: [treasuryToBase(args)],
};
}

return {
__kind: type,
Expand Down Expand Up @@ -167,6 +174,16 @@ export function mapPlugin({
};
}

if (plug.__kind === 'Treasury') {
return {
[pluginKey]: {
authority,
offset,
...treasuryFromBase(plug.fields[0]),
},
};
}

return {
[pluginKey]: {
authority,
Expand Down
20 changes: 20 additions & 0 deletions clients/js/src/plugins/treasury.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { lamports, SolAmount } from '@metaplex-foundation/umi';
import { BaseTreasury } from '../generated';

export type Treasury = {
withdrawn: SolAmount;
};

export type TreasuryArgs = Treasury;

export function treasuryToBase(s: Treasury): BaseTreasury {
return {
withdrawn: s.withdrawn.basisPoints,
};
}

export function treasuryFromBase(s: BaseTreasury): Treasury {
return {
withdrawn: lamports(s.withdrawn),
};
}
13 changes: 12 additions & 1 deletion clients/js/src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import {
VerifiedCreatorsArgs,
Autograph,
VerifiedCreators,
BaseTreasuryArgs,
} from '../generated';
import { RoyaltiesArgs, RoyaltiesPlugin } from './royalties';
import { PluginAuthority } from './pluginAuthority';
import { MasterEdition, MasterEditionArgs } from './masterEdition';
import { Treasury, TreasuryArgs } from './treasury';

// for backwards compatibility
export { pluginAuthority, updateAuthority, ruleSet };
Expand Down Expand Up @@ -88,6 +90,10 @@ export type CreatePluginArgs =
}
| {
type: 'AddBlocker';
}
| {
type: 'Treasury';
data: BaseTreasuryArgs;
};

export type AuthorityArgsV2 = {
Expand Down Expand Up @@ -143,7 +149,10 @@ export type AuthorityManagedPluginArgsV2 =
}
| ({
type: 'VerifiedCreators';
} & VerifiedCreatorsArgs);
} & VerifiedCreatorsArgs)
| ({
type: 'Treasury';
} & TreasuryArgs);

export type AssetAddablePluginArgsV2 =
| OwnerManagedPluginArgsV2
Expand Down Expand Up @@ -181,6 +190,7 @@ export type AddBlockerPlugin = BasePlugin & AddBlocker;
export type ImmutableMetadataPlugin = BasePlugin & ImmutableMetadata;
export type VerifiedCreatorsPlugin = BasePlugin & VerifiedCreators;
export type AutographPlugin = BasePlugin & Autograph;
export type TreasuryPlugin = BasePlugin & Treasury;

export type CommonPluginsList = {
attributes?: AttributesPlugin;
Expand All @@ -204,6 +214,7 @@ export type AssetPluginsList = {

export type CollectionPluginsList = {
masterEdition?: MasterEditionPlugin;
treasury?: TreasuryPlugin;
} & CommonPluginsList;

export type PluginsList = AssetPluginsList & CollectionPluginsList;
4 changes: 2 additions & 2 deletions clients/js/test/plugins/collection/masterEdition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ test('it cannot add masterEdition to asset', async (t) => {
}).sendAndConfirm(umi);

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

Expand All @@ -150,6 +150,6 @@ test('it cannot create asset with masterEdition', async (t) => {
});

await t.throwsAsync(result, {
name: 'InvalidPlugin',
name: 'PluginNotAllowedOnAsset',
});
});
Loading
Loading