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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use super::Test;
use crate::{CoreContext, Result, TestReport};
use diem_rest_client::Client as RestClient;
use diem_sdk::{
crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform},
move_types::account_address::AccountAddress,
transaction_builder::TransactionFactory,
types::{chain_id::ChainId, transaction::authenticator::AuthenticationKey, LocalAccount},
};
use diem_transaction_builder::experimental_stdlib;
use rand::{rngs::StdRng, SeedableRng};
use reqwest::Url;
#[async_trait::async_trait]
pub trait NFTPublicUsageTest: Test {
async fn run<'t>(&self, ctx: &mut NFTPublicUsageContext<'t>) -> Result<()>;
}
pub struct NFTPublicUsageContext<'t> {
core: CoreContext,
public_info: NFTPublicInfo<'t>,
pub report: &'t mut TestReport,
}
impl<'t> NFTPublicUsageContext<'t> {
pub fn new(
core: CoreContext,
public_info: NFTPublicInfo<'t>,
report: &'t mut TestReport,
) -> Self {
Self {
core,
public_info,
report,
}
}
pub fn client(&self) -> RestClient {
RestClient::new(self.public_info.rest_api_url.clone())
}
pub fn url(&self) -> &str {
self.public_info.rest_api_url.as_str()
}
pub fn core(&self) -> &CoreContext {
&self.core
}
pub fn rng(&mut self) -> &mut ::rand::rngs::StdRng {
self.core.rng()
}
pub fn random_account(&mut self) -> LocalAccount {
LocalAccount::generate(self.core.rng())
}
pub fn chain_id(&self) -> ChainId {
self.public_info.chain_id
}
pub fn transaction_factory(&self) -> TransactionFactory {
TransactionFactory::new(self.chain_id())
}
pub async fn mint_bars(&mut self, address: AccountAddress, amount: u64) -> Result<()> {
let mint_nft_txn = self.public_info.bars_account.sign_with_transaction_builder(
self.transaction_factory().payload(
experimental_stdlib::encode_mint_bars_script_function(
address,
"test_artist".as_bytes().to_vec(),
"test_url".as_bytes().to_vec(),
amount,
),
),
);
self.public_info
.rest_client
.submit_and_wait(&mint_nft_txn)
.await?;
Ok(())
}
pub async fn create_user_account(&mut self, auth_key: AuthenticationKey) -> Result<()> {
let create_account_txn = self.public_info.bars_account.sign_with_transaction_builder(
self.transaction_factory().payload(
experimental_stdlib::encode_create_account_script_function(
auth_key.derived_address(),
auth_key.prefix().to_vec(),
),
),
);
self.public_info
.rest_client
.submit_and_wait(&create_account_txn)
.await?;
Ok(())
}
}
pub struct NFTPublicInfo<'t> {
chain_id: ChainId,
rest_api_url: Url,
rest_client: RestClient,
root_account: &'t mut LocalAccount,
bars_account: LocalAccount,
}
impl<'t> NFTPublicInfo<'t> {
pub fn new(
chain_id: ChainId,
rest_api_url_str: String,
root_account: &'t mut LocalAccount,
) -> Self {
let bars_key = Ed25519PrivateKey::generate(&mut StdRng::from_seed([1; 32]));
let bars_address = AuthenticationKey::ed25519(&bars_key.public_key()).derived_address();
let rest_api_url = Url::parse(&rest_api_url_str).unwrap();
Self {
rest_client: RestClient::new(rest_api_url.clone()),
rest_api_url,
chain_id,
root_account,
bars_account: LocalAccount::new(bars_address, bars_key, 0),
}
}
pub async fn init_nft_environment(&mut self) -> Result<()> {
let init_nft_txn = self.root_account.sign_with_transaction_builder(
TransactionFactory::new(self.chain_id)
.payload(experimental_stdlib::encode_nft_initialize_script_function()),
);
self.rest_client.submit_and_wait(&init_nft_txn).await?;
let bars_account_creation_txn = self.root_account.sign_with_transaction_builder(
TransactionFactory::new(self.chain_id).payload(
experimental_stdlib::encode_create_account_script_function(
self.bars_account.address(),
self.bars_account.authentication_key().prefix().to_vec(),
),
),
);
self.rest_client
.submit_and_wait(&bars_account_creation_txn)
.await?;
Ok(())
}
}