Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Jan 25, 2024
1 parent c5aa422 commit 8d4dd69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion rand-extension/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ mod rand_extension {
struct MockedRandExtension;
impl ink::env::test::ChainExtension for MockedRandExtension {
/// The static function id of the chain extension.
fn ext_id(&self) -> u32 {
fn ext_id(&self) -> u16 {
666
}

Expand Down
46 changes: 26 additions & 20 deletions vesting/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ mod vesting_contract {
///
/// # Note:
/// The beneficiary cannot be the zero address.
///
impl VestingContract {
#[ink(constructor, payable)]
pub fn new(
beneficiary: AccountId,
duration_time_in_sec: Timestamp,
) -> Result<Self, Error> {
if beneficiary == AccountId::from([0x0; 32]) {
return Err(Error::InvalidBeneficiary);
return Err(Error::InvalidBeneficiary)
}

// This is multiplied by 1000 to conform to the
// Timestamp fomat in ink.
let duration_time = duration_time_in_sec * 1000;
let duration_time = duration_time_in_sec.checked_mul(1000).unwrap();

let start_time = Self::env().block_timestamp();
let owner = Self::env().caller();
Expand Down Expand Up @@ -106,17 +105,17 @@ mod vesting_contract {
/// vesting ends.
#[ink(message)]
pub fn end_time(&self) -> Timestamp {
self.start_time() + self.duration_time()
self.start_time().checked_add(self.duration_time()).unwrap()
}

/// This returns the amount of time remaining
/// until the end of the vesting period.
#[ink(message)]
pub fn time_remaining(&self) -> Timestamp {
if self.time_now() < self.end_time() {
return self.end_time() - self.time_now();
self.end_time().checked_sub(self.time_now()).unwrap()
} else {
return 0;
0
}
}

Expand All @@ -131,14 +130,16 @@ mod vesting_contract {
/// is currently available for release.
#[ink(message)]
pub fn releasable_balance(&self) -> Balance {
return self.vested_amount() as Balance - self.released_balance();
(self.vested_amount() as Balance)
.checked_sub(self.released_balance())
.unwrap()
}

/// This calculates the amount that has already vested
/// but hasn't been released from the contract yet.
#[ink(message)]
pub fn vested_amount(&self) -> Balance {
return self.vesting_schedule(self.this_contract_balance(), self.time_now());
self.vesting_schedule(self.this_contract_balance(), self.time_now())
}

/// This sends the releasable balance to the beneficiary.
Expand All @@ -147,7 +148,7 @@ mod vesting_contract {
pub fn release(&mut self) -> Result<(), Error> {
let releasable = self.releasable_balance();
if releasable == 0 {
return Err(Error::ZeroReleasableBalance);
return Err(Error::ZeroReleasableBalance)
}

self.released_balance += releasable;
Expand Down Expand Up @@ -187,20 +188,20 @@ mod vesting_contract {
/// If the vesting duration is 200 seconds and 100 seconds have
/// passed since the start time, then 50% of the total_allocation
/// would have vested.
///
pub fn vesting_schedule(
&self,
total_allocation: Balance,
current_time: Timestamp,
) -> Balance {
if current_time < self.start_time() {
return 0;
0
} else if current_time >= self.end_time() {
return total_allocation;
return total_allocation
} else {
return (total_allocation
* (current_time - self.start_time()) as Balance)
/ self.duration_time() as Balance;
* (current_time.checked_sub(self.start_time()).unwrap()) as Balance)
.checked_div(self.duration_time() as Balance)
.unwrap()
}
}
}
Expand All @@ -212,7 +213,8 @@ mod vesting_contract {
/// Checking that the default constructor does its job.
#[ink::test]
fn new_creates_contract_with_correct_values() {
let contract = VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();
let contract =
VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();

assert_eq!(contract.beneficiary(), AccountId::from([0x01; 32]));
assert_eq!(contract.duration_time(), 200 * 1000);
Expand All @@ -223,7 +225,8 @@ mod vesting_contract {
/// There should be some time remaining before the vesting period ends.
#[ink::test]
fn time_remaining_works() {
let contract = VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();
let contract =
VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();
assert!(contract.time_remaining() > 0);
}

Expand All @@ -235,7 +238,8 @@ mod vesting_contract {
/// were released.
#[ink::test]
fn release_before_vesting_period_fails() {
let mut contract = VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();
let mut contract =
VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();

assert_eq!(contract.release(), Err(Error::ZeroReleasableBalance));
assert_eq!(contract.released_balance(), 0);
Expand All @@ -249,7 +253,8 @@ mod vesting_contract {
/// amount we simulated as a deposit.
#[ink::test]
fn release_after_vesting_period_works() {
let mut contract = VestingContract::new(AccountId::from([0x01; 32]), 0).unwrap();
let mut contract =
VestingContract::new(AccountId::from([0x01; 32]), 0).unwrap();
contract.releasable_balance += 1000000;

assert_eq!(contract.release(), Ok(()));
Expand All @@ -262,7 +267,8 @@ mod vesting_contract {
/// the total allocation.
#[ink::test]
fn vesting_schedule_works() {
let contract = VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();
let contract =
VestingContract::new(AccountId::from([0x01; 32]), 200).unwrap();

assert_eq!(
contract.vesting_schedule(1000, contract.start_time() + 100 * 1000),
Expand Down

0 comments on commit 8d4dd69

Please sign in to comment.