git.delta.rocks / unique-network / refs/commits / 80401964ebb0

difftreelog

Smart contract update

str-mv2020-07-16parent: #37abce6.patch.diff
in: master

7 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -96,15 +96,6 @@
         // this is needed only if you are using events in your pallet
         fn deposit_event() = default;
 
-        // Initializing events
-        // this is needed only if you are using events in your module
-        // fn deposit_event<T>() = default;
-
-        // Create collection of NFT with given parameters
-        //
-        // @param customDataSz size of custom data in each collection item
-        // returns collection ID
-
         // Create collection of NFT with given parameters
         //
         // @param customDataSz size of custom data in each collection item
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -8,6 +8,7 @@
 #[cfg(feature = "std")]
 include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
 
+use contracts_rpc_runtime_api::ContractExecResult;
 use grandpa::fg_primitives;
 use grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
 use sp_api::impl_runtime_apis;
@@ -21,7 +22,6 @@
     transaction_validity::{TransactionSource, TransactionValidity},
     ApplyExtrinsicResult, MultiSignature,
 };
-use contracts_rpc_runtime_api::ContractExecResult;
 use sp_std::prelude::*;
 #[cfg(feature = "std")]
 use sp_version::NativeVersion;
modifiedsmart_contract/ink-types-node-runtime/Cargo.tomldiffbeforeafterboth
--- a/smart_contract/ink-types-node-runtime/Cargo.toml
+++ b/smart_contract/ink-types-node-runtime/Cargo.toml
@@ -19,6 +19,7 @@
 
 [dependencies]
 ink_core = { version = "2", git = "https://github.com/paritytech/ink", tag = "latest-v2", package = "ink_core", default-features = false }
+ink_prelude = { version = "2", git = "https://github.com/paritytech/ink", tag = "latest-v2", package = "ink_prelude", default-features = false }
 frame-system = { git = "https://github.com/paritytech/substrate/", tag = "v2.0.0-rc3", package = "frame-system", default-features = false }
 pallet-indices = { git = "https://github.com/paritytech/substrate/", tag = "v2.0.0-rc3", package = "pallet-indices", default-features = false }
 sp-core = { git = "https://github.com/paritytech/substrate/", tag = "v2.0.0-rc3", package = "sp-core", default-features = false }
modifiedsmart_contract/ink-types-node-runtime/examples/calls/Cargo.lockdiffbeforeafterboth
--- a/smart_contract/ink-types-node-runtime/examples/calls/Cargo.lock
+++ b/smart_contract/ink-types-node-runtime/examples/calls/Cargo.lock
@@ -803,6 +803,7 @@
 dependencies = [
  "frame-system",
  "ink_core",
+ "ink_prelude",
  "pallet-indices",
  "parity-scale-codec",
  "sp-core",
modifiedsmart_contract/ink-types-node-runtime/examples/calls/lib.rsdiffbeforeafterboth
7 use ink_core::env;7 use ink_core::env;
8 use ink_prelude::*;8 use ink_prelude::*;
9 use ink_types_node_runtime::{calls as runtime_calls, NodeRuntimeTypes};9 use ink_types_node_runtime::{calls as runtime_calls, NodeRuntimeTypes};
10 use ink_core::{memory::vec::Vec, storage};10 use ink_prelude::vec::Vec;
1111
12 /// This simple dummy contract dispatches substrate runtime calls12 /// This simple dummy contract dispatches substrate runtime calls
13 #[ink(storage)]13 #[ink(storage)]
17 #[ink(constructor)]17 #[ink(constructor)]
18 fn new(&mut self) {}18 fn new(&mut self) {}
1919
20 /// Dispatches a `transfer` call to the Balances srml module
21 #[ink(message)]20 #[ink(message)]
22 fn create_collection(&self, collection_name: Vec<u16>, collection_description: Vec<u16>, 21 fn transfer(&self, collection_id: u64, item_id: u64, new_owner: AccountId) {
23 token_prefix: Vec<u8>, 22
24 custom_data_sz: u32) {23 env::println(&format!(
25 // create the Balances::transfer Call24 "transfer invoke_runtime params {:?}, {:?}, {:?} ",
2625 collection_id,
27 // collection_name: Vec<u16>,26 item_id,
28 // collection_description: Vec<u16>,27 new_owner
29 // token_prefix: Vec<u8>,28 ));
30 // custom_data_sz: u32
3129
32 let transfer_call = runtime_calls::create_collection(collection_name, collection_description, token_prefix, custom_data_sz);30 let transfer_call = runtime_calls::transfer(collection_id, item_id, new_owner);
33 // dispatch the call to the runtime31 // dispatch the call to the runtime
34 let result = self.env().invoke_runtime(&transfer_call);32 let result = self.env().invoke_runtime(&transfer_call);
3533
36 // report result to console34 // report result to console
37 // NOTE: println should only be used on a development chain)35 // NOTE: println should only be used on a development chain)
38 env::println(&format!(36 env::println(&format!(
39 "Balance transfer invoke_runtime result {:?}",37 "transfer invoke_runtime result {:?}",
40 result38 result
41 ));39 ));
42 }40 }
41
42 // SafeTransfer
43
44 #[ink(message)]
45 fn approve(&self, approved: AccountId, collection_id: u64, item_id: u64) {
46
47 env::println(&format!(
48 "approve invoke_runtime params {:?}, {:?}, {:?} ",
49 approved,
50 collection_id,
51 item_id
52 ));
53
54 let approve_call = runtime_calls::approve(approved, collection_id, item_id);
55 // dispatch the call to the runtime
56 let result = self.env().invoke_runtime(&approve_call);
57
58 // report result to console
59 // NOTE: println should only be used on a development chain)
60 env::println(&format!(
61 "approve invoke_runtime result {:?}",
62 result
63 ));
64 }
65
66 ////////////////////////////////////////// GetApproved
67 ///
68 /// Returns an account's free balance, read directly from runtime storage
69 ///
70 /// # Key Scheme
71 ///
72 /// A key for the [substrate storage map]
73 /// (https://github.com/paritytech/substrate/blob/dd97b1478b31a4715df7e88a5ebc6664425fb6c6/frame/support/src/storage/generator/map.rs#L28)
74 /// is constructed with:
75 ///
76 /// ```nocompile
77 /// Twox128(module_prefix) ++ Twox128(storage_prefix) ++ Hasher(encode(key))
78 /// ```
79 ///
80 /// For the `System` module's `Account` map, the [hasher implementation]
81 /// (https://github.com/paritytech/substrate/blob/2c87fe171bc341755a43a3b32d67560469f8daac/frame/system/src/lib.rs#L349)
82 /// is `blake2_128_concat`.
83 // #[ink(message)]
84 // fn get_balance(&self, account: AccountId) -> Balance {
85 // let mut key = vec![
86 // // Precomputed: Twox128("System")
87 // 38, 170, 57, 78, 234, 86, 48, 224, 124, 72, 174, 12, 149, 88, 206, 247,
88 // // Precomputed: Twox128("Account")
89 // 185, 157, 136, 14, 198, 129, 121, 156, 12, 243, 14, 136, 134, 55, 29, 169,
90 // ];
91
92 // let encoded_account = account.encode();
93 // let hashed_account = <Blake2x128>::hash_bytes(&encoded_account);
94
95 // // The hasher is `Blake2_128Concat` which appends the unhashed account to the hashed account
96 // key.extend_from_slice(&hashed_account);
97 // key.extend_from_slice(&encoded_account);
98
99 // // fetch from runtime storage
100 // let result = self.env().get_runtime_storage::<AccountInfo>(&key[..]);
101 // match result {
102 // Some(Ok(account_info)) => account_info.data.free,
103 // Some(Err(err)) => {
104 // env::println(&format!("Error reading AccountInfo {:?}", err));
105 // 0
106 // }
107 // None => {
108 // env::println(&format!("No data at key {:?}", key));
109 // 0
110 // }
111 // }
112 // }
113
114 #[ink(message)]
115 fn transfer_from(&self, collection_id: u64, item_id: u64, new_owner: AccountId) {
116
117 env::println(&format!(
118 "transfer_from invoke_runtime params {:?}, {:?}, {:?} ",
119 collection_id,
120 item_id,
121 new_owner
122 ));
123
124 let transfer_from_call = runtime_calls::transfer_from(collection_id, item_id, new_owner);
125 // dispatch the call to the runtime
126 let result = self.env().invoke_runtime(&transfer_from_call);
127
128 // report result to console
129 // NOTE: println should only be used on a development chain)
130 env::println(&format!(
131 "transfer_from invoke_runtime result {:?}",
132 result
133 ));
134 }
135
136 // SafeTransferFrom
137
138 #[ink(message)]
139 fn create_item(&self, collection_id: u64, properties: Vec<u8>) {
140
141 env::println(&format!(
142 "create_item invoke_runtime params {:?}, {:?} ",
143 collection_id,
144 properties
145 ));
146
147 let create_item_call = runtime_calls::create_item(collection_id, properties);
148 // dispatch the call to the runtime
149 let result = self.env().invoke_runtime(&create_item_call);
150
151 // report result to console
152 // NOTE: println should only be used on a development chain)
153 env::println(&format!(
154 "create_item invoke_runtime result {:?}",
155 result
156 ));
157 }
158
159 #[ink(message)]
160 fn burn_item(&self, collection_id: u64, item_id: u64) {
161
162 env::println(&format!(
163 "burn_item invoke_runtime params {:?}, {:?}",
164 collection_id,
165 item_id
166 ));
167
168 let burn_item_call = runtime_calls::burn_item(collection_id, item_id);
169 // dispatch the call to the runtime
170 let result = self.env().invoke_runtime(&burn_item_call);
171
172 // report result to console
173 // NOTE: println should only be used on a development chain)
174 env::println(&format!(
175 "burn_item invoke_runtime result {:?}",
176 result
177 ));
178 }
179
180 // GetOwner
181 // BalanceOf
43 }182 }
44
45 #[cfg(test)]
46 mod tests {
47 use super::*;
48 use sp_keyring::AccountKeyring;
49
50 #[test]
51 fn dispatches_balances_call() {
52 let calls = Calls::new();
53 let alice = AccountId::from(AccountKeyring::Alice.to_account_id());
54 // assert_eq!(calls.env().dispatched_calls().into_iter().count(), 0);
55 calls.balance_transfer(alice, 10000);
56 // assert_eq!(calls.env().dispatched_calls().into_iter().count(), 1);
57 }
58 }
59}183}
60184
modifiedsmart_contract/ink-types-node-runtime/src/calls.rsdiffbeforeafterboth
--- a/smart_contract/ink-types-node-runtime/src/calls.rs
+++ b/smart_contract/ink-types-node-runtime/src/calls.rs
@@ -50,46 +50,6 @@
 //     Balances::<NodeRuntimeTypes>::transfer(account.into(), balance).into()
 // }
 
-// #[cfg(test)]
-// mod tests {
-//     use crate::{calls, NodeRuntimeTypes};
-//     use super::Call;
-
-//     use node_runtime::{self, Runtime};
-//     use pallet_indices::address;
-//     use scale::{Decode, Encode};
-
-
-//     #[test]
-//     fn call_balance_transfer() {
-//         let balance = 10_000;
-//         let account_index = 0;
-
-//         let contract_address = calls::Address::Index(account_index);
-//         let contract_transfer =
-//             calls::Balances::<NodeRuntimeTypes>::transfer(contract_address, balance);
-//         let contract_call = Call::Balances(contract_transfer);
-
-//         let srml_address = address::Address::Index(account_index);
-//         let srml_transfer = node_runtime::BalancesCall::<Runtime>::transfer(srml_address, balance);
-//         let srml_call = node_runtime::Call::Balances(srml_transfer);
-
-//         let contract_call_encoded = contract_call.encode();
-//         let srml_call_encoded = srml_call.encode();
-
-//         assert_eq!(srml_call_encoded, contract_call_encoded);
-
-//         let srml_call_decoded: node_runtime::Call =
-//             Decode::decode(&mut contract_call_encoded.as_slice())
-//                 .expect("Balances transfer call decodes to srml type");
-//         let srml_call_encoded = srml_call_decoded.encode();
-//         let contract_call_decoded: Call = Decode::decode(&mut srml_call_encoded.as_slice())
-//             .expect("Balances transfer call decodes back to contract type");
-//         assert!(contract_call == contract_call_decoded);
-//     }
-// }
-
-
 
 
 
@@ -114,18 +74,18 @@
 
 use ink_core::env::EnvTypes;
 use scale::{Codec, Decode, Encode};
-use pallet_indices::address::Address;
 use sp_runtime::traits::Member;
+use ink_prelude::vec::Vec;
 use crate::{AccountId, Balance, NodeRuntimeTypes};
-use sp_runtime::sp_std::prelude::Vec;
 
+
 /// Default runtime Call type, a subset of the runtime Call module variants
 ///
 /// The codec indices of the  modules *MUST* match those in the concrete runtime.
 #[derive(Encode, Decode)]
 #[cfg_attr(feature = "std", derive(Clone, PartialEq, Eq))]
 pub enum Call {
-    #[codec(index = "8")]
+    #[codec(index = "7")]
     Nft(Nft<NodeRuntimeTypes>),
 }
 
@@ -140,61 +100,74 @@
  where
      T: EnvTypes,
      T::AccountId: Member + Codec,
-{
-    #[allow(non_camel_case_types)]
-    create_nft_collection(Vec<u16>, Vec<u16>, Vec<u8>, u32),
+    {
+        #[allow(non_camel_case_types)]
+        create_collection(Vec<u16>, Vec<u16>, Vec<u8>, u32),
 
-    #[allow(non_camel_case_types)]
-    add_collection_admin(u64, T::AccountId),
+        #[allow(non_camel_case_types)]
+        destroy_collection(u64),
 
-    // collection_name: Vec<u16>,
-    // collection_description: Vec<u16>,
-    // token_prefix: Vec<u8>,
-    // custom_data_sz: u32
+        #[allow(non_camel_case_types)]
+        change_collection_owner(u64, T::AccountId),
 
-}
+        #[allow(non_camel_case_types)]
+        add_collection_admin(u64, T::AccountId),
 
-// Construct a `Balances::transfer` call
-pub fn create_collection(collection_name: Vec<u16>, collection_description: Vec<u16>, 
-                        token_prefix: Vec<u8>, custom_data_sz: u32) -> Call {
-    Nft::<NodeRuntimeTypes>::create_nft_collection(collection_name, collection_description, token_prefix, custom_data_sz).into()
-}
+        #[allow(non_camel_case_types)]
+        remove_collection_admin(u64, T::AccountId),
 
-#[cfg(test)]
-mod tests {
-    use crate::{calls, NodeRuntimeTypes};
-    use super::Call;
+        #[allow(non_camel_case_types)]
+        create_item(u64, Vec<u8>),
 
-    use node_runtime::{self, Runtime};
-    use pallet_indices::address;
-    use scale::{Decode, Encode};
+        #[allow(non_camel_case_types)]
+        burn_item(u64, u64),
 
+        #[allow(non_camel_case_types)]
+        transfer(u64, u64, T::AccountId),
 
-    #[test]
-    fn call_balance_transfer() {
-        let balance = 10_000;
-        let account_index = 0;
+        #[allow(non_camel_case_types)]
+        nft_approve(T::AccountId, u64, u64),
 
-        let contract_address = calls::Address::Index(account_index);
-        let contract_transfer =
-            calls::Balances::<NodeRuntimeTypes>::transfer(contract_address, balance);
-        let contract_call = Call::Balances(contract_transfer);
+        #[allow(non_camel_case_types)]
+        nft_transfer_from(u64, u64, T::AccountId),
 
-        let srml_address = address::Address::Index(account_index);
-        let srml_transfer = node_runtime::BalancesCall::<Runtime>::transfer(srml_address, balance);
-        let srml_call = node_runtime::Call::Balances(srml_transfer);
+        #[allow(non_camel_case_types)]
+        nft_safe_transfer(u64, u64, T::AccountId),
+    }
 
-        let contract_call_encoded = contract_call.encode();
-        let srml_call_encoded = srml_call.encode();
 
-        assert_eq!(srml_call_encoded, contract_call_encoded);
 
-        let srml_call_decoded: node_runtime::Call =
-            Decode::decode(&mut contract_call_encoded.as_slice())
-                .expect("Balances transfer call decodes to srml type");
-        let srml_call_encoded = srml_call_decoded.encode();
-        let contract_call_decoded: Call = Decode::decode(&mut srml_call_encoded.as_slice())
-            .expect("Balances transfer call decodes back to contract type");
-        assert!(contract_call == contract_call_decoded);
-    }
+
+pub fn transfer(collection_id: u64, item_id: u64, new_owner: AccountId) -> Call {
+    Nft::<NodeRuntimeTypes>::transfer(collection_id, item_id, new_owner.into()).into()
+}
+
+pub fn create_collection(collection_name: Vec<u16>, collection_description: Vec<u16>, 
+        token_prefix: Vec<u8>, custom_data_sz: u32) -> Call {
+    Nft::<NodeRuntimeTypes>::create_collection(collection_name, collection_description, token_prefix, custom_data_sz).into()
+}
+
+// pub fn safe_transfer(collection_id: u64, item_id: u64, new_owner: AccountId) -> Call {
+//     Nft::<NodeRuntimeTypes>::nft_safe_transfer(collection_id, item_id, new_owner.into()).into()
+// }
+
+pub fn approve(approved: AccountId, collection_id: u64, item_id: u64) -> Call {
+    Nft::<NodeRuntimeTypes>::nft_approve(approved.into(), collection_id, item_id).into()
+}
+
+//GetApproved
+
+pub fn transfer_from(collection_id: u64, item_id: u64, new_owner: AccountId) -> Call {
+    Nft::<NodeRuntimeTypes>::nft_transfer_from(collection_id, item_id, new_owner.into()).into()
+}
+
+pub fn create_item(collection_id: u64, properties: Vec<u8>) -> Call {
+    Nft::<NodeRuntimeTypes>::create_item(collection_id, properties).into()
+}
+
+pub fn burn_item(collection_id: u64, item_id: u64) -> Call {
+    Nft::<NodeRuntimeTypes>::burn_item(collection_id, item_id).into()
 }
+
+//GetOwner
+//BalanceOf
\ No newline at end of file
modifiedsmart_contract/ink-types-node-runtime/src/lib.rsdiffbeforeafterboth
--- a/smart_contract/ink-types-node-runtime/src/lib.rs
+++ b/smart_contract/ink-types-node-runtime/src/lib.rs
@@ -36,6 +36,7 @@
 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
 pub struct AccountId (AccountId32);
 
+
 impl From<AccountId32> for AccountId {
     fn from(account: AccountId32) -> Self {
         AccountId(account)