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

fixed failed assertion, removed U128 #1

Open
wants to merge 1 commit into
base: main
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

127 changes: 64 additions & 63 deletions contracts/myContract/src/coffin.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ global STAKE_THRESHOLD = 8;
struct Vampire {
wallet: AztecAddress,
numberOfStakers: u32,
feeRate: U128,
feeRate: Field,
isAlive: bool,
}

Expand All @@ -24,7 +24,7 @@ impl Deserialize<VAMPIRE_SERIALIZED_LEN> for Vampire {
fn deserialize(fields: [Field; VAMPIRE_SERIALIZED_LEN]) -> Vampire {
let wallet = AztecAddress::from_field(fields[0]);
let numberOfStakers = fields[1] as u32;
let feeRate = U128::from_integer(fields[2]);
let feeRate = fields[2];
let isAlive = fields[3] as bool;

Vampire {
Expand All @@ -38,10 +38,10 @@ impl Deserialize<VAMPIRE_SERIALIZED_LEN> for Vampire {

struct Coffin {
vampire: Vampire,
currentFee: U128,
currentFee: Field,
stakingToken: AztecAddress,
rewardsToken: AztecAddress,
rewardsReserve: U128,
rewardsReserve: Field,
periodInDays: u64,
rewardsPerDay: u64,
lastDistribution: u64,
Expand All @@ -65,10 +65,10 @@ impl ToField for Coffin {
self.vampire.numberOfStakers as Field +
self.vampire.feeRate.to_field() +
self.vampire.isAlive as Field +
self.currentFee.to_integer() +
self.currentFee +
self.stakingToken.to_field() +
self.rewardsToken.to_field() +
self.rewardsReserve.to_integer() +
self.rewardsReserve +
self.periodInDays as Field +
self.rewardsPerDay as Field +
self.lastDistribution.to_field() +
Expand All @@ -81,37 +81,35 @@ impl ToField for Coffin {
}
}

#[test]
fn test_to_from_field() {
let field = 1234567890;
// let coffin = Coffin::from_field(field);
// assert(coffin.to_field() == field);
}
// #[test]
// fn test_to_from_field() {
// let field = 1234567890;
// // let coffin = Coffin::from_field(field);
// // assert(coffin.to_field() == field);
// }

global COFFIN_SERIALIZED_LEN: Field = 18;
global COFFIN_SERIALIZED_LEN: Field = 17;

impl Serialize<COFFIN_SERIALIZED_LEN> for Coffin {
fn serialize(Coffin: Coffin) -> [Field; COFFIN_SERIALIZED_LEN] {
fn serialize(self) -> [Field; COFFIN_SERIALIZED_LEN] {
[
Coffin.vampire.wallet.to_field(),
Coffin.vampire.numberOfStakers as Field,
Coffin.vampire.feeRate.to_field(),
Coffin.vampire.isAlive as Field,
Coffin.currentFee.lo,
Coffin.currentFee.hi,
Coffin.stakingToken.to_field(),
Coffin.rewardsToken.to_field(),
Coffin.rewardsReserve.lo,
Coffin.rewardsReserve.hi,
Coffin.periodInDays as Field,
Coffin.rewardsPerDay as Field,
Coffin.lastDistribution as Field +
Coffin.firstStakeStart as Field,
Coffin.stakesEndMax as Field,
Coffin.idleDays as Field,
Coffin.allStakesNumber as Field,
Coffin.activeStakesNumber as Field,
Coffin.isActive as Field
self.vampire.wallet.to_field(),
self.vampire.numberOfStakers as Field,
self.vampire.feeRate.to_field(),
self.vampire.isAlive as Field,
self.currentFee,
self.stakingToken.to_field(),
self.rewardsToken.to_field(),
self.rewardsReserve,
self.periodInDays as Field,
self.rewardsPerDay as Field,
self.lastDistribution as Field,
self.firstStakeStart as Field,
self.stakesEndMax as Field,
self.idleDays as Field,
self.allStakesNumber as Field,
self.activeStakesNumber as Field,
self.isActive as Field
]
}
}
Expand All @@ -122,19 +120,19 @@ impl Deserialize<COFFIN_SERIALIZED_LEN> for Coffin {

Coffin {
vampire,
currentFee: U128 { lo: fields[4], hi: fields[5] },
stakingToken: AztecAddress::from_field(fields[6]),
rewardsToken: AztecAddress::from_field(fields[7]),
rewardsReserve: U128 { lo: fields[8], hi: fields[9] },
periodInDays: fields[10] as u64,
rewardsPerDay: fields[11] as u64,
lastDistribution: fields[12] as u64,
firstStakeStart: fields[13] as u64,
stakesEndMax: fields[14] as u64,
idleDays: fields[15] as u64,
allStakesNumber: fields[16] as u64,
activeStakesNumber: fields[17] as u64,
isActive: fields[18] as bool,
currentFee: fields[4],
stakingToken: AztecAddress::from_field(fields[5]),
rewardsToken: AztecAddress::from_field(fields[6]),
rewardsReserve: fields[7],
periodInDays: fields[8] as u64,
rewardsPerDay: fields[9] as u64,
lastDistribution: fields[10] as u64,
firstStakeStart: fields[11] as u64,
stakesEndMax: fields[12] as u64,
idleDays: fields[13] as u64,
allStakesNumber: fields[14] as u64,
activeStakesNumber: fields[15] as u64,
isActive: fields[16] as bool,
}
}
}
Expand Down Expand Up @@ -182,11 +180,11 @@ impl Coffin {

pub fn getCoffinDaysLeft(&mut self, currentTime: u64) -> u64 {
let coffinPeriod = self.periodInDays;
if (U128::from_integer(self.firstStakeStart) == (U128::from_integer(0))) {
if (self.firstStakeStart == 0) {
coffinPeriod
} else {
let mut activeTimePassed = 0; // How long coffin was active (had stakes), the current time is not included
if ((U128::from_integer(self.stakesEndMax)) < U128::from_integer(currentTime)) {
if (self.stakesEndMax < currentTime) {
// If last stake finished before present moment we have to count active time
// till the end of last stake
activeTimePassed = self.stakesEndMax - self.firstStakeStart + 1;
Expand All @@ -206,7 +204,7 @@ impl Coffin {
rewardsAmountAdded: Field,
currentTime: u64
) {
self.rewardsReserve = self.rewardsReserve + U128::from_integer(rewardsAmountAdded);
self.rewardsReserve = self.rewardsReserve + rewardsAmountAdded;
let daysLeft = self.getCoffinDaysLeft(currentTime);
if (daysLeft > 0) {
self.rewardsPerDay = self.rewardsPerDay + (rewardsAmountAdded *1000) as u64 / daysLeft; // we store rewardsPerDay multiplied by 1000
Expand All @@ -220,8 +218,8 @@ impl Coffin {
maxStakePeriod: Field,
currentTime: u64
) -> bool {
if ((U128::from_integer(daysLockedUp) < U128::from_integer(minStakePeriod))
| (U128::from_integer(daysLockedUp) > U128::from_integer(maxStakePeriod))) {
if (U128::from_integer(daysLockedUp) < U128::from_integer(minStakePeriod))
| (U128::from_integer(daysLockedUp) > U128::from_integer(maxStakePeriod)) {
false
}
let coffinDaysLeft = self.getCoffinDaysLeft(currentTime);
Expand All @@ -236,43 +234,46 @@ impl Coffin {
self.activeStakesNumber -= 1;
}


pub fn distributeRewards(&mut self, currentTime: u64, stake: Stake) {
let rewardsPerDay = self.rewardsPerDay;
let activeStakesNumber = self.activeStakesNumber;
let lastDistribution = self.lastDistribution;
let mut theStake = stake;
// We count rewards till yesterday only, because today we can receive new stakes
if (rewardsPerDay > 0 & activeStakesNumber > 0 & lastDistribution < (currentTime - 1)) {
for day in (lastDistribution + 1) .. currentTime {
let mut dayTotal =0; // total sum for all stakes (stakeAmount * remaining days) for current day to get then share of this total
for day in (lastDistribution + 1)..currentTime {
let mut dayTotal = U128::from_integer(0); // total sum for all stakes (stakeAmount * remaining days) for current day to get then share of this total
// In this loop we calculate total sum for all stakes (stakeAmount * remaining days) to get then share of this total
if ((theStake.end >= day) & (theStake.start <= day) ) {
let daysCoeff = theStake.end - day + 1;
let dayStakeFactor = U128::from_integer(daysCoeff as Field) * theStake.stakeAmount ;
dayTotal += dayStakeFactor.to_field();
if ((theStake.end >= day) & (theStake.start <= day)) {
let daysCoeff = theStake.end - day + 1;
let dayStakeFactor = U128::from_integer(daysCoeff) * U128::from_integer(theStake.stakeAmount);
dayTotal += dayStakeFactor;
}
if (U128::from_integer(dayTotal) > U128::from_integer(0)) {
if (dayTotal > U128::from_integer(0)) {
// In this loop we calculate share in this day rewards and add it to awarded field
if (theStake.end >= day & theStake.start <= day) {
let daysCoeff = theStake.end - day + 1;
let dayStakeFactor = daysCoeff as Field * theStake.stakeAmount.to_field();
let share = self.preciseDiv( dayStakeFactor, dayTotal, 2 ); // Share in percents
let share = self.preciseDiv(dayStakeFactor, dayTotal.to_field(), 2); // Share in percents
let mut todayStakeReward = rewardsPerDay as Field * share;
todayStakeReward = todayStakeReward / 100; // Round down. Remaining rewards will be sent to fee receiver
theStake.awarded += U128::from_integer(todayStakeReward);
theStake.awarded += todayStakeReward;
}
}
}
self.lastDistribution = currentTime - 1;
}
}

pub fn preciseDiv(&mut self, _numerator: Field, _denominator: Field, _precision: Field) -> pub Field {
pub fn preciseDiv(
&mut self,
_numerator: Field,
_denominator: Field,
_precision: Field
) -> pub Field {
let mut numerator = _numerator * (10.pow_32(_precision + 1));
// with rounding of last digit
let quotient = ((numerator / _denominator) + 5) / 10;
quotient
}

}
10 changes: 3 additions & 7 deletions contracts/myContract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ contract Vanhelsing {
#[aztec(storage)]
struct Storage {
coffins: Map<u64, PublicMutable<Coffin>>,
coffinId: PublicMutable<u64>,
coffinId: PublicMutable<u64>,
feeTo: PublicMutable<AztecAddress>,
}

#[aztec(public)]
#[aztec(initializer)]
fn constructor(
feeTo: AztecAddress,
) {
fn constructor(feeTo: AztecAddress) {
storage.feeTo.write(feeTo);
}

Expand All @@ -30,18 +28,16 @@ contract Vanhelsing {
rewardsAmount: Field,
nonce: Field
) {

Token::at(rewardsToken).transfer_public(
context.msg_sender(),
context.this_address(),
rewardsAmount,
nonce
).call(&mut context);

let currentcoffinId = storage.coffinId.read();

let coffin_storage = storage.coffins.at(currentcoffinId);
println(coffin_storage);
let mut coffin_data = coffin_storage.read(); // <======== Failed assertion
}

}
33 changes: 16 additions & 17 deletions contracts/myContract/src/stake.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ struct Stake {
staker: AztecAddress,
stakeID: u64,
targetedVampire: Vampire, // reward pool
stakeAmount: U128,
stakeAmount: Field,
start: u64,
end: u64,
isActive: bool,
awarded:U128
awarded:Field

}

Expand All @@ -26,15 +26,15 @@ impl ToField for Stake {
self.targetedVampire.wallet.to_field() +
self.targetedVampire.numberOfStakers as Field +
self.targetedVampire.isAlive as Field +
self.stakeAmount.to_integer() +
self.stakeAmount +
self.start as Field +
self.end as Field +
self.isActive as Field +
self.awarded.to_integer()
self.awarded
}
}

global STAKE_SERIALIZED_LEN: Field = 12;
global STAKE_SERIALIZED_LEN: Field = 11;

impl Serialize<STAKE_SERIALIZED_LEN> for Stake {
fn serialize(stake: Stake) -> [Field; STAKE_SERIALIZED_LEN] {
Expand All @@ -45,12 +45,11 @@ impl Serialize<STAKE_SERIALIZED_LEN> for Stake {
stake.targetedVampire.numberOfStakers as Field,
stake.targetedVampire.feeRate.to_field(),
stake.targetedVampire.isAlive as Field,
stake.stakeAmount.lo,
stake.stakeAmount.hi,
stake.stakeAmount,
stake.start as Field,
stake.end as Field,
stake.isActive as Field,
stake.awarded.to_integer(),
stake.awarded,
]
}
}
Expand All @@ -60,11 +59,11 @@ impl Deserialize<STAKE_SERIALIZED_LEN> for Stake {
let staker = AztecAddress::from_field(fields[0]);
let stakeID = fields[1] as u64;
let vampire = Vampire::deserialize([fields[2], fields[3], fields[4], fields[5]]);
let stakeAmount = U128 { lo: fields[6], hi: fields[7] };
let start = fields[8] as u64;
let end = fields[9] as u64;
let isActive = fields[10] as bool;
let awarded = U128 { lo: fields[10], hi: fields[12] };
let stakeAmount = fields[6];
let start = fields[7] as u64;
let end = fields[8] as u64;
let isActive = fields[9] as bool;
let awarded = fields[10];

Stake {
staker,
Expand Down Expand Up @@ -110,13 +109,13 @@ impl Stake {
if (awardedAmount > U128::from_integer(0)) {
let mut feeAmount = 0;
let feeRate = theCoffin.vampire.feeRate;
if ((!feeTo.is_zero()) & (feeRate > U128::from_integer(0))) {
let numerator = U128::from_integer(awardedAmount) * feeRate;
if ((!feeTo.is_zero()) & (U128::from_integer(feeRate) > U128::from_integer(0))) {
let numerator = U128::from_integer(awardedAmount) * U128::from_integer(feeRate);
let denominator = 10000;
feeAmount = theCoffin.preciseDiv(numerator.to_field(), denominator, 0);
}

let rewardsBalance = coffin.rewardsReserve - awardedAmount;
let rewardsBalance = U128::from_integer(coffin.rewardsReserve) - awardedAmount;
// ERC20 rewardsToken = ERC20(rewardsTokenAddress);
// if (feeTo != address(0) && feeAmount > 0) {
// require(
Expand Down Expand Up @@ -150,6 +149,6 @@ impl Stake {
}

pub fn getAwardedAmount(&mut self) -> pub U128 {
self.awarded / U128::from_integer(1000) // We store awarded multiplied by 1000. Make division to get real amount of rewards
U128::from_integer(self.awarded) / U128::from_integer(1000) // We store awarded multiplied by 1000. Make division to get real amount of rewards
}
}
19 changes: 16 additions & 3 deletions contracts/myContract/src/test/coffin.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ use dep::aztec::note::note_getter::{MAX_NOTES_PER_PAGE, view_notes};
use dep::aztec::note::note_viewer_options::NoteViewerOptions;
use dep::token::Token;
use dep::std::println;
use dep::authwit::cheatcodes as authwit_cheatcodes;

#[test]
unconstrained fn create_coffin() {
let (
env,
reward_token_contract_address,
staking_token_contract_address,
vanhelsing_contract_address
vanhelsing_contract_address,
owner
) = utils::setup_and_mint(true);

env.impersonate(owner);

let rewards_amount = 1000;
let nonce = 100;
let public_transfer_from_call_interface = Token::at(reward_token_contract_address).transfer_public(owner, vanhelsing_contract_address, rewards_amount, nonce);
authwit_cheatcodes::add_public_authwit_from_call_interface(
owner,
vanhelsing_contract_address,
public_transfer_from_call_interface
);

let create_coffin_call_interface = Vanhelsing::at(vanhelsing_contract_address).createCoffin(
staking_token_contract_address,
reward_token_contract_address,
30,
1000,
100
rewards_amount,
nonce
);
env.call_public(create_coffin_call_interface);
}
Loading