Skip to content

Commit

Permalink
Use invalidator
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbogle committed Feb 4, 2022
1 parent b5d9477 commit 3bc6b34
Show file tree
Hide file tree
Showing 21 changed files with 891 additions and 233 deletions.
4 changes: 4 additions & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ cardinal_token_manager = "mgrMbgLbusR19KEKMa9WsYDAeL94Tavgc9JHRB1CCGz"
cardinal_payment_manager = "cpmTAQfUopUzqu2BAR5EfnUfqJSgZkMoU7QBvkueyEn"
cardinal_paid_claim_approver = "pcaQ9jQLzb8VszyM6oPRoiGsdjizxMyvGjauhKPD5EF"
cardinal_time_invalidator = "tmexpMz3HojAQ4i97rgaJYhPHM9hV6AzWmQ7EprRPGe"
cardinal_use_invalidator = "useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L"
cardinal_rental_counter = "cntQPZbfxBeLa8HVBbA4fApyAKh8mUxUVeaCjBLFSFP"

[programs.devnet]
cardinal_token_manager = "mgrMbgLbusR19KEKMa9WsYDAeL94Tavgc9JHRB1CCGz"
cardinal_payment_manager = "cpmTAQfUopUzqu2BAR5EfnUfqJSgZkMoU7QBvkueyEn"
cardinal_paid_claim_approver = "pcaQ9jQLzb8VszyM6oPRoiGsdjizxMyvGjauhKPD5EF"
cardinal_time_invalidator = "tmexpMz3HojAQ4i97rgaJYhPHM9hV6AzWmQ7EprRPGe"
cardinal_use_invalidator = "useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L"
cardinal_rental_counter = "cntQPZbfxBeLa8HVBbA4fApyAKh8mUxUVeaCjBLFSFP"

[programs.testnet]
cardinal_token_manager = "mgrMbgLbusR19KEKMa9WsYDAeL94Tavgc9JHRB1CCGz"
cardinal_payment_manager = "cpmTAQfUopUzqu2BAR5EfnUfqJSgZkMoU7QBvkueyEn"
cardinal_paid_claim_approver = "pcaQ9jQLzb8VszyM6oPRoiGsdjizxMyvGjauhKPD5EF"
cardinal_time_invalidator = "tmexpMz3HojAQ4i97rgaJYhPHM9hV6AzWmQ7EprRPGe"
cardinal_use_invalidator = "useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L"
cardinal_rental_counter = "cntQPZbfxBeLa8HVBbA4fApyAKh8mUxUVeaCjBLFSFP"

[programs.localnet]
cardinal_token_manager = "mgrMbgLbusR19KEKMa9WsYDAeL94Tavgc9JHRB1CCGz"
cardinal_payment_manager = "cpmTAQfUopUzqu2BAR5EfnUfqJSgZkMoU7QBvkueyEn"
cardinal_paid_claim_approver = "pcaQ9jQLzb8VszyM6oPRoiGsdjizxMyvGjauhKPD5EF"
cardinal_time_invalidator = "tmexpMz3HojAQ4i97rgaJYhPHM9hV6AzWmQ7EprRPGe"
cardinal_use_invalidator = "useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L"
cardinal_rental_counter = "cntQPZbfxBeLa8HVBbA4fApyAKh8mUxUVeaCjBLFSFP"
4 changes: 2 additions & 2 deletions programs/cardinal-use-invalidator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pub enum ErrorCode {
InvalidIssuerTokenAccount,
#[msg("Invalid token manager for this claim approver")]
InvalidTokenManager,
#[msg("Usages not at the maximum")]
InvalidUsages
#[msg("Usages at the maximum")]
InsufficientUsages
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use {
pub struct IncrementUsagesCtx<'info> {
token_manager: Box<Account<'info, TokenManager>>,

#[account(mut, constraint = use_invalidator.usages + num_usages <= use_invalidator.max_usages @ ErrorCode::InvalidUsages)]
#[account(mut, constraint = use_invalidator.max_usages == None || use_invalidator.usages + num_usages <= use_invalidator.max_usages.unwrap() @ ErrorCode::InsufficientUsages)]
use_invalidator: Box<Account<'info, UseInvalidator>>,

#[account(mut)]
payer: Signer<'info>,
system_program: Program<'info, System>,
// TODO maybe add use authority to this state
// #[account(constraint = user.key() == token_manager.recipient_token_account.owner)]
user: Signer<'info>,
}

pub fn handler(ctx: Context<IncrementUsagesCtx>, num_usages: u64) -> ProgramResult {
Expand Down
4 changes: 2 additions & 2 deletions programs/cardinal-use-invalidator/src/instructions/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
};

#[derive(Accounts)]
#[instruction(bump: u8, _max_usages: u64)]
#[instruction(bump: u8, _max_usages: Option<u64>)]
pub struct InitCtx<'info> {
token_manager: Box<Account<'info, TokenManager>>,

Expand All @@ -22,7 +22,7 @@ pub struct InitCtx<'info> {
system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<InitCtx>, bump: u8, max_usages: u64) -> ProgramResult {
pub fn handler(ctx: Context<InitCtx>, bump: u8, max_usages: Option<u64>) -> ProgramResult {
let use_invalidator = &mut ctx.accounts.use_invalidator;
use_invalidator.bump = bump;
use_invalidator.max_usages = max_usages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct InvalidateCtx<'info> {
token_manager: Box<Account<'info, TokenManager>>,

#[account(mut,
constraint = use_invalidator.usages >= use_invalidator.max_usages @ ErrorCode::InvalidUsages,
constraint = use_invalidator.usages >= use_invalidator.max_usages.unwrap() @ ErrorCode::InsufficientUsages,
close = invalidator
)]
use_invalidator: Box<Account<'info, UseInvalidator>>,
Expand Down
2 changes: 1 addition & 1 deletion programs/cardinal-use-invalidator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare_id!("useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L");
pub mod cardinal_use_invalidator {
use super::*;

pub fn init(ctx: Context<InitCtx>, bump: u8, max_usages: u64) -> ProgramResult {
pub fn init(ctx: Context<InitCtx>, bump: u8, max_usages: Option<u64>) -> ProgramResult {
init::handler(ctx, bump, max_usages)
}

Expand Down
2 changes: 1 addition & 1 deletion programs/cardinal-use-invalidator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pub const USE_INVALIDATOR_SIZE: usize = 8 + std::mem::size_of::<UseInvalidator>(
pub struct UseInvalidator {
pub bump: u8,
pub usages: u64,
pub max_usages: u64,
pub max_usages: Option<u64>,
pub use_authority: Pubkey,
}
34 changes: 33 additions & 1 deletion src/claimLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { Connection, PublicKey } from "@solana/web3.js";
import { Keypair, Transaction } from "@solana/web3.js";

import { tokenManager } from "./programs";
import { tokenManager, useInvalidator } from "./programs";
import { TokenManagerKind } from "./programs/tokenManager";
import { findTokenManagerAddress } from "./programs/tokenManager/pda";
import { withFindOrInitAssociatedTokenAccount } from "./utils";
Expand Down Expand Up @@ -41,11 +41,13 @@ export const issueToken = async (
{
rentalMint,
issuerTokenAccountId,
usages = 1,
amount = new BN(1),
kind = TokenManagerKind.Managed,
}: {
rentalMint: PublicKey;
issuerTokenAccountId: PublicKey;
usages?: number;
amount?: BN;
kind?: TokenManagerKind;
}
Expand All @@ -70,6 +72,14 @@ export const issueToken = async (
)
);

const [useInvalidatorIx] = await useInvalidator.instruction.init(
connection,
wallet,
tokenManagerId,
usages
);
transaction.add(useInvalidatorIx);

if (kind === TokenManagerKind.Managed) {
transaction.add(
SPLToken.createSetAuthorityInstruction(
Expand Down Expand Up @@ -166,3 +176,25 @@ export const claimFromLink = async (

return transaction;
};

export const useTransaction = async (
connection: Connection,
wallet: Wallet,
mintId: PublicKey,
usages: number
): Promise<Transaction> => {
const transaction = new Transaction();
const [tokenManagerId] = await findTokenManagerAddress(mintId);

// use
transaction.add(
await useInvalidator.instruction.incrementUsages(
connection,
wallet,
tokenManagerId,
usages
)
);

return transaction;
};
Loading

0 comments on commit 3bc6b34

Please sign in to comment.