Skip to content

Commit

Permalink
fix buyback issues
Browse files Browse the repository at this point in the history
 - take 1 reward token in addition to buyback nft
 - set correct creation height
  • Loading branch information
Alesfatalis committed Apr 14, 2024
1 parent e573199 commit 9f247b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 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,
}
}
}
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

0 comments on commit 9f247b2

Please sign in to comment.