examples/ido-pool: ensure IDO starts in the future (#225)

This commit is contained in:
Henry-E 2021-04-28 00:24:34 +01:00 committed by GitHub
parent 360a16657d
commit af735a49cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -9,7 +9,7 @@ use anchor_spl::token::{self, Burn, Mint, MintTo, TokenAccount, Transfer};
pub mod ido_pool {
use super::*;
#[access_control(InitializePool::accounts(&ctx, nonce))]
#[access_control(InitializePool::accounts(&ctx, nonce) future_start_time(&ctx, start_ido_ts))]
pub fn initialize_pool(
ctx: Context<InitializePool>,
num_ido_tokens: u64,
@ -18,8 +18,8 @@ pub mod ido_pool {
end_deposits_ts: i64,
end_ido_ts: i64,
) -> Result<()> {
if !(start_ido_ts < end_deposits_ts && end_deposits_ts <= end_ido_ts) {
return Err(ErrorCode::InitTime.into());
if !(start_ido_ts < end_deposits_ts && end_deposits_ts < end_ido_ts) {
return Err(ErrorCode::SeqTimes.into());
}
let pool_account = &mut ctx.accounts.pool_account;
@ -133,6 +133,7 @@ pub mod ido_pool {
return Err(ErrorCode::LowRedeemable.into());
}
// Calculate watermelon tokens due.
let watermelon_amount = (amount as u128)
.checked_mul(ctx.accounts.pool_watermelon.amount as u128)
.unwrap()
@ -211,6 +212,7 @@ pub struct InitializePool<'info> {
#[account("token_program.key == &token::ID")]
pub token_program: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub clock: Sysvar<'info, Clock>,
}
impl<'info> InitializePool<'info> {
@ -332,8 +334,10 @@ pub struct PoolAccount {
#[error]
pub enum ErrorCode {
#[msg("IDO must start in the future")]
IdoFuture,
#[msg("IDO times are non-sequential")]
InitTime,
SeqTimes,
#[msg("IDO has not started")]
StartIdoTime,
#[msg("Deposits period has ended")]
@ -354,6 +358,14 @@ pub enum ErrorCode {
// Access control modifiers.
// Asserts the IDO starts in the future.
fn future_start_time<'info>(ctx: &Context<InitializePool<'info>>, start_ido_ts: i64) -> Result<()> {
if !(ctx.accounts.clock.unix_timestamp < start_ido_ts) {
return Err(ErrorCode::IdoFuture.into());
}
Ok(())
}
// Asserts the IDO is in the first phase.
fn unrestricted_phase<'info>(ctx: &Context<ExchangeUsdcForRedeemable<'info>>) -> Result<()> {
if !(ctx.accounts.pool_account.start_ido_ts < ctx.accounts.clock.unix_timestamp) {

View File

@ -110,6 +110,7 @@ describe("ido-pool", () => {
poolUsdc,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
},
signers: [poolAccount],
instructions: [