Skip to content

Commit 22135e5

Browse files
committed
Add ConfidentialTransferInstruction Deposit/Transfer/Withdraw tests
1 parent 3450f17 commit 22135e5

File tree

4 files changed

+559
-63
lines changed

4 files changed

+559
-63
lines changed

token/client/src/token.rs

Lines changed: 153 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub type TokenResult<T> = Result<T, TokenError>;
140140

141141
pub struct Token<T, S> {
142142
client: Arc<dyn ProgramClient<T>>,
143-
pubkey: Pubkey,
143+
pubkey: Pubkey, /*token mint*/
144144
payer: S,
145145
program_id: Pubkey,
146146
memo: Arc<RwLock<Option<String>>>,
@@ -991,19 +991,19 @@ where
991991
.await
992992
}
993993

994-
/// Prepare a token account for closing
994+
/// Prepare a token account with the confidential transfer extension for closing
995995
pub async fn confidential_transfer_empty_account<S2: Signer>(
996996
&self,
997997
token_account: &Pubkey,
998998
authority: &S2,
999999
elgamal_keypair: &ElGamalKeypair,
10001000
) -> TokenResult<T::Output> {
1001-
let state = self.get_account_info(&token_account).await.unwrap();
1001+
let state = self.get_account_info(token_account).await.unwrap();
10021002
let extension =
10031003
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
10041004

10051005
let proof_data = confidential_transfer::instruction::CloseAccountData::new(
1006-
&elgamal_keypair,
1006+
elgamal_keypair,
10071007
&extension.available_balance.try_into().unwrap(),
10081008
)
10091009
.map_err(TokenError::Proof)?;
@@ -1021,6 +1021,155 @@ where
10211021
.await
10221022
}
10231023

1024+
/// Deposit SPL Tokens into the pending balance of a confidential token account
1025+
pub async fn confidential_transfer_deposit<S2: Signer>(
1026+
&self,
1027+
source_token_account: &Pubkey,
1028+
destination_token_account: &Pubkey,
1029+
source_token_authority: &S2,
1030+
amount: u64,
1031+
decimals: u8,
1032+
) -> TokenResult<T::Output> {
1033+
self.process_ixs(
1034+
&[confidential_transfer::instruction::deposit(
1035+
&self.program_id,
1036+
source_token_account,
1037+
&self.pubkey,
1038+
destination_token_account,
1039+
amount,
1040+
decimals,
1041+
&source_token_authority.pubkey(),
1042+
&[],
1043+
)?],
1044+
&[source_token_authority],
1045+
)
1046+
.await
1047+
}
1048+
1049+
/// Withdraw SPL Tokens from the available balance of a confidential token account
1050+
#[allow(clippy::too_many_arguments)]
1051+
pub async fn confidential_transfer_withdraw<S2: Signer>(
1052+
&self,
1053+
source_token_account: &Pubkey,
1054+
destination_token_account: &Pubkey,
1055+
source_token_authority: &S2,
1056+
amount: u64,
1057+
decimals: u8,
1058+
source_available_balance: u64,
1059+
source_elgamal_keypair: &ElGamalKeypair,
1060+
new_source_decryptable_available_balance: AeCiphertext,
1061+
) -> TokenResult<T::Output> {
1062+
let state = self.get_account_info(source_token_account).await.unwrap();
1063+
let extension =
1064+
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
1065+
1066+
let proof_data = confidential_transfer::instruction::WithdrawData::new(
1067+
amount,
1068+
source_elgamal_keypair,
1069+
source_available_balance,
1070+
&extension.available_balance.try_into().unwrap(),
1071+
)
1072+
.map_err(TokenError::Proof)?;
1073+
1074+
self.process_ixs(
1075+
&confidential_transfer::instruction::withdraw(
1076+
&self.program_id,
1077+
source_token_account,
1078+
destination_token_account,
1079+
&self.pubkey,
1080+
amount,
1081+
decimals,
1082+
new_source_decryptable_available_balance,
1083+
&source_token_authority.pubkey(),
1084+
&[],
1085+
&proof_data,
1086+
)?,
1087+
&[source_token_authority],
1088+
)
1089+
.await
1090+
}
1091+
1092+
/// Transfer tokens confidentially
1093+
#[allow(clippy::too_many_arguments)]
1094+
pub async fn confidential_transfer_transfer<S2: Signer>(
1095+
&self,
1096+
source_token_account: &Pubkey,
1097+
destination_token_account: &Pubkey,
1098+
source_token_authority: &S2,
1099+
amount: u64,
1100+
source_available_balance: u64,
1101+
source_elgamal_keypair: &ElGamalKeypair,
1102+
new_source_decryptable_available_balance: AeCiphertext,
1103+
) -> TokenResult<T::Output> {
1104+
let source_state = self.get_account_info(source_token_account).await.unwrap();
1105+
let source_extension =
1106+
source_state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
1107+
1108+
let destination_state = self
1109+
.get_account_info(destination_token_account)
1110+
.await
1111+
.unwrap();
1112+
let destination_extension = destination_state
1113+
.get_extension::<confidential_transfer::ConfidentialTransferAccount>(
1114+
)?;
1115+
1116+
let mint_state = self.get_mint_info().await.unwrap();
1117+
let ct_mint = mint_state
1118+
.get_extension::<confidential_transfer::ConfidentialTransferMint>()
1119+
.unwrap();
1120+
1121+
let proof_data = confidential_transfer::instruction::TransferData::new(
1122+
amount,
1123+
(
1124+
source_available_balance,
1125+
&source_extension.available_balance.try_into().unwrap(),
1126+
),
1127+
source_elgamal_keypair,
1128+
(
1129+
&destination_extension.pubkey_elgamal.try_into().unwrap(),
1130+
&ct_mint.pubkey_auditor.try_into().unwrap(),
1131+
),
1132+
)
1133+
.map_err(TokenError::Proof)?;
1134+
1135+
self.process_ixs(
1136+
&confidential_transfer::instruction::transfer(
1137+
&self.program_id,
1138+
source_token_account,
1139+
destination_token_account,
1140+
&self.pubkey,
1141+
new_source_decryptable_available_balance,
1142+
&source_token_authority.pubkey(),
1143+
&[],
1144+
&proof_data,
1145+
)?,
1146+
&[source_token_authority],
1147+
)
1148+
.await
1149+
}
1150+
1151+
/// Applies the confidential transfer pending balance to the available balance
1152+
pub async fn confidential_transfer_apply_pending_balance<S2: Signer>(
1153+
&self,
1154+
token_account: &Pubkey,
1155+
authority: &S2,
1156+
expected_pending_balance_credit_counter: u64,
1157+
new_decryptable_available_balance: AeCiphertext,
1158+
) -> TokenResult<T::Output> {
1159+
self.process_ixs(
1160+
&[confidential_transfer::instruction::apply_pending_balance(
1161+
&self.program_id,
1162+
token_account,
1163+
expected_pending_balance_credit_counter,
1164+
new_decryptable_available_balance,
1165+
&authority.pubkey(),
1166+
&[],
1167+
)?],
1168+
&[authority],
1169+
)
1170+
.await
1171+
}
1172+
10241173
/// Enable confidential transfer `Deposit` and `Transfer` instructions for a token account
10251174
pub async fn confidential_transfer_enable_balance_credits<S2: Signer>(
10261175
&self,

0 commit comments

Comments
 (0)