Quellcode durchsuchen

anchor group/member example placeholder, extensions not live

John vor 1 Jahr
Ursprung
Commit
1ad8702b85

+ 7 - 0
tokens/token-2022/group/anchor/.gitignore

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

+ 7 - 0
tokens/token-2022/group/anchor/.prettierignore

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

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

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

+ 14 - 0
tokens/token-2022/group/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/group/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.
+};

+ 19 - 0
tokens/token-2022/group/anchor/package.json

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

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

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

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

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

+ 76 - 0
tokens/token-2022/group/anchor/programs/group/src/lib.rs

@@ -0,0 +1,76 @@
+use anchor_lang::prelude::*;
+use anchor_spl::token_2022::spl_token_2022::extension::group_pointer::GroupPointer;
+use anchor_spl::token_interface::{
+    spl_token_2022::{
+        extension::{BaseStateWithExtensions, StateWithExtensions},
+        state::Mint as MintState,
+    },
+    token_group_initialize, Mint, Token2022, TokenGroupInitialize,
+};
+
+declare_id!("4XCDGMD8fsdjUzmYj6d9if8twFt1f23Ym52iDmWK8fFs");
+
+#[program]
+pub mod group {
+
+    use super::*;
+
+    pub fn test_initialize_group(ctx: Context<InitializeGroup>) -> Result<()> {
+        ctx.accounts.check_mint_data()?;
+
+        // // Token Group and Token Member extensions features not enabled yet on the Token2022 program
+        // // This is temporary placeholder to update one extensions are live
+        // // Initializing the "pointers" works, but you can't initialize the group/member data yet
+
+        // let signer_seeds: &[&[&[u8]]] = &[&[b"group", &[ctx.bumps.mint_account]]];
+        // token_group_initialize(
+        //     CpiContext::new(
+        //         ctx.accounts.token_program.to_account_info(),
+        //         TokenGroupInitialize {
+        //             token_program_id: ctx.accounts.token_program.to_account_info(),
+        //             group: ctx.accounts.mint_account.to_account_info(),
+        //             mint: ctx.accounts.mint_account.to_account_info(),
+        //             mint_authority: ctx.accounts.mint_account.to_account_info(),
+        //         },
+        //     )
+        //     .with_signer(signer_seeds),
+        //     Some(ctx.accounts.payer.key()), // update_authority
+        //     10,                             // max_size
+        // )?;
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct InitializeGroup<'info> {
+    #[account(mut)]
+    pub payer: Signer<'info>,
+
+    #[account(
+        init,
+        seeds = [b"group"],
+        bump,
+        payer = payer,
+        mint::decimals = 2,
+        mint::authority = mint_account,
+        mint::freeze_authority = mint_account,
+        extensions::group_pointer::authority = mint_account,
+        extensions::group_pointer::group_address = mint_account,
+    )]
+    pub mint_account: InterfaceAccount<'info, Mint>,
+    pub token_program: Program<'info, Token2022>,
+    pub system_program: Program<'info, System>,
+}
+
+impl<'info> InitializeGroup<'info> {
+    pub fn check_mint_data(&self) -> Result<()> {
+        let mint = &self.mint_account.to_account_info();
+        let mint_data = mint.data.borrow();
+        let mint_with_extension = StateWithExtensions::<MintState>::unpack(&mint_data)?;
+        let extension_data = mint_with_extension.get_extension::<GroupPointer>()?;
+
+        msg!("{:?}", mint_with_extension);
+        msg!("{:?}", extension_data);
+        Ok(())
+    }
+}

+ 21 - 0
tokens/token-2022/group/anchor/tests/group.ts

@@ -0,0 +1,21 @@
+import * as anchor from "@coral-xyz/anchor";
+import { Program } from "@coral-xyz/anchor";
+import { Group } from "../target/types/group";
+
+describe("group", () => {
+  // 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.Group as Program<Group>;
+
+  it("Create Mint with Group Pointer", async () => {
+    const transactionSignature = await program.methods
+      .testInitializeGroup()
+      .accounts({})
+      .rpc({ skipPreflight: true });
+    console.log("Your transaction signature", transactionSignature);
+  });
+});

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

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