Skip to content

Commit

Permalink
token 2022: add InitializeGroup instruction from SPL Token Group in…
Browse files Browse the repository at this point in the history
…terface (#5601)

* token 2022: add `InitializeGroup` instruction from SPL Token Group interface

* feedback: rework rent check and test tx dedupe

* clip clip clippy
  • Loading branch information
Joe C authored Nov 9, 2023
1 parent c817ecb commit a019dcc
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions token/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ spl-associated-token-account = { version = "2.0", path = "../../associated-token
spl-memo = { version = "4.0.0", path = "../../memo/program", features = ["no-entrypoint"] }
spl-token = { version = "4.0", path="../program", features = [ "no-entrypoint" ] }
spl-token-2022 = { version = "0.9", path="../program-2022" }
spl-token-group-interface = { version = "0.1", path="../../token-group/interface" }
spl-token-metadata-interface = { version = "0.2", path="../../token-metadata/interface" }
spl-transfer-hook-interface = { version = "0.3", path="../transfer-hook/interface" }
thiserror = "1.0"
Expand Down
77 changes: 75 additions & 2 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ use {
ConfidentialTransferFeeConfig,
},
cpi_guard, default_account_state, group_pointer, interest_bearing_mint, memo_transfer,
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, ExtensionType,
StateWithExtensionsOwned,
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, Extension,
ExtensionType, StateWithExtensionsOwned,
},
instruction, offchain,
proof::ProofLocation,
Expand All @@ -61,6 +61,7 @@ use {
},
state::{Account, AccountState, Mint, Multisig},
},
spl_token_group_interface::state::TokenGroup,
spl_token_metadata_interface::state::{Field, TokenMetadata},
std::{
fmt, io,
Expand Down Expand Up @@ -3649,4 +3650,76 @@ where
)
.await
}

/// Initialize token-group on a mint
pub async fn token_group_initialize<S: Signers>(
&self,
mint_authority: &Pubkey,
update_authority: &Pubkey,
max_size: u32,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[spl_token_group_interface::instruction::initialize_group(
&self.program_id,
&self.pubkey,
&self.pubkey,
mint_authority,
Some(*update_authority),
max_size,
)],
signing_keypairs,
)
.await
}

async fn get_additional_rent_for_fixed_len_extension<V: Extension + Pod>(
&self,
) -> TokenResult<u64> {
let account = self.get_account(self.pubkey).await?;
let account_lamports = account.lamports;
let mint_state = self.unpack_mint_info(account)?;
if mint_state.get_extension::<V>().is_ok() {
Ok(0)
} else {
let new_account_len = mint_state.try_get_new_account_len::<V>()?;
let new_rent_exempt_minimum = self
.client
.get_minimum_balance_for_rent_exemption(new_account_len)
.await
.map_err(TokenError::Client)?;
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
}
}

/// Initialize token-group on a mint
pub async fn token_group_initialize_with_rent_transfer<S: Signers>(
&self,
payer: &Pubkey,
mint_authority: &Pubkey,
update_authority: &Pubkey,
max_size: u32,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let additional_lamports = self
.get_additional_rent_for_fixed_len_extension::<TokenGroup>()
.await?;
let mut instructions = vec![];
if additional_lamports > 0 {
instructions.push(system_instruction::transfer(
payer,
&self.pubkey,
additional_lamports,
));
}
instructions.push(spl_token_group_interface::instruction::initialize_group(
&self.program_id,
&self.pubkey,
&self.pubkey,
mint_authority,
Some(*update_authority),
max_size,
));
self.process_ixs(&instructions, signing_keypairs).await
}
}
1 change: 1 addition & 0 deletions token/program-2022-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ spl-pod = { version = "0.1.0", path = "../../libraries/pod" }
spl-token-2022 = { version = "0.9", path="../program-2022", features = ["no-entrypoint"] }
spl-instruction-padding = { version = "0.1.0", path="../../instruction-padding/program", features = ["no-entrypoint"] }
spl-token-client = { version = "0.8", path = "../client" }
spl-token-group-interface = { version = "0.1", path = "../../token-group/interface" }
spl-token-metadata-interface = { version = "0.2", path = "../../token-metadata/interface" }
spl-transfer-hook-example = { version = "0.3", path="../transfer-hook/example", features = ["no-entrypoint"] }
spl-transfer-hook-interface = { version = "0.3", path="../transfer-hook/interface" }
Expand Down
Loading

0 comments on commit a019dcc

Please sign in to comment.