Skip to content

Commit

Permalink
Fix compatibility by returning amount as decimal in get_trade_fee.
Browse files Browse the repository at this point in the history
Also add more number formats #683
  • Loading branch information
artemii235 committed Jun 19, 2020
1 parent 5fd0e96 commit 72073d3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,12 @@ pub async fn get_trade_fee(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S
};
let fee_info = try_s!(coin.get_trade_fee().compat().await);
let res = try_s!(json::to_vec(&json!({
"result": fee_info
"result": {
"coin": fee_info.coin,
"amount": fee_info.amount.to_decimal(),
"amount_fraction": fee_info.amount.to_fraction(),
"amount_rat": fee_info.amount.to_ratio(),
}
})));
Ok(try_s!(Response::builder().body(res)))
}
Expand Down
5 changes: 5 additions & 0 deletions mm2src/common/mm_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ impl MmNumber {
pub fn to_ratio(&self) -> BigRational {
self.0.clone()
}

/// Returns decimal representation of number
pub fn to_decimal(&self) -> BigDecimal {
from_ratio_to_dec(&self.0)
}
}

impl From<i32> for MmNumber {
Expand Down
46 changes: 46 additions & 0 deletions mm2src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,52 @@ fn test_common_cashaddresses() {
assert_eq!(obj["address"].as_str().unwrap(), "bchtest:qze8g4gx3z428jjcxzpycpxl7ke7d947gca2a7n2la");
}

#[test]
// https://github.com/KomodoPlatform/atomicDEX-API/issues/683
// trade fee should return numbers in all 3 available formats and
// "amount" must be always in decimal representation for backwards compatibility
fn test_trade_fee_returns_numbers_in_various_formats() {
let coins = json!([
{"coin":"RICK","asset":"RICK","rpcport":8923,"txversion":4,"overwintered":1},
{"coin":"MORTY","asset":"MORTY","rpcport":11608,"txversion":4,"overwintered":1},
{"coin":"ETH","name":"ethereum","etomic":"0x0000000000000000000000000000000000000000","rpcport":80},
{"coin":"JST","name":"jst","etomic":"0xc0eb7AeD740E1796992A08962c15661bDEB58003"}
]);

// start bob and immediately place the order
let mut mm_bob = unwrap! (MarketMakerIt::start (
json! ({
"gui": "nogui",
"netid": 9998,
"dht": "on", // Enable DHT without delay.
"myipaddr": env::var ("BOB_TRADE_IP") .ok(),
"rpcip": env::var ("BOB_TRADE_IP") .ok(),
"canbind": env::var ("BOB_TRADE_PORT") .ok().map (|s| unwrap! (s.parse::<i64>())),
"passphrase": "bob passphrase",
"coins": coins,
"i_am_seed": true,
"rpc_password": "pass",
}),
"pass".into(),
match var ("LOCAL_THREAD_MM") {Ok (ref e) if e == "bob" => Some (local_start()), _ => None}
));
let (_bob_dump_log, _bob_dump_dashboard) = mm_dump (&mm_bob.log_path);
log!({"Bob log path: {}", mm_bob.log_path.display()});
unwrap! (block_on (mm_bob.wait_for_log (22., |log| log.contains (">>>>>>>>> DEX stats "))));
block_on(enable_coins_eth_electrum (&mm_bob, vec!["https://ropsten.infura.io/v3/c01c1b4cf66642528547624e1d6d9d6b"]));

let rc = unwrap! (block_on (mm_bob.rpc (json! ({
"userpass": mm_bob.userpass,
"method": "get_trade_fee",
"coin": "RICK",
}))));
assert!(rc.0.is_success(), "!get_trade_fee: {}", rc.1);
let trade_fee_json: Json = json::from_str(&rc.1).unwrap();
let _amount_dec: BigDecimal = json::from_value(trade_fee_json["result"]["amount"].clone()).unwrap();
let _amount_rat: BigRational = json::from_value(trade_fee_json["result"]["amount_rat"].clone()).unwrap();
let _amount_fraction: Fraction = json::from_value(trade_fee_json["result"]["amount_fraction"].clone()).unwrap();
}

// HOWTO
// 1. Install Firefox.
// 2. Install forked version of wasm-bindgen-cli: cargo install wasm-bindgen-cli --git https://github.com/artemii235/wasm-bindgen.git
Expand Down

0 comments on commit 72073d3

Please sign in to comment.