diff --git a/Cargo.lock b/Cargo.lock index 852d393..3fd9af5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -422,6 +422,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" + [[package]] name = "base64ct" version = "1.6.0" @@ -1895,6 +1901,7 @@ name = "libreplex-monoswap" version = "0.0.0" dependencies = [ "borsh 0.10.3", + "mpl-core", "mpl-token-metadata", "nifty-asset", "nifty-asset-types", @@ -2136,6 +2143,20 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "mpl-core" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de7477a52a59fe4db051c6a83077319288e8bfbd1aaf8511d4f733e3a77b6d24" +dependencies = [ + "base64 0.22.0", + "borsh 0.10.3", + "num-derive 0.3.3", + "num-traits", + "solana-program", + "thiserror", +] + [[package]] name = "mpl-token-metadata" version = "4.1.2" diff --git a/clients/js-v1/src/generated/errors/monoswap.ts b/clients/js-v1/src/generated/errors/monoswap.ts index eaab8b8..e9c0a7f 100644 --- a/clients/js-v1/src/generated/errors/monoswap.ts +++ b/clients/js-v1/src/generated/errors/monoswap.ts @@ -266,6 +266,19 @@ export class InvalidNiftyAssetError extends ProgramError { codeToErrorMap.set(0x12, InvalidNiftyAssetError); nameToErrorMap.set('InvalidNiftyAsset', InvalidNiftyAssetError); +/** InvalidSigner: Invalid Signer */ +export class InvalidSignerError extends ProgramError { + override readonly name: string = 'InvalidSigner'; + + readonly code: number = 0x13; // 19 + + constructor(program: Program, cause?: Error) { + super('Invalid Signer', program, cause); + } +} +codeToErrorMap.set(0x13, InvalidSignerError); +nameToErrorMap.set('InvalidSigner', InvalidSignerError); + /** * Attempts to resolve a custom program error from the provided error code. * @category Errors diff --git a/clients/js-v1/src/generated/instructions/index.ts b/clients/js-v1/src/generated/instructions/index.ts index e66a27d..b187560 100644 --- a/clients/js-v1/src/generated/instructions/index.ts +++ b/clients/js-v1/src/generated/instructions/index.ts @@ -7,6 +7,6 @@ */ export * from './createSwap'; -export * from './swapNifty'; -export * from './swapNiftySPL'; +export * from './swapAsset'; +export * from './swapAssetSPL'; export * from './swapSPL'; diff --git a/clients/js-v1/src/generated/instructions/swapNifty.ts b/clients/js-v1/src/generated/instructions/swapAsset.ts similarity index 78% rename from clients/js-v1/src/generated/instructions/swapNifty.ts rename to clients/js-v1/src/generated/instructions/swapAsset.ts index ca5f69b..0ef46c4 100644 --- a/clients/js-v1/src/generated/instructions/swapNifty.ts +++ b/clients/js-v1/src/generated/instructions/swapAsset.ts @@ -27,7 +27,7 @@ import { } from '../shared'; // Accounts. -export type SwapNiftyInstructionAccounts = { +export type SwapAssetInstructionAccounts = { /** Authority to transfer incoming asset */ authority?: Signer; /** Escrows the asset and encodes state about the swap */ @@ -40,35 +40,35 @@ export type SwapNiftyInstructionAccounts = { escrowedAssetGroup?: PublicKey | Pda; /** Group account for the incoming asset, if applicable */ incomingAssetGroup?: PublicKey | Pda; - /** Nifty asset program account */ - niftyAssetProgram: PublicKey | Pda; + /** Asset program account */ + assetProgram: PublicKey | Pda; }; // Data. -export type SwapNiftyInstructionData = { discriminator: number }; +export type SwapAssetInstructionData = { discriminator: number }; -export type SwapNiftyInstructionDataArgs = {}; +export type SwapAssetInstructionDataArgs = {}; -export function getSwapNiftyInstructionDataSerializer(): Serializer< - SwapNiftyInstructionDataArgs, - SwapNiftyInstructionData +export function getSwapAssetInstructionDataSerializer(): Serializer< + SwapAssetInstructionDataArgs, + SwapAssetInstructionData > { return mapSerializer< - SwapNiftyInstructionDataArgs, + SwapAssetInstructionDataArgs, any, - SwapNiftyInstructionData + SwapAssetInstructionData >( - struct([['discriminator', u8()]], { - description: 'SwapNiftyInstructionData', + struct([['discriminator', u8()]], { + description: 'SwapAssetInstructionData', }), (value) => ({ ...value, discriminator: 2 }) - ) as Serializer; + ) as Serializer; } // Instruction. -export function swapNifty( +export function swapAsset( context: Pick, - input: SwapNiftyInstructionAccounts + input: SwapAssetInstructionAccounts ): TransactionBuilder { // Program ID. const programId = context.programs.getPublicKey( @@ -108,10 +108,10 @@ export function swapNifty( isWritable: true as boolean, value: input.incomingAssetGroup ?? null, }, - niftyAssetProgram: { + assetProgram: { index: 6, isWritable: false as boolean, - value: input.niftyAssetProgram ?? null, + value: input.assetProgram ?? null, }, } satisfies ResolvedAccountsWithIndices; @@ -133,7 +133,7 @@ export function swapNifty( ); // Data. - const data = getSwapNiftyInstructionDataSerializer().serialize({}); + const data = getSwapAssetInstructionDataSerializer().serialize({}); // Bytes Created On Chain. const bytesCreatedOnChain = 0; diff --git a/clients/js-v1/src/generated/instructions/swapNiftySPL.ts b/clients/js-v1/src/generated/instructions/swapAssetSPL.ts similarity index 84% rename from clients/js-v1/src/generated/instructions/swapNiftySPL.ts rename to clients/js-v1/src/generated/instructions/swapAssetSPL.ts index 0477aef..2dee0fc 100644 --- a/clients/js-v1/src/generated/instructions/swapNiftySPL.ts +++ b/clients/js-v1/src/generated/instructions/swapAssetSPL.ts @@ -27,7 +27,7 @@ import { } from '../shared'; // Accounts. -export type SwapNiftySPLInstructionAccounts = { +export type SwapAssetSPLInstructionAccounts = { /** Account to pay for ATA creation */ payer?: Signer; /** Authority to transfer incoming asset */ @@ -39,7 +39,7 @@ export type SwapNiftySPLInstructionAccounts = { /** External asset being swapped for the escrowed asset */ incomingAsset: PublicKey | Pda; /** Group account for the nifty asset, if applicable */ - niftyAssetGroup?: PublicKey | Pda; + assetGroup?: PublicKey | Pda; /** ATA account for the swap marker, if applicable */ swapMarkerAta: PublicKey | Pda; /** ATA account for the authority, if applicable */ @@ -55,30 +55,30 @@ export type SwapNiftySPLInstructionAccounts = { }; // Data. -export type SwapNiftySPLInstructionData = { discriminator: number }; +export type SwapAssetSPLInstructionData = { discriminator: number }; -export type SwapNiftySPLInstructionDataArgs = {}; +export type SwapAssetSPLInstructionDataArgs = {}; -export function getSwapNiftySPLInstructionDataSerializer(): Serializer< - SwapNiftySPLInstructionDataArgs, - SwapNiftySPLInstructionData +export function getSwapAssetSPLInstructionDataSerializer(): Serializer< + SwapAssetSPLInstructionDataArgs, + SwapAssetSPLInstructionData > { return mapSerializer< - SwapNiftySPLInstructionDataArgs, + SwapAssetSPLInstructionDataArgs, any, - SwapNiftySPLInstructionData + SwapAssetSPLInstructionData >( - struct([['discriminator', u8()]], { - description: 'SwapNiftySPLInstructionData', + struct([['discriminator', u8()]], { + description: 'SwapAssetSPLInstructionData', }), (value) => ({ ...value, discriminator: 3 }) - ) as Serializer; + ) as Serializer; } // Instruction. -export function swapNiftySPL( +export function swapAssetSPL( context: Pick, - input: SwapNiftySPLInstructionAccounts + input: SwapAssetSPLInstructionAccounts ): TransactionBuilder { // Program ID. const programId = context.programs.getPublicKey( @@ -113,10 +113,10 @@ export function swapNiftySPL( isWritable: true as boolean, value: input.incomingAsset ?? null, }, - niftyAssetGroup: { + assetGroup: { index: 5, isWritable: true as boolean, - value: input.niftyAssetGroup ?? null, + value: input.assetGroup ?? null, }, swapMarkerAta: { index: 6, @@ -178,7 +178,7 @@ export function swapNiftySPL( ); // Data. - const data = getSwapNiftySPLInstructionDataSerializer().serialize({}); + const data = getSwapAssetSPLInstructionDataSerializer().serialize({}); // Bytes Created On Chain. const bytesCreatedOnChain = 0; diff --git a/clients/js-v1/test/swap.test.ts b/clients/js-v1/test/swap.test.ts index 84cae42..b09df14 100644 --- a/clients/js-v1/test/swap.test.ts +++ b/clients/js-v1/test/swap.test.ts @@ -29,8 +29,8 @@ import { createSwap, fetchSwapMarker, findSwapMarkerPda, - swapNifty, - swapNiftySPL, + swapAsset, + swapAssetSPL, swapSPL, } from '../src'; @@ -105,12 +105,12 @@ test('it can swap nifty-to-nifty and back', async (t) => { t.is(assetAccount.owner, user.publicKey); // Swap them. - await swapNifty(umi, { + await swapAsset(umi, { authority: user, swapMarker, incomingAsset: asset1.publicKey, escrowedAsset: asset2.publicKey, - niftyAssetProgram: ASSET_PROGRAM_ID, + assetProgram: ASSET_PROGRAM_ID, }).sendAndConfirm(umi); // Assets are reversed. @@ -131,12 +131,12 @@ test('it can swap nifty-to-nifty and back', async (t) => { }); // Swap back. - await swapNifty(umi, { + await swapAsset(umi, { authority: user, swapMarker, incomingAsset: asset2.publicKey, escrowedAsset: asset1.publicKey, - niftyAssetProgram: ASSET_PROGRAM_ID, + assetProgram: ASSET_PROGRAM_ID, }).sendAndConfirm(umi); // Back to the original state. @@ -455,7 +455,7 @@ test('it can swap nifty-to-fungible and back', async (t) => { // Swap the fungibles back to the user in exchange for the Nifty asset. - await swapNiftySPL(umi, { + await swapAssetSPL(umi, { payer: umi.identity, authority: user, swapMarker, @@ -486,7 +486,7 @@ test('it can swap nifty-to-fungible and back', async (t) => { }); // Swap back. - await swapNiftySPL(umi, { + await swapAssetSPL(umi, { payer: umi.identity, authority: user, swapMarker, @@ -618,7 +618,7 @@ test('it can swap nifty-to-mplx-legacy and back', async (t) => { // Swap the fungibles back to the user in exchange for the Nifty asset. - await swapNiftySPL(umi, { + await swapAssetSPL(umi, { payer: umi.identity, authority: user, swapMarker, @@ -649,7 +649,7 @@ test('it can swap nifty-to-mplx-legacy and back', async (t) => { }); // Swap back. - await swapNiftySPL(umi, { + await swapAssetSPL(umi, { payer: umi.identity, authority: user, swapMarker, diff --git a/clients/js-v2/src/generated/errors/monoswap.ts b/clients/js-v2/src/generated/errors/monoswap.ts index e2843dc..d599f78 100644 --- a/clients/js-v2/src/generated/errors/monoswap.ts +++ b/clients/js-v2/src/generated/errors/monoswap.ts @@ -45,6 +45,8 @@ export const enum MonoswapProgramErrorCode { INVALID_TOKEN_PROGRAM = 0x11, // 17 /** InvalidNiftyAsset: Invalid Nifty Asset */ INVALID_NIFTY_ASSET = 0x12, // 18 + /** InvalidSigner: Invalid Signer */ + INVALID_SIGNER = 0x13, // 19 } export class MonoswapProgramError extends Error { @@ -147,6 +149,10 @@ if (__DEV__) { 'InvalidNiftyAsset', `Invalid Nifty Asset`, ], + [MonoswapProgramErrorCode.INVALID_SIGNER]: [ + 'InvalidSigner', + `Invalid Signer`, + ], }; } diff --git a/clients/js-v2/src/generated/instructions/index.ts b/clients/js-v2/src/generated/instructions/index.ts index e66a27d..b187560 100644 --- a/clients/js-v2/src/generated/instructions/index.ts +++ b/clients/js-v2/src/generated/instructions/index.ts @@ -7,6 +7,6 @@ */ export * from './createSwap'; -export * from './swapNifty'; -export * from './swapNiftySPL'; +export * from './swapAsset'; +export * from './swapAssetSPL'; export * from './swapSPL'; diff --git a/clients/js-v2/src/generated/instructions/swapNifty.ts b/clients/js-v2/src/generated/instructions/swapAsset.ts similarity index 80% rename from clients/js-v2/src/generated/instructions/swapNifty.ts rename to clients/js-v2/src/generated/instructions/swapAsset.ts index b5b2354..16dfbd2 100644 --- a/clients/js-v2/src/generated/instructions/swapNifty.ts +++ b/clients/js-v2/src/generated/instructions/swapAsset.ts @@ -31,7 +31,7 @@ import { IAccountSignerMeta, TransactionSigner } from '@solana/signers'; import { MONOSWAP_PROGRAM_ADDRESS } from '../programs'; import { ResolvedAccount, getAccountMetaFactory } from '../shared'; -export type SwapNiftyInstruction< +export type SwapAssetInstruction< TProgram extends string = typeof MONOSWAP_PROGRAM_ADDRESS, TAccountAuthority extends string | IAccountMeta = string, TAccountSwapMarker extends string | IAccountMeta = string, @@ -39,7 +39,7 @@ export type SwapNiftyInstruction< TAccountIncomingAsset extends string | IAccountMeta = string, TAccountEscrowedAssetGroup extends string | IAccountMeta = string, TAccountIncomingAssetGroup extends string | IAccountMeta = string, - TAccountNiftyAssetProgram extends string | IAccountMeta = string, + TAccountAssetProgram extends string | IAccountMeta = string, TRemainingAccounts extends readonly IAccountMeta[] = [], > = IInstruction & IInstructionWithData & @@ -64,46 +64,46 @@ export type SwapNiftyInstruction< TAccountIncomingAssetGroup extends string ? WritableAccount : TAccountIncomingAssetGroup, - TAccountNiftyAssetProgram extends string - ? ReadonlyAccount - : TAccountNiftyAssetProgram, + TAccountAssetProgram extends string + ? ReadonlyAccount + : TAccountAssetProgram, ...TRemainingAccounts, ] >; -export type SwapNiftyInstructionData = { discriminator: number }; +export type SwapAssetInstructionData = { discriminator: number }; -export type SwapNiftyInstructionDataArgs = {}; +export type SwapAssetInstructionDataArgs = {}; -export function getSwapNiftyInstructionDataEncoder(): Encoder { +export function getSwapAssetInstructionDataEncoder(): Encoder { return mapEncoder( getStructEncoder([['discriminator', getU8Encoder()]]), (value) => ({ ...value, discriminator: 2 }) ); } -export function getSwapNiftyInstructionDataDecoder(): Decoder { +export function getSwapAssetInstructionDataDecoder(): Decoder { return getStructDecoder([['discriminator', getU8Decoder()]]); } -export function getSwapNiftyInstructionDataCodec(): Codec< - SwapNiftyInstructionDataArgs, - SwapNiftyInstructionData +export function getSwapAssetInstructionDataCodec(): Codec< + SwapAssetInstructionDataArgs, + SwapAssetInstructionData > { return combineCodec( - getSwapNiftyInstructionDataEncoder(), - getSwapNiftyInstructionDataDecoder() + getSwapAssetInstructionDataEncoder(), + getSwapAssetInstructionDataDecoder() ); } -export type SwapNiftyInput< +export type SwapAssetInput< TAccountAuthority extends string = string, TAccountSwapMarker extends string = string, TAccountEscrowedAsset extends string = string, TAccountIncomingAsset extends string = string, TAccountEscrowedAssetGroup extends string = string, TAccountIncomingAssetGroup extends string = string, - TAccountNiftyAssetProgram extends string = string, + TAccountAssetProgram extends string = string, > = { /** Authority to transfer incoming asset */ authority: TransactionSigner; @@ -117,29 +117,29 @@ export type SwapNiftyInput< escrowedAssetGroup?: Address; /** Group account for the incoming asset, if applicable */ incomingAssetGroup?: Address; - /** Nifty asset program account */ - niftyAssetProgram: Address; + /** Asset program account */ + assetProgram: Address; }; -export function getSwapNiftyInstruction< +export function getSwapAssetInstruction< TAccountAuthority extends string, TAccountSwapMarker extends string, TAccountEscrowedAsset extends string, TAccountIncomingAsset extends string, TAccountEscrowedAssetGroup extends string, TAccountIncomingAssetGroup extends string, - TAccountNiftyAssetProgram extends string, + TAccountAssetProgram extends string, >( - input: SwapNiftyInput< + input: SwapAssetInput< TAccountAuthority, TAccountSwapMarker, TAccountEscrowedAsset, TAccountIncomingAsset, TAccountEscrowedAssetGroup, TAccountIncomingAssetGroup, - TAccountNiftyAssetProgram + TAccountAssetProgram > -): SwapNiftyInstruction< +): SwapAssetInstruction< typeof MONOSWAP_PROGRAM_ADDRESS, TAccountAuthority, TAccountSwapMarker, @@ -147,7 +147,7 @@ export function getSwapNiftyInstruction< TAccountIncomingAsset, TAccountEscrowedAssetGroup, TAccountIncomingAssetGroup, - TAccountNiftyAssetProgram + TAccountAssetProgram > { // Program address. const programAddress = MONOSWAP_PROGRAM_ADDRESS; @@ -166,10 +166,7 @@ export function getSwapNiftyInstruction< value: input.incomingAssetGroup ?? null, isWritable: true, }, - niftyAssetProgram: { - value: input.niftyAssetProgram ?? null, - isWritable: false, - }, + assetProgram: { value: input.assetProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, @@ -185,11 +182,11 @@ export function getSwapNiftyInstruction< getAccountMeta(accounts.incomingAsset), getAccountMeta(accounts.escrowedAssetGroup), getAccountMeta(accounts.incomingAssetGroup), - getAccountMeta(accounts.niftyAssetProgram), + getAccountMeta(accounts.assetProgram), ], programAddress, - data: getSwapNiftyInstructionDataEncoder().encode({}), - } as SwapNiftyInstruction< + data: getSwapAssetInstructionDataEncoder().encode({}), + } as SwapAssetInstruction< typeof MONOSWAP_PROGRAM_ADDRESS, TAccountAuthority, TAccountSwapMarker, @@ -197,13 +194,13 @@ export function getSwapNiftyInstruction< TAccountIncomingAsset, TAccountEscrowedAssetGroup, TAccountIncomingAssetGroup, - TAccountNiftyAssetProgram + TAccountAssetProgram >; return instruction; } -export type ParsedSwapNiftyInstruction< +export type ParsedSwapAssetInstruction< TProgram extends string = typeof MONOSWAP_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > = { @@ -221,20 +218,20 @@ export type ParsedSwapNiftyInstruction< escrowedAssetGroup?: TAccountMetas[4] | undefined; /** Group account for the incoming asset, if applicable */ incomingAssetGroup?: TAccountMetas[5] | undefined; - /** Nifty asset program account */ - niftyAssetProgram: TAccountMetas[6]; + /** Asset program account */ + assetProgram: TAccountMetas[6]; }; - data: SwapNiftyInstructionData; + data: SwapAssetInstructionData; }; -export function parseSwapNiftyInstruction< +export function parseSwapAssetInstruction< TProgram extends string, TAccountMetas extends readonly IAccountMeta[], >( instruction: IInstruction & IInstructionWithAccounts & IInstructionWithData -): ParsedSwapNiftyInstruction { +): ParsedSwapAssetInstruction { if (instruction.accounts.length < 7) { // TODO: Coded error. throw new Error('Not enough accounts'); @@ -260,8 +257,8 @@ export function parseSwapNiftyInstruction< incomingAsset: getNextAccount(), escrowedAssetGroup: getNextOptionalAccount(), incomingAssetGroup: getNextOptionalAccount(), - niftyAssetProgram: getNextAccount(), + assetProgram: getNextAccount(), }, - data: getSwapNiftyInstructionDataDecoder().decode(instruction.data), + data: getSwapAssetInstructionDataDecoder().decode(instruction.data), }; } diff --git a/clients/js-v2/src/generated/instructions/swapNiftySPL.ts b/clients/js-v2/src/generated/instructions/swapAssetSPL.ts similarity index 86% rename from clients/js-v2/src/generated/instructions/swapNiftySPL.ts rename to clients/js-v2/src/generated/instructions/swapAssetSPL.ts index 80b67e8..faba078 100644 --- a/clients/js-v2/src/generated/instructions/swapNiftySPL.ts +++ b/clients/js-v2/src/generated/instructions/swapAssetSPL.ts @@ -32,14 +32,14 @@ import { IAccountSignerMeta, TransactionSigner } from '@solana/signers'; import { MONOSWAP_PROGRAM_ADDRESS } from '../programs'; import { ResolvedAccount, getAccountMetaFactory } from '../shared'; -export type SwapNiftySPLInstruction< +export type SwapAssetSPLInstruction< TProgram extends string = typeof MONOSWAP_PROGRAM_ADDRESS, TAccountPayer extends string | IAccountMeta = string, TAccountAuthority extends string | IAccountMeta = string, TAccountSwapMarker extends string | IAccountMeta = string, TAccountEscrowedAsset extends string | IAccountMeta = string, TAccountIncomingAsset extends string | IAccountMeta = string, - TAccountNiftyAssetGroup extends string | IAccountMeta = string, + TAccountAssetGroup extends string | IAccountMeta = string, TAccountSwapMarkerAta extends string | IAccountMeta = string, TAccountAuthorityAta extends string | IAccountMeta = string, TAccountEscrowedAssetProgram extends string | IAccountMeta = string, @@ -70,9 +70,9 @@ export type SwapNiftySPLInstruction< TAccountIncomingAsset extends string ? WritableAccount : TAccountIncomingAsset, - TAccountNiftyAssetGroup extends string - ? WritableAccount - : TAccountNiftyAssetGroup, + TAccountAssetGroup extends string + ? WritableAccount + : TAccountAssetGroup, TAccountSwapMarkerAta extends string ? WritableAccount : TAccountSwapMarkerAta, @@ -95,38 +95,38 @@ export type SwapNiftySPLInstruction< ] >; -export type SwapNiftySPLInstructionData = { discriminator: number }; +export type SwapAssetSPLInstructionData = { discriminator: number }; -export type SwapNiftySPLInstructionDataArgs = {}; +export type SwapAssetSPLInstructionDataArgs = {}; -export function getSwapNiftySPLInstructionDataEncoder(): Encoder { +export function getSwapAssetSPLInstructionDataEncoder(): Encoder { return mapEncoder( getStructEncoder([['discriminator', getU8Encoder()]]), (value) => ({ ...value, discriminator: 3 }) ); } -export function getSwapNiftySPLInstructionDataDecoder(): Decoder { +export function getSwapAssetSPLInstructionDataDecoder(): Decoder { return getStructDecoder([['discriminator', getU8Decoder()]]); } -export function getSwapNiftySPLInstructionDataCodec(): Codec< - SwapNiftySPLInstructionDataArgs, - SwapNiftySPLInstructionData +export function getSwapAssetSPLInstructionDataCodec(): Codec< + SwapAssetSPLInstructionDataArgs, + SwapAssetSPLInstructionData > { return combineCodec( - getSwapNiftySPLInstructionDataEncoder(), - getSwapNiftySPLInstructionDataDecoder() + getSwapAssetSPLInstructionDataEncoder(), + getSwapAssetSPLInstructionDataDecoder() ); } -export type SwapNiftySPLInput< +export type SwapAssetSPLInput< TAccountPayer extends string = string, TAccountAuthority extends string = string, TAccountSwapMarker extends string = string, TAccountEscrowedAsset extends string = string, TAccountIncomingAsset extends string = string, - TAccountNiftyAssetGroup extends string = string, + TAccountAssetGroup extends string = string, TAccountSwapMarkerAta extends string = string, TAccountAuthorityAta extends string = string, TAccountEscrowedAssetProgram extends string = string, @@ -145,7 +145,7 @@ export type SwapNiftySPLInput< /** External asset being swapped for the escrowed asset */ incomingAsset: Address; /** Group account for the nifty asset, if applicable */ - niftyAssetGroup?: Address; + assetGroup?: Address; /** ATA account for the swap marker, if applicable */ swapMarkerAta: Address; /** ATA account for the authority, if applicable */ @@ -160,13 +160,13 @@ export type SwapNiftySPLInput< systemProgram?: Address; }; -export function getSwapNiftySPLInstruction< +export function getSwapAssetSPLInstruction< TAccountPayer extends string, TAccountAuthority extends string, TAccountSwapMarker extends string, TAccountEscrowedAsset extends string, TAccountIncomingAsset extends string, - TAccountNiftyAssetGroup extends string, + TAccountAssetGroup extends string, TAccountSwapMarkerAta extends string, TAccountAuthorityAta extends string, TAccountEscrowedAssetProgram extends string, @@ -174,13 +174,13 @@ export function getSwapNiftySPLInstruction< TAccountAssociatedTokenProgram extends string, TAccountSystemProgram extends string, >( - input: SwapNiftySPLInput< + input: SwapAssetSPLInput< TAccountPayer, TAccountAuthority, TAccountSwapMarker, TAccountEscrowedAsset, TAccountIncomingAsset, - TAccountNiftyAssetGroup, + TAccountAssetGroup, TAccountSwapMarkerAta, TAccountAuthorityAta, TAccountEscrowedAssetProgram, @@ -188,14 +188,14 @@ export function getSwapNiftySPLInstruction< TAccountAssociatedTokenProgram, TAccountSystemProgram > -): SwapNiftySPLInstruction< +): SwapAssetSPLInstruction< typeof MONOSWAP_PROGRAM_ADDRESS, TAccountPayer, TAccountAuthority, TAccountSwapMarker, TAccountEscrowedAsset, TAccountIncomingAsset, - TAccountNiftyAssetGroup, + TAccountAssetGroup, TAccountSwapMarkerAta, TAccountAuthorityAta, TAccountEscrowedAssetProgram, @@ -213,7 +213,7 @@ export function getSwapNiftySPLInstruction< swapMarker: { value: input.swapMarker ?? null, isWritable: true }, escrowedAsset: { value: input.escrowedAsset ?? null, isWritable: true }, incomingAsset: { value: input.incomingAsset ?? null, isWritable: true }, - niftyAssetGroup: { value: input.niftyAssetGroup ?? null, isWritable: true }, + assetGroup: { value: input.assetGroup ?? null, isWritable: true }, swapMarkerAta: { value: input.swapMarkerAta ?? null, isWritable: true }, authorityAta: { value: input.authorityAta ?? null, isWritable: true }, escrowedAssetProgram: { @@ -249,7 +249,7 @@ export function getSwapNiftySPLInstruction< getAccountMeta(accounts.swapMarker), getAccountMeta(accounts.escrowedAsset), getAccountMeta(accounts.incomingAsset), - getAccountMeta(accounts.niftyAssetGroup), + getAccountMeta(accounts.assetGroup), getAccountMeta(accounts.swapMarkerAta), getAccountMeta(accounts.authorityAta), getAccountMeta(accounts.escrowedAssetProgram), @@ -258,15 +258,15 @@ export function getSwapNiftySPLInstruction< getAccountMeta(accounts.systemProgram), ], programAddress, - data: getSwapNiftySPLInstructionDataEncoder().encode({}), - } as SwapNiftySPLInstruction< + data: getSwapAssetSPLInstructionDataEncoder().encode({}), + } as SwapAssetSPLInstruction< typeof MONOSWAP_PROGRAM_ADDRESS, TAccountPayer, TAccountAuthority, TAccountSwapMarker, TAccountEscrowedAsset, TAccountIncomingAsset, - TAccountNiftyAssetGroup, + TAccountAssetGroup, TAccountSwapMarkerAta, TAccountAuthorityAta, TAccountEscrowedAssetProgram, @@ -278,7 +278,7 @@ export function getSwapNiftySPLInstruction< return instruction; } -export type ParsedSwapNiftySPLInstruction< +export type ParsedSwapAssetSPLInstruction< TProgram extends string = typeof MONOSWAP_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > = { @@ -295,7 +295,7 @@ export type ParsedSwapNiftySPLInstruction< /** External asset being swapped for the escrowed asset */ incomingAsset: TAccountMetas[4]; /** Group account for the nifty asset, if applicable */ - niftyAssetGroup?: TAccountMetas[5] | undefined; + assetGroup?: TAccountMetas[5] | undefined; /** ATA account for the swap marker, if applicable */ swapMarkerAta: TAccountMetas[6]; /** ATA account for the authority, if applicable */ @@ -309,17 +309,17 @@ export type ParsedSwapNiftySPLInstruction< /** System program account */ systemProgram: TAccountMetas[11]; }; - data: SwapNiftySPLInstructionData; + data: SwapAssetSPLInstructionData; }; -export function parseSwapNiftySPLInstruction< +export function parseSwapAssetSPLInstruction< TProgram extends string, TAccountMetas extends readonly IAccountMeta[], >( instruction: IInstruction & IInstructionWithAccounts & IInstructionWithData -): ParsedSwapNiftySPLInstruction { +): ParsedSwapAssetSPLInstruction { if (instruction.accounts.length < 12) { // TODO: Coded error. throw new Error('Not enough accounts'); @@ -344,7 +344,7 @@ export function parseSwapNiftySPLInstruction< swapMarker: getNextAccount(), escrowedAsset: getNextAccount(), incomingAsset: getNextAccount(), - niftyAssetGroup: getNextOptionalAccount(), + assetGroup: getNextOptionalAccount(), swapMarkerAta: getNextAccount(), authorityAta: getNextAccount(), escrowedAssetProgram: getNextAccount(), @@ -352,6 +352,6 @@ export function parseSwapNiftySPLInstruction< associatedTokenProgram: getNextOptionalAccount(), systemProgram: getNextAccount(), }, - data: getSwapNiftySPLInstructionDataDecoder().decode(instruction.data), + data: getSwapAssetSPLInstructionDataDecoder().decode(instruction.data), }; } diff --git a/clients/js-v2/src/generated/programs/monoswap.ts b/clients/js-v2/src/generated/programs/monoswap.ts index 23d9ab1..7e93b5f 100644 --- a/clients/js-v2/src/generated/programs/monoswap.ts +++ b/clients/js-v2/src/generated/programs/monoswap.ts @@ -16,8 +16,8 @@ import { } from '../errors'; import { ParsedCreateSwapInstruction, - ParsedSwapNiftyInstruction, - ParsedSwapNiftySPLInstruction, + ParsedSwapAssetInstruction, + ParsedSwapAssetSPLInstruction, ParsedSwapSPLInstruction, } from '../instructions'; import { memcmp } from '../shared'; @@ -47,8 +47,8 @@ export enum MonoswapAccount { export enum MonoswapInstruction { CreateSwap, SwapSPL, - SwapNifty, - SwapNiftySPL, + SwapAsset, + SwapAssetSPL, } export function identifyMonoswapInstruction( @@ -63,10 +63,10 @@ export function identifyMonoswapInstruction( return MonoswapInstruction.SwapSPL; } if (memcmp(data, getU8Encoder().encode(2), 0)) { - return MonoswapInstruction.SwapNifty; + return MonoswapInstruction.SwapAsset; } if (memcmp(data, getU8Encoder().encode(3), 0)) { - return MonoswapInstruction.SwapNiftySPL; + return MonoswapInstruction.SwapAssetSPL; } throw new Error( 'The provided instruction could not be identified as a monoswap instruction.' @@ -83,8 +83,8 @@ export type ParsedMonoswapInstruction< instructionType: MonoswapInstruction.SwapSPL; } & ParsedSwapSPLInstruction) | ({ - instructionType: MonoswapInstruction.SwapNifty; - } & ParsedSwapNiftyInstruction) + instructionType: MonoswapInstruction.SwapAsset; + } & ParsedSwapAssetInstruction) | ({ - instructionType: MonoswapInstruction.SwapNiftySPL; - } & ParsedSwapNiftySPLInstruction); + instructionType: MonoswapInstruction.SwapAssetSPL; + } & ParsedSwapAssetSPLInstruction); diff --git a/clients/rust/src/generated/errors/monoswap.rs b/clients/rust/src/generated/errors/monoswap.rs index 01269ad..b662d71 100644 --- a/clients/rust/src/generated/errors/monoswap.rs +++ b/clients/rust/src/generated/errors/monoswap.rs @@ -67,6 +67,9 @@ pub enum MonoswapError { /// 18 (0x12) - Invalid Nifty Asset #[error("Invalid Nifty Asset")] InvalidNiftyAsset, + /// 19 (0x13) - Invalid Signer + #[error("Invalid Signer")] + InvalidSigner, } impl solana_program::program_error::PrintProgramError for MonoswapError { diff --git a/clients/rust/src/generated/instructions/mod.rs b/clients/rust/src/generated/instructions/mod.rs index 4e3fc17..c298706 100644 --- a/clients/rust/src/generated/instructions/mod.rs +++ b/clients/rust/src/generated/instructions/mod.rs @@ -6,11 +6,11 @@ //! pub(crate) mod r#create_swap; -pub(crate) mod r#swap_nifty; -pub(crate) mod r#swap_nifty_s_p_l; +pub(crate) mod r#swap_asset; +pub(crate) mod r#swap_asset_s_p_l; pub(crate) mod r#swap_s_p_l; pub use self::r#create_swap::*; -pub use self::r#swap_nifty::*; -pub use self::r#swap_nifty_s_p_l::*; +pub use self::r#swap_asset::*; +pub use self::r#swap_asset_s_p_l::*; pub use self::r#swap_s_p_l::*; diff --git a/clients/rust/src/generated/instructions/swap_nifty.rs b/clients/rust/src/generated/instructions/swap_asset.rs similarity index 88% rename from clients/rust/src/generated/instructions/swap_nifty.rs rename to clients/rust/src/generated/instructions/swap_asset.rs index 36e7e9c..3f44be6 100644 --- a/clients/rust/src/generated/instructions/swap_nifty.rs +++ b/clients/rust/src/generated/instructions/swap_asset.rs @@ -9,7 +9,7 @@ use borsh::BorshDeserialize; use borsh::BorshSerialize; /// Accounts. -pub struct SwapNifty { +pub struct SwapAsset { /// Authority to transfer incoming asset pub authority: solana_program::pubkey::Pubkey, /// Escrows the asset and encodes state about the swap @@ -22,11 +22,11 @@ pub struct SwapNifty { pub escrowed_asset_group: Option, /// Group account for the incoming asset, if applicable pub incoming_asset_group: Option, - /// Nifty asset program account - pub nifty_asset_program: solana_program::pubkey::Pubkey, + /// Asset program account + pub asset_program: solana_program::pubkey::Pubkey, } -impl SwapNifty { +impl SwapAsset { pub fn instruction(&self) -> solana_program::instruction::Instruction { self.instruction_with_remaining_accounts(&[]) } @@ -75,11 +75,11 @@ impl SwapNifty { )); } accounts.push(solana_program::instruction::AccountMeta::new_readonly( - self.nifty_asset_program, + self.asset_program, false, )); accounts.extend_from_slice(remaining_accounts); - let data = SwapNiftyInstructionData::new().try_to_vec().unwrap(); + let data = SwapAssetInstructionData::new().try_to_vec().unwrap(); solana_program::instruction::Instruction { program_id: crate::MONOSWAP_ID, @@ -90,17 +90,17 @@ impl SwapNifty { } #[derive(BorshDeserialize, BorshSerialize)] -struct SwapNiftyInstructionData { +struct SwapAssetInstructionData { discriminator: u8, } -impl SwapNiftyInstructionData { +impl SwapAssetInstructionData { fn new() -> Self { Self { discriminator: 2 } } } -/// Instruction builder for `SwapNifty`. +/// Instruction builder for `SwapAsset`. /// /// ### Accounts: /// @@ -110,20 +110,20 @@ impl SwapNiftyInstructionData { /// 3. `[writable]` incoming_asset /// 4. `[writable, optional]` escrowed_asset_group /// 5. `[writable, optional]` incoming_asset_group -/// 6. `[]` nifty_asset_program +/// 6. `[]` asset_program #[derive(Default)] -pub struct SwapNiftyBuilder { +pub struct SwapAssetBuilder { authority: Option, swap_marker: Option, escrowed_asset: Option, incoming_asset: Option, escrowed_asset_group: Option, incoming_asset_group: Option, - nifty_asset_program: Option, + asset_program: Option, __remaining_accounts: Vec, } -impl SwapNiftyBuilder { +impl SwapAssetBuilder { pub fn new() -> Self { Self::default() } @@ -171,13 +171,10 @@ impl SwapNiftyBuilder { self.incoming_asset_group = incoming_asset_group; self } - /// Nifty asset program account + /// Asset program account #[inline(always)] - pub fn nifty_asset_program( - &mut self, - nifty_asset_program: solana_program::pubkey::Pubkey, - ) -> &mut Self { - self.nifty_asset_program = Some(nifty_asset_program); + pub fn asset_program(&mut self, asset_program: solana_program::pubkey::Pubkey) -> &mut Self { + self.asset_program = Some(asset_program); self } /// Add an aditional account to the instruction. @@ -200,24 +197,22 @@ impl SwapNiftyBuilder { } #[allow(clippy::clone_on_copy)] pub fn instruction(&self) -> solana_program::instruction::Instruction { - let accounts = SwapNifty { + let accounts = SwapAsset { authority: self.authority.expect("authority is not set"), swap_marker: self.swap_marker.expect("swap_marker is not set"), escrowed_asset: self.escrowed_asset.expect("escrowed_asset is not set"), incoming_asset: self.incoming_asset.expect("incoming_asset is not set"), escrowed_asset_group: self.escrowed_asset_group, incoming_asset_group: self.incoming_asset_group, - nifty_asset_program: self - .nifty_asset_program - .expect("nifty_asset_program is not set"), + asset_program: self.asset_program.expect("asset_program is not set"), }; accounts.instruction_with_remaining_accounts(&self.__remaining_accounts) } } -/// `swap_nifty` CPI accounts. -pub struct SwapNiftyCpiAccounts<'a, 'b> { +/// `swap_asset` CPI accounts. +pub struct SwapAssetCpiAccounts<'a, 'b> { /// Authority to transfer incoming asset pub authority: &'b solana_program::account_info::AccountInfo<'a>, /// Escrows the asset and encodes state about the swap @@ -230,12 +225,12 @@ pub struct SwapNiftyCpiAccounts<'a, 'b> { pub escrowed_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, /// Group account for the incoming asset, if applicable pub incoming_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, - /// Nifty asset program account - pub nifty_asset_program: &'b solana_program::account_info::AccountInfo<'a>, + /// Asset program account + pub asset_program: &'b solana_program::account_info::AccountInfo<'a>, } -/// `swap_nifty` CPI instruction. -pub struct SwapNiftyCpi<'a, 'b> { +/// `swap_asset` CPI instruction. +pub struct SwapAssetCpi<'a, 'b> { /// The program to invoke. pub __program: &'b solana_program::account_info::AccountInfo<'a>, /// Authority to transfer incoming asset @@ -250,14 +245,14 @@ pub struct SwapNiftyCpi<'a, 'b> { pub escrowed_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, /// Group account for the incoming asset, if applicable pub incoming_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, - /// Nifty asset program account - pub nifty_asset_program: &'b solana_program::account_info::AccountInfo<'a>, + /// Asset program account + pub asset_program: &'b solana_program::account_info::AccountInfo<'a>, } -impl<'a, 'b> SwapNiftyCpi<'a, 'b> { +impl<'a, 'b> SwapAssetCpi<'a, 'b> { pub fn new( program: &'b solana_program::account_info::AccountInfo<'a>, - accounts: SwapNiftyCpiAccounts<'a, 'b>, + accounts: SwapAssetCpiAccounts<'a, 'b>, ) -> Self { Self { __program: program, @@ -267,7 +262,7 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { incoming_asset: accounts.incoming_asset, escrowed_asset_group: accounts.escrowed_asset_group, incoming_asset_group: accounts.incoming_asset_group, - nifty_asset_program: accounts.nifty_asset_program, + asset_program: accounts.asset_program, } } #[inline(always)] @@ -343,7 +338,7 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { )); } accounts.push(solana_program::instruction::AccountMeta::new_readonly( - *self.nifty_asset_program.key, + *self.asset_program.key, false, )); remaining_accounts.iter().for_each(|remaining_account| { @@ -353,7 +348,7 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { is_writable: remaining_account.2, }) }); - let data = SwapNiftyInstructionData::new().try_to_vec().unwrap(); + let data = SwapAssetInstructionData::new().try_to_vec().unwrap(); let instruction = solana_program::instruction::Instruction { program_id: crate::MONOSWAP_ID, @@ -372,7 +367,7 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { if let Some(incoming_asset_group) = self.incoming_asset_group { account_infos.push(incoming_asset_group.clone()); } - account_infos.push(self.nifty_asset_program.clone()); + account_infos.push(self.asset_program.clone()); remaining_accounts .iter() .for_each(|remaining_account| account_infos.push(remaining_account.0.clone())); @@ -385,7 +380,7 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { } } -/// Instruction builder for `SwapNifty` via CPI. +/// Instruction builder for `SwapAsset` via CPI. /// /// ### Accounts: /// @@ -395,14 +390,14 @@ impl<'a, 'b> SwapNiftyCpi<'a, 'b> { /// 3. `[writable]` incoming_asset /// 4. `[writable, optional]` escrowed_asset_group /// 5. `[writable, optional]` incoming_asset_group -/// 6. `[]` nifty_asset_program -pub struct SwapNiftyCpiBuilder<'a, 'b> { - instruction: Box>, +/// 6. `[]` asset_program +pub struct SwapAssetCpiBuilder<'a, 'b> { + instruction: Box>, } -impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { +impl<'a, 'b> SwapAssetCpiBuilder<'a, 'b> { pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self { - let instruction = Box::new(SwapNiftyCpiBuilderInstruction { + let instruction = Box::new(SwapAssetCpiBuilderInstruction { __program: program, authority: None, swap_marker: None, @@ -410,7 +405,7 @@ impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { incoming_asset: None, escrowed_asset_group: None, incoming_asset_group: None, - nifty_asset_program: None, + asset_program: None, __remaining_accounts: Vec::new(), }); Self { instruction } @@ -471,13 +466,13 @@ impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { self.instruction.incoming_asset_group = incoming_asset_group; self } - /// Nifty asset program account + /// Asset program account #[inline(always)] - pub fn nifty_asset_program( + pub fn asset_program( &mut self, - nifty_asset_program: &'b solana_program::account_info::AccountInfo<'a>, + asset_program: &'b solana_program::account_info::AccountInfo<'a>, ) -> &mut Self { - self.instruction.nifty_asset_program = Some(nifty_asset_program); + self.instruction.asset_program = Some(asset_program); self } /// Add an additional account to the instruction. @@ -521,7 +516,7 @@ impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { &self, signers_seeds: &[&[&[u8]]], ) -> solana_program::entrypoint::ProgramResult { - let instruction = SwapNiftyCpi { + let instruction = SwapAssetCpi { __program: self.instruction.__program, authority: self.instruction.authority.expect("authority is not set"), @@ -545,10 +540,10 @@ impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { incoming_asset_group: self.instruction.incoming_asset_group, - nifty_asset_program: self + asset_program: self .instruction - .nifty_asset_program - .expect("nifty_asset_program is not set"), + .asset_program + .expect("asset_program is not set"), }; instruction.invoke_signed_with_remaining_accounts( signers_seeds, @@ -557,7 +552,7 @@ impl<'a, 'b> SwapNiftyCpiBuilder<'a, 'b> { } } -struct SwapNiftyCpiBuilderInstruction<'a, 'b> { +struct SwapAssetCpiBuilderInstruction<'a, 'b> { __program: &'b solana_program::account_info::AccountInfo<'a>, authority: Option<&'b solana_program::account_info::AccountInfo<'a>>, swap_marker: Option<&'b solana_program::account_info::AccountInfo<'a>>, @@ -565,7 +560,7 @@ struct SwapNiftyCpiBuilderInstruction<'a, 'b> { incoming_asset: Option<&'b solana_program::account_info::AccountInfo<'a>>, escrowed_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, incoming_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, - nifty_asset_program: Option<&'b solana_program::account_info::AccountInfo<'a>>, + asset_program: Option<&'b solana_program::account_info::AccountInfo<'a>>, /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`. __remaining_accounts: Vec<( &'b solana_program::account_info::AccountInfo<'a>, diff --git a/clients/rust/src/generated/instructions/swap_nifty_s_p_l.rs b/clients/rust/src/generated/instructions/swap_asset_s_p_l.rs similarity index 92% rename from clients/rust/src/generated/instructions/swap_nifty_s_p_l.rs rename to clients/rust/src/generated/instructions/swap_asset_s_p_l.rs index a3943fa..64c29c3 100644 --- a/clients/rust/src/generated/instructions/swap_nifty_s_p_l.rs +++ b/clients/rust/src/generated/instructions/swap_asset_s_p_l.rs @@ -9,7 +9,7 @@ use borsh::BorshDeserialize; use borsh::BorshSerialize; /// Accounts. -pub struct SwapNiftySPL { +pub struct SwapAssetSPL { /// Account to pay for ATA creation pub payer: solana_program::pubkey::Pubkey, /// Authority to transfer incoming asset @@ -21,7 +21,7 @@ pub struct SwapNiftySPL { /// External asset being swapped for the escrowed asset pub incoming_asset: solana_program::pubkey::Pubkey, /// Group account for the nifty asset, if applicable - pub nifty_asset_group: Option, + pub asset_group: Option, /// ATA account for the swap marker, if applicable pub swap_marker_ata: solana_program::pubkey::Pubkey, /// ATA account for the authority, if applicable @@ -36,7 +36,7 @@ pub struct SwapNiftySPL { pub system_program: solana_program::pubkey::Pubkey, } -impl SwapNiftySPL { +impl SwapAssetSPL { pub fn instruction(&self) -> solana_program::instruction::Instruction { self.instruction_with_remaining_accounts(&[]) } @@ -65,9 +65,9 @@ impl SwapNiftySPL { self.incoming_asset, false, )); - if let Some(nifty_asset_group) = self.nifty_asset_group { + if let Some(asset_group) = self.asset_group { accounts.push(solana_program::instruction::AccountMeta::new( - nifty_asset_group, + asset_group, false, )); } else { @@ -108,7 +108,7 @@ impl SwapNiftySPL { false, )); accounts.extend_from_slice(remaining_accounts); - let data = SwapNiftySPLInstructionData::new().try_to_vec().unwrap(); + let data = SwapAssetSPLInstructionData::new().try_to_vec().unwrap(); solana_program::instruction::Instruction { program_id: crate::MONOSWAP_ID, @@ -119,17 +119,17 @@ impl SwapNiftySPL { } #[derive(BorshDeserialize, BorshSerialize)] -struct SwapNiftySPLInstructionData { +struct SwapAssetSPLInstructionData { discriminator: u8, } -impl SwapNiftySPLInstructionData { +impl SwapAssetSPLInstructionData { fn new() -> Self { Self { discriminator: 3 } } } -/// Instruction builder for `SwapNiftySPL`. +/// Instruction builder for `SwapAssetSPL`. /// /// ### Accounts: /// @@ -138,7 +138,7 @@ impl SwapNiftySPLInstructionData { /// 2. `[writable]` swap_marker /// 3. `[writable]` escrowed_asset /// 4. `[writable]` incoming_asset -/// 5. `[writable, optional]` nifty_asset_group +/// 5. `[writable, optional]` asset_group /// 6. `[writable]` swap_marker_ata /// 7. `[writable]` authority_ata /// 8. `[]` escrowed_asset_program @@ -146,13 +146,13 @@ impl SwapNiftySPLInstructionData { /// 10. `[optional]` associated_token_program /// 11. `[optional]` system_program (default to `11111111111111111111111111111111`) #[derive(Default)] -pub struct SwapNiftySPLBuilder { +pub struct SwapAssetSPLBuilder { payer: Option, authority: Option, swap_marker: Option, escrowed_asset: Option, incoming_asset: Option, - nifty_asset_group: Option, + asset_group: Option, swap_marker_ata: Option, authority_ata: Option, escrowed_asset_program: Option, @@ -162,7 +162,7 @@ pub struct SwapNiftySPLBuilder { __remaining_accounts: Vec, } -impl SwapNiftySPLBuilder { +impl SwapAssetSPLBuilder { pub fn new() -> Self { Self::default() } @@ -199,11 +199,11 @@ impl SwapNiftySPLBuilder { /// `[optional account]` /// Group account for the nifty asset, if applicable #[inline(always)] - pub fn nifty_asset_group( + pub fn asset_group( &mut self, - nifty_asset_group: Option, + asset_group: Option, ) -> &mut Self { - self.nifty_asset_group = nifty_asset_group; + self.asset_group = asset_group; self } /// ATA account for the swap marker, if applicable @@ -276,13 +276,13 @@ impl SwapNiftySPLBuilder { } #[allow(clippy::clone_on_copy)] pub fn instruction(&self) -> solana_program::instruction::Instruction { - let accounts = SwapNiftySPL { + let accounts = SwapAssetSPL { payer: self.payer.expect("payer is not set"), authority: self.authority.expect("authority is not set"), swap_marker: self.swap_marker.expect("swap_marker is not set"), escrowed_asset: self.escrowed_asset.expect("escrowed_asset is not set"), incoming_asset: self.incoming_asset.expect("incoming_asset is not set"), - nifty_asset_group: self.nifty_asset_group, + asset_group: self.asset_group, swap_marker_ata: self.swap_marker_ata.expect("swap_marker_ata is not set"), authority_ata: self.authority_ata.expect("authority_ata is not set"), escrowed_asset_program: self @@ -301,8 +301,8 @@ impl SwapNiftySPLBuilder { } } -/// `swap_nifty_s_p_l` CPI accounts. -pub struct SwapNiftySPLCpiAccounts<'a, 'b> { +/// `swap_asset_s_p_l` CPI accounts. +pub struct SwapAssetSPLCpiAccounts<'a, 'b> { /// Account to pay for ATA creation pub payer: &'b solana_program::account_info::AccountInfo<'a>, /// Authority to transfer incoming asset @@ -314,7 +314,7 @@ pub struct SwapNiftySPLCpiAccounts<'a, 'b> { /// External asset being swapped for the escrowed asset pub incoming_asset: &'b solana_program::account_info::AccountInfo<'a>, /// Group account for the nifty asset, if applicable - pub nifty_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, + pub asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, /// ATA account for the swap marker, if applicable pub swap_marker_ata: &'b solana_program::account_info::AccountInfo<'a>, /// ATA account for the authority, if applicable @@ -329,8 +329,8 @@ pub struct SwapNiftySPLCpiAccounts<'a, 'b> { pub system_program: &'b solana_program::account_info::AccountInfo<'a>, } -/// `swap_nifty_s_p_l` CPI instruction. -pub struct SwapNiftySPLCpi<'a, 'b> { +/// `swap_asset_s_p_l` CPI instruction. +pub struct SwapAssetSPLCpi<'a, 'b> { /// The program to invoke. pub __program: &'b solana_program::account_info::AccountInfo<'a>, /// Account to pay for ATA creation @@ -344,7 +344,7 @@ pub struct SwapNiftySPLCpi<'a, 'b> { /// External asset being swapped for the escrowed asset pub incoming_asset: &'b solana_program::account_info::AccountInfo<'a>, /// Group account for the nifty asset, if applicable - pub nifty_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, + pub asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, /// ATA account for the swap marker, if applicable pub swap_marker_ata: &'b solana_program::account_info::AccountInfo<'a>, /// ATA account for the authority, if applicable @@ -359,10 +359,10 @@ pub struct SwapNiftySPLCpi<'a, 'b> { pub system_program: &'b solana_program::account_info::AccountInfo<'a>, } -impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { +impl<'a, 'b> SwapAssetSPLCpi<'a, 'b> { pub fn new( program: &'b solana_program::account_info::AccountInfo<'a>, - accounts: SwapNiftySPLCpiAccounts<'a, 'b>, + accounts: SwapAssetSPLCpiAccounts<'a, 'b>, ) -> Self { Self { __program: program, @@ -371,7 +371,7 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { swap_marker: accounts.swap_marker, escrowed_asset: accounts.escrowed_asset, incoming_asset: accounts.incoming_asset, - nifty_asset_group: accounts.nifty_asset_group, + asset_group: accounts.asset_group, swap_marker_ata: accounts.swap_marker_ata, authority_ata: accounts.authority_ata, escrowed_asset_program: accounts.escrowed_asset_program, @@ -434,9 +434,9 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { *self.incoming_asset.key, false, )); - if let Some(nifty_asset_group) = self.nifty_asset_group { + if let Some(asset_group) = self.asset_group { accounts.push(solana_program::instruction::AccountMeta::new( - *nifty_asset_group.key, + *asset_group.key, false, )); } else { @@ -483,7 +483,7 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { is_writable: remaining_account.2, }) }); - let data = SwapNiftySPLInstructionData::new().try_to_vec().unwrap(); + let data = SwapAssetSPLInstructionData::new().try_to_vec().unwrap(); let instruction = solana_program::instruction::Instruction { program_id: crate::MONOSWAP_ID, @@ -497,8 +497,8 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { account_infos.push(self.swap_marker.clone()); account_infos.push(self.escrowed_asset.clone()); account_infos.push(self.incoming_asset.clone()); - if let Some(nifty_asset_group) = self.nifty_asset_group { - account_infos.push(nifty_asset_group.clone()); + if let Some(asset_group) = self.asset_group { + account_infos.push(asset_group.clone()); } account_infos.push(self.swap_marker_ata.clone()); account_infos.push(self.authority_ata.clone()); @@ -520,7 +520,7 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { } } -/// Instruction builder for `SwapNiftySPL` via CPI. +/// Instruction builder for `SwapAssetSPL` via CPI. /// /// ### Accounts: /// @@ -529,27 +529,27 @@ impl<'a, 'b> SwapNiftySPLCpi<'a, 'b> { /// 2. `[writable]` swap_marker /// 3. `[writable]` escrowed_asset /// 4. `[writable]` incoming_asset -/// 5. `[writable, optional]` nifty_asset_group +/// 5. `[writable, optional]` asset_group /// 6. `[writable]` swap_marker_ata /// 7. `[writable]` authority_ata /// 8. `[]` escrowed_asset_program /// 9. `[]` incoming_asset_program /// 10. `[optional]` associated_token_program /// 11. `[]` system_program -pub struct SwapNiftySPLCpiBuilder<'a, 'b> { - instruction: Box>, +pub struct SwapAssetSPLCpiBuilder<'a, 'b> { + instruction: Box>, } -impl<'a, 'b> SwapNiftySPLCpiBuilder<'a, 'b> { +impl<'a, 'b> SwapAssetSPLCpiBuilder<'a, 'b> { pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self { - let instruction = Box::new(SwapNiftySPLCpiBuilderInstruction { + let instruction = Box::new(SwapAssetSPLCpiBuilderInstruction { __program: program, payer: None, authority: None, swap_marker: None, escrowed_asset: None, incoming_asset: None, - nifty_asset_group: None, + asset_group: None, swap_marker_ata: None, authority_ata: None, escrowed_asset_program: None, @@ -605,11 +605,11 @@ impl<'a, 'b> SwapNiftySPLCpiBuilder<'a, 'b> { /// `[optional account]` /// Group account for the nifty asset, if applicable #[inline(always)] - pub fn nifty_asset_group( + pub fn asset_group( &mut self, - nifty_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, + asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, ) -> &mut Self { - self.instruction.nifty_asset_group = nifty_asset_group; + self.instruction.asset_group = asset_group; self } /// ATA account for the swap marker, if applicable @@ -708,7 +708,7 @@ impl<'a, 'b> SwapNiftySPLCpiBuilder<'a, 'b> { &self, signers_seeds: &[&[&[u8]]], ) -> solana_program::entrypoint::ProgramResult { - let instruction = SwapNiftySPLCpi { + let instruction = SwapAssetSPLCpi { __program: self.instruction.__program, payer: self.instruction.payer.expect("payer is not set"), @@ -730,7 +730,7 @@ impl<'a, 'b> SwapNiftySPLCpiBuilder<'a, 'b> { .incoming_asset .expect("incoming_asset is not set"), - nifty_asset_group: self.instruction.nifty_asset_group, + asset_group: self.instruction.asset_group, swap_marker_ata: self .instruction @@ -766,14 +766,14 @@ impl<'a, 'b> SwapNiftySPLCpiBuilder<'a, 'b> { } } -struct SwapNiftySPLCpiBuilderInstruction<'a, 'b> { +struct SwapAssetSPLCpiBuilderInstruction<'a, 'b> { __program: &'b solana_program::account_info::AccountInfo<'a>, payer: Option<&'b solana_program::account_info::AccountInfo<'a>>, authority: Option<&'b solana_program::account_info::AccountInfo<'a>>, swap_marker: Option<&'b solana_program::account_info::AccountInfo<'a>>, escrowed_asset: Option<&'b solana_program::account_info::AccountInfo<'a>>, incoming_asset: Option<&'b solana_program::account_info::AccountInfo<'a>>, - nifty_asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, + asset_group: Option<&'b solana_program::account_info::AccountInfo<'a>>, swap_marker_ata: Option<&'b solana_program::account_info::AccountInfo<'a>>, authority_ata: Option<&'b solana_program::account_info::AccountInfo<'a>>, escrowed_asset_program: Option<&'b solana_program::account_info::AccountInfo<'a>>, diff --git a/clients/rust/tests/swap_test.rs b/clients/rust/tests/swap_test.rs index 3ffe6ae..e24caff 100644 --- a/clients/rust/tests/swap_test.rs +++ b/clients/rust/tests/swap_test.rs @@ -3,7 +3,7 @@ use borsh::BorshDeserialize; use libreplex_monoswap_client::{ accounts::{SwapMarker, SwapSeeds}, - instructions::{CreateSwapBuilder, SwapNiftySPLBuilder}, + instructions::{CreateSwapBuilder, SwapAssetSPLBuilder}, }; use nifty_asset::accounts::Asset; use solana_program_test::tokio; @@ -129,13 +129,13 @@ async fn create() { assert_eq!(asset_data.owner, user); // Swap Nifty asset for fungibles. - let ix = SwapNiftySPLBuilder::new() + let ix = SwapAssetSPLBuilder::new() .payer(context.payer.pubkey()) .authority(user) // User has the authority to transfer the incoming nifty asset .swap_marker(swap_marker) .swap_marker_ata(swap_marker_ata) // ATA where fungibles are escrowed .incoming_asset(asset) // Nifty asset incoming - .nifty_asset_group(None) // No Nifty group + .asset_group(None) // No Nifty group .escrowed_asset(mint) // Escrowed fungibles mint .authority_ata(user_ata) // Escrowed mint ata owned by the user .incoming_asset_program(nifty_asset::ID) // Nifty program needed to transfer the asset diff --git a/program/Cargo.toml b/program/Cargo.toml index 7df65af..9f694d2 100644 --- a/program/Cargo.toml +++ b/program/Cargo.toml @@ -32,3 +32,4 @@ solana-program = "<1.18" spl-associated-token-account = { version = "2", features = ["no-entrypoint"] } spl-token-2022 = { version = "2", features = ["no-entrypoint"] } thiserror = "^1.0" +mpl-core = "0.4.4" diff --git a/program/idl.json b/program/idl.json index 01976f2..daf81a5 100644 --- a/program/idl.json +++ b/program/idl.json @@ -235,7 +235,7 @@ } }, { - "name": "SwapNifty", + "name": "SwapAsset", "accounts": [ { "name": "authority", @@ -288,11 +288,11 @@ ] }, { - "name": "niftyAssetProgram", + "name": "assetProgram", "isMut": false, "isSigner": false, "docs": [ - "Nifty asset program account" + "Asset program account" ] } ], @@ -303,7 +303,7 @@ } }, { - "name": "SwapNiftySPL", + "name": "SwapAssetSPL", "accounts": [ { "name": "payer", @@ -346,7 +346,7 @@ ] }, { - "name": "niftyAssetGroup", + "name": "assetGroup", "isMut": true, "isSigner": false, "isOptional": true, @@ -615,6 +615,11 @@ "code": 18, "name": "InvalidNiftyAsset", "msg": "Invalid Nifty Asset" + }, + { + "code": 19, + "name": "InvalidSigner", + "msg": "Invalid Signer" } ], "metadata": { diff --git a/program/src/error.rs b/program/src/error.rs index 95a9440..04c9546 100644 --- a/program/src/error.rs +++ b/program/src/error.rs @@ -65,6 +65,9 @@ pub enum MonoswapError { /// 18 - Invalid Nifty Asset #[error("Invalid Nifty Asset")] InvalidNiftyAsset, + /// 19 - Invalid Signer + #[error("Invalid Signer")] + InvalidSigner, } impl PrintProgramError for MonoswapError { diff --git a/program/src/instruction.rs b/program/src/instruction.rs index da6452a..659d073 100644 --- a/program/src/instruction.rs +++ b/program/src/instruction.rs @@ -42,8 +42,8 @@ pub enum MonoswapInstruction { #[account(3, writable, name="incoming_asset", desc = "External asset being swapped for the escrowed asset")] #[account(4, optional, writable, name="escrowed_asset_group", desc = "Group account for the escrowed asset, if applicable")] #[account(5, optional, writable, name="incoming_asset_group", desc = "Group account for the incoming asset, if applicable")] - #[account(6, name="nifty_asset_program", desc = "Nifty asset program account")] - SwapNifty, + #[account(6, name="asset_program", desc = "Asset program account")] + SwapAsset, /// Swap paired nifty-spl assets. #[account(0, writable, signer, name="payer", desc = "Account to pay for ATA creation")] @@ -51,14 +51,14 @@ pub enum MonoswapInstruction { #[account(2, writable, name="swap_marker", desc = "Escrows the asset and encodes state about the swap")] #[account(3, writable, name="escrowed_asset", desc = "The currently escrowed asset")] #[account(4, writable, name="incoming_asset", desc = "External asset being swapped for the escrowed asset")] - #[account(5, optional, writable, name="nifty_asset_group", desc = "Group account for the nifty asset, if applicable")] + #[account(5, optional, writable, name="asset_group", desc = "Group account for the nifty asset, if applicable")] #[account(6, writable, name="swap_marker_ata", desc = "ATA account for the swap marker, if applicable")] #[account(7, writable, name="authority_ata", desc = "ATA account for the authority, if applicable")] #[account(8, name="escrowed_asset_program", desc = "Transfer Program ID of the incoming asset")] #[account(9, name="incoming_asset_program", desc = "Transfer Program ID of the external asset")] #[account(10, optional, name="associated_token_program", desc = "The SPL associated token program account program")] #[account(11, name="system_program", desc = "System program account")] - SwapNiftySPL, + SwapAssetSPL, } #[repr(C)] diff --git a/program/src/interface.rs b/program/src/interface.rs new file mode 100644 index 0000000..16542ce --- /dev/null +++ b/program/src/interface.rs @@ -0,0 +1,85 @@ +use nifty_asset::instructions::TransferCpi as NiftyTransferCpi; +use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, msg}; + +use crate::{processor::MonoswapError, require}; + +pub trait AssetInterface<'a, 'b> { + fn transfer(&self, recipient: &'a AccountInfo<'a>) -> ProgramResult; +} + +pub struct TransferSigner<'a, 'b> { + account: &'a AccountInfo<'a>, + signers_seeds: &'b [&'b [&'b [u8]]], +} + +pub struct NiftyAsset<'a, 'b> { + asset: &'a AccountInfo<'a>, + transfer_authority: TransferSigner<'a, 'b>, + group: Option<&'a AccountInfo<'a>>, + program: &'a AccountInfo<'a>, +} + +pub struct NiftyAssetInputs<'a, 'b> { + pub asset: &'a AccountInfo<'a>, + pub transfer_authority: &'a AccountInfo<'a>, + pub group: Option<&'a AccountInfo<'a>>, + pub program: &'a AccountInfo<'a>, + pub signers_seeds: &'b [&'b [&'b [u8]]], +} + +impl<'a, 'b> NiftyAsset<'a, 'b> { + pub fn new(inputs: NiftyAssetInputs<'a, 'b>) -> Result { + let NiftyAssetInputs { + asset, + transfer_authority, + group, + program, + signers_seeds, + } = inputs; + + require!( + asset.owner == &nifty_asset::ID, + MonoswapError::InvalidNiftyAsset, + "Asset account is not owned by the Nifty program" + ); + + require!( + !asset.data_is_empty(), + MonoswapError::InvalidNiftyAsset, + "Asset account is empty" + ); + + require!( + transfer_authority.is_signer || !signers_seeds.is_empty(), + MonoswapError::InvalidSigner, + "Authority is not a signer" + ); + + let transfer_authority = TransferSigner { + account: transfer_authority, + signers_seeds, + }; + + Ok(Self { + asset, + transfer_authority, + group, + program, + }) + } +} + +impl<'a, 'b> AssetInterface<'a, 'b> for NiftyAsset<'a, 'b> { + fn transfer(&self, recipient: &'a AccountInfo<'a>) -> ProgramResult { + msg!("Transferring Nifty asset..."); + + NiftyTransferCpi { + __program: self.program, + asset: self.asset, + signer: self.transfer_authority.account, + recipient, + group: self.group, + } + .invoke_signed(self.transfer_authority.signers_seeds) + } +} diff --git a/program/src/lib.rs b/program/src/lib.rs index 139b119..1e4833a 100644 --- a/program/src/lib.rs +++ b/program/src/lib.rs @@ -3,6 +3,7 @@ pub mod asset_detection; pub mod entrypoint; pub mod error; pub mod instruction; +pub mod interface; pub mod processor; pub mod state; pub mod transfer; diff --git a/program/src/processor/mod.rs b/program/src/processor/mod.rs index 63ee72c..7a1c701 100644 --- a/program/src/processor/mod.rs +++ b/program/src/processor/mod.rs @@ -1,11 +1,11 @@ mod create_swap; -mod swap_nifty; -mod swap_nifty_spl; +mod swap_asset; +mod swap_asset_spl; mod swap_spl; pub use create_swap::*; -pub use swap_nifty::*; -pub use swap_nifty_spl::*; +pub use swap_asset::*; +pub use swap_asset_spl::*; pub use swap_spl::*; use std::cmp::{max, min}; @@ -41,7 +41,7 @@ pub use spl_token_2022::{ pub use crate::error::MonoswapError; pub use crate::instruction::accounts::{ - CreateSwapAccounts, SwapNiftyAccounts, SwapNiftySPLAccounts, SwapSPLAccounts, + CreateSwapAccounts, SwapAssetAccounts, SwapAssetSPLAccounts, SwapSPLAccounts, }; pub use crate::instruction::{CreateSwapArgs, MonoswapInstruction}; pub use crate::state::SwapMarker; @@ -70,16 +70,16 @@ pub fn process_instruction<'a>( msg!("Instruction: Create Swap"); process_create_swap(accounts, args) } - MonoswapInstruction::SwapNifty => { - msg!("Instruction: Swap Nifty"); - process_swap_nifty(accounts) + MonoswapInstruction::SwapAsset => { + msg!("Instruction: Swap Asset"); + process_swap_asset(accounts) } MonoswapInstruction::SwapSPL => { msg!("Instruction: Swap SPL"); process_swap_spl(accounts) } - MonoswapInstruction::SwapNiftySPL => { - msg!("Instruction: Swap Nifty SPL"); + MonoswapInstruction::SwapAssetSPL => { + msg!("Instruction: Swap Asset SPL"); process_swap_nifty_spl(accounts) } } diff --git a/program/src/processor/swap_asset.rs b/program/src/processor/swap_asset.rs new file mode 100644 index 0000000..fd01782 --- /dev/null +++ b/program/src/processor/swap_asset.rs @@ -0,0 +1,98 @@ +use crate::interface::{AssetInterface, NiftyAsset, NiftyAssetInputs}; + +use super::*; + +pub fn process_swap_asset<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramResult { + let ctx = SwapAssetAccounts::context(accounts)?; + + let mut swap_marker = SwapMarker::load(ctx.accounts.swap_marker)?; + + let escrowed_asset_info = ctx.accounts.escrowed_asset.clone(); + let incoming_asset_info = ctx.accounts.incoming_asset.clone(); + + // Check signer. + assert_signer("authority", ctx.accounts.authority)?; + + let asset1_pub = escrowed_asset_info.key; + let asset1_bytes = asset1_pub.to_bytes(); + let asset2_pub = incoming_asset_info.key; + let asset2_bytes = asset2_pub.to_bytes(); + + // Check the swap marker account is derived from the correct seeds and owned by this program. + let swap_seeds = SwapSeeds { + namespace: &swap_marker.namespace, + asset1: asset1_pub, + asset2: asset2_pub, + }; + + let (swap_marker_pubkey, bump) = SwapMarker::find_pda(swap_seeds.clone()); + assert_same_pubkeys("swap_marker", ctx.accounts.swap_marker, &swap_marker_pubkey)?; + + // Swap marker signer seeds. + let signers_seeds: &[&[&[u8]]] = &[&[ + b"swap_marker", + &swap_seeds.namespace.to_bytes(), + &min(asset1_bytes, asset2_bytes), + &max(asset1_bytes, asset2_bytes), + &[bump], + ]]; + + let incoming_asset_type = detect_asset(&incoming_asset_info)?; + let escrowed_asset_type = detect_asset(&escrowed_asset_info)?; + + match incoming_asset_type { + AssetType::NiftyAsset => { + msg!("Incoming Nifty asset detected"); + + let incoming_asset = NiftyAsset::new(NiftyAssetInputs { + asset: ctx.accounts.incoming_asset, + transfer_authority: ctx.accounts.authority, + group: ctx.accounts.incoming_asset_group, + program: ctx.accounts.asset_program, + signers_seeds: &[], + })?; + + // Transfer Nifty asset from owner to the swap marker. + incoming_asset.transfer(ctx.accounts.swap_marker)?; + } + _ => { + msg!("Invalid incoming asset detected"); + return Err(MonoswapError::InvalidNiftyAsset.into()); + } + } + + match escrowed_asset_type { + AssetType::NiftyAsset => { + msg!("Escrowed Nifty asset detected"); + + let escrowed_asset = NiftyAsset::new(NiftyAssetInputs { + asset: ctx.accounts.escrowed_asset, + transfer_authority: ctx.accounts.swap_marker, + group: ctx.accounts.escrowed_asset_group, + program: ctx.accounts.asset_program, + signers_seeds, + })?; + + // Transfer Nifty asset from the swap marker to the owner. + escrowed_asset.transfer(ctx.accounts.authority)?; + } + _ => { + msg!("Invalid escrowed asset detected"); + return Err(MonoswapError::InvalidNiftyAsset.into()); + } + } + + // Update SwapMarker state. + // Accounts have swapped, so update the escrowed and external assets. + // This allows indexing to figure out what swaps are available for any given asset. + std::mem::swap( + &mut swap_marker.escrowed_asset, + &mut swap_marker.external_asset, + ); + std::mem::swap( + &mut swap_marker.escrowed_amount, + &mut swap_marker.external_amount, + ); + + swap_marker.save(ctx.accounts.swap_marker) +} diff --git a/program/src/processor/swap_nifty_spl.rs b/program/src/processor/swap_asset_spl.rs similarity index 96% rename from program/src/processor/swap_nifty_spl.rs rename to program/src/processor/swap_asset_spl.rs index e574ccd..4bbc3ee 100644 --- a/program/src/processor/swap_nifty_spl.rs +++ b/program/src/processor/swap_asset_spl.rs @@ -1,7 +1,7 @@ use super::*; pub fn process_swap_nifty_spl<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramResult { - let ctx = SwapNiftySPLAccounts::context(accounts)?; + let ctx = SwapAssetSPLAccounts::context(accounts)?; let mut swap_marker = SwapMarker::load(ctx.accounts.swap_marker)?; @@ -49,7 +49,7 @@ pub fn process_swap_nifty_spl<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramRes asset_info: ctx.accounts.incoming_asset, signer_info: ctx.accounts.authority, recipient_info: ctx.accounts.swap_marker, - group_asset_opt_info: ctx.accounts.nifty_asset_group, + group_asset_opt_info: ctx.accounts.asset_group, signer_seeds: &[], }; @@ -97,7 +97,7 @@ pub fn process_swap_nifty_spl<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramRes asset_info: ctx.accounts.escrowed_asset, signer_info: ctx.accounts.swap_marker, recipient_info: ctx.accounts.authority, - group_asset_opt_info: ctx.accounts.nifty_asset_group, + group_asset_opt_info: ctx.accounts.asset_group, signer_seeds, }; diff --git a/program/src/processor/swap_nifty.rs b/program/src/processor/swap_nifty.rs deleted file mode 100644 index bba3b67..0000000 --- a/program/src/processor/swap_nifty.rs +++ /dev/null @@ -1,77 +0,0 @@ -use super::*; - -pub fn process_swap_nifty<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramResult { - let ctx = SwapNiftyAccounts::context(accounts)?; - - let mut swap_marker = SwapMarker::load(ctx.accounts.swap_marker)?; - - let escrowed_asset_info = ctx.accounts.escrowed_asset.clone(); - let incoming_asset_info = ctx.accounts.incoming_asset.clone(); - - // Check signer. - assert_signer("authority", ctx.accounts.authority)?; - - let asset1_pub = escrowed_asset_info.key; - let asset1_bytes = asset1_pub.to_bytes(); - let asset2_pub = incoming_asset_info.key; - let asset2_bytes = asset2_pub.to_bytes(); - - // Check the swap marker account is derived from the correct seeds and owned by this program. - let swap_seeds = SwapSeeds { - namespace: &swap_marker.namespace, - asset1: asset1_pub, - asset2: asset2_pub, - }; - - let (swap_marker_pubkey, bump) = SwapMarker::find_pda(swap_seeds.clone()); - assert_same_pubkeys("swap_marker", ctx.accounts.swap_marker, &swap_marker_pubkey)?; - - // Swap marker signer seeds. - let signer_seeds: &[&[&[u8]]] = &[&[ - b"swap_marker", - &swap_seeds.namespace.to_bytes(), - &min(asset1_bytes, asset2_bytes), - &max(asset1_bytes, asset2_bytes), - &[bump], - ]]; - - // Transfer Nifty asset from authority signer to the swap marker. - let transfer_params = TransferNiftyParams { - nifty_program_info: ctx.accounts.nifty_asset_program, - asset_info: ctx.accounts.incoming_asset, - signer_info: ctx.accounts.authority, - recipient_info: ctx.accounts.swap_marker, - group_asset_opt_info: ctx.accounts.incoming_asset_group, - signer_seeds: &[], - }; - - msg!("Transferring Nifty asset into escrow."); - check_and_transfer_nifty(transfer_params)?; - - // Transfer escrowed Nifty asset from the swap marker to the authority signer. - let transfer_params = TransferNiftyParams { - nifty_program_info: ctx.accounts.nifty_asset_program, - asset_info: ctx.accounts.escrowed_asset, - signer_info: ctx.accounts.swap_marker, - recipient_info: ctx.accounts.authority, - group_asset_opt_info: ctx.accounts.escrowed_asset_group, - signer_seeds, - }; - - msg!("Transferring Nifty asset out of escrow."); - check_and_transfer_nifty(transfer_params)?; - - // Update SwapMarker state. - // Accounts have swapped, so update the escrowed and external assets. - // This allows indexing to figure out what swaps are available for any given asset. - std::mem::swap( - &mut swap_marker.escrowed_asset, - &mut swap_marker.external_asset, - ); - std::mem::swap( - &mut swap_marker.escrowed_amount, - &mut swap_marker.external_amount, - ); - - swap_marker.save(ctx.accounts.swap_marker) -} diff --git a/program/src/transfer.rs b/program/src/transfer.rs index 789b3bf..cda6413 100644 --- a/program/src/transfer.rs +++ b/program/src/transfer.rs @@ -28,6 +28,28 @@ pub struct TransferNiftyParams<'a, 'b> { pub signer_seeds: &'b [&'b [&'b [u8]]], } +pub fn transfer_nifty(params: TransferNiftyParams<'_, '_>) -> ProgramResult { + let TransferNiftyParams { + nifty_program_info, + signer_info, + asset_info, + recipient_info, + group_asset_opt_info, + signer_seeds, + } = params; + + // Transfer Nifty asset from authority signer to the swap marker. + NiftyTransferCpi { + __program: nifty_program_info, + asset: asset_info, + signer: signer_info, + recipient: recipient_info, + group: group_asset_opt_info, + } + .invoke_signed(signer_seeds)?; + Ok(()) +} + pub fn check_and_transfer_nifty(params: TransferNiftyParams<'_, '_>) -> ProgramResult { let TransferNiftyParams { nifty_program_info,