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
/*
 * SPDX-License-Identifier: Apache-2.0
 */

// Use the Fabric Contract modules
use fabric_contract::contract::*;
use fabric_contract::data::*;

// Use the log crate to support logging
use log::{info};

// Our own asset types
use crate::types::Asset;



/// Structure for the AssetContract, on which implemenation transaction functions will be added
pub struct AssetTransfer {}

/// Implementation of the contract trait for the AssetContract
/// There are default implementation methods, but can be modified if you wish
///
/// Recommended that the name() function is always modified
impl Contract for AssetTransfer {
    //! Name of the contract
    fn name(&self) -> String {
        format!("AssetTransfer")
    }
}

/// The contract implementation
/// Should be marked with the macro `#[contrant_impl]`
#[Contract_Impl]
impl AssetTransfer {
    pub fn new() -> AssetTransfer {
        AssetTransfer {}
    }

    #[Transaction]
    pub fn init_ledger(&self) -> Result<(), ContractError> {
        // get the collection that is backed by the world state
        let world = Ledger::access_ledger().get_collection(CollectionName::World);

        world.create(Asset::new(
            "asset1".to_string(),
            "blue".to_string(),
            5,
            "Tomoko".to_string(),
            300,
        ))?;
        world.create(Asset::new(
            "asset2".to_string(),
            "red".to_string(),
            5,
            "Brad".to_string(),
            400,
        ))?;
        world.create(Asset::new(
            "asset3".to_string(),
            "green".to_string(),
            10,
            "Jin Soo".to_string(),
            500,
        ))?;
        world.create(Asset::new(
            "asset4".to_string(),
            "yellow".to_string(),
            10,
            "Max".to_string(),
            600,
        ))?;
        world.create(Asset::new(
            "asset5".to_string(),
            "black".to_string(),
            15,
            "Adriana".to_string(),
            700,
        ))?;
        world.create(Asset::new(
            "asset6".to_string(),
            "white".to_string(),
            15,
            "Michel".to_string(),
            800,
        ))?;

        Ok(())
    }

    /// CreateAsset issues a new asset to the world state with given details.
    #[Transaction(submit)]
    pub fn create_asset(
        &self,
        id: String,
        color: String,
        size: i32,
        owner: String,
        appraised_value: i32,
    ) -> Result<(), ContractError> {
        // get the collection that is backed by the world state
        info!("create_asset");
        let world = Ledger::access_ledger().get_collection(CollectionName::World);
        
        // create the new asset
        let new_asset = Asset::new(id, color, size, owner, appraised_value);        
        world.create(new_asset)?;

        Ok(())
    }

    #[Transaction(evaluate)]
    pub fn read_asset(&self, id: String) -> Result<Asset, ContractError> {
        let world = Ledger::access_ledger().get_collection(CollectionName::World);
        let asset = world.retrieve::<Asset>(&id)?;
        Ok(asset)
    }

    #[Transaction(submit)]
    pub fn update_asset(
        &self,
        id: String,
        color: String,
        size: i32,
        owner: String,
        appraised_value: i32,
    ) -> Result<(), ContractError> {
        let world = Ledger::access_ledger().get_collection(CollectionName::World);

        match world.update::<Asset>(Asset::new(id, color, size, owner, appraised_value)) {
            Ok(_) => Ok(()),
            Err(e) => {
                return Err(ContractError::from((
                    "That asset is not found".to_string(),
                    e,
                )))
            }
        }
    }

    #[Transaction(submit)]
    pub fn delete_asset(&self, id: String) -> Result<(), ContractError> {
        let world = Ledger::access_ledger().get_collection(CollectionName::World);

        match world.delete_state(&id) {
            Err(e) => Err(ContractError::from((
                format!("Unable to delete asset {}", id),
                e,
            ))),
            Ok(_) => Ok(()),
        }
    }

    #[Transaction(evaluate)]
    pub fn asset_exists(&self, id: String) -> Result<bool, ContractError> {
        let world = Ledger::access_ledger().get_collection(CollectionName::World);

        match world.state_exists(id.as_str()) {
            Err(e) => Err(ContractError::from((
                format!("Unable to check asset {}", id),
                e,
            ))),
            Ok(r) => Ok(r),
        }
    }

    #[Transaction(submit)]
    pub fn transfer_asset(&self, id: String, new_owner: String) -> Result<(), ContractError> {
        let world = Ledger::access_ledger().get_collection(CollectionName::World);
        let asset = world.retrieve::<Asset>(&id);

        match asset {
            Ok(mut a) => {
                a.update_owner(new_owner);
                world.update::<Asset>(a)?;
                Ok(())
            }
            Err(e) => Err(ContractError::from((
                format!("Unable to check asset {}", id),
                e,
            ))),
        }
    }
}