This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nft.rs
92 lines (81 loc) · 2.67 KB
/
nft.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
81
82
83
84
85
86
87
88
89
90
91
92
use near::{signer::Signer, Contract, NetworkConfig, Tokens};
use near_contract_standards::non_fungible_token::metadata::TokenMetadata;
use near_token::NearToken;
use serde_json::json;
#[tokio::main]
async fn main() {
let network = near_workspaces::sandbox().await.unwrap();
let nft = network.dev_create_account().await.unwrap();
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);
let contract = Contract(nft.id().clone());
let nft_signer = Signer::new(Signer::from_workspace(&nft)).unwrap();
// Deploying token contract
contract
.deploy(include_bytes!("../resources/nft.wasm").to_vec())
.with_init_call(
"new_default_meta",
json!({
"owner_id": nft.id().to_string(),
}),
)
.unwrap()
.with_signer(nft_signer.clone())
.send_to(&network)
.await
.unwrap();
// Mint NFT via contract call
contract
.call_function(
"nft_mint",
json!({
"token_id": "1",
"receiver_id": account.id().to_string(),
"token_metadata": TokenMetadata {
title: Some("My NFT".to_string()),
description: Some("My first NFT".to_string()),
..Default::default()
}
}),
)
.unwrap()
.transaction()
.deposit(NearToken::from_millinear(100))
.with_signer(nft.id().clone(), nft_signer.clone())
.send_to(&network)
.await
.unwrap();
// Verifying that account has our nft token
let tokens = Tokens::of(account.id().clone())
.nft_assets(nft.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
assert_eq!(tokens.data.len(), 1);
println!("Account has {}", tokens.data.first().unwrap().token_id);
Tokens::of(account.id().clone())
.send_to(nft.id().clone())
.nft(nft.id().clone(), "1".to_string())
.unwrap()
.with_signer(nft_signer.clone())
.send_to(&network)
.await
.unwrap();
// Verifying that account doesn't have nft anymore
let tokens = Tokens::of(account.id().clone())
.nft_assets(nft.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
assert!(tokens.data.is_empty());
let tokens = Tokens::of(nft.id().clone())
.nft_assets(nft.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
assert_eq!(tokens.data.len(), 1);
println!("nft has {}", tokens.data.first().unwrap().token_id);
}