-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.rs
80 lines (68 loc) · 2.45 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use near_contract_standards::fungible_token::metadata::{
FungibleTokenMetadata, FungibleTokenMetadataProvider,
};
use near_contract_standards::fungible_token::FungibleToken;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::{ValidAccountId, U128};
use near_sdk::{near_bindgen, AccountId, PanicOnDefault, PromiseOrValue};
near_sdk::setup_alloc!();
#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
pub struct Contract {
token: FungibleToken,
}
#[near_bindgen]
impl Contract {
#[init]
pub fn new() -> Self {
Self {
token: FungibleToken::new(b"t".to_vec()),
}
}
pub fn mint(&mut self, account_id: ValidAccountId, amount: U128) {
self.token.internal_register_account(account_id.as_ref());
self.token
.internal_deposit(account_id.as_ref(), amount.into());
}
pub fn burn(&mut self, account_id: ValidAccountId, amount: U128) {
self.token
.internal_withdraw(account_id.as_ref(), amount.into());
}
}
near_contract_standards::impl_fungible_token_core!(Contract, token);
near_contract_standards::impl_fungible_token_storage!(Contract, token);
#[near_bindgen]
impl FungibleTokenMetadataProvider for Contract {
fn ft_metadata(&self) -> FungibleTokenMetadata {
unimplemented!()
}
}
#[cfg(test)]
mod tests {
use near_sdk::test_utils::{accounts, VMContextBuilder};
use near_sdk::{env, testing_env, MockedBlockchain};
use super::*;
#[test]
fn test_basics() {
let mut context = VMContextBuilder::new();
testing_env!(context.build());
let mut contract = Contract::new();
testing_env!(context
.attached_deposit(125 * env::storage_byte_cost())
.build());
contract.mint(accounts(0), 1_000_000.into());
assert_eq!(contract.ft_balance_of(accounts(0)), 1_000_000.into());
testing_env!(context
.attached_deposit(125 * env::storage_byte_cost())
.build());
contract.storage_deposit(Some(accounts(1)), None);
testing_env!(context
.attached_deposit(1)
.predecessor_account_id(accounts(0))
.build());
contract.ft_transfer(accounts(1), 1_000.into(), None);
assert_eq!(contract.ft_balance_of(accounts(1)), 1_000.into());
contract.burn(accounts(1), 500.into());
assert_eq!(contract.ft_balance_of(accounts(1)), 500.into());
}
}