Skip to content

Commit

Permalink
feat: remove delegation chaining from delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
mootz12 committed Apr 25, 2024
1 parent db27596 commit 6e4fd1b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
111 changes: 106 additions & 5 deletions contracts/tests/tests/votes/test_delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ fn test_delegation_chain_only_delegates_balance() {
votes_client.set_vote_sequence(&(cur_ledger + 99));
votes_client.set_vote_sequence(&(cur_ledger + 199));
votes_client.set_vote_sequence(&(cur_ledger + 299));
votes_client.set_vote_sequence(&(cur_ledger + 399));

let initial_amount_frodo = 50 * 10i128.pow(7);
votes_client.mint(&frodo, &initial_amount_frodo);
Expand Down Expand Up @@ -209,11 +210,40 @@ fn test_delegation_chain_only_delegates_balance() {
);
assert_eq!(votes_client.get_votes(&merry), transfer_amount);

e.jump(100);

// verify joining a delegation chain delegates to the immediate delegate
votes_client.delegate(&merry, &pippin);

assert_eq!(votes_client.get_delegate(&merry), pippin);
assert_eq!(votes_client.get_delegate(&pippin), samwise);

assert_eq!(votes_client.balance(&merry), transfer_amount);
assert_eq!(
votes_client.balance(&pippin),
initial_amount_pippen - transfer_amount
);

assert_eq!(votes_client.get_votes(&pippin), transfer_amount);
assert_eq!(
votes_client.get_votes(&samwise),
initial_amount_pippen - transfer_amount
);
assert_eq!(
votes_client.get_votes(&frodo),
initial_amount_samwise + initial_amount_frodo
);
assert_eq!(votes_client.get_votes(&merry), 0);

// verify checkpoints for pippin
assert_eq!(
votes_client.get_past_votes(&pippin, &(e.ledger().sequence() - 201)),
votes_client.get_past_votes(&pippin, &(e.ledger().sequence() - 301)),
initial_amount_pippen
);
assert_eq!(
votes_client.get_past_votes(&pippin, &(e.ledger().sequence() - 201)),
0
);
assert_eq!(
votes_client.get_past_votes(&pippin, &(e.ledger().sequence() - 101)),
0
Expand All @@ -225,31 +255,57 @@ fn test_delegation_chain_only_delegates_balance() {

// verify checkpoints for samwise
assert_eq!(
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 201)),
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 301)),
initial_amount_samwise
);
assert_eq!(
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 101)),
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 201)),
initial_amount_samwise + initial_amount_pippen
);
assert_eq!(
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 1)),
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 101)),
initial_amount_pippen
);
assert_eq!(
votes_client.get_past_votes(&samwise, &(e.ledger().sequence() - 1)),
initial_amount_pippen - transfer_amount
);

// verify checkpoints for frodo
assert_eq!(
votes_client.get_past_votes(&frodo, &(e.ledger().sequence() - 301)),
initial_amount_frodo
);
assert_eq!(
votes_client.get_past_votes(&frodo, &(e.ledger().sequence() - 201)),
initial_amount_frodo
);
assert_eq!(
votes_client.get_past_votes(&frodo, &(e.ledger().sequence() - 101)),
initial_amount_frodo
initial_amount_frodo + initial_amount_samwise
);
assert_eq!(
votes_client.get_past_votes(&frodo, &(e.ledger().sequence() - 1)),
initial_amount_frodo + initial_amount_samwise
);

// verify checkpoints for merry
assert_eq!(
votes_client.get_past_votes(&merry, &(e.ledger().sequence() - 301)),
0
);
assert_eq!(
votes_client.get_past_votes(&merry, &(e.ledger().sequence() - 201)),
0
);
assert_eq!(
votes_client.get_past_votes(&merry, &(e.ledger().sequence() - 101)),
0
);
assert_eq!(
votes_client.get_past_votes(&merry, &(e.ledger().sequence() - 1)),
transfer_amount
);
}

#[test]
Expand All @@ -273,3 +329,48 @@ fn test_delegation_to_current_delegate() {
let result = votes_client.try_delegate(&samwise, &frodo);
assert_eq!(result.err(), Some(Ok(Error::from_contract_error(101))));
}

#[test]
fn test_delegation_and_change_delegation() {
let e = Env::default();
e.mock_all_auths();
e.set_default_info();

let bombadil = Address::generate(&e);
let samwise = Address::generate(&e);
let frodo = Address::generate(&e);
let pippin = Address::generate(&e);
let governor = Address::generate(&e);

let (token_id, token_client) = create_stellar_token(&e, &bombadil);
let (_, votes_client) = create_bonding_token_votes(&e, &token_id, &governor);

let initial_amount_frodo = 50 * 10i128.pow(7);
token_client.mint(&frodo, &initial_amount_frodo);
votes_client.deposit(&frodo, &initial_amount_frodo);

votes_client.delegate(&frodo, &samwise);

assert_eq!(votes_client.get_delegate(&frodo), samwise);
assert_eq!(votes_client.get_votes(&frodo), 0);
assert_eq!(votes_client.get_votes(&samwise), initial_amount_frodo);
assert_eq!(votes_client.get_votes(&pippin), 0);

e.jump(123);

votes_client.delegate(&frodo, &pippin);

assert_eq!(votes_client.get_delegate(&frodo), pippin);
assert_eq!(votes_client.get_votes(&frodo), 0);
assert_eq!(votes_client.get_votes(&samwise), 0);
assert_eq!(votes_client.get_votes(&pippin), initial_amount_frodo);

e.jump(123);

votes_client.delegate(&frodo, &frodo);

assert_eq!(votes_client.get_delegate(&frodo), frodo);
assert_eq!(votes_client.get_votes(&frodo), initial_amount_frodo);
assert_eq!(votes_client.get_votes(&samwise), 0);
assert_eq!(votes_client.get_votes(&pippin), 0);
}
3 changes: 1 addition & 2 deletions contracts/votes/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,14 @@ impl Votes for TokenVotes {
if cur_delegate == delegatee {
panic_with_error!(e, TokenVotesError::InvalidDelegateeError);
}
let dest_delegate = storage::get_delegate(&e, &delegatee);
let balance = storage::get_balance(&e, &account);
let vote_ledgers = storage::get_vote_ledgers(&e);
if balance > 0 {
move_voting_units(
&e,
&vote_ledgers,
Some(&cur_delegate),
Some(&dest_delegate),
Some(&delegatee),
balance,
);
}
Expand Down

0 comments on commit 6e4fd1b

Please sign in to comment.