Make module for address_lookup_table functions

This commit is contained in:
Christian Kamm 2022-03-01 13:56:24 +01:00
parent a7975d390d
commit eb38fb4f18
6 changed files with 36 additions and 40 deletions

View File

@ -0,0 +1,17 @@
mod solana_address_lookup_table_instruction;
pub use solana_address_lookup_table_instruction::*;
use solana_program::pubkey::Pubkey;
use std::str::FromStr;
pub fn id() -> Pubkey {
Pubkey::from_str(&"AddressLookupTab1e1111111111111111111111111").unwrap()
}
/// The maximum number of addresses that a lookup table can hold
pub const LOOKUP_TABLE_MAX_ADDRESSES: usize = 256;
/// The serialized size of lookup table metadata
pub const LOOKUP_TABLE_META_SIZE: usize = 56;
pub const LOOKUP_TABLE_MAX_ACCOUNT_SIZE: usize =
LOOKUP_TABLE_META_SIZE + LOOKUP_TABLE_MAX_ADDRESSES * solana_program::pubkey::PUBKEY_BYTES;

View File

@ -3,6 +3,7 @@
// Unfortunately we can't import the module since it depends on solana_sdk!
//
use {
super::id,
serde::{Deserialize, Serialize},
solana_program::{
clock::Slot,
@ -10,24 +11,8 @@ use {
pubkey::Pubkey,
system_program,
},
std::str::FromStr,
};
// NOTE: Manual additions start
pub fn id() -> Pubkey {
Pubkey::from_str(&"AddressLookupTab1e1111111111111111111111111").unwrap()
}
/// The maximum number of addresses that a lookup table can hold
pub const LOOKUP_TABLE_MAX_ADDRESSES: usize = 256;
/// The serialized size of lookup table metadata
pub const LOOKUP_TABLE_META_SIZE: usize = 56;
pub const LOOKUP_TABLE_MAX_ACCOUNT_SIZE: usize =
LOOKUP_TABLE_META_SIZE + LOOKUP_TABLE_MAX_ADDRESSES * solana_program::pubkey::PUBKEY_BYTES;
// NOTE: Manual additions end
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum ProgramInstruction {
/// Create an address lookup table

View File

@ -1,7 +1,7 @@
use anchor_lang::prelude::*;
use crate::address_lookup_table;
use crate::error::*;
use crate::solana_address_lookup_table_instruction;
use crate::state::*;
#[derive(Accounts)]
@ -56,7 +56,7 @@ pub fn create_account(
// (later, in deposit etc) will need a payer!
let rent = Rent::get()?;
let required_lamports = rent
.minimum_balance(solana_address_lookup_table_instruction::LOOKUP_TABLE_MAX_ACCOUNT_SIZE)
.minimum_balance(address_lookup_table::LOOKUP_TABLE_MAX_ACCOUNT_SIZE)
.max(1)
.saturating_sub(ctx.accounts.address_lookup_table.lamports());
if required_lamports > 0 {
@ -76,12 +76,11 @@ pub fn create_account(
// Now create the account.
// TODO: We could save some CU here by not using create_lookup_table():
// it - unnecessarily - derives the lookup table address again.
let (instruction, _expected_adress_map_address) =
solana_address_lookup_table_instruction::create_lookup_table(
ctx.accounts.account.key(),
ctx.accounts.payer.key(),
address_lookup_table_recent_slot,
);
let (instruction, _expected_adress_map_address) = address_lookup_table::create_lookup_table(
ctx.accounts.account.key(),
ctx.accounts.payer.key(),
address_lookup_table_recent_slot,
);
let account_infos = [
ctx.accounts.address_lookup_table.to_account_info(),
ctx.accounts.account.to_account_info(),

View File

@ -4,7 +4,7 @@ use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use solana_program::pubkey::PUBKEY_BYTES;
use crate::solana_address_lookup_table_instruction;
use crate::address_lookup_table;
use crate::state::*;
#[derive(Accounts)]
@ -55,12 +55,10 @@ impl<'info> Deposit<'info> {
fn address_lookup_table_contains(table: &AccountInfo, pubkey: &Pubkey) -> Result<bool> {
let table_data = table.try_borrow_data()?;
let pk_ref = pubkey.as_ref();
Ok(
table_data[solana_address_lookup_table_instruction::LOOKUP_TABLE_META_SIZE..]
.chunks(PUBKEY_BYTES)
.find(|&d| d == pk_ref)
.is_some(),
)
Ok(table_data[address_lookup_table::LOOKUP_TABLE_META_SIZE..]
.chunks(PUBKEY_BYTES)
.find(|&d| d == pk_ref)
.is_some())
}
// TODO: It may make sense to have the token_index passed in from the outside.
@ -97,7 +95,7 @@ pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
// NOTE: Unfortunately extend() _requires_ a payer, even though we've already
// fully funded the address lookup table. No further transfer will be necessary.
// We'll pass the account as payer.
let mut instruction = solana_address_lookup_table_instruction::extend_lookup_table(
let mut instruction = address_lookup_table::extend_lookup_table(
ctx.accounts.address_lookup_table.key(),
ctx.accounts.account.key(),
ctx.accounts.account.key(),

View File

@ -6,9 +6,9 @@ use anchor_lang::prelude::*;
use instructions::*;
pub mod address_lookup_table;
mod error;
mod instructions;
pub mod solana_address_lookup_table_instruction;
pub mod state;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

View File

@ -204,7 +204,7 @@ impl<'keypair> ClientInstruction for DepositInstruction<'keypair> {
token_account: self.token_account,
token_authority: self.token_authority.pubkey(),
token_program: Token::id(),
address_lookup_table_program: mango_v4::solana_address_lookup_table_instruction::id(),
address_lookup_table_program: mango_v4::address_lookup_table::id(),
};
let instruction = make_instruction(program_id, &accounts, instruction);
@ -438,11 +438,8 @@ impl<'keypair> ClientInstruction for CreateAccountInstruction<'keypair> {
)
.0;
let address_lookup_table =
mango_v4::solana_address_lookup_table_instruction::derive_lookup_table_address(
&account,
self.recent_slot,
)
.0;
mango_v4::address_lookup_table::derive_lookup_table_address(&account, self.recent_slot)
.0;
let accounts = mango_v4::accounts::CreateAccount {
group: self.group,
@ -452,7 +449,7 @@ impl<'keypair> ClientInstruction for CreateAccountInstruction<'keypair> {
payer: self.payer.pubkey(),
system_program: System::id(),
rent: sysvar::rent::Rent::id(),
address_lookup_table_program: mango_v4::solana_address_lookup_table_instruction::id(),
address_lookup_table_program: mango_v4::address_lookup_table::id(),
};
let instruction = make_instruction(program_id, &accounts, instruction);