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

Commit 2ad468f

Browse files
Fix confidential self transfer with fees (#3193)
* token-2022: fix confidential self-transfers with fee * token-2022: update tests for confidential self-transfer with fees
1 parent 3af176d commit 2ad468f

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

token/program-2022-test/tests/confidential_transfer.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use {
1010
signer::keypair::Keypair, transaction::TransactionError, transport::TransportError,
1111
},
1212
spl_token_2022::{
13-
error::TokenError,
1413
extension::{
1514
confidential_transfer::{
1615
ConfidentialTransferAccount, ConfidentialTransferMint, EncryptedWithheldAmount,
@@ -869,9 +868,7 @@ async fn ct_transfer_with_fee() {
869868
)
870869
.await;
871870

872-
// Fee is 2.5%, so what is left is 97 in Alice account
873-
//
874-
// TODO: make self transfers not not take fees
871+
// Self-transfers does not incur a fee
875872
token
876873
.confidential_transfer_transfer_with_fee(
877874
&alice_meta.token_account,
@@ -890,7 +887,7 @@ async fn ct_transfer_with_fee() {
890887
.check_balances(
891888
&token,
892889
ConfidentialTokenAccountBalances {
893-
pending_balance: 97,
890+
pending_balance: 100,
894891
available_balance: 0,
895892
decryptable_available_balance: 0,
896893
},
@@ -902,7 +899,7 @@ async fn ct_transfer_with_fee() {
902899
&alice_meta.token_account,
903900
&alice,
904901
2,
905-
alice_meta.ae_key.encrypt(97_u64),
902+
alice_meta.ae_key.encrypt(100_u64),
906903
)
907904
.await
908905
.unwrap();
@@ -912,8 +909,8 @@ async fn ct_transfer_with_fee() {
912909
&token,
913910
ConfidentialTokenAccountBalances {
914911
pending_balance: 0,
915-
available_balance: 97,
916-
decryptable_available_balance: 97,
912+
available_balance: 100,
913+
decryptable_available_balance: 100,
917914
},
918915
)
919916
.await;
@@ -923,8 +920,8 @@ async fn ct_transfer_with_fee() {
923920
&alice_meta.token_account,
924921
&bob_meta.token_account,
925922
&alice,
926-
97, // amount
927-
97, // available balance
923+
100, // amount
924+
100, // available balance
928925
&alice_meta.elgamal_keypair,
929926
alice_meta.ae_key.encrypt(0_u64),
930927
&epoch_info,
@@ -958,23 +955,14 @@ async fn ct_transfer_with_fee() {
958955
);
959956

960957
// Alice account cannot be closed since there are withheld fees from self-transfer
961-
let error = token
958+
token
962959
.confidential_transfer_empty_account(
963960
&alice_meta.token_account,
964961
&alice,
965962
&alice_meta.elgamal_keypair,
966963
)
967964
.await
968-
.unwrap_err();
969-
assert_eq!(
970-
error,
971-
TokenClientError::Client(Box::new(TransportError::TransactionError(
972-
TransactionError::InstructionError(
973-
1,
974-
InstructionError::Custom(TokenError::ConfidentialTransferAccountHasBalance as u32)
975-
)
976-
)))
977-
);
965+
.unwrap();
978966

979967
let err = token
980968
.confidential_transfer_empty_account(

token/program-2022/src/extension/confidential_transfer/processor.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,20 @@ fn process_transfer(
546546
&ciphertext_hi,
547547
new_source_decryptable_available_balance,
548548
)?;
549+
550+
let fee_ciphertext = if token_account_info.key == destination_token_account_info.key {
551+
None
552+
} else {
553+
Some(proof_data.fee_ciphertext)
554+
};
555+
549556
process_destination_for_transfer(
550557
destination_token_account_info,
551558
mint_info,
552559
&proof_data.transfer_with_fee_pubkeys.destination_pubkey,
553560
&ciphertext_lo,
554561
&ciphertext_hi,
555-
Some(proof_data.fee_ciphertext),
562+
fee_ciphertext,
556563
)?;
557564
} else {
558565
// mint is not extended for fees

0 commit comments

Comments
 (0)