Skip to content

Commit

Permalink
add elections period check
Browse files Browse the repository at this point in the history
  • Loading branch information
sczembor committed Aug 28, 2023
1 parent 6391bbd commit 8af8b66
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion contracts/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub const CLASS_KYC_SBT: ClassId = 2;
pub const MINT_TOTAL_COST: Balance = mint_deposit(1);
pub const MINT_TOTAL_COST_WITH_KYC: Balance = mint_deposit(2);

pub const ELECTIONS_START: u64 = 1693519199000; // Thu Aug 31 2023 21:59:59 GMT in miliseconds
pub const ELECTIONS_END: u64 = 1695419999000; // Fri Sep 22 2023 21:59:59 GMT in miliseconds

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
Expand Down Expand Up @@ -114,6 +117,7 @@ impl Contract {
/// @claim_sig: standard base64 serialized ed25519 signature.
/// If `metadata.expires_at` is None then we set it to ` now+self.ttl`.
/// Panics if `metadata.expires_at > now+self.ttl`.
/// Throws an error if trying to mint during the elections period.
// TODO: update result to return TokenId
#[handle_result]
#[payable]
Expand All @@ -123,6 +127,13 @@ impl Contract {
claim_sig: String,
memo: Option<String>,
) -> Result<Promise, CtrError> {
let now_ms = env::block_timestamp_ms();
if now_ms > ELECTIONS_START && now_ms <= ELECTIONS_END {
return Err(CtrError::BadRequest(
"IAH SBT cannot be mint during the elections period".to_owned(),
));
}

let user = env::signer_account_id();
if !is_supported_account(user.as_ref().chars()) {
return Err(CtrError::BadRequest(
Expand All @@ -146,7 +157,6 @@ impl Contract {
);
let num_tokens = if claim.verified_kyc { 2 } else { 1 };

let now_ms = env::block_timestamp_ms();
let now = now_ms / 1000;
if claim.timestamp > now {
return Err(CtrError::BadRequest(
Expand Down Expand Up @@ -578,4 +588,24 @@ pub mod tests {
Ok(_) => panic!("expected DuplicatedID, got: Ok"),
}
}

#[test]
fn mint_during_elections() {
let signer = acc_claimer();
let (mut ctx, mut ctr, k) = setup(&signer, &acc_u1());

ctx.block_timestamp = (ELECTIONS_START + 1) * 1_000_000;
testing_env!(ctx.clone());
let (_, c_str, sig) = mk_claim_sign(start() / SECOND, "0x1a", &k, false);
let res = ctr.sbt_mint(c_str.clone(), sig.clone(), None);
assert!(res.is_err());
assert_bad_request(res, "IAH SBT cannot be mint during the elections period");

ctx.block_timestamp = ELECTIONS_END * 1_000_000;
testing_env!(ctx);
let (_, c_str, sig) = mk_claim_sign(start() / SECOND, "0x1a", &k, false);
let res = ctr.sbt_mint(c_str.clone(), sig.clone(), None);
assert!(res.is_err());
assert_bad_request(res, "IAH SBT cannot be mint during the elections period");
}
}

0 comments on commit 8af8b66

Please sign in to comment.