1

I would like to create a contract (something like an ICO) with an opening and a closing time. The storage stuct looks like this:

    #[ink(storage)]
    #[derive(SpreadAllocate)]
    pub struct Postbox {
        **snip**
        open_time: ink_env::Environment::Timestamp,
        close_time: ink_env::Environment::Timestamp,
        **snip**
    }

and I get the

error[E0223]: ambiguous associated type
  --> lib.rs:14:20
   |
14 |         open_time: ink_env::Environment::Timestamp,
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as ink_env::Environment>::Timestamp`

error while building it.

First question:
How can I store a timestamp in the storage?

Also I would like to close the ICO in 1 week after opening.

Second question:
In Solidity I can use block.timestamp() + 7 days . Is there anything like that in ink?

1

1 Answer 1

1

You could have something like this..

pub struct PostBox {
    open_time: Timestamp,
    close_time: Timestamp,
}

impl PostBox {
    #[ink(constructor)]
    pub fn new() -> Self {
        Self { 
            open_time: Self::env().block_timestamp(),
            close_time: Self::env().block_timestamp()
        }
    }

/// get open_time
#[ink(message)]
pub fn get_open_time(&self) -> Timestamp {
    self.open_time
}

/// get close_time
#[ink(message)]
pub fn get_close_time(&self) -> Timestamp {
    self.close_time
}

/// add 7 days to close_time
#[ink(message)]
pub fn add_seven_days(&mut self) -> Timestamp {
    // https://www.unixtimestamp.com
    // 7 days = 604800000 in Unix time
    let day = 86400000;
    let week = day * 7;
    self.close_time = self.close_time + week;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.