Skip to content

Commit

Permalink
add deposit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hawthorne-abendsen committed Jul 31, 2024
1 parent 2dd9d82 commit 8ffba88
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reflector-dao-contract"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[lib]
Expand Down
14 changes: 13 additions & 1 deletion src/extensions/env_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use soroban_sdk::{panic_with_error, Address, Env};

use crate::types;

use types::{error::Error, ballot::Ballot};
use types::{error::Error, ballot::Ballot, ballot_category::BallotCategory};
const ADMIN_KEY: &str = "admin";
const LAST_BALLOT_ID: &str = "last_ballot_id";
const LAST_UNLOCK: &str = "last_unlock";
Expand Down Expand Up @@ -32,6 +32,10 @@ pub trait EnvExtensions {

fn set_ballot(&self, ballot_id: u64, ballot: &Ballot);

fn set_deposit(&self, ballot_category: BallotCategory, amount: i128);

fn get_deposit(&self, ballot_category: BallotCategory) -> i128;

fn get_dao_balance(&self) -> i128;

fn set_dao_balance(&self, balance: i128);
Expand Down Expand Up @@ -102,6 +106,14 @@ impl EnvExtensions for Env {
get_persistent_storage(&self).set(&ballot_id, ballot);
}

fn set_deposit(&self, ballot_category: BallotCategory, amount: i128) {
get_instance_storage(&self).set(&ballot_category, &amount);
}

fn get_deposit(&self, ballot_category: BallotCategory) -> i128 {
get_instance_storage(&self).get(&ballot_category).unwrap()
}

fn get_last_unlock(&self) -> u64 {
get_instance_storage(&self).get(&LAST_UNLOCK).unwrap_or(0)
}
Expand Down
60 changes: 46 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![no_std]
use extensions::env_extensions::EnvExtensions;
use soroban_sdk::{contract, contractimpl, token::TokenClient, Address, Env, Vec};
use soroban_sdk::{contract, contractimpl, token::TokenClient, Address, Env, Map, Vec};
use types::{
ballot::Ballot, ballot_init_params::BallotInitParams, ballot_status::BallotStatus,
ballot_category::BallotCategory, contract_config::ContractConfig, error::Error,
ballot::Ballot, ballot_category::BallotCategory, ballot_init_params::BallotInitParams,
ballot_status::BallotStatus, contract_config::ContractConfig, error::Error,
};

mod extensions;
Expand Down Expand Up @@ -39,6 +39,8 @@ impl DAOContract {
/// # Panics
///
/// Panics if the contract has been already initialized
/// Panics if the deposit amounts is invalid
/// Panics if the deposit amount is not set for all categories
pub fn config(e: Env, config: ContractConfig) {
// check admin permissions
config.admin.require_auth();
Expand All @@ -54,13 +56,31 @@ impl DAOContract {
e.set_admin(&config.admin);
e.set_token(&config.token);
e.set_last_unlock(e.ledger().timestamp());
e.set_last_ballot_id(0);
//set deposit params
set_deposit(&e, config.deposit_params);
// transfer tokens to the DAO contract
token(&e).transfer(&config.admin, &e.current_contract_address(), &config.amount);
// set initial DAO balance
update_dao_balance(&e, &config.amount.into());
}

/// Sets the deposit amount for each ballot category
/// Requires admin permissions
///
/// # Arguments
///
/// * `deposit_params` - Map of deposit amounts for each ballot category
///
/// # Panics
///
/// Panics if the caller doesn't match admin address
/// Panics if the deposit amount is invalid
/// Panics if the deposit amount is not set for all categories
pub fn set_deposit(e: Env, deposit_params: Map<BallotCategory, i128>) {
e.panic_if_not_admin();
set_deposit(&e, deposit_params);
}

/// Unlocks tokens distributed to the developer organization and operators on a weekly basis
/// Requires admin permissions
///
Expand Down Expand Up @@ -174,14 +194,12 @@ impl DAOContract {
// generate new ballot id
let ballot_id = e.get_last_ballot_id() + 1;
// calculate deposit requirements for the ballot
// TODO: do we want to adjust deposit amounts per category in the future?
let deposit = match params.category {
BallotCategory::AddNode => 50_000_0000000,
BallotCategory::AddPriceFeed => 100_000_0000000,
BallotCategory::AddAsset => 5_000_0000000,
BallotCategory::General => 10_000_0000000
};
if params.title.len() < 10 || params.title.len() > 40 || params.description.len() < 10 || params.description.len() > 160 {
let deposit = e.get_deposit(params.category);
if params.title.len() < 10
|| params.title.len() > 40
|| params.description.len() < 10
|| params.description.len() > 160
{
e.panic_with_error(Error::InvalidBallotParams);
}
// create a ballot object
Expand Down Expand Up @@ -253,7 +271,7 @@ impl DAOContract {
e.panic_with_error(Error::RefundUnavailable);
}
(ballot.deposit * 125) / 100
},
}
_ => e.panic_with_error(Error::RefundUnavailable),
};
// refund tokens to the initiator address
Expand Down Expand Up @@ -288,7 +306,11 @@ impl DAOContract {
e.panic_with_error(Error::BallotClosed);
}
// resolve new status
let new_status = if accepted { BallotStatus::Accepted } else { BallotStatus::Rejected };
let new_status = if accepted {
BallotStatus::Accepted
} else {
BallotStatus::Rejected
};
// calculate the amount of DAO tokens to burn
let burn_amount = match new_status {
BallotStatus::Rejected => (ballot.deposit * 25) / 100,
Expand All @@ -305,6 +327,16 @@ impl DAOContract {
}
}

fn set_deposit(e: &Env, deposit_params: Map<BallotCategory, i128>) {
for category in BallotCategory::iterator() {
let amount = deposit_params.get(category).unwrap_or(0);
if amount <= 0 {
e.panic_with_error(Error::InvalidAmount);
}
e.set_deposit(category, amount);
}
}

// fetch ballot from the persistent storage
fn get_ballot(e: &Env, ballot_id: u64) -> Ballot {
// fetch ballot by ID
Expand Down
6 changes: 6 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fn init_contract_with_admin<'a>() -> (Env, DAOContractClient<'a>, ContractConfig
admin: admin.clone(),
token,
amount: amount,
deposit_params: Map::from_array(&env, [
(BallotCategory::AddNode, 50_000_0000000),
(BallotCategory::AddPriceFeed, 100_000_0000000),
(BallotCategory::AddAsset, 5_000_0000000),
(BallotCategory::General, 10_000_0000000),
])
};

//set admin
Expand Down
13 changes: 13 additions & 0 deletions src/types/ballot_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ pub enum BallotCategory {
AddPriceFeed = 1,
AddAsset = 2,
General = 3
}

impl BallotCategory {
pub fn iterator() -> impl Iterator<Item = BallotCategory> {
[
BallotCategory::AddNode,
BallotCategory::AddPriceFeed,
BallotCategory::AddAsset,
BallotCategory::General,
]
.iter()
.copied()
}
}
6 changes: 5 additions & 1 deletion src/types/contract_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use soroban_sdk::{contracttype, Address};
use soroban_sdk::{contracttype, Address, Map};

use super::ballot_category::BallotCategory;

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -11,4 +13,6 @@ pub struct ContractConfig {
pub token: Address,
/// Initial funding amount
pub amount: i128,
/// Initial deposit amounts for each ballot category
pub deposit_params: Map<BallotCategory, i128>
}

0 comments on commit 8ffba88

Please sign in to comment.