Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional developers reward #320

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ oracle-config.yaml
/.vim/
/oracles/
/.vscode/
/.idea/
/.idea/
/bootstrap.yaml
/oracle_config.yaml
/pool_config.yaml
/pool_config_updated.yaml
/prepare_update_conf.yaml
/prepare_update_conf.yaml.bk
/rustup
/send.sh
2 changes: 2 additions & 0 deletions core/src/box_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod oracle_box;
mod pool_box;
mod refresh_box;
mod update_box;
mod devreward_box;

pub use ballot_box::*;
pub use buyback_box::*;
pub use oracle_box::*;
pub use pool_box::*;
pub use refresh_box::*;
pub use update_box::*;
pub use devreward_box::*;
11 changes: 5 additions & 6 deletions core/src/box_kind/buyback_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,22 @@ impl BuybackBoxWrapper {
}

pub fn new_without_reward_token(&self) -> ErgoBoxCandidate {
// take only buyback nft
let tokens = vec![self
// take only buyback nft - todo: 1 gort should remain as well
let tokens_vec = vec![self
.ergo_box
.tokens
.as_ref()
.unwrap()
.get(0)
.unwrap()
.clone()]
.try_into()
.unwrap();
.clone()];
let tokens = tokens_vec.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: self.ergo_box.creation_height, // todo: should be increased!!!
}
}
}
49 changes: 49 additions & 0 deletions core/src/box_kind/devreward_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

use ergo_lib::ergotree_ir::chain::ergo_box::{BoxTokens, ErgoBoxCandidate};
use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox;
use thiserror::Error;
use ergo_lib::ergotree_ir::chain::token::Token;

use crate::spec_token::RewardTokenId;
use crate::TokenAmount;

#[derive(Debug, Error)]
pub enum DevRewardBoxError {}

#[derive(Debug, Clone)]
pub struct DevRewardBoxWrapper {
ergo_box: ErgoBox,
reward_token_id: RewardTokenId,
}


#[allow(clippy::todo)]
impl DevRewardBoxWrapper {
pub fn new(ergo_box: ErgoBox, reward_token_id: RewardTokenId) -> Self {
Self {
ergo_box,
reward_token_id,
}
}

pub fn get_box(&self) -> &ErgoBox {
&self.ergo_box
}

pub fn add_reward_tokens(&self, to_add: TokenAmount, height: u32) -> ErgoBoxCandidate {
let tokens_vec = self.ergo_box.tokens.as_ref().unwrap();
let reward_token = tokens_vec.get(1).unwrap().clone();
let upd_amt: TokenAmount = (reward_token.amount.as_u64() + to_add.as_u64()).try_into().unwrap();
let upd_token: Token = (reward_token.token_id, upd_amt).into();

let tokens: BoxTokens = vec![tokens_vec.get(0).unwrap().clone(), upd_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: height,
}
}
}
42 changes: 40 additions & 2 deletions core/src/oracle_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::box_kind::{
BuybackBoxWrapper, CollectedOracleBox, OracleBox, OracleBoxError, OracleBoxWrapper,
OracleBoxWrapperInputs, PoolBox, PoolBoxError, PoolBoxWrapper, PoolBoxWrapperInputs,
PostedOracleBox, RefreshBoxError, RefreshBoxWrapper, RefreshBoxWrapperInputs, UpdateBoxError,
UpdateBoxWrapper, UpdateBoxWrapperInputs, VoteBallotBoxWrapper,
UpdateBoxWrapper, UpdateBoxWrapperInputs, VoteBallotBoxWrapper, DevRewardBoxWrapper,
};
use crate::datapoint_source::DataPointSourceError;
use crate::oracle_config::ORACLE_CONFIG;
Expand All @@ -12,7 +12,7 @@ use crate::pool_config::POOL_CONFIG;
use crate::scans::{GenericTokenScan, NodeScanRegistry, ScanError, ScanGetBoxes};
use crate::spec_token::{
BallotTokenId, BuybackTokenId, OracleTokenId, PoolTokenId, RefreshTokenId, RewardTokenId,
TokenIdKind, UpdateTokenId,
TokenIdKind, UpdateTokenId, DevRewardTokenId
};
use crate::util::get_token_count;
use anyhow::Error;
Expand Down Expand Up @@ -87,6 +87,11 @@ pub trait BuybackBoxSource {
fn get_buyback_box(&self) -> Result<Option<BuybackBoxWrapper>>;
}

pub trait DevRewardBoxSource {
fn get_dev_reward_box(&self) -> Result<Option<DevRewardBoxWrapper>>;
}


/// Overarching struct which allows for acquiring the state of the whole oracle pool protocol
#[derive(Debug)]
pub struct OraclePool {
Expand All @@ -98,6 +103,7 @@ pub struct OraclePool {
ballot_boxes_scan: BallotBoxesScan,
update_box_scan: UpdateBoxScan,
buyback_box_scan: Option<BuybackBoxScan>,
dev_reward_box_scan: Option<DevRewardBoxScan>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -150,6 +156,13 @@ pub struct BuybackBoxScan {
reward_token_id: RewardTokenId,
}

#[derive(Debug)]
pub struct DevRewardBoxScan {
scan: GenericTokenScan<DevRewardTokenId>,
reward_token_id: RewardTokenId,
}


/// The state of the oracle pool when it is in the Live Epoch stage
#[derive(Debug, Clone)]
pub struct LiveEpochState {
Expand Down Expand Up @@ -223,6 +236,15 @@ impl OraclePool {
reward_token_id: pool_config.token_ids.reward_token_id.clone(),
});

let dev_reward_box_scan =
node_scan_registry
.dev_reward_token_scan
.clone()
.map(|scan| DevRewardBoxScan {
scan,
reward_token_id: pool_config.token_ids.reward_token_id.clone(),
});

log::debug!("Scans loaded");

Ok(OraclePool {
Expand All @@ -234,6 +256,7 @@ impl OraclePool {
refresh_box_scan,
update_box_scan,
buyback_box_scan,
dev_reward_box_scan,
})
}

Expand Down Expand Up @@ -312,6 +335,12 @@ impl OraclePool {
.map(|b| b as &dyn BuybackBoxSource)
}

pub fn get_dev_reward_box_source(&self) -> Option<&dyn DevRewardBoxSource> {
self.dev_reward_box_scan
.as_ref()
.map(|b| b as &dyn DevRewardBoxSource)
}

pub fn get_total_oracle_token_count(&self) -> Result<u64> {
Ok(self
.oracle_datapoint_scan
Expand Down Expand Up @@ -442,3 +471,12 @@ impl BuybackBoxSource for BuybackBoxScan {
.map(|ergo_box| BuybackBoxWrapper::new(ergo_box, self.reward_token_id.clone())))
}
}

impl DevRewardBoxSource for DevRewardBoxScan {
fn get_dev_reward_box(&self) -> Result<Option<DevRewardBoxWrapper>> {
Ok(self
.scan
.get_box()?
.map(|ergo_box| DevRewardBoxWrapper::new(ergo_box, self.reward_token_id.clone())))
}
}
1 change: 1 addition & 0 deletions core/src/pool_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub fn build_action(
change_address,
&oracle_public_key,
op.get_buyback_box_source(),
op.get_dev_reward_box_source()
)
.map_err(Into::into)
.map(|(action, report)| (action.into(), report.into())),
Expand Down
44 changes: 40 additions & 4 deletions core/src/pool_commands/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::box_kind::RefreshBox;
use crate::box_kind::RefreshBoxWrapper;
use crate::oracle_config::BASE_FEE;
use crate::oracle_state::BuybackBoxSource;
use crate::oracle_state::DevRewardBoxSource;
use crate::oracle_state::DataSourceError;
use crate::oracle_state::PoolBoxSource;
use crate::oracle_state::PostedDatapointBoxesSource;
Expand Down Expand Up @@ -75,6 +76,7 @@ pub fn build_refresh_action(
change_address: Address,
my_oracle_pk: &EcPoint,
buyback_box_source: Option<&dyn BuybackBoxSource>,
dev_reward_box_source: Option<&dyn DevRewardBoxSource>
) -> Result<(RefreshAction, RefreshActionReport), RefreshActionError> {
let tx_fee = *BASE_FEE;
let in_pool_box = pool_box_source.get_pool_box()?;
Expand Down Expand Up @@ -121,6 +123,11 @@ pub fn build_refresh_action(
.transpose()?
.flatten();

let in_dev_reward_box_opt = dev_reward_box_source
.map(|s| s.get_dev_reward_box())
.transpose()?
.flatten();

let unspent_boxes = wallet.get_unspent_wallet_boxes()?;
let box_selector = SimpleBoxSelector::new();
let selection = box_selector.select(unspent_boxes, tx_fee, &[])?;
Expand Down Expand Up @@ -171,10 +178,27 @@ pub fn build_refresh_action(
log::debug!("No reward tokens in buyback box");
}
};

input_boxes.append(&mut valid_in_oracle_raw_boxes);
input_boxes.append(selection.boxes.as_vec().clone().as_mut());
output_candidates.append(&mut out_oracle_boxes);


let mut dev_reward_in_position: i16 = -1;
let mut dev_reward_out_position: i16 = -1;

if let Some(dev_reward_box) = in_dev_reward_box_opt {
log::info!("Found dev reward box id {:?}", dev_reward_box.get_box().box_id());
println!("here 1");
input_boxes.push(dev_reward_box.get_box().clone());
dev_reward_in_position = (input_boxes.len() as i16) - 1;
let dev_reward_amount = TokenAmount::try_from((valid_in_oracle_boxes.len() as u64) - (1 as u64)).unwrap();
let out_dev_reward_box = dev_reward_box.add_reward_tokens(dev_reward_amount, height.0);
output_candidates.push(out_dev_reward_box);
dev_reward_out_position = (output_candidates.len() as i16) - 1;
println!("here 2");
};

let box_selection = BoxSelection {
boxes: input_boxes.clone().try_into().unwrap(),
change_boxes: selection.change_boxes,
Expand All @@ -196,12 +220,22 @@ pub fn build_refresh_action(
.iter()
.enumerate()
.for_each(|(idx, ob)| {
let outindex = (idx as i32 + 2).into(); // first two output boxes are pool box and refresh box
let outindex = (idx as i32 + 2).into(); // first two output boxes are pool box and refresh box // todo: buyback fix
let ob_ctx_ext = ContextExtension {
values: vec![(0, outindex)].into_iter().collect(),
};
b.set_context_extension(ob.get_box().box_id(), ob_ctx_ext);
});

if (dev_reward_in_position > -1) {
let box_id = input_boxes.get(dev_reward_in_position as usize).unwrap().box_id();
let ctx_ext = ContextExtension {
values: vec![(0 as u8, dev_reward_out_position.into()),
(1 as u8, (0 as i8).into())].into_iter().collect(),
};
b.set_context_extension(box_id, ctx_ext);
}

let tx = b.build()?;
let report = RefreshActionReport {
oracle_boxes_collected: valid_in_oracle_boxes
Expand Down Expand Up @@ -339,9 +373,8 @@ fn build_out_oracle_boxes(
.map(|in_ob| {
let mut reward_token_new = in_ob.reward_token();
reward_token_new.amount = if &in_ob.public_key() == my_public_key {
let increment: TokenAmount =
// additional 1 reward token per collected oracle box goes to the collector
(1 + valid_oracle_boxes.len() as u64).try_into().unwrap();
// 2 reward tokens per collected oracle box goes to the collector
let increment: TokenAmount = (2 as u64).try_into().unwrap();
reward_token_new.amount.checked_add(&increment).unwrap()
} else {
reward_token_new
Expand Down Expand Up @@ -585,6 +618,7 @@ mod tests {
change_address.address(),
&oracle_pub_key,
None,
None,
)
.unwrap();

Expand Down Expand Up @@ -632,6 +666,7 @@ mod tests {
change_address.address(),
&oracle_pub_key,
None,
None,
);
dbg!(&wrong_epoch_res);
assert!(matches!(
Expand Down Expand Up @@ -681,6 +716,7 @@ mod tests {
change_address.address(),
&oracle_pub_key,
Some(&buyback_source),
None
)
.unwrap();

Expand Down
3 changes: 3 additions & 0 deletions core/src/pool_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::spec_token::PoolTokenId;
use crate::spec_token::RefreshTokenId;
use crate::spec_token::RewardTokenId;
use crate::spec_token::UpdateTokenId;
use crate::spec_token::DevRewardTokenId;

pub const DEFAULT_POOL_CONFIG_FILE_NAME: &str = "pool_config.yaml";
pub static POOL_CONFIG_FILE_PATH: sync::OnceCell<PathBuf> = sync::OnceCell::new();
Expand All @@ -49,6 +50,7 @@ pub struct PoolConfig {
pub ballot_box_wrapper_inputs: BallotBoxWrapperInputs,
pub token_ids: TokenIds,
pub buyback_token_id: Option<BuybackTokenId>,
pub dev_reward_token_id: Option<DevRewardTokenId>
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Copy, Clone)]
Expand Down Expand Up @@ -153,6 +155,7 @@ impl PoolConfig {
update_box_wrapper_inputs,
token_ids,
buyback_token_id: None,
dev_reward_token_id: None,
})
}

Expand Down
Loading
Loading