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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! marketplace contract
//!
//! Seller publishing process
//!
//! seller only need invoke [`dtoken_seller_publish`](fn.dtoken_seller_publish.html) method to publish products
//!
//! Buyer purchase and use process
//!
//! first of all, buyers should invoke [`buy_dtoken`](fn.buy_dtoken.html) method to buy the released products
//!
//! Second, buyer invoke the [`use_token`](fn.use_token.html) method to consume token

#![cfg_attr(not(feature = "mock"), no_std)]
#![feature(proc_macro_hygiene)]
extern crate alloc;
extern crate common;
extern crate ontio_std as ostd;
use ostd::abi::{EventBuilder, Sink, Source};
use ostd::database;
use ostd::prelude::*;
use ostd::runtime;
use ostd::types::{Address, U128};
mod basic;
use basic::*;
mod dtoken;
mod split_policy;
use common::*;
use dtoken::*;
use ostd::contract::wasm;
use ostd::runtime::{check_witness, current_txhash};

#[cfg(test)]
mod test;

const KEY_SELLER_ITEM_INFO: &[u8] = b"01";
const KEY_DTOKEN_CONTRACT: &[u8] = b"03";
const KEY_SPLIT_POLICY_CONTRACT: &[u8] = b"04";
const KEY_ADMIN: &[u8] = b"05";

//AbtTQJYKfQxq4UdygDsbLVjE8uRrJ2H3tP
//AYnhakv7kC9R5ppw65JoE2rt6xDzCjCTvD

const DEFAULT_SPLIT_CONTRACT: Address = ostd::macros::base58!("ANzKSQWm7gLvJGrnMok2hoLQAoiLmuC5wq");
const DEFAULT_DTOKEN_CONTRACT: Address =
    ostd::macros::base58!("Aeth97vqFY51c5SziJWPT7zbU6ExtFXcKs");
// dtoken testnet Aeth97vqFY51c5SziJWPT7zbU6ExtFXcKs
// dtoken mainnet AQJzHbcT9pti1zzV2cRZ92B1i1z8QNN2n6

/// set dtoken contract address as the default dtoken contract address,
/// marketplace contract will invoke dtoken contract to pay the fee
/// need admin signature
/// `new_addr` is the new dtoken contract address
pub fn set_dtoken_contract(new_addr: &Address) -> bool {
    assert!(check_witness(CONTRACT_COMMON.admin()));
    database::put(KEY_DTOKEN_CONTRACT, new_addr);
    true
}

/// init contract
/// set dtoken and split contract address
pub fn init(dtoken: &Address, split_policy: &Address) -> bool {
    assert!(check_witness(CONTRACT_COMMON.admin()));
    database::put(KEY_DTOKEN_CONTRACT, dtoken);
    database::put(KEY_SPLIT_POLICY_CONTRACT, split_policy);
    true
}

/// query the default dtoken contract address
pub fn get_dtoken_contract() -> Address {
    database::get::<_, Address>(KEY_DTOKEN_CONTRACT).unwrap_or(DEFAULT_DTOKEN_CONTRACT)
}

/// set split contract address as the default split contract address,
///
/// When there are multiple data owners, split contract is used to set the income distribution strategy.
///
/// need admin signature
pub fn set_split_policy_contract(new_addr: &Address) -> bool {
    assert!(check_witness(CONTRACT_COMMON.admin()));
    database::put(KEY_SPLIT_POLICY_CONTRACT, new_addr);
    true
}

/// query the default split contract address
fn get_split_policy_contract() -> Address {
    database::get::<_, Address>(KEY_SPLIT_POLICY_CONTRACT).unwrap_or(DEFAULT_SPLIT_CONTRACT)
}

/// need old admin signature
///
/// update the admin address, admin has the right to set the default dtoken and split contract address
fn update_admin(new_admin: &Address) -> bool {
    let old_admin = get_admin();
    assert!(check_witness(&old_admin));
    database::put(KEY_ADMIN, new_admin);
    true
}

/// query admin address
fn get_admin() -> Address {
    database::get::<_, Address>(KEY_ADMIN).unwrap_or(*CONTRACT_COMMON.admin())
}

/// seller publish product, need seller signature
///
/// `resource_id` used to mark the only commodity in the chain
///
/// `resource_ddo_bytes` is the result of ResourceDDO struct serialization
///
/// `item_bytes` is the result of DTokenItem struct serialization
///
/// `split_policy_param_bytes` is the result of RegisterParam struct serialization
///
/// # Example
/// ```no_run
/// use common::{Fee,TokenType};
/// let resource_id = b"resource_id";
/// let ddo = ResourceDDO {
///        token_resource_ty_endpoints: vec![],
///        item_meta_hash: H256::repeat_byte(1),
///        manager: manager.clone(),
///        dtoken_contract_address: Some(dtoken_contract_address.clone()),
///        mp_contract_address: None,
///        split_policy_contract_address: None,
///    };
/// let contract_addr = Address::repeat_byte(4);
///    let fee = Fee {
///        contract_addr,
///        contract_type: TokenType::ONG,
///        count: 0,
///    };
///    let mut templates = vec![];
///    templates.push(token_template.clone());
///    let dtoken_item = DTokenItem {
///        fee,
///        expired_date: 1,
///        stocks: 1,
///        templates,
///    };
///  let split_param = b"test";
///  assert!(supper::dtoken_seller_publish(
///        resource_id,
///        &ddo.to_bytes(),
///        &dtoken_item.to_bytes(),
///        split_param
///    ));
/// ```
pub fn dtoken_seller_publish(
    resource_id: &[u8],
    resource_ddo: ResourceDDO,
    item: DTokenItem,
    split_policy_param: &[u8],
) -> bool {
    dtoken_seller_publish_inner(resource_id, resource_ddo, item, split_policy_param, true)
}

pub fn dtoken_seller_publish_inner(
    item_id: &[u8],
    resource_ddo: ResourceDDO,
    item: DTokenItem,
    split_policy_param_bytes: &[u8],
    is_publish: bool,
) -> bool {
    let admin = get_admin();
    assert!(runtime::check_witness(&resource_ddo.manager) && runtime::check_witness(&admin));
    let resource =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(item_id));
    if is_publish {
        assert!(resource.is_none());
    } else {
        assert!(resource.is_some());
    }
    assert_ne!(item.token_template_ids.len(), 0);

    //verify token_template_id creator sig
    // authorize mp address
    verify_auth(
        &resource_ddo.dtoken_contract_address,
        item.token_template_ids.as_slice(),
    );

    let seller = SellerItemInfo::new(item.clone(), resource_ddo.clone());
    database::put(utils::generate_seller_item_info_key(item_id), seller);

    //invoke split_policy contract
    let split_addr = get_split_policy_contract();
    assert!(split_policy::register(
        &resource_ddo
            .split_policy_contract_address
            .unwrap_or(split_addr),
        item_id,
        split_policy_param_bytes
    ));

    //event
    let mut method = "dtokenSellerPublish";
    if !is_publish {
        method = "update"
    }
    EventBuilder::new()
        .string(method)
        .bytearray(item_id)
        .bytearray(resource_ddo.to_bytes().as_slice())
        .bytearray(item.to_bytes().as_slice())
        .notify();
    true
}

pub fn update(
    resource_id: &[u8],
    resource_ddo: ResourceDDO,
    item: DTokenItem,
    split_policy_param_bytes: &[u8],
) -> bool {
    dtoken_seller_publish_inner(
        resource_id,
        resource_ddo,
        item,
        split_policy_param_bytes,
        false,
    )
}

pub fn delete(resource_id: &[u8]) -> bool {
    let item_info =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(resource_id))
            .unwrap();
    let admin = get_admin();
    assert!(check_witness(&item_info.resource_ddo.manager) || check_witness(&admin));
    database::delete(utils::generate_seller_item_info_key(resource_id));
    EventBuilder::new()
        .string("delete")
        .bytearray(resource_id)
        .notify();
    true
}

pub fn get_seller_item_info(resource_id: &[u8]) -> Vec<u8> {
    let r = runtime::storage_read(utils::generate_seller_item_info_key(resource_id).as_slice())
        .map(|val: Vec<u8>| val);
    if let Some(rr) = r {
        rr
    } else {
        vec![]
    }
}

/// buy dtoken from reseller
///
/// The seller can sell what he bought before he used it
///
/// `resource_id` used to mark the only commodity in the chain
///
/// `n` is the number of purchases
///
/// `buyer_account` is buyer address, need this address signature
///
/// `reseller_account` is reseller address, need this address signature
pub fn buy_dtoken_from_reseller(
    resource_id: &[u8],
    n: U128,
    buyer_account: &Address,
    reseller_account: &Address,
) -> bool {
    assert!(runtime::check_witness(buyer_account) && runtime::check_witness(reseller_account));
    let item_info =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(resource_id))
            .unwrap();
    let oi = OrderId {
        item_id: resource_id.to_vec(),
        tx_hash: current_txhash(),
    };
    let split_contract = get_split_policy_contract();
    assert!(transfer_fee(
        &oi,
        buyer_account,
        item_info.resource_ddo.accountant_contract_address,
        &item_info
            .resource_ddo
            .split_policy_contract_address
            .unwrap_or(split_contract),
        item_info.item.fee.clone(),
        n
    ));

    transfer_dtoken(
        &item_info.resource_ddo.dtoken_contract_address,
        item_info.item.token_template_ids.as_slice(),
        reseller_account,
        buyer_account,
        n,
    );
    EventBuilder::new()
        .string("buyDTokenFromReseller")
        .bytearray(resource_id)
        .number(n)
        .address(buyer_account)
        .address(reseller_account)
        .notify();
    true
}

/// Buy more than one dtoken at a time
///
/// `resource_ids` is array of resource_id which used to mark the only commodity in the chain
///
/// `ns` is array of n which is the number of purchases. the length of resource_ids must be the same with the length of ns.
///
/// `buyer_account` is buyer address, need this address signature
pub fn buy_dtokens(
    resource_ids: Vec<Vec<u8>>,
    ns: Vec<U128>,
    buyer_account: &Address,
    payer: &Address,
) -> Vec<Vec<Vec<u8>>> {
    let l = resource_ids.len();
    assert_eq!(l, ns.len());
    (0..l)
        .map(|i| buy_dtoken(resource_ids[i].as_slice(), ns[i], buyer_account, payer))
        .collect::<Vec<Vec<Vec<u8>>>>()
}

fn get_token_template_ids(resource_id: &[u8]) -> Vec<Vec<u8>> {
    let item_info =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(resource_id))
            .unwrap();
    return item_info.item.token_template_ids;
}

/// buy dtoken
///
/// `resource_id` used to mark the only commodity in the chain
///
/// `n` is the number of purchases
///
/// `buyer_account` is buyer address, need this address signature
pub fn buy_dtoken(
    resource_id: &[u8],
    n: U128,
    buyer_account: &Address,
    payer: &Address,
) -> Vec<Vec<u8>> {
    assert!(runtime::check_witness(buyer_account) && runtime::check_witness(payer));
    let mut item_info =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(resource_id))
            .unwrap();
    let now = runtime::timestamp();
    assert!(now <= item_info.item.expired_date);
    assert!(item_info.item.sold <= item_info.item.stocks);
    item_info.item.sold = n.checked_add(item_info.item.sold as U128).unwrap() as u64;
    assert!(item_info.item.sold <= item_info.item.stocks);
    let oi = OrderId {
        item_id: resource_id.to_vec(),
        tx_hash: current_txhash(),
    };
    assert!(transfer_fee(
        &oi,
        payer,
        item_info.resource_ddo.accountant_contract_address.clone(),
        &item_info
            .resource_ddo
            .split_policy_contract_address
            .unwrap_or(get_split_policy_contract()),
        item_info.item.fee.clone(),
        n
    ));
    database::put(
        utils::generate_seller_item_info_key(resource_id),
        &item_info,
    );
    //TODO
    let token_ids = generate_dtoken(
        &item_info.resource_ddo.dtoken_contract_address,
        item_info.item.token_template_ids.as_slice(),
        buyer_account,
        n,
    );
    EventBuilder::new()
        .string("buyDToken")
        .bytearray(resource_id)
        .number(n)
        .address(buyer_account)
        .address(payer)
        .notify();
    token_ids
}

/// buy_dtoken_reward
///
/// This method can only be called for items that the fee.count is 0, The buyer can reward the seller with any number of tokens.
///
/// `resource_id` used to mark the only commodity in the chain
///
/// `n` is the number of purchases
///
/// `buyer_account` is buyer address, need this address signature
///
/// `payer` is the address who pay the fee
///
/// `unit_price` unit price the buyer is willing to pay
///
pub fn buy_dtoken_reward(
    resource_id: &[u8],
    n: U128,
    buyer_account: &Address,
    payer: &Address,
    unit_price: U128,
) -> Vec<Vec<u8>> {
    assert!(runtime::check_witness(buyer_account) && runtime::check_witness(payer));
    let mut item_info =
        database::get::<_, SellerItemInfo>(utils::generate_seller_item_info_key(resource_id))
            .unwrap();
    assert!(item_info.item.fee.count == 0);
    let now = runtime::timestamp();
    assert!(now < item_info.item.expired_date);

    assert!(item_info.item.sold < item_info.item.stocks);
    item_info.item.sold = n.checked_add(item_info.item.sold as U128).unwrap() as u64;
    assert!(item_info.item.sold <= item_info.item.stocks);
    let oi = OrderId {
        item_id: resource_id.to_vec(),
        tx_hash: current_txhash(),
    };
    let mut fee = item_info.item.fee.clone();
    fee.count = unit_price as u64;
    assert!(transfer_fee(
        &oi,
        payer,
        item_info.resource_ddo.accountant_contract_address.clone(),
        &item_info
            .resource_ddo
            .split_policy_contract_address
            .unwrap_or(get_split_policy_contract()),
        fee,
        n
    ));
    database::put(
        utils::generate_seller_item_info_key(resource_id),
        &item_info,
    );
    let res = generate_dtoken(
        &item_info.resource_ddo.dtoken_contract_address,
        item_info.item.token_template_ids.as_slice(),
        buyer_account,
        n,
    );
    EventBuilder::new()
        .string("buyDTokenReward")
        .bytearray(resource_id)
        .number(n)
        .address(buyer_account)
        .address(payer)
        .number(unit_price)
        .notify();
    res
}

// inner method
fn transfer_fee(
    oi: &OrderId,
    payer: &Address,
    accountant_contract_address: Option<Address>,
    split_contract_address: &Address,
    fee: Fee,
    n: U128,
) -> bool {
    let res = match accountant_contract_address {
        Some(accountant_addr) => wasm::call_contract(
            &accountant_addr,
            (
                "transferAmount",
                (oi.to_bytes(), payer, split_contract_address, fee, n),
            ),
        ),
        _ => {
            let amt = n.checked_mul(fee.count as U128).unwrap();
            wasm::call_contract(
                split_contract_address,
                ("transferWithdraw", (payer, oi.item_id.as_slice(), amt)),
            )
        }
    };
    verify_result(res);
    true
}

fn verify_result(res: Option<Vec<u8>>) {
    if let Some(r) = res {
        let mut source = Source::new(r.as_slice());
        let r: bool = source.read().unwrap();
        assert!(r);
    } else {
        panic!("call contract failed")
    }
}

#[no_mangle]
pub fn invoke() {
    let input = runtime::input();
    let mut source = Source::new(&input);
    let action: &[u8] = source.read().unwrap();
    let mut sink = Sink::new(12);
    match action {
        b"updateAdmin" => {
            let new_admin = source.read().unwrap();
            sink.write(update_admin(&new_admin));
        }
        b"getAdmin" => {
            sink.write(get_admin());
        }
        b"init" => {
            let (dtoken, split_policy) = source.read().unwrap();
            sink.write(init(dtoken, split_policy));
        }
        b"setSplitPolicyContract" => {
            let new_addr = source.read().unwrap();
            sink.write(set_split_policy_contract(new_addr));
        }
        b"getSplitPolicyContract" => sink.write(get_split_policy_contract()),
        b"setDTokenContract" => {
            let new_addr = source.read().unwrap();
            sink.write(set_dtoken_contract(&new_addr));
        }
        b"getDTokenContract" => {
            sink.write(get_dtoken_contract());
        }
        b"migrate" => {
            let (code, vm_type, name, version, author, email, desc) = source.read().unwrap();
            sink.write(CONTRACT_COMMON.migrate(code, vm_type, name, version, author, email, desc));
        }
        b"update" => {
            let (resource_id, resource_ddo, item, split_policy_param_bytes) =
                source.read().unwrap();
            sink.write(update(
                resource_id,
                resource_ddo,
                item,
                split_policy_param_bytes,
            ));
        }
        b"delete" => {
            let resource_id = source.read().unwrap();
            sink.write(delete(resource_id));
        }
        b"dtokenSellerPublish" => {
            let (resource_id, resource_ddo, item, split_policy_param_bytes) =
                source.read().unwrap();
            sink.write(dtoken_seller_publish(
                resource_id,
                resource_ddo,
                item,
                split_policy_param_bytes,
            ));
        }
        b"getSellerItemInfo" => {
            let resource_id = source.read().unwrap();
            sink.write(get_seller_item_info(resource_id))
        }
        b"buyDTokenFromReseller" => {
            let (resource_id, n, buyer_account, reseller_account) = source.read().unwrap();
            sink.write(buy_dtoken_from_reseller(
                resource_id,
                n,
                buyer_account,
                reseller_account,
            ));
        }
        b"buyDTokens" => {
            let (resource_ids, ns, buyer, payer) = source.read().unwrap();
            sink.write(buy_dtokens(resource_ids, ns, buyer, payer));
        }
        b"buyDToken" => {
            let (resource_id, n, buyer_account, payer) = source.read().unwrap();
            sink.write(buy_dtoken(resource_id, n, buyer_account, payer));
        }
        b"buyDTokenReward" => {
            let (resource_id, n, buyer_account, payer, unit_price) = source.read().unwrap();
            sink.write(buy_dtoken_reward(
                resource_id,
                n,
                buyer_account,
                payer,
                unit_price,
            ));
        }
        b"getTokenTemplates" => {
            let resource_id = source.read().unwrap();
            sink.write(get_token_template_ids(resource_id));
        }
        _ => {
            let method = str::from_utf8(action).ok().unwrap();
            panic!("marketplace contract, not support method:{}", method)
        }
    }
    runtime::ret(sink.bytes());
}

mod events {
    use super::*;
    use ostd::macros::event;
    #[event(dtokenSellerPublishEvent)]
    pub fn dtoken_seller_publish_event(resource_id: &[u8], resource_ddo: &[u8], item: &[u8]) {}
    #[event(buyDtokenFromReseller)]
    pub fn buy_dtoken_from_reseller(
        resource_id: &str,
        n: U128,
        buyer_account: &Address,
        reseller_account: &Address,
    ) {
    }
    #[event(buyDtoken)]
    pub fn buy_dtoken(resource_id: &[u8], n: U128, buyer_account: &Address) {}
    #[event(useToken)]
    pub fn use_token(resource_id: &[u8], account: &Address, token_template_bytes: &[u8], n: U128) {}
    #[event(useTokenByAgent)]
    pub fn use_token_by_agent(
        resource_id: &[u8],
        account: &Address,
        agent: &Address,
        token_template_bytes: &[u8],
        n: U128,
    ) {
    }
    #[event(setDtokenAgents)]
    pub fn set_agents(resource_id: &[u8], account: &Address, n: U128) {}
}

mod utils {
    use super::*;
    use alloc::vec::Vec;
    pub fn generate_seller_item_info_key(resource_id: &[u8]) -> Vec<u8> {
        [KEY_SELLER_ITEM_INFO, resource_id].concat()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}