From fc75abffb6c475b0f947dc4402443a6093b33af4 Mon Sep 17 00:00:00 2001 From: Mitch Gildenberg Date: Thu, 19 Dec 2024 06:18:44 -0500 Subject: [PATCH 1/3] feat: add anchor bindings anchor-lang --- Cargo.lock | 23 ++++++++++++++ lang/Cargo.toml | 1 + lang/src/address_lookup_table_program.rs | 39 ++++++++++++++++++++++++ lang/src/lib.rs | 1 + 4 files changed, 64 insertions(+) create mode 100644 lang/src/address_lookup_table_program.rs diff --git a/Cargo.lock b/Cargo.lock index 5ce5100ad8..6552850299 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,6 +265,7 @@ dependencies = [ "bincode", "borsh 0.10.3", "bytemuck", + "shrinkwraprs", "solana-program", "thiserror", ] @@ -2227,6 +2228,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.10.5" @@ -3765,6 +3775,19 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "shrinkwraprs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e63e6744142336dfb606fe2b068afa2e1cca1ee6a5d8377277a92945d81fa331" +dependencies = [ + "bitflags 1.3.2", + "itertools 0.8.2", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/lang/Cargo.toml b/lang/Cargo.toml index f1678680fe..09f390bbcf 100644 --- a/lang/Cargo.toml +++ b/lang/Cargo.toml @@ -58,3 +58,4 @@ borsh = "0.10.3" bytemuck = "1" solana-program = "2" thiserror = "1" +shrinkwraprs = "0.3.0" diff --git a/lang/src/address_lookup_table_program.rs b/lang/src/address_lookup_table_program.rs new file mode 100644 index 0000000000..0683567236 --- /dev/null +++ b/lang/src/address_lookup_table_program.rs @@ -0,0 +1,39 @@ +use crate::prelude::*; +use crate::solana_program::address_lookup_table; +use solana_program::pubkey::Pubkey; +use solana_program::address_lookup_table::state::AddressLookupTable as SolanaAddressLookupTable; + +#[derive(Debug, Clone)] +pub struct AddressLookupTable; +impl anchor_lang::Id for AddressLookupTable { + fn id() -> Pubkey { + address_lookup_table::program::ID + } +} + +#[derive(Clone, shrinkwraprs::Shrinkwrap)] +pub struct AddressLookupTableAccount<'info>(pub SolanaAddressLookupTable<'info>); + +impl AccountSerialize for AddressLookupTableAccount<'_> {} + +impl<'info> AccountDeserialize for AddressLookupTableAccount<'info> { + fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result { + // Deserialize into the temporary struct + let table = SolanaAddressLookupTable::deserialize(buf) + .map_err(|_| ProgramError::InvalidAccountData)?; + + // Construct a new AddressLookupTable with a lifetime tied to 'info + let new_table = SolanaAddressLookupTable { + meta: table.meta, + addresses: std::borrow::Cow::Owned(table.addresses.into_owned()), + }; + + Ok(Self(new_table)) + } +} + +impl Owner for AddressLookupTableAccount<'_> { + fn owner() -> Pubkey { + address_lookup_table::program::ID + } +} diff --git a/lang/src/lib.rs b/lang/src/lib.rs index dd4d3b64b8..ce348d7adf 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -45,6 +45,7 @@ pub mod event; pub mod idl; pub mod system_program; mod vec; +pub mod address_lookup_table_program; #[cfg(feature = "lazy-account")] mod lazy; From bd2ab1573e86a74e8d82ea5934477e90ee624637 Mon Sep 17 00:00:00 2001 From: Mitch Gildenberg Date: Thu, 19 Dec 2024 13:56:50 -0500 Subject: [PATCH 2/3] comments patch#1 --- Cargo.lock | 23 ------------------ lang/Cargo.toml | 1 - lang/src/address_lookup_table_program.rs | 31 +++++++++++++++++++++--- lang/src/lib.rs | 3 ++- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6552850299..5ce5100ad8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,7 +265,6 @@ dependencies = [ "bincode", "borsh 0.10.3", "bytemuck", - "shrinkwraprs", "solana-program", "thiserror", ] @@ -2228,15 +2227,6 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" -[[package]] -name = "itertools" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.10.5" @@ -3775,19 +3765,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "shrinkwraprs" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e63e6744142336dfb606fe2b068afa2e1cca1ee6a5d8377277a92945d81fa331" -dependencies = [ - "bitflags 1.3.2", - "itertools 0.8.2", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/lang/Cargo.toml b/lang/Cargo.toml index 09f390bbcf..f1678680fe 100644 --- a/lang/Cargo.toml +++ b/lang/Cargo.toml @@ -58,4 +58,3 @@ borsh = "0.10.3" bytemuck = "1" solana-program = "2" thiserror = "1" -shrinkwraprs = "0.3.0" diff --git a/lang/src/address_lookup_table_program.rs b/lang/src/address_lookup_table_program.rs index 0683567236..79090e36bf 100644 --- a/lang/src/address_lookup_table_program.rs +++ b/lang/src/address_lookup_table_program.rs @@ -1,7 +1,8 @@ use crate::prelude::*; use crate::solana_program::address_lookup_table; -use solana_program::pubkey::Pubkey; use solana_program::address_lookup_table::state::AddressLookupTable as SolanaAddressLookupTable; +use solana_program::pubkey::Pubkey; +use std::ops::{Deref, DerefMut}; #[derive(Debug, Clone)] pub struct AddressLookupTable; @@ -11,8 +12,8 @@ impl anchor_lang::Id for AddressLookupTable { } } -#[derive(Clone, shrinkwraprs::Shrinkwrap)] -pub struct AddressLookupTableAccount<'info>(pub SolanaAddressLookupTable<'info>); +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AddressLookupTableAccount<'info>(SolanaAddressLookupTable<'info>); impl AccountSerialize for AddressLookupTableAccount<'_> {} @@ -32,8 +33,32 @@ impl<'info> AccountDeserialize for AddressLookupTableAccount<'info> { } } +impl<'info> Deref for AddressLookupTableAccount<'info> { + type Target = SolanaAddressLookupTable<'info>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'info> DerefMut for AddressLookupTableAccount<'info> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl Owner for AddressLookupTableAccount<'_> { fn owner() -> Pubkey { address_lookup_table::program::ID } } + + #[cfg(feature = "idl-build")] + mod idl_build { + use super::*; + + impl crate::IdlBuild for AddressLookupTableAccount<'_> {} + impl crate::Discriminator for AddressLookupTableAccount<'_> { + const DISCRIMINATOR: &'static [u8] = &[]; + } + } diff --git a/lang/src/lib.rs b/lang/src/lib.rs index ce348d7adf..803b70d9f2 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -45,11 +45,12 @@ pub mod event; pub mod idl; pub mod system_program; mod vec; -pub mod address_lookup_table_program; +mod address_lookup_table_program; #[cfg(feature = "lazy-account")] mod lazy; +pub use crate::address_lookup_table_program::*; pub use crate::bpf_upgradeable_state::*; pub use anchor_attribute_access_control::access_control; pub use anchor_attribute_account::{account, declare_id, pubkey, zero_copy}; From 984973a486a2a53b6afef430e2e6b06b2aa2245a Mon Sep 17 00:00:00 2001 From: Mitch Gildenberg Date: Mon, 23 Dec 2024 05:01:03 -0500 Subject: [PATCH 3/3] add test dir and prelude --- lang/src/lib.rs | 1 + .../address-lookup-table-program/Anchor.toml | 9 ++++++ tests/address-lookup-table-program/Cargo.toml | 8 +++++ .../address_lookup_table_state-keypair.json | 1 + .../migrations/deploy.ts | 12 +++++++ .../address-lookup-table-program/package.json | 16 ++++++++++ .../program_with_different_programdata.json | 1 + .../address-lookup-table-program/Cargo.toml | 19 ++++++++++++ .../address-lookup-table-program/Xargo.toml | 2 ++ .../address-lookup-table-program/src/lib.rs | 30 ++++++++++++++++++ .../tests/address-lookup-table.ts | 31 +++++++++++++++++++ .../tsconfig.json | 11 +++++++ 12 files changed, 141 insertions(+) create mode 100644 tests/address-lookup-table-program/Anchor.toml create mode 100644 tests/address-lookup-table-program/Cargo.toml create mode 100644 tests/address-lookup-table-program/address_lookup_table_state-keypair.json create mode 100644 tests/address-lookup-table-program/migrations/deploy.ts create mode 100644 tests/address-lookup-table-program/package.json create mode 100644 tests/address-lookup-table-program/program_with_different_programdata.json create mode 100644 tests/address-lookup-table-program/programs/address-lookup-table-program/Cargo.toml create mode 100644 tests/address-lookup-table-program/programs/address-lookup-table-program/Xargo.toml create mode 100644 tests/address-lookup-table-program/programs/address-lookup-table-program/src/lib.rs create mode 100644 tests/address-lookup-table-program/tests/address-lookup-table.ts create mode 100644 tests/address-lookup-table-program/tsconfig.json diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 803b70d9f2..ed66e8a821 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -404,6 +404,7 @@ impl Key for Pubkey { /// All programs should include it via `anchor_lang::prelude::*;`. pub mod prelude { pub use super::{ + address_lookup_table_program::{AddressLookupTable, AddressLookupTableAccount}, access_control, account, accounts::account::Account, accounts::account_loader::AccountLoader, accounts::interface::Interface, accounts::interface_account::InterfaceAccount, accounts::program::Program, diff --git a/tests/address-lookup-table-program/Anchor.toml b/tests/address-lookup-table-program/Anchor.toml new file mode 100644 index 0000000000..05c0441427 --- /dev/null +++ b/tests/address-lookup-table-program/Anchor.toml @@ -0,0 +1,9 @@ +[programs.localnet] +bpf_upgradeable_state = "Cum9tTyj5HwcEiAmhgaS7Bbj4UczCwsucrCkxRECzM4e" + +[provider] +cluster = "localnet" +wallet = "~/.config/solana/id.json" + +[scripts] +test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts" diff --git a/tests/address-lookup-table-program/Cargo.toml b/tests/address-lookup-table-program/Cargo.toml new file mode 100644 index 0000000000..97d6280542 --- /dev/null +++ b/tests/address-lookup-table-program/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +members = [ + "programs/*" +] +resolver = "2" + +[profile.release] +overflow-checks = true diff --git a/tests/address-lookup-table-program/address_lookup_table_state-keypair.json b/tests/address-lookup-table-program/address_lookup_table_state-keypair.json new file mode 100644 index 0000000000..ae39fe9564 --- /dev/null +++ b/tests/address-lookup-table-program/address_lookup_table_state-keypair.json @@ -0,0 +1 @@ +[114,99,192,17,48,208,90,184,231,46,220,91,47,115,132,253,218,163,228,101,8,121,220,138,41,140,176,127,254,91,51,28,176,244,174,182,223,57,57,125,117,201,31,213,9,39,207,212,100,173,88,252,61,235,89,156,53,86,4,90,16,251,191,219] \ No newline at end of file diff --git a/tests/address-lookup-table-program/migrations/deploy.ts b/tests/address-lookup-table-program/migrations/deploy.ts new file mode 100644 index 0000000000..82fb175fa2 --- /dev/null +++ b/tests/address-lookup-table-program/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. +}; diff --git a/tests/address-lookup-table-program/package.json b/tests/address-lookup-table-program/package.json new file mode 100644 index 0000000000..d3416e5955 --- /dev/null +++ b/tests/address-lookup-table-program/package.json @@ -0,0 +1,16 @@ +{ + "name": "address-lookup-table", + "version": "0.24.0", + "license": "(MIT OR Apache-2.0)", + "homepage": "https://github.com/coral-xyz/anchor#readme", + "bugs": { + "url": "https://github.com/coral-xyz/anchor/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/coral-xyz/anchor.git" + }, + "engines": { + "node": ">=17" + } +} diff --git a/tests/address-lookup-table-program/program_with_different_programdata.json b/tests/address-lookup-table-program/program_with_different_programdata.json new file mode 100644 index 0000000000..8825b0f8e9 --- /dev/null +++ b/tests/address-lookup-table-program/program_with_different_programdata.json @@ -0,0 +1 @@ +[86,234,116,86,82,140,116,250,254,32,75,217,35,39,9,238,39,98,242,254,25,216,201,66,1,239,93,12,81,19,34,108,219,67,158,98,245,234,81,126,228,157,205,206,130,5,14,54,1,21,88,246,128,124,240,93,157,49,102,19,253,19,205,178] \ No newline at end of file diff --git a/tests/address-lookup-table-program/programs/address-lookup-table-program/Cargo.toml b/tests/address-lookup-table-program/programs/address-lookup-table-program/Cargo.toml new file mode 100644 index 0000000000..8cf288cff4 --- /dev/null +++ b/tests/address-lookup-table-program/programs/address-lookup-table-program/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "address-lookup-table-program" +version = "0.1.0" +description = "Created with Anchor" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "address_lookup_table_program" + +[features] +no-entrypoint = [] +no-idl = [] +cpi = ["no-entrypoint"] +default = [] +idl-build = ["anchor-lang/idl-build"] + +[dependencies] +anchor-lang = { path = "../../../../lang" } diff --git a/tests/address-lookup-table-program/programs/address-lookup-table-program/Xargo.toml b/tests/address-lookup-table-program/programs/address-lookup-table-program/Xargo.toml new file mode 100644 index 0000000000..475fb71ed1 --- /dev/null +++ b/tests/address-lookup-table-program/programs/address-lookup-table-program/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] diff --git a/tests/address-lookup-table-program/programs/address-lookup-table-program/src/lib.rs b/tests/address-lookup-table-program/programs/address-lookup-table-program/src/lib.rs new file mode 100644 index 0000000000..185abea65f --- /dev/null +++ b/tests/address-lookup-table-program/programs/address-lookup-table-program/src/lib.rs @@ -0,0 +1,30 @@ +use anchor_lang::prelude::*; + +use crate::program::AddressLookupTableTest; + +declare_id!("Cum9tTyj5HwcEiAmhgaS7Bbj4UczCwsucrCkxRECzM4e"); + +#[program] +pub mod address_lookup_table_test { + use super::*; + + pub fn test_read( + ctx: Context, + ) -> Result<()> { + Ok(()) + } +} + +#[error_code] +pub enum CustomError { + InvalidProgramDataAddress, + AccountNotProgram, +} + +#[derive(Accounts)] +pub struct Test<'info> { + #[account(mut)] + pub authority: Signer<'info>, + pub table: Account<'info, AddressLookupTable>, + pub lut_program: Program<'info, AddressLookupTableProgram>, +} diff --git a/tests/address-lookup-table-program/tests/address-lookup-table.ts b/tests/address-lookup-table-program/tests/address-lookup-table.ts new file mode 100644 index 0000000000..a948a30f7a --- /dev/null +++ b/tests/address-lookup-table-program/tests/address-lookup-table.ts @@ -0,0 +1,31 @@ +import * as anchor from "@coral-xyz/anchor"; +import { AnchorError, Program } from "@coral-xyz/anchor"; +import { PublicKey } from "@solana/web3.js"; +import { assert } from "chai"; +import { + AddressLookupTable, + AddressLookupTableProgram, +} from "../target/types/address_lookup_table_program"; + +describe("address_lookup_table_program", () => { + const provider = anchor.AnchorProvider.env(); + // Configure the client to use the local cluster. + anchor.setProvider(provider); + + const program = anchor.workspace + .AddressLookupTableProgram as Program; + const lutProgramAddress = new anchor.web3.PublicKey( + "AddressLookupTab1e1111111111111111111111111" + ); + + it("Test loads", async () => { + const tx = await program.rpc.test({ + accounts: { + authority: provider.wallet.publicKey, + lutProgram: lutProgramAddress, + table: lutProgramAddress, // Just a dummy value. fix + }, + signers: [settings], + }); + }); +}); diff --git a/tests/address-lookup-table-program/tsconfig.json b/tests/address-lookup-table-program/tsconfig.json new file mode 100644 index 0000000000..b3b6656d38 --- /dev/null +++ b/tests/address-lookup-table-program/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "types": ["mocha", "chai"], + "typeRoots": ["./node_modules/@types"], + "lib": ["es2015"], + "module": "commonjs", + "target": "es6", + "esModuleInterop": true, + "skipLibCheck": true + } +}