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
use alloc::{
    format,
    string::{String, ToString},
    vec::Vec,
};
use casper_contract::contract_api::storage;
use casper_types::{account::AccountHash, URef, U512};

use crate::{constants, ndpc_utils::contract_package_hash};
/// Droplinked Events, Including Mint(Record), PublishRequest, ApprovedPublish, DisapprovedPublish, CancelRequest, Buy, and Payment
pub enum DropLinkedEvent {
    Mint {
        recipient: AccountHash,
        token_id: u64,
        holder_id: u64,
        amount: u64,
        comission: u64,
        price: u64,
    },
    PublishRequest {
        owner: AccountHash,
        publisher: AccountHash,
        amount: u64,
        holder_id: u64,
        request_id: u64,
    },
    ApprovedPublish {
        request_id: u64,
        approved_id: u64,
    },
    DisapprovedPublish {
        approved_id: u64,
    },
    CancelRequest {
        request_id: u64,
    },
    Buy {
        amount: u64,
        approved_id: u64,
        buyer: AccountHash,
    },
    Payment {
        recipient: String,
        amounts: Vec<U512>,
    },
}

/// Emits the given event into the urefs that contract creates, and would be detected by droplinked's Qserver when the transaction is done
/// 
/// It gets a DroplinkedEvent, and depending on its type, puts some key-value variables into the urefs that contract creates, these then 
/// woule be detected by droplinked's Qserver (using the contractpackagehash on each dictionary)
pub fn emit(event: DropLinkedEvent) {
    let mut events = Vec::new();
    let package = contract_package_hash();
    match event {
        DropLinkedEvent::Mint {
            recipient,
            token_id,
            holder_id,
            amount,
            comission,
            price,
        } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_mint".to_string());
            param.insert("recipient", recipient.to_string());
            param.insert("token_id", token_id.to_string());
            param.insert("holder_id", holder_id.to_string());
            param.insert("amount", amount.to_string());
            param.insert("comission", comission.to_string());
            param.insert("price", price.to_string());
            events.push(param);
        }
        DropLinkedEvent::PublishRequest {
            owner,
            publisher,
            amount,
            holder_id,
            request_id,
        } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_publish_request".to_string());
            param.insert("owner", owner.to_string());
            param.insert("publisher", publisher.to_string());
            param.insert("amount", amount.to_string());
            param.insert("holder_id", holder_id.to_string());
            param.insert("request_id", request_id.to_string());
            events.push(param);
        }
        DropLinkedEvent::DisapprovedPublish { approved_id } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_disapproved_publish".to_string());
            param.insert("approved_id", approved_id.to_string());
            events.push(param);
        }
        DropLinkedEvent::CancelRequest { request_id } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_cancel_request".to_string());
            param.insert("request_id", request_id.to_string());
            events.push(param);
        }
        DropLinkedEvent::ApprovedPublish {
            request_id,
            approved_id,
        } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_approved_publish".to_string());
            param.insert("request_id", request_id.to_string());
            param.insert("approved_id", approved_id.to_string());
            events.push(param);
        }
        DropLinkedEvent::Buy {
            amount,
            approved_id,
            buyer,
        } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_buy".to_string());
            param.insert("amount", amount.to_string());
            param.insert("approved_id", approved_id.to_string());
            param.insert("buyer", buyer.to_string());
            events.push(param);
        }
        DropLinkedEvent::Payment { recipient, amounts } => {
            let mut param = alloc::collections::BTreeMap::new();
            param.insert(constants::CONTRACTPACKAGEHASH, package.to_string());
            param.insert("event_type", "droplinked_direct_pay".to_string());
            param.insert(
                "amounts",
                format!("{},{},{}", amounts[0], amounts[1], amounts[2]),
            );
            param.insert("recipient", recipient);
            events.push(param);
        }
    }
    for param in events {
        let _: URef = storage::new_uref(param);
    }
}