Order execution and using decimal

This commit is contained in:
2026-07-29 02:28:54 +02:00
parent 2696618ca0
commit e0230425ec
7 changed files with 72 additions and 17 deletions
+6 -4
View File
@@ -1,13 +1,14 @@
use hypersdk::Decimal;
use pulse_sdk::prelude::*;
use serde_json::Value;
use std::collections::HashMap;
fn number(value: &Value, field: &str) -> Result<f64, String> {
fn number(value: &Value, field: &str) -> Result<Decimal, String> {
let raw = value[field]
.as_str()
.ok_or_else(|| format!("asset context field {field} must be a string"))?;
raw.parse::<f64>()
raw.parse::<Decimal>()
.map_err(|error| format!("could not parse asset context field {field} ({raw}): {error}"))
}
@@ -56,7 +57,7 @@ pub async fn fetch_watch_list(
let previous_day_price = number(context, "prevDayPx")?;
let volume_24h = number(context, "dayNtlVlm")?;
if previous_day_price <= 0.0 {
if previous_day_price.is_zero() || previous_day_price.is_sign_negative() {
return Err(format!("{symbol} has an invalid previous-day price"));
}
@@ -66,7 +67,8 @@ pub async fn fetch_watch_list(
symbol: Symbol(symbol.to_owned()),
price: USD(price),
volume_24h: USD(volume_24h),
trend: ((price / previous_day_price) - 1.0) * 100.0,
trend: ((price / previous_day_price) - <Decimal as From<i32>>::from(1))
* <Decimal as From<i32>>::from(100),
},
);
}