Browse Source

anchor immutable owner example, and minor misc update

John 1 year ago
parent
commit
69c2783279
37 changed files with 253 additions and 8 deletions
  1. 1 2
      tokens/token-2022/default-account-state/anchor/programs/default-account-state/src/lib.rs
  2. 0 0
      tokens/token-2022/immutable-owner/anchor/.gitignore
  3. 0 0
      tokens/token-2022/immutable-owner/anchor/.prettierignore
  4. 18 0
      tokens/token-2022/immutable-owner/anchor/Anchor.toml
  5. 0 0
      tokens/token-2022/immutable-owner/anchor/Cargo.toml
  6. 0 0
      tokens/token-2022/immutable-owner/anchor/migrations/deploy.ts
  7. 0 0
      tokens/token-2022/immutable-owner/anchor/package.json
  8. 22 0
      tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/Cargo.toml
  9. 0 0
      tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/Xargo.toml
  10. 75 0
      tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/src/lib.rs
  11. 62 0
      tokens/token-2022/immutable-owner/anchor/tests/immutable-owner.ts
  12. 0 0
      tokens/token-2022/immutable-owner/anchor/tsconfig.json
  13. 1 2
      tokens/token-2022/interest-bearing/anchor/programs/interest-bearing/src/lib.rs
  14. 1 2
      tokens/token-2022/non-transferable/anchor/programs/non-transferable/src/lib.rs
  15. 7 0
      tokens/token-2022/transfer-fee/anchor/.gitignore
  16. 7 0
      tokens/token-2022/transfer-fee/anchor/.prettierignore
  17. 0 0
      tokens/token-2022/transfer-fee/anchor/Anchor.toml
  18. 14 0
      tokens/token-2022/transfer-fee/anchor/Cargo.toml
  19. 12 0
      tokens/token-2022/transfer-fee/anchor/migrations/deploy.ts
  20. 20 0
      tokens/token-2022/transfer-fee/anchor/package.json
  21. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/Cargo.toml
  22. 2 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/Xargo.toml
  23. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/harvest.rs
  24. 1 2
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/initialize.rs
  25. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/mod.rs
  26. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/transfer.rs
  27. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/update_fee.rs
  28. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/withdraw.rs
  29. 0 0
      tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/lib.rs
  30. 0 0
      tokens/token-2022/transfer-fee/anchor/tests/transfer-fee.ts
  31. 0 0
      tokens/token-2022/transfer-fee/anchor/tsconfig.json
  32. 0 0
      tokens/token-2022/transfer-fee/native/cicd.sh
  33. 0 0
      tokens/token-2022/transfer-fee/native/package.json
  34. 0 0
      tokens/token-2022/transfer-fee/native/program/Cargo.toml
  35. 0 0
      tokens/token-2022/transfer-fee/native/program/src/lib.rs
  36. 0 0
      tokens/token-2022/transfer-fee/native/tests/test.ts
  37. 10 0
      tokens/token-2022/transfer-fee/native/tsconfig.json

+ 1 - 2
tokens/token-2022/default-account-state/anchor/programs/default-account-state/src/lib.rs

@@ -24,8 +24,7 @@ pub mod default_account_state {
         // 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);

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/.gitignore → tokens/token-2022/immutable-owner/anchor/.gitignore


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/.prettierignore → tokens/token-2022/immutable-owner/anchor/.prettierignore


+ 18 - 0
tokens/token-2022/immutable-owner/anchor/Anchor.toml

@@ -0,0 +1,18 @@
+[toolchain]
+
+[features]
+resolution = true
+skip-lint = false
+
+[programs.localnet]
+immutable_owner = "6g5URpqqurW8RbKjuGeRCVZBKky3J4kYcLeotQ6vj6UT"
+
+[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"

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/Cargo.toml → tokens/token-2022/immutable-owner/anchor/Cargo.toml


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/migrations/deploy.ts → tokens/token-2022/immutable-owner/anchor/migrations/deploy.ts


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/package.json → tokens/token-2022/immutable-owner/anchor/package.json


+ 22 - 0
tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/Cargo.toml

@@ -0,0 +1,22 @@
+[package]
+name = "immutable-owner"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "immutable_owner"
+
+[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"
+

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/Xargo.toml → tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/Xargo.toml


+ 75 - 0
tokens/token-2022/immutable-owner/anchor/programs/immutable-owner/src/lib.rs

@@ -0,0 +1,75 @@
+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::{immutable_owner_initialize, ImmutableOwnerInitialize, Mint, Token2022},
+};
+
+declare_id!("6g5URpqqurW8RbKjuGeRCVZBKky3J4kYcLeotQ6vj6UT");
+
+#[program]
+pub mod immutable_owner {
+    use super::*;
+
+    // There is currently not an anchor constraint to automatically initialize the ImmutableOwner extension
+    // We can manually create and initialize the token account via CPIs in the instruction handler
+    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::ImmutableOwner,
+        ])?;
+
+        // 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 token account with the immutable owner extension
+        immutable_owner_initialize(CpiContext::new(
+            ctx.accounts.token_program.to_account_info(),
+            ImmutableOwnerInitialize {
+                token_program_id: ctx.accounts.token_program.to_account_info(),
+                token_account: ctx.accounts.token_account.to_account_info(),
+            },
+        ))?;
+
+        // 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(),
+            },
+        ))?;
+        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>,
+}

+ 62 - 0
tokens/token-2022/immutable-owner/anchor/tests/immutable-owner.ts

@@ -0,0 +1,62 @@
+import * as anchor from "@coral-xyz/anchor";
+import { Program } from "@coral-xyz/anchor";
+import { ImmutableOwner } from "../target/types/immutable_owner";
+import {
+  AuthorityType,
+  TOKEN_2022_PROGRAM_ID,
+  createMint,
+  setAuthority,
+} from "@solana/spl-token";
+
+describe("immutable-owner", () => {
+  // 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.ImmutableOwner as Program<ImmutableOwner>;
+
+  const tokenKeypair = new anchor.web3.Keypair();
+
+  it("Create Token Account with ImmutableOwner extension", async () => {
+    const mint = await createMint(
+      connection,
+      wallet.payer, // Payer of the transaction and initialization fees
+      wallet.publicKey, // Mint Authority
+      null, // Optional Freeze Authority
+      2, // Decimals of Mint
+      undefined, // Optional keypair
+      undefined, // Options for confirming the transaction
+      TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+    );
+
+    const transactionSignature = await program.methods
+      .initialize()
+      .accounts({
+        mintAccount: mint,
+        tokenAccount: tokenKeypair.publicKey,
+      })
+      .signers([tokenKeypair])
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+
+  it("Attempt to change token account owner, expect fail", async () => {
+    try {
+      await setAuthority(
+        connection, // Connection to use
+        wallet.payer, // Payer of the transaction fee
+        tokenKeypair.publicKey, // Token Account
+        wallet.publicKey, // Owner of the Token Account
+        AuthorityType.AccountOwner, // Type of Authority
+        new anchor.web3.Keypair().publicKey, // Random address as new account Owner
+        undefined, // Additional signers
+        undefined, // Confirmation options
+        TOKEN_2022_PROGRAM_ID // Token Extension Program ID
+      );
+    } catch (error) {
+      console.log("\nExpect Error:", error.logs);
+    }
+  });
+});

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/tsconfig.json → tokens/token-2022/immutable-owner/anchor/tsconfig.json


+ 1 - 2
tokens/token-2022/interest-bearing/anchor/programs/interest-bearing/src/lib.rs

@@ -30,8 +30,7 @@ pub mod interest_bearing {
         // Calculate space required for mint and extension data
         let mint_size = ExtensionType::try_calculate_account_len::<PodMint>(&[
             ExtensionType::InterestBearingConfig,
-        ])
-        .unwrap();
+        ])?;
 
         // Calculate minimum lamports required for size of mint account with extensions
         let lamports = (Rent::get()?).minimum_balance(mint_size);

+ 1 - 2
tokens/token-2022/non-transferable/anchor/programs/non-transferable/src/lib.rs

@@ -20,8 +20,7 @@ pub mod non_transferable {
     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::NonTransferable])
-                .unwrap();
+            ExtensionType::try_calculate_account_len::<PodMint>(&[ExtensionType::NonTransferable])?;
 
         // Calculate minimum lamports required for size of mint account with extensions
         let lamports = (Rent::get()?).minimum_balance(mint_size);

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

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

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

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

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/Anchor.toml → tokens/token-2022/transfer-fee/anchor/Anchor.toml


+ 14 - 0
tokens/token-2022/transfer-fee/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/transfer-fee/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/transfer-fee/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"
+  }
+}

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/Cargo.toml → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/Cargo.toml


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

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

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/harvest.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/harvest.rs


+ 1 - 2
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/initialize.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/initialize.rs

@@ -39,8 +39,7 @@ pub fn process_initialize(
 ) -> Result<()> {
     // Calculate space required for mint and extension data
     let mint_size =
-        ExtensionType::try_calculate_account_len::<PodMint>(&[ExtensionType::TransferFeeConfig])
-            .unwrap();
+        ExtensionType::try_calculate_account_len::<PodMint>(&[ExtensionType::TransferFeeConfig])?;
 
     // Calculate minimum lamports required for size of mint account with extensions
     let lamports = (Rent::get()?).minimum_balance(mint_size);

+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/mod.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/mod.rs


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/transfer.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/transfer.rs


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/update_fee.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/update_fee.rs


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/instructions/withdraw.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/instructions/withdraw.rs


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/programs/transfer-fee/src/lib.rs → tokens/token-2022/transfer-fee/anchor/programs/transfer-fee/src/lib.rs


+ 0 - 0
tokens/token-2022/transfer-fees/anchor/tests/transfer-fee.ts → tokens/token-2022/transfer-fee/anchor/tests/transfer-fee.ts


+ 0 - 0
tokens/token-2022/transfer-fees/native/tsconfig.json → tokens/token-2022/transfer-fee/anchor/tsconfig.json


+ 0 - 0
tokens/token-2022/transfer-fees/native/cicd.sh → tokens/token-2022/transfer-fee/native/cicd.sh


+ 0 - 0
tokens/token-2022/transfer-fees/native/package.json → tokens/token-2022/transfer-fee/native/package.json


+ 0 - 0
tokens/token-2022/transfer-fees/native/program/Cargo.toml → tokens/token-2022/transfer-fee/native/program/Cargo.toml


+ 0 - 0
tokens/token-2022/transfer-fees/native/program/src/lib.rs → tokens/token-2022/transfer-fee/native/program/src/lib.rs


+ 0 - 0
tokens/token-2022/transfer-fees/native/tests/test.ts → tokens/token-2022/transfer-fee/native/tests/test.ts


+ 10 - 0
tokens/token-2022/transfer-fee/native/tsconfig.json

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