Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add anchor bindings anchor-lang #3440

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lang/src/address_lookup_table_program.rs
mgild marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::prelude::*;
use crate::solana_program::address_lookup_table;
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;
impl anchor_lang::Id for AddressLookupTable {
fn id() -> Pubkey {
address_lookup_table::program::ID
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AddressLookupTableAccount<'info>(SolanaAddressLookupTable<'info>);

impl AccountSerialize for AddressLookupTableAccount<'_> {}

impl<'info> AccountDeserialize for AddressLookupTableAccount<'info> {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
// 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<'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] = &[];
}
}
3 changes: 3 additions & 0 deletions lang/src/lib.rs
mgild marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ pub mod event;
pub mod idl;
pub mod system_program;
mod vec;
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};
Expand Down Expand Up @@ -402,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,
Expand Down
9 changes: 9 additions & 0 deletions tests/address-lookup-table-program/Anchor.toml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions tests/address-lookup-table-program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 12 additions & 0 deletions tests/address-lookup-table-program/migrations/deploy.ts
Original file line number Diff line number Diff line change
@@ -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.
};
16 changes: 16 additions & 0 deletions tests/address-lookup-table-program/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
Original file line number Diff line number Diff line change
@@ -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<Test>,
) -> 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>,
}
31 changes: 31 additions & 0 deletions tests/address-lookup-table-program/tests/address-lookup-table.ts
Original file line number Diff line number Diff line change
@@ -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<AddressLookupTableProgram>;
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],
});
});
});
11 changes: 11 additions & 0 deletions tests/address-lookup-table-program/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true,
"skipLibCheck": true
}
}