Skip to content

Commit

Permalink
Merge pull request #328 from Alesfatalis/i327_buyback_issues
Browse files Browse the repository at this point in the history
i327 buyback issues
  • Loading branch information
kushti authored Apr 23, 2024
2 parents 3a22293 + 574c643 commit fd70a4a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
32 changes: 21 additions & 11 deletions core/src/box_kind/buyback_box.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
}
}
}
13 changes: 13 additions & 0 deletions core/src/datapoint_source/coingecko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetsExchangeRate<KgAu, NanoErg>, DataPointSourceError> {
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=XAU";
let resp = reqwest::get(url).await?;
Expand All @@ -29,6 +30,18 @@ pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, Dat
}
}

#[cfg(test)]
pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, 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<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD";
Expand Down
33 changes: 27 additions & 6 deletions core/src/pool_commands/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -725,7 +746,7 @@ mod tests {
.unwrap()
.amount
.as_u64(),
&190,
&189,
"reward tokens should be added to the pool box"
)
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.71.1
1.74.1

0 comments on commit fd70a4a

Please sign in to comment.