Skip to content

Commit

Permalink
Adding ability to use update delegate for Reroll.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Aug 8, 2024
1 parent 6376558 commit 61bb5fc
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CARGO_TERM_COLOR=always
NODE_VERSION=20.x
PROGRAMS=["mpl-hybrid"]
RUST_VERSION=1.75.0
SOLANA_VERSION=1.18.15
SOLANA_VERSION=1.18.21
COMMIT_USER_NAME=github-actions
COMMIT_USER_EMAIL=[email protected]
DEPLOY_SOLANA_VERSION=1.18.15
DEPLOY_SOLANA_VERSION=1.18.21
13 changes: 13 additions & 0 deletions clients/js/src/generated/errors/mplHybrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ export class NumericalOverflowError extends ProgramError {
codeToErrorMap.set(0x177b, NumericalOverflowError);
nameToErrorMap.set('NumericalOverflow', NumericalOverflowError);

/** InvalidUpdateAuthority: Invalid Update Authority */
export class InvalidUpdateAuthorityError extends ProgramError {
override readonly name: string = 'InvalidUpdateAuthority';

readonly code: number = 0x177c; // 6012

constructor(program: Program, cause?: Error) {
super('Invalid Update Authority', program, cause);
}
}
codeToErrorMap.set(0x177c, InvalidUpdateAuthorityError);
nameToErrorMap.set('InvalidUpdateAuthority', InvalidUpdateAuthorityError);

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/instructions/captureV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
// Accounts.
export type CaptureV1InstructionAccounts = {
owner: Signer;
authority?: Signer;
authority?: PublicKey | Pda;
escrow: PublicKey | Pda;
asset: PublicKey | Pda;
collection: PublicKey | Pda;
Expand Down Expand Up @@ -172,7 +172,7 @@ export function captureV1(

// Default values.
if (!resolvedAccounts.authority.value) {
resolvedAccounts.authority.value = context.identity;
resolvedAccounts.authority.value = context.identity.publicKey;
}
if (!resolvedAccounts.userTokenAccount.value) {
resolvedAccounts.userTokenAccount.value = findAssociatedTokenPda(context, {
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/instructions/releaseV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
// Accounts.
export type ReleaseV1InstructionAccounts = {
owner: Signer;
authority?: Signer;
authority?: PublicKey | Pda;
escrow: PublicKey | Pda;
asset: PublicKey | Pda;
collection: PublicKey | Pda;
Expand Down Expand Up @@ -172,7 +172,7 @@ export function releaseV1(

// Default values.
if (!resolvedAccounts.authority.value) {
resolvedAccounts.authority.value = context.identity;
resolvedAccounts.authority.value = context.identity.publicKey;
}
if (!resolvedAccounts.userTokenAccount.value) {
resolvedAccounts.userTokenAccount.value = findAssociatedTokenPda(context, {
Expand Down
98 changes: 97 additions & 1 deletion clients/js/test/capture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
string,
publicKey as publicKeySerializer,
} from '@metaplex-foundation/umi/serializers';
import { transfer } from '@metaplex-foundation/mpl-core';
import { addCollectionPlugin, transfer } from '@metaplex-foundation/mpl-core';
import {
captureV1,
EscrowV1,
Expand Down Expand Up @@ -103,3 +103,99 @@ test('it can swap tokens for an asset', async (t) => {
token: tokenMint.publicKey,
}).sendAndConfirm(umi);
});

test('it can swap tokens for an asset as UpdateDelegate', async (t) => {
// Given a Umi instance using the project's plugin.
const umi = await createUmi();
const feeLocation = generateSigner(umi);
const { assets, collection } = await createCoreCollection(umi);
const tokenMint = generateSigner(umi);
await createFungible(umi, {
name: 'Test Token',
uri: 'www.fungible.com',
sellerFeeBasisPoints: {
basisPoints: 0n,
identifier: '%',
decimals: 2,
},
mint: tokenMint,
}).sendAndConfirm(umi);

await mintV1(umi, {
mint: tokenMint.publicKey,
tokenStandard: TokenStandard.Fungible,
tokenOwner: umi.identity.publicKey,
amount: 1000,
}).sendAndConfirm(umi);

const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
string({ size: 'variable' }).serialize('escrow'),
publicKeySerializer().serialize(collection.publicKey),
]);

// Transfer the assets to the escrow.
// eslint-disable-next-line no-restricted-syntax
for (const asset of assets) {
// eslint-disable-next-line no-await-in-loop
await transfer(umi, {
asset,
collection,
newOwner: escrow,
}).sendAndConfirm(umi);
}

await initEscrowV1(umi, {
escrow,
collection: collection.publicKey,
token: tokenMint.publicKey,
feeLocation: feeLocation.publicKey,
name: 'Test Escrow',
uri: 'www.test.com',
max: 9,
min: 0,
amount: 5,
feeAmount: 1,
// eslint-disable-next-line no-bitwise
path: 1 << Path.RerollMetadata,
solFeeAmount: 1000000n,
}).sendAndConfirm(umi);

await addCollectionPlugin(umi, {
collection: collection.publicKey,
plugin: {
type: 'UpdateDelegate',
additionalDelegates: [],
authority: { type: 'Address', address: publicKey(escrow) },
},
}).sendAndConfirm(umi);

const escrowData = await fetchEscrowV1(umi, escrow);

t.like(escrowData, <EscrowV1>{
publicKey: publicKey(escrow),
collection: collection.publicKey,
token: tokenMint.publicKey,
feeLocation: feeLocation.publicKey,
name: 'Test Escrow',
uri: 'www.test.com',
max: 9n,
min: 0n,
amount: 5n,
feeAmount: 1n,
count: 1n,
// eslint-disable-next-line no-bitwise
path: 1 << Path.RerollMetadata,
bump: escrow[1],
solFeeAmount: 1_000_000n,
});

await captureV1(umi, {
owner: umi.identity,
authority: escrow,
escrow,
asset: assets[0].publicKey,
collection: collection.publicKey,
feeProjectAccount: escrowData.feeLocation,
token: tokenMint.publicKey,
}).sendAndConfirm(umi);
});
86 changes: 86 additions & 0 deletions clients/js/test/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
string,
publicKey as publicKeySerializer,
} from '@metaplex-foundation/umi/serializers';
import { addCollectionPlugin } from '@metaplex-foundation/mpl-core';
import {
EscrowV1,
fetchEscrowV1,
Expand Down Expand Up @@ -91,3 +92,88 @@ test('it can swap an asset for tokens', async (t) => {
token: tokenMint.publicKey,
}).sendAndConfirm(umi);
});

test('it can swap an asset for tokens as UpdateDelegate', async (t) => {
// Given a Umi instance using the project's plugin.
const umi = await createUmi();
const feeLocation = generateSigner(umi);
const { assets, collection } = await createCoreCollection(umi);
const tokenMint = generateSigner(umi);
await createFungible(umi, {
name: 'Test Token',
uri: 'www.fungible.com',
sellerFeeBasisPoints: {
basisPoints: 0n,
identifier: '%',
decimals: 2,
},
mint: tokenMint,
}).sendAndConfirm(umi);

const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
string({ size: 'variable' }).serialize('escrow'),
publicKeySerializer().serialize(collection.publicKey),
]);

await mintV1(umi, {
mint: tokenMint.publicKey,
tokenStandard: TokenStandard.Fungible,
tokenOwner: escrow,
amount: 1000,
}).sendAndConfirm(umi);

await initEscrowV1(umi, {
escrow,
collection: collection.publicKey,
token: tokenMint.publicKey,
feeLocation: feeLocation.publicKey,
name: 'Test Escrow',
uri: 'www.test.com',
max: 9,
min: 0,
amount: 5,
feeAmount: 1,
// eslint-disable-next-line no-bitwise
path: 1 << Path.RerollMetadata,
solFeeAmount: 1000000n,
}).sendAndConfirm(umi);

await addCollectionPlugin(umi, {
collection: collection.publicKey,
plugin: {
type: 'UpdateDelegate',
additionalDelegates: [],
authority: { type: 'Address', address: publicKey(escrow) },
},
}).sendAndConfirm(umi);

const escrowData = await fetchEscrowV1(umi, escrow);

t.like(escrowData, <EscrowV1>{
publicKey: publicKey(escrow),
collection: collection.publicKey,
token: tokenMint.publicKey,
feeLocation: feeLocation.publicKey,
name: 'Test Escrow',
uri: 'www.test.com',
max: 9n,
min: 0n,
amount: 5n,
feeAmount: 1n,
count: 1n,
// eslint-disable-next-line no-bitwise
path: 1 << Path.RerollMetadata,
bump: escrow[1],
solFeeAmount: 1_000_000n,
});

await releaseV1(umi, {
owner: umi.identity,
authority: escrow,
escrow,
asset: assets[0].publicKey,
collection: collection.publicKey,
feeProjectAccount: escrowData.feeLocation,
token: tokenMint.publicKey,
}).sendAndConfirm(umi);
});
3 changes: 3 additions & 0 deletions clients/rust/src/generated/errors/mpl_hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub enum MplHybridError {
/// 6011 (0x177B) - Numerical Overflow
#[error("Numerical Overflow")]
NumericalOverflow,
/// 6012 (0x177C) - Invalid Update Authority
#[error("Invalid Update Authority")]
InvalidUpdateAuthority,
}

impl solana_program::program_error::PrintProgramError for MplHybridError {
Expand Down
8 changes: 4 additions & 4 deletions clients/rust/src/generated/instructions/capture_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl CaptureV1 {
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.authority,
true,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.escrow,
Expand Down Expand Up @@ -146,7 +146,7 @@ impl CaptureV1InstructionData {
/// ### Accounts:
///
/// 0. `[writable, signer]` owner
/// 1. `[writable, signer]` authority
/// 1. `[writable]` authority
/// 2. `[writable]` escrow
/// 3. `[writable]` asset
/// 4. `[writable]` collection
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'a, 'b> CaptureV1Cpi<'a, 'b> {
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.authority.key,
true,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.escrow.key,
Expand Down Expand Up @@ -601,7 +601,7 @@ impl<'a, 'b> CaptureV1Cpi<'a, 'b> {
/// ### Accounts:
///
/// 0. `[writable, signer]` owner
/// 1. `[writable, signer]` authority
/// 1. `[writable]` authority
/// 2. `[writable]` escrow
/// 3. `[writable]` asset
/// 4. `[writable]` collection
Expand Down
8 changes: 4 additions & 4 deletions clients/rust/src/generated/instructions/release_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ReleaseV1 {
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.authority,
true,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.escrow,
Expand Down Expand Up @@ -146,7 +146,7 @@ impl ReleaseV1InstructionData {
/// ### Accounts:
///
/// 0. `[writable, signer]` owner
/// 1. `[writable, signer]` authority
/// 1. `[writable]` authority
/// 2. `[writable]` escrow
/// 3. `[writable]` asset
/// 4. `[writable]` collection
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'a, 'b> ReleaseV1Cpi<'a, 'b> {
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.authority.key,
true,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.escrow.key,
Expand Down Expand Up @@ -601,7 +601,7 @@ impl<'a, 'b> ReleaseV1Cpi<'a, 'b> {
/// ### Accounts:
///
/// 0. `[writable, signer]` owner
/// 1. `[writable, signer]` authority
/// 1. `[writable]` authority
/// 2. `[writable]` escrow
/// 3. `[writable]` asset
/// 4. `[writable]` collection
Expand Down
9 changes: 7 additions & 2 deletions idls/mplHybrid.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
{
"name": "authority",
"isMut": true,
"isSigner": true
"isSigner": false
},
{
"name": "escrow",
Expand Down Expand Up @@ -208,7 +208,7 @@
{
"name": "authority",
"isMut": true,
"isSigner": true
"isSigner": false
},
{
"name": "escrow",
Expand Down Expand Up @@ -766,6 +766,11 @@
"code": 6011,
"name": "NumericalOverflow",
"msg": "Numerical Overflow"
},
{
"code": 6012,
"name": "InvalidUpdateAuthority",
"msg": "Invalid Update Authority"
}
],
"metadata": {
Expand Down
2 changes: 2 additions & 0 deletions programs/mpl-hybrid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ pub enum MplHybridError {
InvalidMintAccount,
#[msg("Numerical Overflow")]
NumericalOverflow,
#[msg("Invalid Update Authority")]
InvalidUpdateAuthority,
}
Loading

0 comments on commit 61bb5fc

Please sign in to comment.