From e5731992898e0211ecf301991bf32b8f720c4878 Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sun, 14 Apr 2024 12:21:48 +0200 Subject: [PATCH 1/3] fix test by mocking coingecko datasource api call --- core/src/datapoint_source/coingecko.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/src/datapoint_source/coingecko.rs b/core/src/datapoint_source/coingecko.rs index 6955b2d0..1022fe4e 100644 --- a/core/src/datapoint_source/coingecko.rs +++ b/core/src/datapoint_source/coingecko.rs @@ -7,6 +7,7 @@ use super::assets_exchange_rate::Btc; use super::assets_exchange_rate::Usd; use super::erg_xau::KgAu; +#[cfg(not(test))] pub async fn get_kgau_nanoerg() -> Result, DataPointSourceError> { let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=XAU"; let resp = reqwest::get(url).await?; @@ -29,6 +30,18 @@ pub async fn get_kgau_nanoerg() -> Result, Dat } } +#[cfg(test)] +pub async fn get_kgau_nanoerg() -> Result, DataPointSourceError> { + let nanoerg_per_troy_ounce = NanoErg::from_erg(1.0 / 0.0008162); + let nanoerg_per_kg = KgAu::from_troy_ounce(nanoerg_per_troy_ounce); + let rate = AssetsExchangeRate { + per1: KgAu {}, + get: NanoErg {}, + rate: nanoerg_per_kg, + }; + Ok(rate) +} + #[cfg(not(test))] pub async fn get_usd_nanoerg() -> Result, DataPointSourceError> { let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD"; From 9f247b2e6336eb9325aaf5594cd893d7591c1e4a Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sun, 14 Apr 2024 12:23:25 +0200 Subject: [PATCH 2/3] fix buyback issues - take 1 reward token in addition to buyback nft - set correct creation height --- core/src/box_kind/buyback_box.rs | 32 +++++++++++++++++++----------- core/src/pool_commands/refresh.rs | 33 +++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/core/src/box_kind/buyback_box.rs b/core/src/box_kind/buyback_box.rs index cfda19f5..afc9ff62 100644 --- a/core/src/box_kind/buyback_box.rs +++ b/core/src/box_kind/buyback_box.rs @@ -1,7 +1,9 @@ use std::vec; +use crate::oracle_types::BlockHeight; use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox; use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBoxCandidate; +use ergo_lib::ergotree_ir::chain::token::Token; use thiserror::Error; use crate::spec_token::RewardTokenId; @@ -42,24 +44,32 @@ impl BuybackBoxWrapper { }) } - pub fn new_without_reward_token(&self) -> ErgoBoxCandidate { - // take only buyback nft - let tokens = vec![self - .ergo_box - .tokens - .as_ref() - .unwrap() - .get(0) - .unwrap() - .clone()] + pub fn new_with_one_reward_token(&self, creation_height: BlockHeight) -> ErgoBoxCandidate { + let single_reward_token = Token { + token_id: self.reward_token_id.token_id(), + amount: 1.try_into().unwrap(), + }; + + // take buyback nft and at least one reward token + let tokens = vec![ + self.ergo_box + .tokens + .as_ref() + .unwrap() + .get(0) + .unwrap() + .clone(), + single_reward_token, + ] .try_into() .unwrap(); + ErgoBoxCandidate { value: self.ergo_box.value, ergo_tree: self.ergo_box.ergo_tree.clone(), tokens: Some(tokens), additional_registers: self.ergo_box.additional_registers.clone(), - creation_height: self.ergo_box.creation_height, + creation_height: creation_height.0, } } } diff --git a/core/src/pool_commands/refresh.rs b/core/src/pool_commands/refresh.rs index f4393926..9f0ba0cd 100644 --- a/core/src/pool_commands/refresh.rs +++ b/core/src/pool_commands/refresh.rs @@ -160,9 +160,13 @@ pub fn build_refresh_action( height, rate, reward_decrement, - Some(buyback_reward_token.amount), + Some( + (buyback_reward_token.amount.as_u64() - 1) + .try_into() + .unwrap(), + ), )?; - let out_buyback_box = buyback_box.new_without_reward_token(); + let out_buyback_box = buyback_box.new_with_one_reward_token(height); output_candidates.remove(0); output_candidates.insert(0, out_pool_box_w_buyback_rewards); // should be at index 2 (checked in the contract of the buyback input box) @@ -694,8 +698,8 @@ mod tests { .as_ref() .unwrap() .len(), - 1, - "only one token should be in output buyback box" + 2, + "only two tokens should be in output buyback box" ); assert_eq!( action_with_buyback @@ -710,8 +714,25 @@ mod tests { .unwrap() .token_id, buyback_token_id, - "only buyback nft should be in output buyback box" + "buyback nft should be in output buyback box" ); + assert_eq!( + action_with_buyback + .tx + .output_candidates + .get(2) + .unwrap() + .tokens + .as_ref() + .unwrap() + .get(1) + .unwrap() + .amount + .as_u64(), + &1, + "one reward token should be in output buyback box" + ); + assert_eq!( action_with_buyback .tx @@ -725,7 +746,7 @@ mod tests { .unwrap() .amount .as_u64(), - &190, + &189, "reward tokens should be added to the pool box" ) } From 574c6432e78c10ff17bafd2332dd930b94cfaf41 Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sun, 14 Apr 2024 12:29:23 +0200 Subject: [PATCH 3/3] update rust-toolchain to 1.74.1 --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index 68bc7ff2..80627411 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.71.1 +1.74.1