Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,10 @@ mod tests {
.unwrap();

match &config.validation.price.model {
PriceModel::Fixed { amount, token } => {
PriceModel::Fixed { amount, token, strict } => {
assert_eq!(*amount, 1000000);
assert_eq!(token, "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU");
assert!(!strict);
}
_ => panic!("Expected Fixed price model"),
}
Expand Down
36 changes: 34 additions & 2 deletions crates/lib/src/fee/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ impl TotalFeeCalculation {
transfer_fee_amount: 0,
}
}

pub fn get_total_fee_lamports(&self) -> Result<u64, KoraError> {
self.base_fee
.checked_add(self.kora_signature_fee)
.and_then(|sum| sum.checked_add(self.fee_payer_outflow))
.and_then(|sum| sum.checked_add(self.payment_instruction_fee))
.and_then(|sum| sum.checked_add(self.transfer_fee_amount))
.ok_or_else(|| {
log::error!("Fee calculation overflow: base_fee={}, kora_signature_fee={}, fee_payer_outflow={}, payment_instruction_fee={}, transfer_fee_amount={}",
self.base_fee, self.kora_signature_fee, self.fee_payer_outflow, self.payment_instruction_fee, self.transfer_fee_amount);
KoraError::ValidationError("Fee calculation overflow".to_string())
})
}
}

pub struct FeeConfigUtil {}
Expand Down Expand Up @@ -274,14 +287,33 @@ impl FeeConfigUtil {

match &config.validation.price.model {
PriceModel::Free => Ok(TotalFeeCalculation::new_fixed(0)),
PriceModel::Fixed { .. } => {
PriceModel::Fixed { strict, .. } => {
let fixed_fee_lamports = config
.validation
.price
.get_required_lamports_with_fixed(rpc_client, price_source)
.await?;

Ok(TotalFeeCalculation::new_fixed(fixed_fee_lamports))
if *strict {
let fee_calculation = Self::estimate_transaction_fee(
rpc_client,
transaction,
fee_payer,
is_payment_required,
)
.await?;

Ok(TotalFeeCalculation::new(
fixed_fee_lamports,
fee_calculation.base_fee,
fee_calculation.kora_signature_fee,
fee_calculation.fee_payer_outflow,
fee_calculation.payment_instruction_fee,
fee_calculation.transfer_fee_amount,
))
} else {
Ok(TotalFeeCalculation::new_fixed(fixed_fee_lamports))
}
}
PriceModel::Margin { .. } => {
// Get the raw transaction
Expand Down
7 changes: 5 additions & 2 deletions crates/lib/src/fee/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use utoipa::ToSchema;
#[serde(tag = "type", rename_all = "lowercase")]
pub enum PriceModel {
Margin { margin: f64 },
Fixed { amount: u64, token: String },
Fixed { amount: u64, token: String, strict: bool },
Free,
}

Expand All @@ -31,7 +31,7 @@ impl PriceConfig {
rpc_client: &RpcClient,
price_source: PriceSource,
) -> Result<u64, KoraError> {
if let PriceModel::Fixed { amount, token } = &self.model {
if let PriceModel::Fixed { amount, token, .. } = &self.model {
return TokenUtil::calculate_token_value_in_lamports(
*amount,
&Pubkey::from_str(token).map_err(|e| {
Expand Down Expand Up @@ -117,6 +117,7 @@ mod tests {
model: PriceModel::Fixed {
amount: 1_000_000, // 1 USDC (1,000,000 base units with 6 decimals)
token: usdc_mint.to_string(),
strict: false,
},
};

Expand All @@ -143,6 +144,7 @@ mod tests {
model: PriceModel::Fixed {
amount: 500000000, // 0.5 tokens (500,000,000 base units with 9 decimals)
token: custom_token.to_string(),
strict: false,
},
};

Expand All @@ -169,6 +171,7 @@ mod tests {
model: PriceModel::Fixed {
amount: 1000, // 0.001 USDC (1,000 base units with 6 decimals)
token: usdc_mint.to_string(),
strict: false,
},
};

Expand Down
8 changes: 8 additions & 0 deletions crates/lib/src/tests/account_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ impl MintAccountMockBuilder {
ExtensionType::TransferFeeConfig => {
state.init_extension::<extension::transfer_fee::TransferFeeConfig>(true)?;
}
ExtensionType::PermanentDelegate => {
state.init_extension::<extension::permanent_delegate::PermanentDelegate>(
true,
)?;
}
ExtensionType::TransferHook => {
state.init_extension::<extension::transfer_hook::TransferHook>(true)?;
}
// Add other extension types as needed
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/tests/toml_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl ConfigBuilder {

pub fn with_fixed_price(mut self, amount: u64, token: &str) -> Self {
self.validation.price_config = Some(format!(
"[validation.price]\ntype = \"fixed\"\namount = {amount}\ntoken = \"{token}\"\n"
"[validation.price]\ntype = \"fixed\"\namount = {amount}\ntoken = \"{token}\"\nstrict = false\n"
));
self
}
Expand Down
3 changes: 3 additions & 0 deletions crates/lib/src/transaction/versioned_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ impl VersionedTransactionOps for VersionedTransactionResolved {
&payment_destination,
)
.await?;

// Validate strict pricing if enabled
TransactionValidator::validate_strict_pricing_with_fee(&fee_calculation)?;
}

// Get latest blockhash and update transaction
Expand Down
Loading
Loading