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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::jsonrpc::error::Web3Error;
use crate::{client::Web3, types::SendTxOption};
use clarity::constants::ZERO_ADDRESS;
use clarity::Address as EthAddress;
use clarity::{abi::encode_call, PrivateKey as EthPrivateKey};
use clarity::{abi::Token, Address, Uint256};
use std::time::Duration;
use tokio::time::timeout as future_timeout;
pub static ERC721_GAS_LIMIT: u128 = 100_000;
impl Web3 {
pub async fn check_erc721_approved(
&self,
erc721: Address,
own_address: Address,
token_id: Uint256,
) -> Result<Option<EthAddress>, Web3Error> {
let payload = encode_call("getApproved(uint256)", &[Token::Uint(token_id.clone())])?;
let val = self
.simulate_transaction(erc721, 0u8.into(), payload, own_address, None)
.await?;
let mut data: [u8; 20] = Default::default();
data.copy_from_slice(&val[12..]);
let owner_address = EthAddress::from_slice(&data);
match owner_address {
Ok(address_response) => {
if address_response == *ZERO_ADDRESS {
Ok(None)
} else {
Ok(Some(address_response))
}
}
Err(e) => Err(Web3Error::BadResponse(e.to_string())),
}
}
pub async fn approve_erc721_transfers(
&self,
erc721: Address,
eth_private_key: EthPrivateKey,
target_contract: Address,
token_id: Uint256,
timeout: Option<Duration>,
options: Vec<SendTxOption>,
) -> Result<Uint256, Web3Error> {
let own_address = eth_private_key.to_address();
let payload = encode_call(
"approve(address,uint256)",
&[target_contract.into(), Token::Uint(token_id.clone())],
)?;
let txid = self
.send_transaction(
erc721,
payload,
0u32.into(),
own_address,
eth_private_key,
options,
)
.await?;
if let Some(timeout) = timeout {
future_timeout(
timeout,
self.wait_for_transaction(txid.clone(), timeout, None),
)
.await??;
}
Ok(txid)
}
pub async fn erc721_send(
&self,
recipient: Address,
erc721: Address,
token_id: Uint256,
sender_private_key: EthPrivateKey,
wait_timeout: Option<Duration>,
options: Vec<SendTxOption>,
) -> Result<Uint256, Web3Error> {
let sender_address = sender_private_key.to_address();
let mut has_gas_limit = false;
let mut options = options;
for option in options.iter() {
if let SendTxOption::GasLimit(_) = option {
has_gas_limit = true;
break;
}
}
if !has_gas_limit {
options.push(SendTxOption::GasLimit(ERC721_GAS_LIMIT.into()));
}
let tx_hash = self
.send_transaction(
erc721,
encode_call(
"transferFrom(address,address,uint256)",
&[
sender_address.into(),
recipient.into(),
Token::Uint(token_id.clone()),
],
)?,
0u32.into(),
sender_address,
sender_private_key,
options,
)
.await?;
if let Some(timeout) = wait_timeout {
future_timeout(
timeout,
self.wait_for_transaction(tx_hash.clone(), timeout, None),
)
.await??;
}
Ok(tx_hash)
}
pub async fn get_erc721_name(
&self,
erc721: Address,
caller_address: Address,
) -> Result<String, Web3Error> {
let payload = encode_call("name()", &[])?;
let name = self
.simulate_transaction(erc721, 0u8.into(), payload, caller_address, None)
.await?;
match String::from_utf8(name) {
Ok(mut val) => {
val.retain(|v| !v.is_control());
let val = val.trim().to_string();
Ok(val)
}
Err(_e) => Err(Web3Error::ContractCallError(
"name is not valid utf8".to_string(),
)),
}
}
pub async fn get_erc721_symbol(
&self,
erc721: Address,
caller_address: Address,
) -> Result<String, Web3Error> {
let payload = encode_call("symbol()", &[])?;
let symbol = self
.simulate_transaction(erc721, 0u8.into(), payload, caller_address, None)
.await?;
match String::from_utf8(symbol) {
Ok(mut val) => {
val.retain(|v| !v.is_control());
let val = val.trim().to_string();
Ok(val)
}
Err(_e) => Err(Web3Error::ContractCallError(
"name is not valid utf8".to_string(),
)),
}
}
pub async fn get_erc721_supply(
&self,
erc721: Address,
caller_address: Address,
) -> Result<Uint256, Web3Error> {
let payload = encode_call("totalSupply()", &[])?;
let decimals = self
.simulate_transaction(erc721, 0u8.into(), payload, caller_address, None)
.await?;
Ok(Uint256::from_bytes_be(match decimals.get(0..32) {
Some(val) => val,
None => {
return Err(Web3Error::ContractCallError(
"Bad response from ERC721 Total Supply".to_string(),
))
}
}))
}
pub async fn get_erc721_uri(
&self,
erc721: Address,
caller_address: Address,
token_id: Uint256,
) -> Result<String, Web3Error> {
let payload = encode_call("tokenURI(uint256)", &[Token::Uint(token_id.clone())])?;
let symbol = self
.simulate_transaction(erc721, 0u8.into(), payload, caller_address, None)
.await?;
match String::from_utf8(symbol) {
Ok(mut val) => {
val.retain(|v| !v.is_control());
let val = val.trim().to_string();
Ok(val)
}
Err(_e) => Err(Web3Error::ContractCallError(
"name is not valid utf8".to_string(),
)),
}
}
pub async fn get_erc721_owner_of(
&self,
erc721: Address,
own_address: Address,
token_id: Uint256,
) -> Result<EthAddress, Web3Error> {
let payload = encode_call("ownerOf(uint256)", &[Token::Uint(token_id.clone())])?;
let val = self
.simulate_transaction(erc721, 0u8.into(), payload, own_address, None)
.await?;
let mut data: [u8; 20] = Default::default();
data.copy_from_slice(&val[12..]);
let owner_address = EthAddress::from_slice(&data);
match owner_address {
Ok(address_response) => Ok(address_response),
Err(e) => Err(Web3Error::BadResponse(e.to_string())),
}
}
}
#[test]
fn test_erc721_metadata() {
use actix::System;
let runner = System::new();
let web3 = Web3::new("https://eth.althea.net", Duration::from_secs(30));
let bayc_address = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"
.parse()
.unwrap();
let caller_address = "0x503828976D22510aad0201ac7EC88293211D23Da"
.parse()
.unwrap();
let token_id = 1039_i32;
let token_id_uint = Uint256::from_bytes_be(&token_id.to_be_bytes());
let token_id_uri = ":ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/1039";
runner.block_on(async move {
let num: Uint256 = 1000u32.into();
assert!(
web3.get_erc721_supply(bayc_address, caller_address)
.await
.unwrap()
> num
);
assert_eq!(
web3.get_erc721_symbol(bayc_address, caller_address)
.await
.unwrap(),
"BAYC"
);
assert_eq!(
web3.get_erc721_name(bayc_address, caller_address)
.await
.unwrap(),
"BoredApeYachtClub"
);
assert_eq!(
web3.get_erc721_uri(bayc_address, caller_address, token_id_uint)
.await
.unwrap(),
token_id_uri
);
})
}