Parcourir la source

anchor memotransfer example

John il y a 1 an
Parent
commit
e8d88222dc

+ 7 - 0
tokens/token-2022/memo-transfer/anchor/.gitignore

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

+ 7 - 0
tokens/token-2022/memo-transfer/anchor/.prettierignore

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

+ 18 - 0
tokens/token-2022/memo-transfer/anchor/Anchor.toml

@@ -0,0 +1,18 @@
+[toolchain]
+
+[features]
+resolution = true
+skip-lint = false
+
+[programs.localnet]
+memo_transfer = "5BQyC7y2Pc283woThq11uZRqsgcRbBRLKz4yQ8BJadi2"
+
+[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/memo-transfer/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/memo-transfer/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.
+};

+ 21 - 0
tokens/token-2022/memo-transfer/anchor/package.json

@@ -0,0 +1,21 @@
+{
+  "scripts": {
+    "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
+    "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
+  },
+  "dependencies": {
+    "@coral-xyz/anchor": "^0.30.0",
+    "@solana/spl-memo": "^0.2.5",
+    "@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/memo-transfer/anchor/programs/memo-transfer/Cargo.toml

@@ -0,0 +1,21 @@
+[package]
+name = "memo-transfer"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "memo_transfer"
+
+[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 = { version = "0.30.0", features = ["init-if-needed"] }
+anchor-spl = "0.30.0"

+ 2 - 0
tokens/token-2022/memo-transfer/anchor/programs/memo-transfer/Xargo.toml

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

+ 102 - 0
tokens/token-2022/memo-transfer/anchor/programs/memo-transfer/src/lib.rs

@@ -0,0 +1,102 @@
+use anchor_lang::prelude::*;
+use anchor_lang::system_program::{create_account, CreateAccount};
+use anchor_spl::{
+    token_2022::{
+        initialize_account3,
+        spl_token_2022::{extension::ExtensionType, pod::PodAccount},
+        InitializeAccount3,
+    },
+    token_interface::{
+        memo_transfer_disable, memo_transfer_initialize, MemoTransfer, Mint, Token2022,
+        TokenAccount,
+    },
+};
+
+declare_id!("5BQyC7y2Pc283woThq11uZRqsgcRbBRLKz4yQ8BJadi2");
+
+#[program]
+pub mod memo_transfer {
+    use super::*;
+
+    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
+        // Calculate space required for token and extension data
+        let token_account_size =
+            ExtensionType::try_calculate_account_len::<PodAccount>(&[ExtensionType::MemoTransfer])?;
+
+        // Calculate minimum lamports required for size of token account with extensions
+        let lamports = (Rent::get()?).minimum_balance(token_account_size);
+
+        // Invoke System Program to create new account with space for token account 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.token_account.to_account_info(),
+                },
+            ),
+            lamports,                          // Lamports
+            token_account_size as u64,         // Space
+            &ctx.accounts.token_program.key(), // Owner Program
+        )?;
+
+        // Initialize the standard token account data
+        initialize_account3(CpiContext::new(
+            ctx.accounts.token_program.to_account_info(),
+            InitializeAccount3 {
+                account: ctx.accounts.token_account.to_account_info(),
+                mint: ctx.accounts.mint_account.to_account_info(),
+                authority: ctx.accounts.payer.to_account_info(),
+            },
+        ))?;
+
+        // Initialize the memo transfer extension
+        // This instruction must come after the token account initialization
+        memo_transfer_initialize(CpiContext::new(
+            ctx.accounts.token_program.to_account_info(),
+            MemoTransfer {
+                token_program_id: ctx.accounts.token_program.to_account_info(),
+                account: ctx.accounts.token_account.to_account_info(),
+                owner: ctx.accounts.payer.to_account_info(),
+            },
+        ))?;
+        Ok(())
+    }
+
+    pub fn disable(ctx: Context<Disable>) -> Result<()> {
+        memo_transfer_disable(CpiContext::new(
+            ctx.accounts.token_program.to_account_info(),
+            MemoTransfer {
+                token_program_id: ctx.accounts.token_program.to_account_info(),
+                account: ctx.accounts.token_account.to_account_info(),
+                owner: ctx.accounts.owner.to_account_info(),
+            },
+        ))?;
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct Initialize<'info> {
+    #[account(mut)]
+    pub payer: Signer<'info>,
+
+    #[account(mut)]
+    pub token_account: Signer<'info>,
+    pub mint_account: InterfaceAccount<'info, Mint>,
+    pub token_program: Program<'info, Token2022>,
+    pub system_program: Program<'info, System>,
+}
+
+#[derive(Accounts)]
+pub struct Disable<'info> {
+    #[account(mut)]
+    pub owner: Signer<'info>,
+
+    #[account(
+        mut,
+        token::authority = owner,
+    )]
+    pub token_account: InterfaceAccount<'info, TokenAccount>,
+    pub token_program: Program<'info, Token2022>,
+}

+ 200 - 0
tokens/token-2022/memo-transfer/anchor/tests/memo-transfer.ts

@@ -0,0 +1,200 @@
+import * as anchor from "@coral-xyz/anchor";
+import { Program } from "@coral-xyz/anchor";
+import { MemoTransfer } from "../target/types/memo_transfer";
+import {
+  TOKEN_2022_PROGRAM_ID,
+  createAccount,
+  createMint,
+  createTransferInstruction,
+  mintTo,
+} from "@solana/spl-token";
+import { createMemoInstruction } from "@solana/spl-memo";
+import { Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
+
+describe("memo-transfer", () => {
+  // Configure the client to use the local cluster.
+  const provider = anchor.AnchorProvider.env();
+  const connection = provider.connection;
+  const wallet = provider.wallet as anchor.Wallet;
+  anchor.setProvider(provider);
+
+  const program = anchor.workspace.MemoTransfer as Program<MemoTransfer>;
+
+  const mintKeypair = new anchor.web3.Keypair();
+  const tokenKeypair = new anchor.web3.Keypair();
+
+  it("Create Token Account with RequiredMemo extension", async () => {
+    await createMint(
+      connection,
+      wallet.payer, // Payer of the transaction and initialization fees
+      wallet.publicKey, // Mint Authority
+      null, // Optional Freeze Authority
+      2, // Decimals of Mint
+      mintKeypair, // Optional keypair
+      undefined, // Options for confirming the transaction
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    const transactionSignature = await program.methods
+      .initialize()
+      .accounts({
+        mintAccount: mintKeypair.publicKey,
+        tokenAccount: tokenKeypair.publicKey,
+      })
+      .signers([tokenKeypair])
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Attempt transfer without memo, expect fail", async () => {
+    // Create a new token account to transfer to
+    const sourceTokenAccount = await createAccount(
+      connection,
+      wallet.payer, // Payer to create Token Account
+      mintKeypair.publicKey, // Mint Account address
+      wallet.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,
+      mintKeypair.publicKey,
+      sourceTokenAccount,
+      wallet.payer,
+      1,
+      [],
+      null,
+      TOKEN_2022_PROGRAM_ID
+    );
+
+    const transferInstruction = createTransferInstruction(
+      sourceTokenAccount, // Source Token Account
+      tokenKeypair.publicKey, // Destination Token Account
+      wallet.publicKey, // Source Token Account owner
+      1, // Amount
+      undefined, // Additional signers
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    const transaction = new Transaction().add(transferInstruction);
+
+    try {
+      // Send transaction
+      await sendAndConfirmTransaction(
+        connection,
+        transaction,
+        [wallet.payer] // Signers
+      );
+    } catch (error) {
+      console.log("\nExpect Error:", error.logs);
+    }
+  });
+
+  it("Attempt transfer with memo, expect success", async () => {
+    // Create a new token account to transfer to
+    const sourceTokenAccount = await createAccount(
+      connection,
+      wallet.payer, // Payer to create Token Account
+      mintKeypair.publicKey, // Mint Account address
+      wallet.publicKey, // Token Account owner
+      new anchor.web3.Keypair(), // Optional keypair, default to Associated Token Account
+      undefined, // Confirmation options
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    await mintTo(
+      connection,
+      wallet.payer,
+      mintKeypair.publicKey,
+      sourceTokenAccount,
+      wallet.payer,
+      1,
+      [],
+      null,
+      TOKEN_2022_PROGRAM_ID
+    );
+
+    const memoInstruction = createMemoInstruction("hello, world", [
+      wallet.publicKey,
+    ]);
+
+    const transferInstruction = createTransferInstruction(
+      sourceTokenAccount, // Source Token Account
+      tokenKeypair.publicKey, // Destination Token Account
+      wallet.publicKey, // Source Token Account owner
+      1, // Amount
+      undefined, // Additional signers
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    const transaction = new Transaction().add(
+      memoInstruction,
+      transferInstruction
+    );
+
+    const transactionSignature = await sendAndConfirmTransaction(
+      connection,
+      transaction,
+      [wallet.payer] // Signers
+    );
+
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Disable RequiredMemo extension", async () => {
+    const transactionSignature = await program.methods
+      .disable()
+      .accounts({
+        tokenAccount: tokenKeypair.publicKey,
+      })
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Attempt transfer without memo, expect success", async () => {
+    // Create a new token account to transfer to
+    const sourceTokenAccount = await createAccount(
+      connection,
+      wallet.payer, // Payer to create Token Account
+      mintKeypair.publicKey, // Mint Account address
+      wallet.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,
+      mintKeypair.publicKey,
+      sourceTokenAccount,
+      wallet.payer,
+      1,
+      [],
+      null,
+      TOKEN_2022_PROGRAM_ID
+    );
+
+    const transferInstruction = createTransferInstruction(
+      sourceTokenAccount, // Source Token Account
+      tokenKeypair.publicKey, // Destination Token Account
+      wallet.publicKey, // Source Token Account owner
+      1, // Amount
+      undefined, // Additional signers
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    const transaction = new Transaction().add(transferInstruction);
+
+    const transactionSignature = await sendAndConfirmTransaction(
+      connection,
+      transaction,
+      [wallet.payer] // Signers
+    );
+
+    console.log("Your transaction signature", transactionSignature);
+  });
+});

+ 10 - 0
tokens/token-2022/memo-transfer/anchor/tsconfig.json

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