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
#[cfg(target_arch = "wasm32")]
use std::convert::TryInto;
#[cfg(target_arch = "wasm32")]
use std::str::FromStr;
use crate::primitives::Contract;
#[cfg(target_arch = "wasm32")]
use crate::types::Address;
#[cfg(target_arch = "wasm32")]
use crate::types::Raw;
use sewup_derive::ewasm_lib_fn;
pub use super::erc20::{
balance_of, name, symbol, BALANCE_OF_ABI, BALANCE_OF_SIG, NAME_ABI, NAME_SIG, SYMBOL_ABI,
SYMBOL_SIG,
};
#[cfg(target_arch = "wasm32")]
use super::helpers::{
copy_into_address, copy_into_storage_value, get_approval, get_balance, get_token_approval,
get_token_owner, set_approval, set_balance, set_token_approval, set_token_owner,
};
#[cfg(target_arch = "wasm32")]
use crate::utils::{caller, ewasm_return_bool};
#[cfg(target_arch = "wasm32")]
use bitcoin::util::uint::Uint256;
#[cfg(target_arch = "wasm32")]
use hex::decode;
#[cfg(target_arch = "wasm32")]
use ewasm_api::{log3, log4};
#[ewasm_lib_fn("6352211e",
constant=true,
inputs=[{ "name": "_tokenId", "type": "uint256" }],
outputs=[{ "name": "_owner", "type": "address" }]
)]
pub fn owner_of(contract: &Contract) {
let token_id: [u8; 32] = contract.input_data[4..36]
.try_into()
.expect("token id should be byte32");
let owner = get_token_owner(&token_id);
ewasm_api::finish_data(&Raw::from(owner).as_bytes().to_vec());
}
#[cfg(target_arch = "wasm32")]
fn do_transfer(owner: Address, to: Address, token_id: [u8; 32]) {
let mut balance = get_balance(&owner);
let mut value = Uint256::from_be_bytes(balance.bytes)
- Uint256::from_u64(1u64).expect("uint256 one should valid");
let mut buffer = value.to_be_bytes();
set_balance(&owner, ©_into_storage_value(&buffer));
balance = get_balance(&to);
value = Uint256::from_be_bytes(balance.bytes)
+ Uint256::from_u64(1u64).expect("uint256 one should valid");
buffer = value.to_be_bytes();
set_balance(&to, ©_into_storage_value(&buffer));
set_token_owner(&token_id, &to);
set_token_approval(&token_id, &to);
let topic: [u8; 32] =
decode("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
.unwrap()
.try_into()
.unwrap();
log4(
&Vec::<u8>::with_capacity(0),
&topic.into(),
&Raw::from(owner).to_bytes32().into(),
&Raw::from(to).to_bytes32().into(),
&token_id.into(),
);
}
#[ewasm_lib_fn("a9059cbb",
inputs=[
{ "name": "_to", "type": "address" },
{ "name": "_tokenId", "type": "uint256" }
],
name=transfer,
stateMutability=nonpayable
)]
pub fn transfer(contract: &Contract) {
let to = copy_into_address(&contract.input_data[16..36]);
let token_id: [u8; 32] = contract.input_data[36..68]
.try_into()
.expect("token id should be byte32");
let sender = caller();
let owner = get_token_owner(&token_id);
if owner != sender {
ewasm_api::revert();
}
do_transfer(owner.into(), to, token_id);
}
#[ewasm_lib_fn("23b872dd",
inputs=[
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_tokenId", "type": "uint256" }
],
stateMutability=nonpayable
)]
pub fn transfer_from(contract: &Contract) {
let sender = caller();
let owner = copy_into_address(&contract.input_data[16..36]);
let to = copy_into_address(&contract.input_data[48..68]);
let token_id = contract.input_data[68..100].try_into().unwrap();
if sender != get_token_approval(&token_id) && !get_approval(&owner, &sender) {
ewasm_api::revert();
}
do_transfer(owner.into(), to, token_id);
}
#[ewasm_lib_fn("095ea7b3",
inputs=[
{ "name": "_to", "type": "address" },
{ "name": "_tokenId", "type": "uint256" }
],
stateMutability=nonpayable
)]
pub fn approve(contract: &Contract) {
let sender = caller();
let spender = copy_into_address(&contract.input_data[16..36]);
let token_id: [u8; 32] = contract.input_data[36..68]
.try_into()
.expect("token id should be byte32");
set_token_approval(&token_id, &spender);
let topic: [u8; 32] =
decode("8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925")
.unwrap()
.try_into()
.unwrap();
log4(
&Vec::<u8>::with_capacity(0),
&topic.into(),
&Raw::from(sender).to_bytes32().into(),
&Raw::from(spender).to_bytes32().into(),
&token_id.into(),
);
}
#[ewasm_lib_fn("081812fc",
inputs=[ { "name": "_tokenId", "type": "uint256" } ],
outputs=[{ "name": "_owner", "type": "address" }],
stateMutability=nonpayable
)]
pub fn get_approved(contract: &Contract) {
let token_id: [u8; 32] = contract.input_data[4..36]
.try_into()
.expect("token id should be byte32");
let spender = get_token_approval(&token_id);
ewasm_api::finish_data(&Raw::from(spender).as_bytes().to_vec());
}
#[ewasm_lib_fn("a22cb465",
inputs=[
{ "name": "_operator", "type": "address" },
{ "name": "_approved", "type": "bool" }
],
name=setApprovalForAll,
stateMutability=nonpayable
)]
pub fn set_approval_for_all(contract: &Contract) {
let sender = caller();
let operator = copy_into_address(&contract.input_data[16..36]);
let is_approved = contract.input_data[67] == 1;
set_approval(&sender, &operator, is_approved);
let topic: [u8; 32] =
decode("17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31")
.unwrap()
.try_into()
.unwrap();
log3(
&Vec::<u8>::with_capacity(0),
&topic.into(),
&Raw::from(sender).to_bytes32().into(),
&Raw::from(operator).to_bytes32().into(),
);
}
#[ewasm_lib_fn("e985e9c5",
inputs=[
{ "name": "owner", "type": "address" },
{ "name": "operator", "type": "address" }
],
outputs=[{ "name": "_approved", "type": "bool" }],
stateMutability=nonpayable
)]
pub fn is_approved_for_all(contract: &Contract) {
let owner = copy_into_address(&contract.input_data[16..36]);
let operator = copy_into_address(&contract.input_data[48..68]);
ewasm_return_bool(get_approval(&owner, &operator));
}
#[ewasm_lib_fn("6914db60",
constant=true,
inputs=[ { "name": "_tokenId", "type": "uint256" } ],
outputs=[ { "name": "_infoUrl", "type": "string" } ]
)]
pub fn token_metadata(_contract: &Contract) {
}
#[ewasm_lib_fn("b88d4fde")]
pub fn safe_transfer_from_with_data(_contract: &Contract) {
}
#[ewasm_lib_fn("42842e0e",
inputs=[
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_tokenId", "type": "uint256" }
],
stateMutability=nonpayable
)]
pub fn safe_transfer_from(_contract: &Contract) {
}
#[cfg(target_arch = "wasm32")]
pub fn mint(addr: &str, tokens: Vec<&str>) {
let address = Address::from_str(addr).expect("address invalid");
let topic: [u8; 32] =
decode("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
.unwrap()
.try_into()
.unwrap();
for token in tokens.iter() {
let token_id: [u8; 32] = decode(token)
.expect("token id should be hex format")
.try_into()
.expect("token id should be byte32");
set_token_owner(&token_id, &address);
log4(
&Vec::<u8>::with_capacity(0),
&topic.into(),
&Raw::from(0u32).to_bytes32().into(),
&Raw::from(&address).to_bytes32().into(),
&token_id.into(),
);
}
set_balance(&address, &Raw::from(tokens.len()).to_bytes32().into());
}