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

Commit cd22c3e

Browse files
committed
token 2022: add InitializeGroup instruction from SPL Token Group interface
1 parent 3ee9e63 commit cd22c3e

File tree

10 files changed

+484
-5
lines changed

10 files changed

+484
-5
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

+76-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use {
4141
ConfidentialTransferFeeConfig,
4242
},
4343
cpi_guard, default_account_state, group_pointer, interest_bearing_mint, memo_transfer,
44-
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, ExtensionType,
45-
StateWithExtensionsOwned,
44+
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, Extension,
45+
ExtensionType, Length, StateWithExtensionsOwned,
4646
},
4747
instruction, offchain,
4848
proof::ProofLocation,
@@ -59,6 +59,7 @@ use {
5959
},
6060
state::{Account, AccountState, Mint, Multisig},
6161
},
62+
spl_token_group_interface::state::TokenGroup,
6263
spl_token_metadata_interface::state::{Field, TokenMetadata},
6364
std::{
6465
fmt, io,
@@ -3848,4 +3849,77 @@ where
38483849
)
38493850
.await
38503851
}
3852+
3853+
/// Initialize token-group on a mint
3854+
pub async fn token_group_initialize<S: Signers>(
3855+
&self,
3856+
mint_authority: &Pubkey,
3857+
update_authority: &Pubkey,
3858+
max_size: u32,
3859+
signing_keypairs: &S,
3860+
) -> TokenResult<T::Output> {
3861+
self.process_ixs(
3862+
&[spl_token_group_interface::instruction::initialize_group(
3863+
&self.program_id,
3864+
&self.pubkey,
3865+
&self.pubkey,
3866+
mint_authority,
3867+
Some(*update_authority),
3868+
max_size,
3869+
)],
3870+
signing_keypairs,
3871+
)
3872+
.await
3873+
}
3874+
3875+
async fn get_additional_rent_for_fixed_len_extension<V: Extension + Pod>(
3876+
&self,
3877+
) -> TokenResult<u64> {
3878+
let account = self.get_account(self.pubkey).await?;
3879+
let account_lamports = account.lamports;
3880+
let new_account_len = account
3881+
.data
3882+
.len()
3883+
.saturating_add(size_of::<ExtensionType>())
3884+
.saturating_add(size_of::<Length>())
3885+
.saturating_add(size_of::<V>());
3886+
let new_rent_exempt_minimum = self
3887+
.client
3888+
.get_minimum_balance_for_rent_exemption(new_account_len)
3889+
.await
3890+
.map_err(TokenError::Client)?;
3891+
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
3892+
}
3893+
3894+
/// Initialize token-group on a mint
3895+
#[allow(clippy::too_many_arguments)]
3896+
pub async fn token_group_initialize_with_rent_transfer<S: Signers>(
3897+
&self,
3898+
payer: &Pubkey,
3899+
mint_authority: &Pubkey,
3900+
update_authority: &Pubkey,
3901+
max_size: u32,
3902+
signing_keypairs: &S,
3903+
) -> TokenResult<T::Output> {
3904+
let additional_lamports = self
3905+
.get_additional_rent_for_fixed_len_extension::<TokenGroup>()
3906+
.await?;
3907+
let mut instructions = vec![];
3908+
if additional_lamports > 0 {
3909+
instructions.push(system_instruction::transfer(
3910+
payer,
3911+
&self.pubkey,
3912+
additional_lamports,
3913+
));
3914+
}
3915+
instructions.push(spl_token_group_interface::instruction::initialize_group(
3916+
&self.program_id,
3917+
&self.pubkey,
3918+
&self.pubkey,
3919+
mint_authority,
3920+
Some(*update_authority),
3921+
max_size,
3922+
));
3923+
self.process_ixs(&instructions, signing_keypairs).await
3924+
}
38513925
}

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.7", 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)