Browse Source

anchor default-account-state example

John 1 year ago
parent
commit
6e6fb351a3

+ 7 - 0
tokens/token-2022/default-account-state/anchor/.gitignore

@@ -0,0 +1,7 @@
+.anchor
+.DS_Store
+target
+**/*.rs.bk
+node_modules
+test-ledger
+.yarn

+ 7 - 0
tokens/token-2022/default-account-state/anchor/.prettierignore

@@ -0,0 +1,7 @@
+.anchor
+.DS_Store
+target
+node_modules
+dist
+build
+test-ledger

+ 18 - 0
tokens/token-2022/default-account-state/anchor/Anchor.toml

@@ -0,0 +1,18 @@
+[toolchain]
+
+[features]
+resolution = true
+skip-lint = false
+
+[programs.localnet]
+default_account_state = "5LdYbHiUsFxVG8bfqoeBkhBYMRmWZb3BoLuABgYW7coB"
+
+[registry]
+url = "https://api.apr.dev"
+
+[provider]
+cluster = "Localnet"
+wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

+ 14 - 0
tokens/token-2022/default-account-state/anchor/Cargo.toml

@@ -0,0 +1,14 @@
+[workspace]
+members = [
+    "programs/*"
+]
+resolver = "2"
+
+[profile.release]
+overflow-checks = true
+lto = "fat"
+codegen-units = 1
+[profile.release.build-override]
+opt-level = 3
+incremental = false
+codegen-units = 1

+ 12 - 0
tokens/token-2022/default-account-state/anchor/migrations/deploy.ts

@@ -0,0 +1,12 @@
+// Migrations are an early feature. Currently, they're nothing more than this
+// single deploy script that's invoked from the CLI, injecting a provider
+// configured from the workspace's Anchor.toml.
+
+const anchor = require("@coral-xyz/anchor");
+
+module.exports = async function (provider) {
+  // Configure client to use the provider.
+  anchor.setProvider(provider);
+
+  // Add your deploy script here.
+};

+ 20 - 0
tokens/token-2022/default-account-state/anchor/package.json

@@ -0,0 +1,20 @@
+{
+  "scripts": {
+    "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
+    "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
+  },
+  "dependencies": {
+    "@coral-xyz/anchor": "^0.30.0",
+    "@solana/spl-token": "^0.4.6"
+  },
+  "devDependencies": {
+    "@types/bn.js": "^5.1.0",
+    "@types/chai": "^4.3.0",
+    "@types/mocha": "^9.0.0",
+    "chai": "^4.3.4",
+    "mocha": "^9.0.3",
+    "prettier": "^2.6.2",
+    "ts-mocha": "^10.0.0",
+    "typescript": "^4.3.5"
+  }
+}

+ 21 - 0
tokens/token-2022/default-account-state/anchor/programs/default-account-state/Cargo.toml

@@ -0,0 +1,21 @@
+[package]
+name = "default-account-state"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "default_account_state"
+
+[features]
+default = []
+cpi = ["no-entrypoint"]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
+
+[dependencies]
+anchor-lang = "0.30.0"
+anchor-spl = "0.30.0"

+ 2 - 0
tokens/token-2022/default-account-state/anchor/programs/default-account-state/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 140 - 0
tokens/token-2022/default-account-state/anchor/programs/default-account-state/src/lib.rs

@@ -0,0 +1,140 @@
+use anchor_lang::prelude::*;
+use anchor_lang::system_program::{create_account, CreateAccount};
+use anchor_spl::{
+    token_2022::{
+        initialize_mint2,
+        spl_token_2022::{extension::ExtensionType, pod::PodMint, state::AccountState},
+        InitializeMint2,
+    },
+    token_interface::{
+        default_account_state_initialize, default_account_state_update,
+        DefaultAccountStateInitialize, DefaultAccountStateUpdate, Mint, Token2022,
+    },
+};
+
+declare_id!("5LdYbHiUsFxVG8bfqoeBkhBYMRmWZb3BoLuABgYW7coB");
+
+#[program]
+pub mod default_account_state {
+    use super::*;
+
+    // There is currently not an anchor constraint to automatically initialize the DefaultAccountState extension
+    // We can manually create and initialize the mint account via CPIs in the instruction handler
+    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
+        // Calculate space required for mint and extension data
+        let mint_size = ExtensionType::try_calculate_account_len::<PodMint>(&[
+            ExtensionType::DefaultAccountState,
+        ])
+        .unwrap();
+
+        // Calculate minimum lamports required for size of mint account with extensions
+        let lamports = (Rent::get()?).minimum_balance(mint_size);
+
+        // Invoke System Program to create new account with space for mint and extension data
+        create_account(
+            CpiContext::new(
+                ctx.accounts.system_program.to_account_info(),
+                CreateAccount {
+                    from: ctx.accounts.payer.to_account_info(),
+                    to: ctx.accounts.mint_account.to_account_info(),
+                },
+            ),
+            lamports,                          // Lamports
+            mint_size as u64,                  // Space
+            &ctx.accounts.token_program.key(), // Owner Program
+        )?;
+
+        // Initialize the NonTransferable extension
+        // This instruction must come before the instruction to initialize the mint data
+        default_account_state_initialize(
+            CpiContext::new(
+                ctx.accounts.token_program.to_account_info(),
+                DefaultAccountStateInitialize {
+                    token_program_id: ctx.accounts.token_program.to_account_info(),
+                    mint: ctx.accounts.mint_account.to_account_info(),
+                },
+            ),
+            &AccountState::Frozen, // default frozen token accounts
+        )?;
+
+        // Initialize the standard mint account data
+        initialize_mint2(
+            CpiContext::new(
+                ctx.accounts.token_program.to_account_info(),
+                InitializeMint2 {
+                    mint: ctx.accounts.mint_account.to_account_info(),
+                },
+            ),
+            2,                               // decimals
+            &ctx.accounts.payer.key(),       // mint authority
+            Some(&ctx.accounts.payer.key()), // freeze authority
+        )?;
+        Ok(())
+    }
+
+    pub fn update_default_state(
+        ctx: Context<UpdateDefaultState>,
+        account_state: AnchorAccountState,
+    ) -> Result<()> {
+        // Convert AnchorAccountState to spl_token_2022::state::AccountState
+        let account_state = account_state.to_spl_account_state();
+
+        default_account_state_update(
+            CpiContext::new(
+                ctx.accounts.token_program.to_account_info(),
+                DefaultAccountStateUpdate {
+                    token_program_id: ctx.accounts.token_program.to_account_info(),
+                    mint: ctx.accounts.mint_account.to_account_info(),
+                    freeze_authority: ctx.accounts.freeze_authority.to_account_info(),
+                },
+            ),
+            &account_state,
+        )?;
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct Initialize<'info> {
+    #[account(mut)]
+    pub payer: Signer<'info>,
+    #[account(mut)]
+    pub mint_account: Signer<'info>,
+
+    pub token_program: Program<'info, Token2022>,
+    pub system_program: Program<'info, System>,
+}
+
+#[derive(Accounts)]
+pub struct UpdateDefaultState<'info> {
+    #[account(mut)]
+    pub freeze_authority: Signer<'info>,
+    #[account(
+        mut,
+        mint::freeze_authority = freeze_authority,
+    )]
+    pub mint_account: InterfaceAccount<'info, Mint>,
+
+    pub token_program: Program<'info, Token2022>,
+    pub system_program: Program<'info, System>,
+}
+
+// Custom enum to implement AnchorSerialize and AnchorDeserialize
+// This is required to pass the enum as an argument to the instruction
+#[derive(AnchorSerialize, AnchorDeserialize)]
+pub enum AnchorAccountState {
+    Uninitialized,
+    Initialized,
+    Frozen,
+}
+
+// Implement conversion from AnchorAccountState to spl_token_2022::state::AccountState
+impl AnchorAccountState {
+    pub fn to_spl_account_state(&self) -> AccountState {
+        match self {
+            AnchorAccountState::Uninitialized => AccountState::Uninitialized,
+            AnchorAccountState::Initialized => AccountState::Initialized,
+            AnchorAccountState::Frozen => AccountState::Frozen,
+        }
+    }
+}

+ 97 - 0
tokens/token-2022/default-account-state/anchor/tests/default-account-state.ts

@@ -0,0 +1,97 @@
+import * as anchor from "@coral-xyz/anchor";
+import { Program } from "@coral-xyz/anchor";
+import { DefaultAccountState } from "../target/types/default_account_state";
+import {
+  TOKEN_2022_PROGRAM_ID,
+  mintTo,
+  createAccount,
+} from "@solana/spl-token";
+
+describe("default-account-state", () => {
+  const provider = anchor.AnchorProvider.env();
+  const connection = provider.connection;
+  const wallet = provider.wallet as anchor.Wallet;
+  anchor.setProvider(provider);
+
+  const program = anchor.workspace
+    .DefaultAccountState as Program<DefaultAccountState>;
+
+  const mintKeypair = new anchor.web3.Keypair();
+
+  it("Create Mint with DefaultAccountState extension", async () => {
+    const transactionSignature = await program.methods
+      .initialize()
+      .accounts({ mintAccount: mintKeypair.publicKey })
+      .signers([mintKeypair])
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Attempt Mint Token, expect fail", async () => {
+    const amount = 1;
+
+    // Create a token account, default state is frozen
+    const tokenAccount = await createAccount(
+      connection,
+      wallet.payer, // Payer to create Token Account
+      mintKeypair.publicKey, // Mint Account address
+      wallet.payer.publicKey, // Token Account owner
+      new anchor.web3.Keypair(), // Optional keypair
+      undefined, // Confirmation options
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    try {
+      // Attempt to mint tokens, expect error
+      await mintTo(
+        connection,
+        wallet.payer, // Transaction fee payer
+        mintKeypair.publicKey, // Mint
+        tokenAccount, // Mint to
+        wallet.payer, // Mint authority
+        amount, // Amount
+        [], // Additional signers
+        null, // Commitment
+        TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+      );
+    } catch (error) {
+      console.log("\nExpect Error:", error.logs);
+    }
+  });
+
+  it("Update DefaultAccountState", async () => {
+    // Update the default state to initialized (not frozen)
+    const transactionSignature = await program.methods
+      .updateDefaultState({ initialized: {} })
+      .accounts({ mintAccount: mintKeypair.publicKey })
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Attempt Mint Token, expect success", async () => {
+    const amount = 1;
+
+    // Create a token account, default state is initialized (not frozen)
+    const tokenAccount = await createAccount(
+      connection,
+      wallet.payer, // Payer to create Token Account
+      mintKeypair.publicKey, // Mint Account address
+      wallet.payer.publicKey, // Token Account owner
+      new anchor.web3.Keypair(), // Optional keypair
+      undefined, // Confirmation options
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    await mintTo(
+      connection,
+      wallet.payer, // Transaction fee payer
+      mintKeypair.publicKey, // Mint
+      tokenAccount, // Mint to
+      wallet.payer, // Mint authority
+      amount, // Amount
+      [], // Additional signers
+      null, // Commitment
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+  });
+});

+ 10 - 0
tokens/token-2022/default-account-state/anchor/tsconfig.json

@@ -0,0 +1,10 @@
+{
+  "compilerOptions": {
+    "types": ["mocha", "chai"],
+    "typeRoots": ["./node_modules/@types"],
+    "lib": ["es2015"],
+    "module": "commonjs",
+    "target": "es6",
+    "esModuleInterop": true
+  }
+}