Skip to content

Commit

Permalink
add ballot ttl extension during creation; fix tests; improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hawthorne-abendsen committed Jul 31, 2024
1 parent c54bc41 commit 1adc217
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const UNLOCK_PERIOD: u32 = 604800;
// 2 weeks
const BALLOT_DURATION: u32 = 604800 * 2;

// 2 months
const BALLOT_RENTAL_PERIOD: u32 = 17280 * 30 * 2;

#[contract]
pub struct DAOContract;

Expand Down Expand Up @@ -175,7 +178,9 @@ impl DAOContract {
BallotCategory::AddAsset => 5_000_0000000,
BallotCategory::General => 10_000_0000000
};
// TODO: validate input length for title (min 10 max 40) and description (min 10 max 160)
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
let ballot = Ballot {
initiator: params.initiator,
Expand All @@ -192,7 +197,8 @@ impl DAOContract {
update_dao_balance(&e, &deposit);
// save ballot
e.set_ballot(ballot_id, &ballot);
// TODO: pay persistent storage rental for 2 months ahead
// extend ballot TTL
e.extend_ballot_ttl(ballot_id, e.ledger().sequence() + BALLOT_RENTAL_PERIOD);
// update ID counter
e.set_last_ballot_id(ballot_id);
// return created ballot ID
Expand Down
55 changes: 42 additions & 13 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
#![cfg(test)]

use super::*;
use soroban_sdk::{testutils::{Address as _, Ledger, LedgerInfo}, token::StellarAssetClient, vec, Env, String};
use soroban_sdk::{
testutils::{storage::Persistent, Address as _, Ledger, LedgerInfo},
token::StellarAssetClient,
vec, Env, String,
};

fn init_contract_with_admin<'a>() -> (Env, DAOContractClient<'a>, ContractConfig) {
let env = Env::default();

let admin = Address::generate(&env);

let contract_id = env.register_contract(None, DAOContract);
let client: DAOContractClient<'a> =
DAOContractClient::new(&env, &contract_id);
let client: DAOContractClient<'a> = DAOContractClient::new(&env, &contract_id);

let token = env.register_stellar_asset_contract(admin.clone());

env.mock_all_auths();

StellarAssetClient::new(&env, &token).mint(&admin, &10000000000);
// extend ttl for the contract and token
env.as_contract(&contract_id, || {
env.storage()
.instance()
.extend_ttl(1_000_000, 1_000_000);
});

env.as_contract(&token, || {
env.storage()
.instance()
.extend_ttl(1_000_000, 1_000_000);
});

let amount = 10_000_000_000_0000000;

StellarAssetClient::new(&env, &token).mint(&admin, &amount);

let init_data = ContractConfig {
admin: admin.clone(),
token,
amount: 10000000000,
amount: amount,
};

//set admin
Expand All @@ -34,16 +52,15 @@ fn init_contract_with_admin<'a>() -> (Env, DAOContractClient<'a>, ContractConfig
fn test() {
let (env, client, config) = init_contract_with_admin();


let owner = Address::generate(&env);

let token_client = StellarAssetClient::new(&env, &config.token);
token_client.mint(&owner, &200000);
token_client.mint(&owner, &200_000_0000000);

env.as_contract(&client.address, || {
let balance = env.get_dao_balance();
assert_eq!(balance, 10000000000);
});
let balance = env.get_dao_balance();
assert_eq!(balance, 10_000_000_000_0000000);
});

let ballot_id = client.create_ballot(&BallotInitParams {
category: BallotCategory::AddAsset,
Expand All @@ -54,7 +71,7 @@ fn test() {

env.as_contract(&client.address, || {
let balance = env.get_dao_balance();
assert_eq!(balance, 10000005000);
assert_eq!(balance, 10_000_005_000_0000000);
});

client.vote(&ballot_id, &true);
Expand All @@ -69,17 +86,24 @@ fn test() {
initiator: owner.clone(),
});

env.as_contract(&client.address, || {
let entry_ttl = env.storage().persistent().get_ttl(&ballot_id);
assert_eq!(entry_ttl, BALLOT_RENTAL_PERIOD);
});

let ledger_info = env.ledger().get();
let ledger_sequence: u32 = 17280 * 14;
env.ledger().set(LedgerInfo {
timestamp: (UNLOCK_PERIOD * 2) as u64,
sequence_number: ledger_sequence,
..ledger_info
});

client.retract_ballot(&ballot_id);

env.as_contract(&client.address, || {
let balance = env.get_dao_balance();
assert_eq!(balance, 10000005000 - 6250);//6250 is the deposit + 25% for draft status
assert_eq!(balance, 10_000_005_000_0000000 - 6_250_0000000); //6250 is the deposit + 25% for draft status
});

let ballot_id = client.create_ballot(&BallotInitParams {
Expand All @@ -89,13 +113,18 @@ fn test() {
initiator: owner.clone(),
});

env.as_contract(&client.address, || {
let entry_ttl = env.storage().persistent().get_ttl(&ballot_id);
assert_eq!(entry_ttl, ledger_sequence + BALLOT_RENTAL_PERIOD);
});

client.vote(&ballot_id, &false);

client.retract_ballot(&ballot_id);

env.as_contract(&client.address, || {
let balance = env.get_dao_balance();
assert_eq!(balance, 10000002500 - 3750);//3750 is 75% the deposit
assert_eq!(balance, 10_000_002_500_0000000 - 3_750_0000000); //3750 is 75% the deposit
});

let developer = Address::generate(&env);
Expand Down
2 changes: 2 additions & 0 deletions src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum Error {
Unauthorized = 2,
/// Operation amount is invalid
InvalidAmount = 3,
/// Invalid ballot create parameters
InvalidBallotParams = 4,
/// Last unlock process has been executed less than a week ago
UnlockUnavailable = 10,
/// Proposal has been created less than two weeks ago and refund is not available yet, or the ballot has been closed
Expand Down

0 comments on commit 1adc217

Please sign in to comment.