-
Notifications
You must be signed in to change notification settings - Fork 129
/
lib.rs
65 lines (53 loc) · 1.98 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
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, UnorderedSet};
use near_sdk::json_types::ValidAccountId;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, AccountId, Balance, PanicOnDefault, Promise, StorageUsage};
use crate::internal::*;
pub use crate::mint::*;
pub use crate::nft_core::*;
mod internal;
mod mint;
mod nft_core;
#[global_allocator]
static ALLOC: near_sdk::wee_alloc::WeeAlloc<'_> = near_sdk::wee_alloc::WeeAlloc::INIT;
pub type TokenId = String;
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct Token {
pub owner_id: AccountId,
pub meta: String,
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
/// AccountID -> Account balance.
pub accounts: LookupMap<AccountId, UnorderedSet<TokenId>>,
pub tokens: LookupMap<TokenId, Token>,
pub owner_id: AccountId,
pub total_supply: u64,
/// The storage size in bytes for one account.
pub extra_storage_in_bytes_per_token: StorageUsage,
}
#[near_bindgen]
impl Contract {
#[init]
pub fn new(owner_id: ValidAccountId) -> Self {
assert!(!env::state_exists(), "Already initialized");
let mut this = Self {
accounts: LookupMap::new(b"a".to_vec()),
tokens: LookupMap::new(b"t".to_vec()),
owner_id: owner_id.into(),
total_supply: 0,
extra_storage_in_bytes_per_token: 0,
};
let initial_storage_usage = env::storage_usage();
let tmp_account_id = unsafe { String::from_utf8_unchecked(vec![b'a'; 64]) };
let mut u = UnorderedSet::new(prefix(&tmp_account_id));
u.insert(&tmp_account_id);
this.extra_storage_in_bytes_per_token = env::storage_usage() - initial_storage_usage
+ (tmp_account_id.len() - this.owner_id.len()) as u64;
this.accounts.remove(&tmp_account_id);
this
}
}