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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::prelude::*;

pub mod neo {
    use crate::prelude::*;
    pub fn call_contract<T: crate::abi::VmValueEncoder>(
        contract_address: &Address, param: T,
    ) -> Vec<u8> {
        let mut builder = crate::abi::VmValueBuilder::new();
        param.serialize(&mut builder);
        crate::runtime::call_contract(contract_address, &builder.bytes())
    }
}

pub mod ontid {
    use super::super::types::u128_to_neo_bytes;
    use crate::abi::{Decoder, Encoder, Error, Sink, Source};
    use crate::macros::base58;
    use crate::prelude::*;
    use crate::runtime;

    const VERSION: u8 = 0;
    const ONTID_CONTRACT_ADDRESS: Address = base58!("AFmseVrdL9f9oyCzZefL9tG6Ubvho7BUwN");

    pub struct DDOAttribute {
        key: Vec<u8>,
        value: Vec<u8>,
        value_type: Vec<u8>,
    }
    impl Encoder for DDOAttribute {
        fn encode(&self, sink: &mut Sink) {
            sink.write(self.key.as_slice());
            sink.write(self.value.as_slice());
            sink.write(self.value_type.as_slice());
        }
    }
    impl<'a> Decoder<'a> for DDOAttribute {
        fn decode(source: &mut Source<'a>) -> Result<Self, Error> {
            let key: &[u8] = source.read().unwrap();
            let value: &[u8] = source.read().unwrap();
            let value_type: &[u8] = source.read().unwrap();
            Ok(DDOAttribute {
                key: key.to_vec(),
                value: value.to_vec(),
                value_type: value_type.to_vec(),
            })
        }
    }

    pub struct Signer {
        id: Vec<u8>,
        index: u32,
    }
    impl Encoder for Signer {
        fn encode(&self, sink: &mut Sink) {
            sink.write(self.id.as_slice());
            sink.write(self.index);
        }
    }

    impl<'a> Decoder<'a> for Signer {
        fn decode(source: &mut Source<'a>) -> Result<Self, Error> {
            let id: &[u8] = source.read().unwrap();
            let index: u32 = source.read().unwrap();
            Ok(Signer { id: id.to_vec(), index })
        }
    }

    pub struct Group {
        pub members: Vec<Vec<u8>>,
        pub threshold: u32,
    }

    impl Encoder for Group {
        fn encode(&self, sink: &mut Sink) {
            sink.write(self.members.as_slice());
            sink.write(self.threshold);
        }
    }

    impl<'a> Decoder<'a> for Group {
        fn decode(source: &mut Source<'a>) -> Result<Self, Error> {
            let members: Vec<Vec<u8>> = source.read().unwrap();
            let threshold: u32 = source.read().unwrap();
            Ok(Group { members, threshold })
        }
    }

    fn serialize_group(group: &Group) -> Vec<u8> {
        let mut sink = Sink::new(64);
        sink.write_var_bytes(u128_to_neo_bytes(U128::new(group.members.len() as u128)).as_slice());
        for mem in group.members.iter() {
            sink.write_var_bytes(mem);
        }
        sink.write_var_bytes(u128_to_neo_bytes(U128::new(group.threshold as u128)).as_slice());
        sink.bytes().to_vec()
    }

    fn serialize_signers(signers: &[Signer]) -> Vec<u8> {
        let mut sink = Sink::new(64);
        sink.write_var_bytes(u128_to_neo_bytes(U128::new(signers.len() as u128)).as_slice());
        for signer in signers.iter() {
            sink.write_var_bytes(signer.id.as_slice());
            sink.write_var_bytes(u128_to_neo_bytes(U128::new(signer.index as u128)).as_slice());
        }
        sink.bytes().to_vec()
    }

    pub fn reg_id_with_controller(ont_id: &[u8], group: &Group, signers: &[Signer]) -> bool {
        let mut sink = Sink::new(32);
        sink.write(ont_id);
        sink.write(serialize_group(group));
        sink.write(serialize_signers(signers));
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("regIDWithController");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(&ONTID_CONTRACT_ADDRESS, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }

    pub fn add_attributes_by_controller(
        ont_id: &[u8], attributes: &[DDOAttribute], signers: &[Signer],
    ) -> bool {
        let mut sink = Sink::new(32);
        sink.write(ont_id);
        sink.write_native_varuint(attributes.len() as u64);
        for attr in attributes.iter() {
            sink.write(attr.key.as_slice());
            sink.write(attr.value_type.as_slice());
            sink.write(attr.value.as_slice());
        }
        sink.write(serialize_signers(signers));
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("addAttributesByController");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(&ONTID_CONTRACT_ADDRESS, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }

    pub fn verify_signature(ont_id: &[u8], index: U128) -> bool {
        verify_sig_inner("verifySignature", ont_id, index)
    }

    fn verify_sig_inner(method: &str, ont_id: &[u8], index: U128) -> bool {
        let mut sink = Sink::new(32);
        sink.write(ont_id);
        sink.write(u128_to_neo_bytes(index));
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write(method);
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(&ONTID_CONTRACT_ADDRESS, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }
}

pub mod wasm {
    use crate::abi::Sink;
    use crate::prelude::*;
    pub fn call_contract<T: crate::abi::Encoder>(contract_address: &Address, param: T) -> Vec<u8> {
        let mut sink = Sink::new(16);
        sink.write(param);
        crate::runtime::call_contract(contract_address, sink.bytes())
    }
}

///Used when a transaction contains transfers between multiple addresses.
pub struct TransferParam {
    ///
    pub from: Address,
    pub to: Address,
    pub amount: U128,
}

///This module provides the operation API related to ont assets, such as balanceof, transfer, etc.
pub mod ont {
    use crate::macros::base58;
    use crate::prelude::*;

    const ONT_CONTRACT_ADDRESS: Address = base58!("AFmseVrdL9f9oyCzZefL9tG6UbvhUMqNMV");

    ///Transfer method of ont assets, Transfer ont assets from the from address to the to address
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::ont;
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///   let input= input();
    ///   let mut source = Source::new(&input);
    ///   let (from, to, amount) = source.read().unwrap();
    ///   ont::transfer(from,to, amount);
    /// ```
    pub fn transfer(from: &Address, to: &Address, val: U128) -> bool {
        let state = [TransferParam { from: *from, to: *to, amount: val }];
        super::util::transfer_inner(&ONT_CONTRACT_ADDRESS, state.as_ref())
    }
    ///transfer_multi method of ont assets,Multiple transfers in one transaction
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ont,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    /// # use ontio_std::types::{Address, U128};
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let trs: Vec<(Address,Address,U128)> = source.read().unwrap();
    ///     let mut ts = Vec::<TransferParam>::new();
    ///     for tr in trs.iter() {
    ///         let trans = TransferParam{
    ///            from:tr.0,
    ///            to:tr.1,
    ///            amount:tr.2,
    ///         };
    ///         ts.push(trans)
    ///     }
    ///     ont::transfer_multi(ts.as_slice());
    /// ```
    pub fn transfer_multi(transfer: &[TransferParam]) -> bool {
        super::util::transfer_inner(&ONT_CONTRACT_ADDRESS, transfer)
    }
    ///from-address can allow to-address to transfer a certain amount of assets from  from-address.
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ont,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///   let input = input();
    ///   let mut source = Source::new(&input);
    ///   let (from,to,amount) = source.read().unwrap();
    ///   ont::approve(from, to, amount);
    /// ```
    pub fn approve(from: &Address, to: &Address, amount: U128) -> bool {
        super::util::approve_inner(&ONT_CONTRACT_ADDRESS, from, to, amount)
    }
    ///Query the balance of ont assets
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ont,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let addr = source.read().unwrap();
    ///     ont::balance_of(addr);
    /// ```
    pub fn balance_of(address: &Address) -> U128 {
        super::util::balance_of_inner(&ONT_CONTRACT_ADDRESS, &address)
    }
    ///This method is used in conjunction with the approve method to query the number of approve
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ont,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///   let input= input();
    ///   let mut source = Source::new(&input);
    ///   let (from, to) = source.read().unwrap();
    ///   ont::allowance(from,to);
    /// ```
    pub fn allowance(from: &Address, to: &Address) -> U128 {
        super::util::allowance_inner(&ONT_CONTRACT_ADDRESS, from, to)
    }
    ///Spender transfers a certain amount of ont from from-address to to-address
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ont,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///   let input= input();
    ///   let mut source = Source::new(&input);
    ///   let (spender, from, to, amount) = source.read().unwrap();
    ///   ont::transfer_from(spender, from, to, amount);
    /// ```
    pub fn transfer_from(sender: &Address, from: &Address, to: &Address, amount: U128) -> bool {
        super::util::transfer_from_inner(&ONT_CONTRACT_ADDRESS, sender, from, to, amount)
    }
}

///This module provides the operation API related to ong assets, such as balanceof, transfer, etc.
pub mod ong {
    use crate::prelude::*;

    use crate::macros::base58;
    use crate::types::{Address, U128};

    const ONG_CONTRACT_ADDRESS: Address = base58!("AFmseVrdL9f9oyCzZefL9tG6UbvhfRZMHJ");

    ///Transfer method of ong assets, Transfer ont assets from the from address to the to address
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::ong;
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///   let input = input();
    ///   let mut source = Source::new(&input);
    ///   let (from, to, amount) = source.read().unwrap();
    ///   ong::transfer(from,to, amount);
    /// ```
    pub fn transfer(from: &Address, to: &Address, val: U128) -> bool {
        let state = [TransferParam { from: *from, to: *to, amount: val }];
        super::util::transfer_inner(&ONG_CONTRACT_ADDRESS, state.as_ref())
    }
    ///transfer_multi method of ong assets,Multiple transfers in one transaction
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ong,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    /// # use ontio_std::types::{Address,U128};
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let trs: Vec<(Address,Address,U128)> = source.read().unwrap();
    ///     let mut transfers = Vec::<TransferParam>::new();
    ///     for tr in trs.iter() {
    ///         transfers.push(TransferParam{
    ///             from:tr.0,
    ///             to:tr.1,
    ///             amount:tr.2,
    ///         })
    ///     }
    ///     ong::transfer_multi(transfers.as_slice());
    /// ```
    pub fn transfer_multi(transfer: &[super::TransferParam]) -> bool {
        super::util::transfer_inner(&ONG_CONTRACT_ADDRESS, transfer)
    }
    ///Query the balance of ong assets
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ong,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let addr = source.read().unwrap();
    ///     ong::balance_of(addr);
    /// ```
    pub fn balance_of(address: &Address) -> U128 {
        super::util::balance_of_inner(&ONG_CONTRACT_ADDRESS, &address)
    }
    ///from-address can allow to-address to transfer a certain amount of assets from  from-address.
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ong,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let (from,to,amount) = source.read().unwrap();
    ///     ong::approve(from, to, amount);
    /// ```
    pub fn approve(from: &Address, to: &Address, amount: U128) -> bool {
        super::util::approve_inner(&ONG_CONTRACT_ADDRESS, from, to, amount)
    }
    ///This method is used in conjunction with the approve method to query the number of approve
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ong};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let (from, to) = source.read().unwrap();
    ///     ong::allowance(from,to);
    /// ```
    pub fn allowance(from: &Address, to: &Address) -> U128 {
        super::util::allowance_inner(&ONG_CONTRACT_ADDRESS, from, to)
    }
    ///Spender transfers a certain amount of ong from from-address to to-address
    /// # Example
    /// ```no_run
    /// # use ontio_std::contract::{ong,TransferParam};
    /// # use ontio_std::abi::{Sink, Source};
    /// # use ontio_std::runtime::input;
    ///     let input = input();
    ///     let mut source = Source::new(&input);
    ///     let (spender, from, to, amount) = source.read().unwrap();
    ///     ong::transfer_from(spender, from, to, amount);
    /// ```
    pub fn transfer_from(sender: &Address, from: &Address, to: &Address, amount: U128) -> bool {
        super::util::transfer_from_inner(&ONG_CONTRACT_ADDRESS, sender, from, to, amount)
    }
}

pub(crate) mod util {
    use super::super::abi::Sink;
    use super::super::runtime;
    use super::super::types::{u128_from_neo_bytes, u128_to_neo_bytes, Address, U128};

    const VERSION: u8 = 0;
    pub(crate) fn transfer_inner(
        contract_address: &Address, transfer: &[super::TransferParam],
    ) -> bool {
        let mut sink = Sink::new(64);
        sink.write_native_varuint(transfer.len() as u64);

        for state in transfer.iter() {
            sink.write_native_address(&state.from);
            sink.write_native_address(&state.to);
            sink.write(u128_to_neo_bytes(state.amount));
        }
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("transfer");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(contract_address, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }

    pub(crate) fn approve_inner(
        contract_address: &Address, from: &Address, to: &Address, amount: U128,
    ) -> bool {
        let mut sink = Sink::new(64);
        sink.write_native_address(from);
        sink.write_native_address(to);
        sink.write(u128_to_neo_bytes(amount));
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("approve");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(contract_address, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }

    pub(crate) fn transfer_from_inner(
        contract_address: &Address, sender: &Address, from: &Address, to: &Address, amount: U128,
    ) -> bool {
        let mut sink = Sink::new(64);
        sink.write_native_address(sender);
        sink.write_native_address(from);
        sink.write_native_address(to);
        sink.write(u128_to_neo_bytes(amount));
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("transferFrom");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(contract_address, sink_param.bytes());

        !output.is_empty() && output[0] == 1u8
    }

    pub(crate) fn allowance_inner(
        contract_address: &Address, from: &Address, to: &Address,
    ) -> U128 {
        let mut sink = Sink::new(64);
        sink.write_native_address(from);
        sink.write_native_address(to);
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("allowance");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(contract_address, sink_param.bytes());
        if !output.is_empty() {
            return u128_from_neo_bytes(&output);
        }
        U128::new(0)
    }

    pub(crate) fn balance_of_inner(contract_address: &Address, address: &Address) -> U128 {
        let mut sink = Sink::new(64);
        sink.write_native_address(address);
        let mut sink_param = Sink::new(64);
        sink_param.write(VERSION);
        sink_param.write("balanceOf");
        sink_param.write(sink.bytes());
        let output = runtime::call_contract(contract_address, sink_param.bytes());
        if !output.is_empty() {
            return u128_from_neo_bytes(&output);
        }
        U128::new(0)
    }
}