Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit a019dcc

Browse files
author
Joe C
authored
token 2022: add InitializeGroup instruction from SPL Token Group interface (#5601)
* token 2022: add `InitializeGroup` instruction from SPL Token Group interface * feedback: rework rent check and test tx dedupe * clip clip clippy
1 parent c817ecb commit a019dcc

File tree

10 files changed

+467
-4
lines changed

10 files changed

+467
-4
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/client/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ spl-associated-token-account = { version = "2.0", path = "../../associated-token
2424
spl-memo = { version = "4.0.0", path = "../../memo/program", features = ["no-entrypoint"] }
2525
spl-token = { version = "4.0", path="../program", features = [ "no-entrypoint" ] }
2626
spl-token-2022 = { version = "0.9", path="../program-2022" }
27+
spl-token-group-interface = { version = "0.1", path="../../token-group/interface" }
2728
spl-token-metadata-interface = { version = "0.2", path="../../token-metadata/interface" }
2829
spl-transfer-hook-interface = { version = "0.3", path="../transfer-hook/interface" }
2930
thiserror = "1.0"

token/client/src/token.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use {
4343
ConfidentialTransferFeeConfig,
4444
},
4545
cpi_guard, default_account_state, group_pointer, interest_bearing_mint, memo_transfer,
46-
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, ExtensionType,
47-
StateWithExtensionsOwned,
46+
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, Extension,
47+
ExtensionType, StateWithExtensionsOwned,
4848
},
4949
instruction, offchain,
5050
proof::ProofLocation,
@@ -61,6 +61,7 @@ use {
6161
},
6262
state::{Account, AccountState, Mint, Multisig},
6363
},
64+
spl_token_group_interface::state::TokenGroup,
6465
spl_token_metadata_interface::state::{Field, TokenMetadata},
6566
std::{
6667
fmt, io,
@@ -3649,4 +3650,76 @@ where
36493650
)
36503651
.await
36513652
}
3653+
3654+
/// Initialize token-group on a mint
3655+
pub async fn token_group_initialize<S: Signers>(
3656+
&self,
3657+
mint_authority: &Pubkey,
3658+
update_authority: &Pubkey,
3659+
max_size: u32,
3660+
signing_keypairs: &S,
3661+
) -> TokenResult<T::Output> {
3662+
self.process_ixs(
3663+
&[spl_token_group_interface::instruction::initialize_group(
3664+
&self.program_id,
3665+
&self.pubkey,
3666+
&self.pubkey,
3667+
mint_authority,
3668+
Some(*update_authority),
3669+
max_size,
3670+
)],
3671+
signing_keypairs,
3672+
)
3673+
.await
3674+
}
3675+
3676+
async fn get_additional_rent_for_fixed_len_extension<V: Extension + Pod>(
3677+
&self,
3678+
) -> TokenResult<u64> {
3679+
let account = self.get_account(self.pubkey).await?;
3680+
let account_lamports = account.lamports;
3681+
let mint_state = self.unpack_mint_info(account)?;
3682+
if mint_state.get_extension::<V>().is_ok() {
3683+
Ok(0)
3684+
} else {
3685+
let new_account_len = mint_state.try_get_new_account_len::<V>()?;
3686+
let new_rent_exempt_minimum = self
3687+
.client
3688+
.get_minimum_balance_for_rent_exemption(new_account_len)
3689+
.await
3690+
.map_err(TokenError::Client)?;
3691+
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
3692+
}
3693+
}
3694+
3695+
/// Initialize token-group on a mint
3696+
pub async fn token_group_initialize_with_rent_transfer<S: Signers>(
3697+
&self,
3698+
payer: &Pubkey,
3699+
mint_authority: &Pubkey,
3700+
update_authority: &Pubkey,
3701+
max_size: u32,
3702+
signing_keypairs: &S,
3703+
) -> TokenResult<T::Output> {
3704+
let additional_lamports = self
3705+
.get_additional_rent_for_fixed_len_extension::<TokenGroup>()
3706+
.await?;
3707+
let mut instructions = vec![];
3708+
if additional_lamports > 0 {
3709+
instructions.push(system_instruction::transfer(
3710+
payer,
3711+
&self.pubkey,
3712+
additional_lamports,
3713+
));
3714+
}
3715+
instructions.push(spl_token_group_interface::instruction::initialize_group(
3716+
&self.program_id,
3717+
&self.pubkey,
3718+
&self.pubkey,
3719+
mint_authority,
3720+
Some(*update_authority),
3721+
max_size,
3722+
));
3723+
self.process_ixs(&instructions, signing_keypairs).await
3724+
}
36523725
}

token/program-2022-test/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ spl-pod = { version = "0.1.0", path = "../../libraries/pod" }
2929
spl-token-2022 = { version = "0.9", path="../program-2022", features = ["no-entrypoint"] }
3030
spl-instruction-padding = { version = "0.1.0", path="../../instruction-padding/program", features = ["no-entrypoint"] }
3131
spl-token-client = { version = "0.8", path = "../client" }
32+
spl-token-group-interface = { version = "0.1", path = "../../token-group/interface" }
3233
spl-token-metadata-interface = { version = "0.2", path = "../../token-metadata/interface" }
3334
spl-transfer-hook-example = { version = "0.3", path="../transfer-hook/example", features = ["no-entrypoint"] }
3435
spl-transfer-hook-interface = { version = "0.3", path="../transfer-hook/interface" }

0 commit comments

Comments
 (0)