Skip to content

Commit fd70a4a

Browse files
authored
Merge pull request #328 from Alesfatalis/i327_buyback_issues
i327 buyback issues
2 parents 3a22293 + 574c643 commit fd70a4a

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

core/src/box_kind/buyback_box.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::vec;
22

3+
use crate::oracle_types::BlockHeight;
34
use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox;
45
use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
6+
use ergo_lib::ergotree_ir::chain::token::Token;
57
use thiserror::Error;
68

79
use crate::spec_token::RewardTokenId;
@@ -42,24 +44,32 @@ impl BuybackBoxWrapper {
4244
})
4345
}
4446

45-
pub fn new_without_reward_token(&self) -> ErgoBoxCandidate {
46-
// take only buyback nft
47-
let tokens = vec![self
48-
.ergo_box
49-
.tokens
50-
.as_ref()
51-
.unwrap()
52-
.get(0)
53-
.unwrap()
54-
.clone()]
47+
pub fn new_with_one_reward_token(&self, creation_height: BlockHeight) -> ErgoBoxCandidate {
48+
let single_reward_token = Token {
49+
token_id: self.reward_token_id.token_id(),
50+
amount: 1.try_into().unwrap(),
51+
};
52+
53+
// take buyback nft and at least one reward token
54+
let tokens = vec![
55+
self.ergo_box
56+
.tokens
57+
.as_ref()
58+
.unwrap()
59+
.get(0)
60+
.unwrap()
61+
.clone(),
62+
single_reward_token,
63+
]
5564
.try_into()
5665
.unwrap();
66+
5767
ErgoBoxCandidate {
5868
value: self.ergo_box.value,
5969
ergo_tree: self.ergo_box.ergo_tree.clone(),
6070
tokens: Some(tokens),
6171
additional_registers: self.ergo_box.additional_registers.clone(),
62-
creation_height: self.ergo_box.creation_height,
72+
creation_height: creation_height.0,
6373
}
6474
}
6575
}

core/src/datapoint_source/coingecko.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::assets_exchange_rate::Btc;
77
use super::assets_exchange_rate::Usd;
88
use super::erg_xau::KgAu;
99

10+
#[cfg(not(test))]
1011
pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, DataPointSourceError> {
1112
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=XAU";
1213
let resp = reqwest::get(url).await?;
@@ -29,6 +30,18 @@ pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, Dat
2930
}
3031
}
3132

33+
#[cfg(test)]
34+
pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, DataPointSourceError> {
35+
let nanoerg_per_troy_ounce = NanoErg::from_erg(1.0 / 0.0008162);
36+
let nanoerg_per_kg = KgAu::from_troy_ounce(nanoerg_per_troy_ounce);
37+
let rate = AssetsExchangeRate {
38+
per1: KgAu {},
39+
get: NanoErg {},
40+
rate: nanoerg_per_kg,
41+
};
42+
Ok(rate)
43+
}
44+
3245
#[cfg(not(test))]
3346
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
3447
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD";

core/src/pool_commands/refresh.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ pub fn build_refresh_action(
160160
height,
161161
rate,
162162
reward_decrement,
163-
Some(buyback_reward_token.amount),
163+
Some(
164+
(buyback_reward_token.amount.as_u64() - 1)
165+
.try_into()
166+
.unwrap(),
167+
),
164168
)?;
165-
let out_buyback_box = buyback_box.new_without_reward_token();
169+
let out_buyback_box = buyback_box.new_with_one_reward_token(height);
166170
output_candidates.remove(0);
167171
output_candidates.insert(0, out_pool_box_w_buyback_rewards);
168172
// should be at index 2 (checked in the contract of the buyback input box)
@@ -694,8 +698,8 @@ mod tests {
694698
.as_ref()
695699
.unwrap()
696700
.len(),
697-
1,
698-
"only one token should be in output buyback box"
701+
2,
702+
"only two tokens should be in output buyback box"
699703
);
700704
assert_eq!(
701705
action_with_buyback
@@ -710,8 +714,25 @@ mod tests {
710714
.unwrap()
711715
.token_id,
712716
buyback_token_id,
713-
"only buyback nft should be in output buyback box"
717+
"buyback nft should be in output buyback box"
714718
);
719+
assert_eq!(
720+
action_with_buyback
721+
.tx
722+
.output_candidates
723+
.get(2)
724+
.unwrap()
725+
.tokens
726+
.as_ref()
727+
.unwrap()
728+
.get(1)
729+
.unwrap()
730+
.amount
731+
.as_u64(),
732+
&1,
733+
"one reward token should be in output buyback box"
734+
);
735+
715736
assert_eq!(
716737
action_with_buyback
717738
.tx
@@ -725,7 +746,7 @@ mod tests {
725746
.unwrap()
726747
.amount
727748
.as_u64(),
728-
&190,
749+
&189,
729750
"reward tokens should be added to the pool box"
730751
)
731752
}

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.71.1
1+
1.74.1

0 commit comments

Comments
 (0)