Skip to content

Commit

Permalink
In use by (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbogle authored Nov 4, 2022
1 parent a86152a commit 370a4ea
Show file tree
Hide file tree
Showing 22 changed files with 1,574 additions and 987 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions programs/cardinal-creator-standard/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ pub enum ErrorCode {
UnknownAccount,
#[msg("Account not found in instruction")]
AccountNotFound,
#[msg("Token already in use")]
TokenAlreadyInUse,
#[msg("Invalid token user")]
InvalidTokenUser,
#[msg("Token currently in use")]
TokenCurentlyInUse,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pub mod init_mint_manager;
pub mod remove_in_use_by;
pub mod set_in_use_by;
pub mod update_mint_manager;

pub use init_mint_manager::*;
pub use remove_in_use_by::*;
pub use set_in_use_by::*;
pub use update_mint_manager::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::errors::ErrorCode;
use crate::state::*;
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct RemoveInUseByCtx<'info> {
#[account(mut)]
mint_manager: Box<Account<'info, MintManager>>,
#[account(constraint = mint_manager.in_use_by.expect("Token not in use") == user.key() @ ErrorCode::InvalidTokenUser)]
user: Signer<'info>,
}

pub fn handler(ctx: Context<RemoveInUseByCtx>) -> Result<()> {
let mint_manager = &mut ctx.accounts.mint_manager;
mint_manager.in_use_by = None;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::errors::ErrorCode;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct SetInUseByIx {
pub in_use_by: Pubkey,
}

#[derive(Accounts)]
pub struct SetInUseByCtx<'info> {
#[account(mut)]
mint_manager: Box<Account<'info, MintManager>>,
holder: Signer<'info>,

#[account(constraint = holder_token_account.owner == holder.key() && holder_token_account.mint == mint_manager.mint @ ErrorCode::InvalidHolderTokenAccount)]
holder_token_account: Box<Account<'info, TokenAccount>>,
}

pub fn handler(ctx: Context<SetInUseByCtx>, ix: SetInUseByIx) -> Result<()> {
let mint_manager = &mut ctx.accounts.mint_manager;

if mint_manager.in_use_by.is_some() {
return Err(error!(ErrorCode::TokenAlreadyInUse));
}
mint_manager.in_use_by = Some(ix.in_use_by);
Ok(())
}
2 changes: 2 additions & 0 deletions programs/cardinal-creator-standard/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod mint_manager;
pub use mint_manager::init_mint_manager::*;
pub use mint_manager::remove_in_use_by::*;
pub use mint_manager::set_in_use_by::*;
pub use mint_manager::update_mint_manager::*;

pub mod ruleset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct ApproveCtx<'info> {
}

pub fn handler(ctx: Context<ApproveCtx>) -> Result<()> {
if ctx.accounts.mint_manager.in_use_by.is_some() {
return Err(error!(ErrorCode::TokenCurentlyInUse));
}

let mint = ctx.accounts.mint.key();
let mint_manager_seeds = &[
MINT_MANAGER_SEED.as_bytes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub struct BurnCtx<'info> {
}

pub fn handler(ctx: Context<BurnCtx>) -> Result<()> {
if ctx.accounts.mint_manager.in_use_by.is_some() {
return Err(error!(ErrorCode::TokenCurentlyInUse));
}

let mint_manager_key = ctx.accounts.mint.key();
let mint_manager_seeds = &[
MINT_MANAGER_SEED.as_bytes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub struct RevokeCtx<'info> {
}

pub fn handler(ctx: Context<RevokeCtx>) -> Result<()> {
if ctx.accounts.mint_manager.in_use_by.is_some() {
return Err(error!(ErrorCode::TokenCurentlyInUse));
}

let mint = ctx.accounts.mint.key();
let mint_manager_seeds = &[
MINT_MANAGER_SEED.as_bytes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ pub fn handler(ctx: Context<TransferCtx>) -> Result<()> {
}
////////////////////////////////////////////////////////////

if ctx.accounts.mint_manager.in_use_by.is_some() {
return Err(error!(ErrorCode::TokenCurentlyInUse));
}

let mint = ctx.accounts.mint.key();
let mint_manager_seeds = &[
MINT_MANAGER_SEED.as_bytes(),
Expand Down
10 changes: 10 additions & 0 deletions programs/cardinal-creator-standard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ solana_program::declare_id!("creatS3mfzrTGjwuLD1Pa2HXJ1gmq6WXb4ssnwUbJez");

#[program]
pub mod cardinal_creator_standard {
use crate::instructions::mint_manager::{RemoveInUseByCtx, SetInUseByCtx, SetInUseByIx};

use super::*;

// mint_manager
Expand All @@ -23,6 +25,14 @@ pub mod cardinal_creator_standard {
mint_manager::update_mint_manager::handler(ctx, ix)
}

pub fn set_in_use_by(ctx: Context<SetInUseByCtx>, ix: SetInUseByIx) -> Result<()> {
mint_manager::set_in_use_by::handler(ctx, ix)
}

pub fn remove_in_use_by(ctx: Context<RemoveInUseByCtx>) -> Result<()> {
mint_manager::remove_in_use_by::handler(ctx)
}

// ruleset
pub fn init_ruleset(ctx: Context<InitRulesetCtx>, ix: InitRulesetIx) -> Result<()> {
ruleset::init_ruleset::handler(ctx, ix)
Expand Down
2 changes: 1 addition & 1 deletion programs/cardinal-creator-standard/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct MintManager {
pub mint: Pubkey,
pub authority: Pubkey,
pub ruleset: Pubkey,
pub in_use_by: Pubkey,
pub in_use_by: Option<Pubkey>,
}

pub const RULESET_SEED: &str = "ruleset";
Expand Down
79 changes: 78 additions & 1 deletion sdk/cardinal_creator_standard.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0",
"version": "0.1.5",
"name": "cardinal_creator_standard",
"instructions": [
{
Expand Down Expand Up @@ -96,6 +96,50 @@
}
]
},
{
"name": "setInUseBy",
"accounts": [
{
"name": "mintManager",
"isMut": true,
"isSigner": false
},
{
"name": "holder",
"isMut": false,
"isSigner": true
},
{
"name": "holderTokenAccount",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "ix",
"type": {
"defined": "SetInUseByIx"
}
}
]
},
{
"name": "removeInUseBy",
"accounts": [
{
"name": "mintManager",
"isMut": true,
"isSigner": false
},
{
"name": "user",
"isMut": false,
"isSigner": true
}
],
"args": []
},
{
"name": "initRuleset",
"accounts": [
Expand Down Expand Up @@ -527,6 +571,12 @@
{
"name": "ruleset",
"type": "publicKey"
},
{
"name": "inUseBy",
"type": {
"option": "publicKey"
}
}
]
}
Expand Down Expand Up @@ -593,6 +643,18 @@
}
],
"types": [
{
"name": "SetInUseByIx",
"type": {
"kind": "struct",
"fields": [
{
"name": "inUseBy",
"type": "publicKey"
}
]
}
},
{
"name": "UpdateMintManagerIx",
"type": {
Expand Down Expand Up @@ -769,6 +831,21 @@
"code": 6014,
"name": "AccountNotFound",
"msg": "Account not found in instruction"
},
{
"code": 6015,
"name": "TokenAlreadyInUse",
"msg": "Token already in use"
},
{
"code": 6016,
"name": "InvalidTokenUser",
"msg": "Invalid token user"
},
{
"code": 6017,
"name": "TokenCurentlyInUse",
"msg": "Token currently in use"
}
],
"metadata": {
Expand Down
37 changes: 22 additions & 15 deletions sdk/generated/accounts/MintManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type MintManagerArgs = {
mint: web3.PublicKey
authority: web3.PublicKey
ruleset: web3.PublicKey
inUseBy: beet.COption<web3.PublicKey>
}

export const mintManagerDiscriminator = [202, 47, 44, 178, 55, 215, 117, 40]
Expand All @@ -36,7 +37,8 @@ export class MintManager implements MintManagerArgs {
readonly version: number,
readonly mint: web3.PublicKey,
readonly authority: web3.PublicKey,
readonly ruleset: web3.PublicKey
readonly ruleset: web3.PublicKey,
readonly inUseBy: beet.COption<web3.PublicKey>
) {}

/**
Expand All @@ -48,7 +50,8 @@ export class MintManager implements MintManagerArgs {
args.version,
args.mint,
args.authority,
args.ruleset
args.ruleset,
args.inUseBy
)
}

Expand Down Expand Up @@ -115,36 +118,38 @@ export class MintManager implements MintManagerArgs {

/**
* Returns the byteSize of a {@link Buffer} holding the serialized data of
* {@link MintManager}
* {@link MintManager} for the provided args.
*
* @param args need to be provided since the byte size for this account
* depends on them
*/
static get byteSize() {
return mintManagerBeet.byteSize
static byteSize(args: MintManagerArgs) {
const instance = MintManager.fromArgs(args)
return mintManagerBeet.toFixedFromValue({
accountDiscriminator: mintManagerDiscriminator,
...instance,
}).byteSize
}

/**
* Fetches the minimum balance needed to exempt an account holding
* {@link MintManager} data from rent
*
* @param args need to be provided since the byte size for this account
* depends on them
* @param connection used to retrieve the rent exemption information
*/
static async getMinimumBalanceForRentExemption(
args: MintManagerArgs,
connection: web3.Connection,
commitment?: web3.Commitment
): Promise<number> {
return connection.getMinimumBalanceForRentExemption(
MintManager.byteSize,
MintManager.byteSize(args),
commitment
)
}

/**
* Determines if the provided {@link Buffer} has the correct byte size to
* hold {@link MintManager} data.
*/
static hasCorrectByteSize(buf: Buffer, offset = 0) {
return buf.byteLength - offset === MintManager.byteSize
}

/**
* Returns a readable version of {@link MintManager} properties
* and can be used to convert to JSON and/or logging
Expand All @@ -156,6 +161,7 @@ export class MintManager implements MintManagerArgs {
mint: this.mint.toBase58(),
authority: this.authority.toBase58(),
ruleset: this.ruleset.toBase58(),
inUseBy: this.inUseBy,
}
}
}
Expand All @@ -164,7 +170,7 @@ export class MintManager implements MintManagerArgs {
* @category Accounts
* @category generated
*/
export const mintManagerBeet = new beet.BeetStruct<
export const mintManagerBeet = new beet.FixableBeetStruct<
MintManager,
MintManagerArgs & {
accountDiscriminator: number[] /* size: 8 */
Expand All @@ -177,6 +183,7 @@ export const mintManagerBeet = new beet.BeetStruct<
['mint', beetSolana.publicKey],
['authority', beetSolana.publicKey],
['ruleset', beetSolana.publicKey],
['inUseBy', beet.coption(beetSolana.publicKey)],
],
MintManager.fromArgs,
'MintManager'
Expand Down
Loading

0 comments on commit 370a4ea

Please sign in to comment.