Skip to content

Commit ddded3b

Browse files
committedSep 30, 2024··
solana: fix base fee token handling
1 parent 75ebbfb commit ddded3b

File tree

1 file changed

+33
-18
lines changed
  • solana/programs/matching-engine/src/processor/auction/settle

1 file changed

+33
-18
lines changed
 

‎solana/programs/matching-engine/src/processor/auction/settle/complete.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,35 @@ fn handle_settle_auction_complete(
115115
amount: u64,
116116
}
117117

118-
let (executor_result, best_offer_result) = match execute_penalty {
118+
let (base_fee_result, best_offer_result) = match execute_penalty {
119+
// When there is no penalty, we will give everything to the best offer token account.
119120
None => {
120121
// If the token account happens to not exist anymore, we will revert.
121-
let best_offer =
122+
let best_offer_token_data =
122123
utils::checked_deserialize_token_account(best_offer_token, &common::USDC_MINT)
123124
.ok_or_else(|| MatchingEngineError::BestOfferTokenRequired)?;
124125

125126
(
126-
None, // executor_result
127+
None, // base_fee_result
127128
TokenAccountResult {
128-
balance_before: best_offer.amount,
129+
balance_before: best_offer_token_data.amount,
129130
amount: repayment,
130131
}
131132
.into(),
132133
)
133134
}
135+
// Otherwise, determine how the repayment should be divvied up.
134136
_ => {
135-
let base_fee_token_data =
136-
utils::checked_deserialize_token_account(base_fee_token, &common::USDC_MINT)
137-
.ok_or_else(|| MatchingEngineError::BaseFeeTokenRequired)?;
138-
139-
// If the token account happens to not exist anymore, we will give everything to the
140-
// base fee token account.
141-
match utils::checked_deserialize_token_account(best_offer_token, &common::USDC_MINT) {
142-
Some(best_offer) => {
137+
match (
138+
utils::checked_deserialize_token_account(base_fee_token, &common::USDC_MINT),
139+
utils::checked_deserialize_token_account(best_offer_token, &common::USDC_MINT),
140+
) {
141+
(Some(base_fee_token_data), Some(best_offer_token_data)) => {
143142
if base_fee_token.key() == best_offer_token.key() {
144143
(
145-
None, // executor_result
144+
None, // base_fee_result
146145
TokenAccountResult {
147-
balance_before: best_offer.amount,
146+
balance_before: best_offer_token_data.amount,
148147
amount: repayment,
149148
}
150149
.into(),
@@ -157,27 +156,43 @@ fn handle_settle_auction_complete(
157156
}
158157
.into(),
159158
TokenAccountResult {
160-
balance_before: best_offer.amount,
159+
balance_before: best_offer_token_data.amount,
161160
amount: repayment.saturating_sub(base_fee),
162161
}
163162
.into(),
164163
)
165164
}
166165
}
167-
None => (
166+
// If the best offer token account does not exist, we will give everything to the
167+
// base fee token account.
168+
(Some(base_fee_token_data), None) => (
168169
TokenAccountResult {
169170
balance_before: base_fee_token_data.amount,
170171
amount: repayment,
171172
}
172173
.into(),
173174
None, // best_offer_result
174175
),
176+
// If the base fee token account does not exist, we will give everything to the best
177+
// offer token account.
178+
(None, Some(best_offer_data)) => {
179+
(
180+
None, // base_fee_result
181+
TokenAccountResult {
182+
balance_before: best_offer_data.amount,
183+
amount: repayment,
184+
}
185+
.into(),
186+
)
187+
}
188+
// Otherwise revert.
189+
_ => return err!(MatchingEngineError::BestOfferTokenRequired),
175190
}
176191
}
177192
};
178193

179194
// Transfer base fee token his bounty if there are any.
180-
let settled_executor_result = match executor_result {
195+
let settled_base_fee_result = match base_fee_result {
181196
Some(TokenAccountResult {
182197
balance_before,
183198
amount,
@@ -235,7 +250,7 @@ fn handle_settle_auction_complete(
235250
emit!(crate::events::AuctionSettled {
236251
auction: ctx.accounts.auction.key(),
237252
best_offer_token: settled_best_offer_result,
238-
base_fee_token: settled_executor_result,
253+
base_fee_token: settled_base_fee_result,
239254
with_execute: Default::default(),
240255
});
241256

0 commit comments

Comments
 (0)