Skip to content

Commit

Permalink
remove amount validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiloc committed Feb 5, 2025
1 parent 9e1c3a6 commit 85a972e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 55 deletions.
3 changes: 1 addition & 2 deletions emily/handler/src/api/handlers/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ pub async fn create_deposit(
Err(e) => return Err(e),
}

let limits = accessors::get_limits(&context).await?;
let deposit_info = body.validate(&limits, context.settings.is_mainnet)?;
let deposit_info = body.validate(context.settings.is_mainnet)?;

// Make table entry.
let deposit_entry: DepositEntry = DepositEntry {
Expand Down
83 changes: 30 additions & 53 deletions emily/handler/src/api/models/deposit/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use std::str::FromStr;

use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode;
use bitcoin::{Amount, OutPoint, ScriptBuf, Txid};
use bitcoin::{OutPoint, ScriptBuf, Txid};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use sbtc::deposits::{CreateDepositRequest, DepositInfo};

use crate::api::models::common::{Fulfillment, Status};
use crate::api::models::limits::Limits;
use crate::common::error::Error;

/// Query structure for the GetDepositsQuery struct.
Expand Down Expand Up @@ -84,8 +83,8 @@ where
impl CreateDepositRequestBody {
/// Validates that the deposit request is valid.
/// This includes validating the request fields, if their content matches the transaction
/// and if the amount is within the limits.
pub fn validate(&self, limits: &Limits, is_mainnet: bool) -> Result<DepositInfo, Error> {
/// and if the amount is higher than the dust limit.
pub fn validate(&self, is_mainnet: bool) -> Result<DepositInfo, Error> {
let deposit_req = CreateDepositRequest {
outpoint: OutPoint {
txid: parse_with_custom_error(
Expand Down Expand Up @@ -124,20 +123,10 @@ impl CreateDepositRequestBody {
.value
.to_sat();

// Even if no limits are set, the deposit amount should be higher than the dust limit.
let min = limits.per_deposit_minimum.unwrap_or(DEPOSIT_DUST_LIMIT);
if amount < min {
if amount < DEPOSIT_DUST_LIMIT {
return Err(Error::HttpRequest(
StatusCode::BAD_REQUEST,
format!("deposit amount below minimum ({})", min),
));
}

let cap = limits.per_deposit_cap.unwrap_or(Amount::MAX_MONEY.to_sat());
if amount > cap {
return Err(Error::HttpRequest(
StatusCode::BAD_REQUEST,
format!("deposit amount exceeds cap ({})", cap),
format!("deposit amount below dust limit ({})", DEPOSIT_DUST_LIMIT),
));
}

Expand Down Expand Up @@ -185,6 +174,8 @@ pub struct UpdateDepositsRequestBody {
#[cfg(test)]
mod tests {
use super::*;
use bitcoin::consensus::encode::serialize_hex;
use sbtc::testing::deposits::tx_setup;
use test_case::test_case;

const CREATE_DEPOSIT_VALID: &str =
Expand Down Expand Up @@ -214,23 +205,14 @@ mod tests {
const CREATE_DEPOSIT_MISMATCH_DEPOSIT_SCRIPT: &str =
include_str!("../../../../tests/fixtures/create-deposit-mismatch-deposit-script.json");

pub fn create_test_limits(min: Option<u64>, max: Option<u64>) -> Limits {
Limits {
per_deposit_minimum: min,
per_deposit_cap: max,
..Default::default()
}
}

pub fn parse_request(json: &str) -> CreateDepositRequestBody {
serde_json::from_str(json).expect("failed to parse request")
}

#[tokio::test]
async fn test_deposit_validate_happy_path() {
let deposit_request = parse_request(CREATE_DEPOSIT_VALID);
let limits = create_test_limits(None, None);
assert!(deposit_request.validate(&limits, true).is_ok());
assert!(deposit_request.validate(true).is_ok());
}

#[test_case(CREATE_DEPOSIT_INVALID_TXID, "invalid bitcoin txid"; "invalid_txid")]
Expand All @@ -250,44 +232,39 @@ mod tests {
#[tokio::test]
async fn test_deposit_validate_errors(input: &str, expected_error: &str) {
let deposit_request = parse_request(input);
let limits = create_test_limits(Some(DEPOSIT_DUST_LIMIT), None);

let result = deposit_request.validate(&limits, true);
let result = deposit_request.validate(true);
assert_eq!(
result.unwrap_err().to_string(),
format!("HTTP request failed with status code 400 Bad Request: {expected_error}")
);
}

// CREATE_DEPOSIT_VALID has a deposit amount of 1_000_000 satoshis.
#[test_case(CREATE_DEPOSIT_VALID, None, None; "no_limits")]
#[test_case(CREATE_DEPOSIT_VALID, Some(DEPOSIT_DUST_LIMIT), None; "min_limit")]
#[test_case(CREATE_DEPOSIT_VALID, None, Some(1_000_000_000); "max_limit")]
#[test_case(CREATE_DEPOSIT_VALID, Some(DEPOSIT_DUST_LIMIT), Some(1_000_000_000); "min_max_limit")]
#[tokio::test]
async fn test_deposit_validate_limits(input: &str, min: Option<u64>, max: Option<u64>) {
let deposit_request = parse_request(input);
let limits = create_test_limits(min, max);
assert!(deposit_request.validate(&limits, true).is_ok());
}

#[test_case(CREATE_DEPOSIT_VALID, Some(1_000_000 + 1), None, "deposit amount below minimum (1000001)"; "below_min_limit")]
#[test_case(CREATE_DEPOSIT_VALID, None, Some(999_999), "deposit amount exceeds cap (999999)"; "above_max_limit")]
#[tokio::test]
async fn test_deposit_validate_limits_errors(
input: &str,
min: Option<u64>,
max: Option<u64>,
expected_error: &str,
) {
let deposit_request = parse_request(input);
let limits = create_test_limits(min, max);

let result = deposit_request.validate(&limits, true);
async fn test_deposit_validate_limits_errors() {
let deposit = tx_setup(14, DEPOSIT_DUST_LIMIT - 1, &[DEPOSIT_DUST_LIMIT - 1]);
let deposit_request = CreateDepositRequestBody {
bitcoin_txid: deposit.tx.compute_txid().to_string(),
bitcoin_tx_output_index: 0,
reclaim_script: deposit
.reclaims
.first()
.unwrap()
.reclaim_script()
.to_hex_string(),
deposit_script: deposit
.deposits
.first()
.unwrap()
.deposit_script()
.to_hex_string(),
transaction_hex: serialize_hex(&deposit.tx),
};
let result = deposit_request.validate(true);

assert_eq!(
result.unwrap_err().to_string(),
format!("HTTP request failed with status code 400 Bad Request: {expected_error}")
format!("HTTP request failed with status code 400 Bad Request: deposit amount below dust limit ({DEPOSIT_DUST_LIMIT})")
);
}
}

0 comments on commit 85a972e

Please sign in to comment.