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

Commit c51ccf0

Browse files
[confidential-extension] Refactor and add comments for readability (#3921)
* pass over `InitializeAccount` to `Withdraw` instructions * add `valid_as_source` and `valid_as_destination` functions * pass over `Transfer` instruction * pass over `ApplyPendingBalance` to `HarvestWithheldTokensToMint` instructions * refactor pending balance credit counter increment
1 parent 72a41d4 commit c51ccf0

File tree

3 files changed

+257
-274
lines changed

3 files changed

+257
-274
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,10 @@ async fn ct_transfer() {
992992
assert_eq!(
993993
err,
994994
TokenClientError::Client(Box::new(TransportError::TransactionError(
995-
TransactionError::InstructionError(0, InstructionError::InvalidAccountData)
995+
TransactionError::InstructionError(
996+
0,
997+
InstructionError::Custom(TokenError::ConfidentialTransferAccountHasBalance as u32)
998+
)
996999
)))
9971000
);
9981001

@@ -1202,7 +1205,10 @@ async fn ct_transfer_with_fee() {
12021205
assert_eq!(
12031206
err,
12041207
TokenClientError::Client(Box::new(TransportError::TransactionError(
1205-
TransactionError::InstructionError(0, InstructionError::InvalidAccountData)
1208+
TransactionError::InstructionError(
1209+
0,
1210+
InstructionError::Custom(TokenError::ConfidentialTransferAccountHasBalance as u32)
1211+
)
12061212
)))
12071213
);
12081214

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Extension for ConfidentialTransferAccount {
141141
}
142142

143143
impl ConfidentialTransferAccount {
144-
/// Check if a `ConfidentialTransferAccount` has been approved for use
144+
/// Check if a `ConfidentialTransferAccount` has been approved for use.
145145
pub fn approved(&self) -> ProgramResult {
146146
if bool::from(&self.approved) {
147147
Ok(())
@@ -150,7 +150,7 @@ impl ConfidentialTransferAccount {
150150
}
151151
}
152152

153-
/// Check if a `ConfidentialTransferAccount` is in a closable state
153+
/// Check if a `ConfidentialTransferAccount` is in a closable state.
154154
pub fn closable(&self) -> ProgramResult {
155155
if self.pending_balance_lo == EncryptedBalance::zeroed()
156156
&& self.pending_balance_hi == EncryptedBalance::zeroed()
@@ -164,12 +164,52 @@ impl ConfidentialTransferAccount {
164164
}
165165

166166
/// Check if a base account of a `ConfidentialTransferAccount` accepts non-confidential
167-
/// transfers
167+
/// transfers.
168168
pub fn non_confidential_transfer_allowed(&self) -> ProgramResult {
169169
if bool::from(&self.allow_non_confidential_credits) {
170170
Ok(())
171171
} else {
172172
Err(TokenError::NonConfidentialTransfersDisabled.into())
173173
}
174174
}
175+
176+
/// Checks if a `ConfidentialTransferAccount` is configured to send funds.
177+
pub fn valid_as_source(&self) -> ProgramResult {
178+
self.approved()
179+
}
180+
181+
/// Checks if a confidential extension is configured to receive funds.
182+
///
183+
/// A destination account can receive funds if the following conditions are satisfied:
184+
/// 1. The account is approved by the confidential transfer mint authority
185+
/// 2. The account is not disabled by the account owner
186+
/// 3. The number of credits into the account has reached the maximum credit counter
187+
pub fn valid_as_destination(&self) -> ProgramResult {
188+
self.approved()?;
189+
190+
if !bool::from(self.allow_confidential_credits) {
191+
return Err(TokenError::ConfidentialTransferDepositsAndTransfersDisabled.into());
192+
}
193+
194+
let new_destination_pending_balance_credit_counter =
195+
u64::from(self.pending_balance_credit_counter)
196+
.checked_add(1)
197+
.ok_or(TokenError::Overflow)?;
198+
if new_destination_pending_balance_credit_counter
199+
> u64::from(self.maximum_pending_balance_credit_counter)
200+
{
201+
return Err(TokenError::MaximumPendingBalanceCreditCounterExceeded.into());
202+
}
203+
204+
Ok(())
205+
}
206+
207+
/// Increments a confidential extension pending balance credit counter.
208+
pub fn increment_pending_balance_credit_counter(&mut self) -> ProgramResult {
209+
self.pending_balance_credit_counter = (u64::from(self.pending_balance_credit_counter)
210+
.checked_add(1)
211+
.ok_or(TokenError::Overflow)?)
212+
.into();
213+
Ok(())
214+
}
175215
}

0 commit comments

Comments
 (0)