Skip to content

Commit

Permalink
feat: keeping track of stake (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Nov 21, 2024
1 parent 10c2536 commit b3aaa8d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
27 changes: 25 additions & 2 deletions contracts/DelegatedStaking.aes
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,43 @@ payable contract DelegatedStaking =
record state =
{ staking_validator : option(StakingValidator)
, main_staking : MainStaking
, hc_election : HCElection
, last_known_epoch : int
, epoch_rewards : map((int * int), int)
, total_stake : int
, stakes : map(address, int) }

stateful entrypoint init(main_staking : MainStaking) =
stateful entrypoint init(main_staking : MainStaking, hc_election : HCElection) =
{ staking_validator = None,
main_staking = main_staking,
hc_election = hc_election,
last_known_epoch = 0,
epoch_rewards = {},
total_stake = 0,
stakes = {} }

entrypoint get_state() = state

payable stateful entrypoint register_validator() =
put(state{ staking_validator = Some(state.main_staking.new_validator(value = Call.value)) })
update_epoch_rewards()

function get_staking_validator() =
switch(state.staking_validator)
Some(staking_validator) => staking_validator
None => abort("register_validator not yet called")

// we need to make sure to know the epoch a reward was paid for, as payouts are delayed and we need to distribute based on historic percentages
stateful entrypoint force_update_epoch_rewards() = update_epoch_rewards()
stateful function update_epoch_rewards() =
let epoch = state.hc_election.epoch()
if (state.last_known_epoch != epoch)
put(state{ epoch_rewards[(state.last_known_epoch, epoch)] = Contract.balance
, last_known_epoch = epoch }) //TODO actually balance diff
else ()

payable stateful entrypoint stake() =
state.main_staking.stake(value = Call.value, Contract.address)
state.main_staking.stake(value = Call.value, Contract.address)
put(state{ stakes[Call.caller = 0] @ previous_stake = previous_stake + Call.value
, total_stake @ previous_total_stake = previous_total_stake + Call.value })
update_epoch_rewards()
8 changes: 4 additions & 4 deletions package-lock.json

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

36 changes: 31 additions & 5 deletions test/stakingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ describe("StakingContract", () => {
await hcElection.step_eoe(aeSdk.address, 0, 0, 0, false);
}

async function getStakingState() {
async function getMainStakingState() {
return mainStaking.get_state().then((res) => res.decodedResult);
}

async function getDelegatedStakingState() {
return delegatedStaking.get_state().then((res) => res.decodedResult);
}

before(async () => {
aeSdk = utils.getSdk();
await utils.rollbackHeight(aeSdk, 0);
Expand Down Expand Up @@ -61,7 +65,10 @@ describe("StakingContract", () => {
.replace("contract DelegatedStaking", "main contract DelegatedStaking"),
fileSystem: utils.getFilesystem(DELEGATED_STAKING_CONTRACT_SOURCE),
});
await delegatedStaking.init(mainStaking.$options.address, { amount: 10 });
await delegatedStaking.init(
mainStaking.$options.address,
hcElection.$options.address,
);

await utils.createSnapshot(aeSdk);
});
Expand All @@ -82,7 +89,7 @@ describe("StakingContract", () => {

it("DelegatedStaking: register_validator", async () => {
await delegatedStaking.register_validator({ amount: 10 });
console.log(JSON.stringify(await getStakingState(), null, 2));
console.log(await getMainStakingState());
});

it("MainStaking: reward", async () => {
Expand All @@ -93,11 +100,30 @@ describe("StakingContract", () => {
},
);

console.log(JSON.stringify(await getStakingState(), null, 2));
console.log(await getMainStakingState());
});

it("DelegatedStaking: stake", async () => {
console.log(await getDelegatedStakingState());
await delegatedStaking.stake({ amount: 100 });
console.log(JSON.stringify(await getStakingState(), null, 2));
console.log(await getDelegatedStakingState());
await nextEpoch();
await delegatedStaking.stake({ amount: 100 });

console.log(await getDelegatedStakingState());
await nextEpoch();
console.log(
await aeSdk.getBalance(
delegatedStaking.$options.address.replace("ct_", "ak_"),
),
);
await nextEpoch();
console.log(await getDelegatedStakingState());
await delegatedStaking.stake({ amount: 100 });
console.log(await getDelegatedStakingState());
await nextEpoch();
await delegatedStaking.force_update_epoch_rewards();
console.log(await getDelegatedStakingState());
console.log(await getMainStakingState());
});
});

0 comments on commit b3aaa8d

Please sign in to comment.