Skip to content

Commit 9167153

Browse files
rodrigoioyzclaude
andcommitted
Add compute_expected_synth_amount to utils.ak
Forward direction ADA lovelaces → synth USD micro-tokens. Includes 4 unit tests: 1 ADA, 2 ADA, 1.5 ADA and round-trip. All 21 tests passing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4f93f96 commit 9167153

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

on-chain/lib/utils.ak

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ pub fn protocol_payout(
4747
fee_amount * pow(10, abs_exp) / raw_price
4848
}
4949

50+
/// Convierte ADA (lovelaces) a synth tokens (micro-USD).
51+
/// Dirección forward del oracle: ADA → synth USD.
52+
///
53+
/// synth_amount = ada_lovelaces * raw_price / 10^abs_exp
54+
///
55+
/// Ejemplo (ADA/USD $0.70, exponent=-8):
56+
/// 1_000_000 lovelaces → 700_000 micro-USD ($0.70)
57+
pub fn compute_expected_synth_amount(
58+
ada_lovelaces: Int,
59+
raw_price: Int,
60+
exponent: Int,
61+
) -> Int {
62+
let abs_exp = if exponent < 0 { -exponent } else { exponent }
63+
ada_lovelaces * raw_price / pow(10, abs_exp)
64+
}
65+
5066
fn pow(base: Int, exp: Int) -> Int {
5167
if exp == 0 {
5268
1
@@ -160,3 +176,28 @@ test protocol_payout_2pct() {
160176
test protocol_payout_zero_fee() {
161177
protocol_payout(700_000, 0, 70_000_000, -8) == 0
162178
}
179+
180+
// compute_expected_synth_amount -----------------------------------------------
181+
182+
// 1 ADA ($0.70) → 700_000 micro-USD
183+
test synth_amount_1_ada() {
184+
compute_expected_synth_amount(1_000_000, 70_000_000, -8) == 700_000
185+
}
186+
187+
// 2 ADA ($1.40) → 1_400_000 micro-USD
188+
test synth_amount_2_ada() {
189+
compute_expected_synth_amount(2_000_000, 70_000_000, -8) == 1_400_000
190+
}
191+
192+
// 1.5 ADA ($1.05) → 1_050_000 micro-USD
193+
test synth_amount_1_5_ada() {
194+
compute_expected_synth_amount(1_500_000, 70_000_000, -8) == 1_050_000
195+
}
196+
197+
// inverso: synth→ADA y luego ADA→synth debe recuperar el valor original
198+
// 700_000 micro-USD → 1_000_000 lovelaces → 700_000 micro-USD
199+
test synth_amount_round_trip() {
200+
let lovelaces = 1_000_000
201+
let synth = compute_expected_synth_amount(lovelaces, 70_000_000, -8)
202+
synth == 700_000
203+
}

0 commit comments

Comments
 (0)