-
-
Save Act0r1/d0444cfe2217389138cc6a2309bddcca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use near_sdk::collections::UnorderedMap; | |
use near_sdk::near_bindgen; | |
use near_sdk::{ | |
borsh::{self, BorshDeserialize, BorshSerialize}, | |
AccountId, PanicOnDefault}; | |
use near_sdk::log; | |
// 1 Ⓝ in yoctoNEAR | |
// const PRIZE_AMOUNT: u128 = 5_000_000_000_000_000_000_000_000; | |
const NANOSECONDS: u64 = 1_000_000_000; | |
#[near_bindgen] | |
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)] | |
pub struct Contract { | |
account:UnorderedMap<AccountId, Amount>, | |
} | |
#[derive(BorshDeserialize, BorshSerialize, Debug)] | |
pub struct Amount { | |
amount:i32, | |
time:u64, | |
expiration_time:u64, | |
} | |
#[near_bindgen] | |
impl Contract { | |
#[init] | |
pub fn new() -> Self { | |
Self { | |
account: UnorderedMap::new(b"vvv") | |
} | |
} | |
pub fn make_invest(&mut self, amount:i32) { | |
let balance_on_account:u128 = near_sdk::env::account_balance(); | |
if amount as u128 > balance_on_account { | |
log!("You doesn't have enough balance for this"); | |
panic!("You can't use more than {}", balance_on_account) | |
} | |
else { | |
let time = near_sdk::env::block_timestamp(); | |
// add 30 days to give expiration date for this invest | |
let dt_month = 2_592_000_000_000_000 as u64; | |
let expiration_time = time + dt_month; | |
let account_id = near_sdk::env::predecessor_account_id(); | |
let existing = self.account.insert(&account_id, &Amount{ | |
amount, | |
time, | |
expiration_time, | |
}); | |
assert!(existing.is_none(), "Account already exists") | |
} | |
} | |
pub fn get_info(&self) { | |
let account_id = near_sdk::env::predecessor_account_id(); | |
let get = self.account.get(&account_id); | |
match get { | |
Some(get) => log!("This account have a amount {:?}, expiration_time : {:?}", get.amount, get.expiration_time / NANOSECONDS), | |
None => log!("Couldn't get account") | |
} | |
} | |
// this function just add funds to account which call this contract | |
// so my contract doesn't support cross contract call | |
pub fn add_funds(&mut self, add_amount:i32) { | |
let account_id = near_sdk::env::predecessor_account_id(); | |
let get = self.account.get(&account_id); | |
// let (time, amount) = match get{ | |
// Some(get) => get.time, | |
// None => panic!("You don't funds yet"), | |
// }; | |
let (expiration_time, amount) = match get { | |
Some(get) => (get.expiration_time, get.amount), | |
None => panic!("You don't funds yet"), | |
}; | |
let new_funds = amount + add_amount; | |
// update field time, but expiration_time remains the same | |
let time = near_sdk::env::block_timestamp(); | |
self.account.insert(&account_id, &Amount{ | |
amount:new_funds, | |
time:time, | |
expiration_time, | |
}); | |
} | |
// send back all tokens to signer account | |
pub fn get_all_tokens(&mut self) -> i32{ | |
let values = self.account.values(); | |
let all_tokens:i32 = values.into_iter().map(|all_tokens|all_tokens.amount).sum(); | |
log!("All tokens = {}", all_tokens); | |
all_tokens | |
} | |
// func for frontend realization | |
// pub fn get_rewards(&mut self, amount: u128) {} | |
pub fn claim_tokens(&mut self) { | |
// let account = near_sdk::env::predecessor_account_id(); | |
} | |
pub fn attached_deposit(&self) { | |
let amount = near_sdk::env::attached_deposit(); | |
log!("attached deposit = {}", amount); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment