Skip to content
Draft
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
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions programs/libreplex_monoswap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []
test-bpf = []

[dependencies]
anchor-lang = {version = "~0.29", features = ["init-if-needed"]}
anchor-spl = {version = "~0.29"}
libreplex_fair_launch = {version = "*", path="../libreplex_fair_launch", features =["cpi", "no-entrypoint"]}
anchor-lang = { version = "~0.29", features = ["init-if-needed"] }
anchor-spl = { version = "~0.29" }
libreplex_fair_launch = { version = "*", path = "../libreplex_fair_launch", features = [
"cpi",
"no-entrypoint",
] }
spl-associated-token-account = "2.2.0"
solana-program = {version = "1.17.22"}
libreplex_shared = {version = "*", path = "../libreplex_shared", features=["cpi"]}
mpl-token-metadata = { version="~3" }
solana-program = { version = "1.17.22" }
libreplex_shared = { version = "*", path = "../libreplex_shared", features = [
"cpi",
] }
mpl-token-metadata = { version = "~3" }
nifty-asset = "0.0.1-alpha.3"

[dev-dependencies]
mocha = "0.1.1"
assert_matches = "1.5.0"
mocha = "0.1.1"
solana-program-test = "1.17.22"
solana-sdk = "1.17.22"
spl-token-2022 = "1.0"
spl-token = "4"
35 changes: 3 additions & 32 deletions programs/libreplex_monoswap/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
use anchor_lang::prelude::*;

#[error_code]
pub enum LegacyInscriptionErrorCode {

#[msg("Metadata has a bad mint")]
BadMint,

#[msg("Cannot inscribe a fungible asset")]
CannotInscribeFungible,

#[msg("Bad authority")]
BadAuthority,

#[msg("Bad authority for holder inscription")]
BadAuthorityForHolderInscription,

#[msg("Bad authority for update auth inscription")]
BadAuthorityForUpdateAuthInscription,

#[msg("Multi Signature threshold must be one to create / edit inscriptions")]
MultiSigThresholdMustBeOne,

#[msg("Not squads member")]
NotSquadsMember,

#[msg("Inscription V2 key mismatch")]
Inscription2KeyMismatch,

#[msg("Inscription V3 key mismatch")]
InscriptionV3KeyMismatch,

#[msg("Metadata data missmatch")]
DataHashMismatch,

pub enum MonoSwapError {
#[msg("Invalid Marker State")]
InvalidMarkerState,
}
46 changes: 22 additions & 24 deletions programs/libreplex_monoswap/src/instructions/create_monoswap.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@


use anchor_lang::prelude::*;
use anchor_spl::{associated_token::AssociatedToken, token::spl_token, token_interface::{Mint,TokenAccount, spl_token_2022}};
use anchor_spl::{
associated_token::AssociatedToken,
token::spl_token,
token_interface::{spl_token_2022, Mint, TokenAccount},
};
use libreplex_shared::operations::transfer_generic_spl;

use crate::SwapMarker;


#[derive(Clone, AnchorDeserialize, AnchorSerialize)]
pub struct CreateMonoSwapInput {
pub mint_outgoing_amount: u64,
pub mint_incoming_amount: u64
pub mint_incoming_amount: u64,
}

#[derive(Accounts)]
pub struct CreateMonoSwapCtx<'info> {

#[account(init,
payer = payer,
payer = payer,
space = SwapMarker::SIZE,
seeds = ["swap_marker".as_bytes(),
seeds = ["swap_marker".as_bytes(),
namespace.key().as_ref(),
mint_outgoing.key().as_ref(),
mint_incoming.key().as_ref()], // always indexed by the incoming mint
bump,)]
pub swap_marker: Account<'info, SwapMarker>,

#[account(mut)]
#[account(mut)]
pub mint_incoming: InterfaceAccount<'info, Mint>,

// each mint has to exist - there must be enough
pub mint_outgoing: InterfaceAccount<'info, Mint>,
// each mint has to exist - there must be enough
pub mint_outgoing: InterfaceAccount<'info, Mint>,

// it is the responsibility of each swapper program to create enough
// of the outgoing mint so that the swap can happen. It is deposited
// of the outgoing mint so that the swap can happen. It is deposited
// from this account
#[account(mut,
associated_token::mint = mint_outgoing,
associated_token::authority = mint_outgoing_owner
)]
)]
pub mint_outgoing_token_account_source: InterfaceAccount<'info, TokenAccount>,
// escrow holders are organised by namespace + incoming mint -

// escrow holders are organised by namespace + incoming mint -
// that way you can get wallet contents to see what swaps are available to you
/// CHECK: Checked in transfer logic
#[account(
seeds = ["swap_escrow".as_bytes(),
seeds = ["swap_escrow".as_bytes(),
namespace.key().as_ref(),
mint_incoming.key().as_ref()], // always indexed by the incoming mint
bump)]
Expand All @@ -60,15 +60,14 @@ pub struct CreateMonoSwapCtx<'info> {
)]
pub mint_outgoing_token_account_escrow: InterfaceAccount<'info, TokenAccount>,


#[account(mut)]
pub payer: Signer<'info>,

// leave this here for integrations
#[account(mut)]
pub mint_outgoing_owner: Signer<'info>,

// any account that can sign this. this is useful for grouping swaps
// any account that can sign this. this is useful for grouping swaps
pub namespace: Signer<'info>,

/// CHECK: Checked in constraint
Expand All @@ -81,14 +80,14 @@ pub struct CreateMonoSwapCtx<'info> {

#[account()]
pub system_program: Program<'info, System>,

}

pub fn create_swap(ctx: Context<CreateMonoSwapCtx>, input: CreateMonoSwapInput) -> Result<()> {

pub fn process_create_swap(
ctx: Context<CreateMonoSwapCtx>,
input: CreateMonoSwapInput,
) -> Result<()> {
let swap_marker = &mut ctx.accounts.swap_marker;
let mint_outgoing = &mut ctx.accounts.mint_outgoing;


swap_marker.namespace = ctx.accounts.namespace.key();
swap_marker.mint_incoming = ctx.accounts.mint_incoming.key();
Expand All @@ -98,7 +97,7 @@ pub fn create_swap(ctx: Context<CreateMonoSwapCtx>, input: CreateMonoSwapInput)

swap_marker.used = false;

// transfer the outgoing mint into escrow -
// transfer the outgoing mint into escrow -
let token_program = &ctx.accounts.token_program;
let mint_outgoing_token_account_source = &ctx.accounts.mint_outgoing_token_account_source;
let mint_outgoing_token_account_escrow = &ctx.accounts.mint_outgoing_token_account_escrow;
Expand All @@ -124,7 +123,6 @@ pub fn create_swap(ctx: Context<CreateMonoSwapCtx>, input: CreateMonoSwapInput)
mint_outgoing.decimals,
input.mint_outgoing_amount,
)?;


Ok(())
}
110 changes: 110 additions & 0 deletions programs/libreplex_monoswap/src/instructions/create_nifty_swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use anchor_lang::prelude::*;
use anchor_spl::{
associated_token::AssociatedToken,
token_interface::{Mint, TokenAccount, TokenInterface},
};
use libreplex_shared::operations::transfer_generic_spl;

use crate::{MarkerState, NiftyMarker};

// Swaps are created by transferring a token in.
#[derive(Accounts)]
pub struct CreateNiftySwapCtx<'info> {
// any account that can sign this. this is useful for grouping swaps
pub namespace: Signer<'info>,

#[account(mut)]
pub payer: Signer<'info>,

#[account(init,
payer = payer,
space = 8 + NiftyMarker::INIT_SPACE,
seeds = [
"nifty_marker".as_bytes(),
namespace.key().as_ref(),
asset.key().as_ref(),
mint.key().as_ref()
],
bump,
)]
pub nifty_marker: Account<'info, NiftyMarker>,

#[account(
constraint = asset.owner == nifty_program.key
)]
pub asset: UncheckedAccount<'info>,

pub mint: InterfaceAccount<'info, Mint>,

// escrow holders are organised by namespace + incoming mint -
// that way you can get wallet contents to see what swaps are available to you
/// CHECK: Checked in transfer logic
#[account(
seeds = [
"nifty_escrow".as_bytes(),
namespace.key().as_ref(),
asset.key().as_ref(),
mint.key().as_ref(),
], // always indexed by the incoming mint
bump
)]
pub escrow_owner: UncheckedAccount<'info>,

#[account(
init,
payer = payer,
associated_token::mint = mint,
associated_token::authority = escrow_owner,
token::token_program = token_program,
)]
pub escrow_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(
mut,
token::mint = mint,
token::authority = payer,
token::token_program = token_program,
)]
pub source_token_account: InterfaceAccount<'info, TokenAccount>,

pub token_program: Interface<'info, TokenInterface>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,

#[account(
address = nifty_asset::ID,
)]
pub nifty_program: UncheckedAccount<'info>,
}

pub fn process_create_nifty_swap(ctx: Context<CreateNiftySwapCtx>, amount: u64) -> Result<()> {
let swap_marker = &mut ctx.accounts.nifty_marker;
let mint = &ctx.accounts.mint;

swap_marker.namespace = ctx.accounts.namespace.key();
swap_marker.mint = ctx.accounts.mint.key();
swap_marker.asset = ctx.accounts.asset.key();
swap_marker.amount = amount;
swap_marker.state = MarkerState::FungibleEscrowed;

// transfer the outgoing mint into escrow -
let token_program = &ctx.accounts.token_program;
let associated_token_program = &ctx.accounts.associated_token_program;

transfer_generic_spl(
&token_program.to_account_info(),
&ctx.accounts.source_token_account.to_account_info(),
&ctx.accounts.escrow_token_account.to_account_info(),
&ctx.accounts.payer.to_account_info(),
&mint.to_account_info(),
&ctx.accounts.escrow_owner.to_account_info(),
&associated_token_program.to_account_info(),
&ctx.accounts.system_program.to_account_info(),
None, // payer signs
&ctx.accounts.payer.to_account_info(),
mint.decimals,
amount,
)?;

Ok(())
}
8 changes: 6 additions & 2 deletions programs/libreplex_monoswap/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

pub mod create_monoswap;
pub use create_monoswap::*;

pub mod create_nifty_swap;
pub use create_nifty_swap::*;

pub mod swap;
pub use swap::*;
pub use swap::*;

pub mod nifty_swap;
pub use nifty_swap::*;
Loading