From e82d9a0db33effc2525d77342f0b404e442f3c1d Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sun, 10 Nov 2019 22:39:03 +0100 Subject: [PATCH] [examples/lang] remove all lang 1.0 examples --- examples/lang/delegator/.cargo/config | 4 - examples/lang/delegator/.gitignore | 9 - .../lang/delegator/.ink/abi_gen/Cargo.toml | 15 - examples/lang/delegator/.ink/abi_gen/main.rs | 7 - examples/lang/delegator/Cargo.toml | 81 --- examples/lang/delegator/README.md | 29 - .../lang/delegator/accumulator/Cargo.toml | 49 -- .../lang/delegator/accumulator/src/lib.rs | 52 -- examples/lang/delegator/adder/Cargo.toml | 54 -- examples/lang/delegator/adder/src/lib.rs | 46 -- examples/lang/delegator/build-all.sh | 6 - examples/lang/delegator/src/lib.rs | 110 ---- examples/lang/delegator/subber/Cargo.toml | 51 -- examples/lang/delegator/subber/src/lib.rs | 45 -- examples/lang/erc20/.cargo/config | 4 - examples/lang/erc20/.ink/abi_gen/Cargo.toml | 15 - examples/lang/erc20/.ink/abi_gen/main.rs | 7 - examples/lang/erc20/Cargo.toml | 62 -- examples/lang/erc20/src/lib.rs | 237 -------- examples/lang/erc721/.cargo/config | 4 - examples/lang/erc721/.ink/abi_gen/Cargo.toml | 15 - examples/lang/erc721/.ink/abi_gen/main.rs | 7 - examples/lang/erc721/Cargo.toml | 62 -- examples/lang/erc721/src/lib.rs | 528 ------------------ examples/lang/events/.cargo/config | 4 - examples/lang/events/.gitignore | 9 - examples/lang/events/.ink/abi_gen/Cargo.toml | 15 - examples/lang/events/.ink/abi_gen/main.rs | 8 - examples/lang/events/Cargo.toml | 62 -- examples/lang/events/src/lib.rs | 87 --- examples/lang/flipper/.cargo/config | 4 - examples/lang/flipper/.gitignore | 9 - examples/lang/flipper/.ink/abi_gen/Cargo.toml | 15 - examples/lang/flipper/.ink/abi_gen/main.rs | 8 - examples/lang/flipper/Cargo.toml | 62 -- examples/lang/flipper/src/lib.rs | 69 --- examples/lang/incrementer/.cargo/config | 4 - examples/lang/incrementer/.gitignore | 9 - .../lang/incrementer/.ink/abi_gen/Cargo.toml | 15 - .../lang/incrementer/.ink/abi_gen/main.rs | 8 - examples/lang/incrementer/Cargo.toml | 63 --- examples/lang/incrementer/src/lib.rs | 76 --- examples/lang/shared_vec/.cargo/config | 4 - examples/lang/shared_vec/.gitignore | 9 - .../lang/shared_vec/.ink/abi_gen/Cargo.toml | 15 - examples/lang/shared_vec/.ink/abi_gen/main.rs | 8 - examples/lang/shared_vec/Cargo.toml | 62 -- examples/lang/shared_vec/src/lib.rs | 259 --------- 48 files changed, 2383 deletions(-) delete mode 100644 examples/lang/delegator/.cargo/config delete mode 100644 examples/lang/delegator/.gitignore delete mode 100644 examples/lang/delegator/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/delegator/.ink/abi_gen/main.rs delete mode 100644 examples/lang/delegator/Cargo.toml delete mode 100644 examples/lang/delegator/README.md delete mode 100644 examples/lang/delegator/accumulator/Cargo.toml delete mode 100644 examples/lang/delegator/accumulator/src/lib.rs delete mode 100644 examples/lang/delegator/adder/Cargo.toml delete mode 100644 examples/lang/delegator/adder/src/lib.rs delete mode 100755 examples/lang/delegator/build-all.sh delete mode 100644 examples/lang/delegator/src/lib.rs delete mode 100644 examples/lang/delegator/subber/Cargo.toml delete mode 100644 examples/lang/delegator/subber/src/lib.rs delete mode 100644 examples/lang/erc20/.cargo/config delete mode 100644 examples/lang/erc20/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/erc20/.ink/abi_gen/main.rs delete mode 100644 examples/lang/erc20/Cargo.toml delete mode 100644 examples/lang/erc20/src/lib.rs delete mode 100644 examples/lang/erc721/.cargo/config delete mode 100644 examples/lang/erc721/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/erc721/.ink/abi_gen/main.rs delete mode 100644 examples/lang/erc721/Cargo.toml delete mode 100644 examples/lang/erc721/src/lib.rs delete mode 100644 examples/lang/events/.cargo/config delete mode 100644 examples/lang/events/.gitignore delete mode 100644 examples/lang/events/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/events/.ink/abi_gen/main.rs delete mode 100644 examples/lang/events/Cargo.toml delete mode 100644 examples/lang/events/src/lib.rs delete mode 100644 examples/lang/flipper/.cargo/config delete mode 100644 examples/lang/flipper/.gitignore delete mode 100644 examples/lang/flipper/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/flipper/.ink/abi_gen/main.rs delete mode 100644 examples/lang/flipper/Cargo.toml delete mode 100644 examples/lang/flipper/src/lib.rs delete mode 100644 examples/lang/incrementer/.cargo/config delete mode 100644 examples/lang/incrementer/.gitignore delete mode 100644 examples/lang/incrementer/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/incrementer/.ink/abi_gen/main.rs delete mode 100644 examples/lang/incrementer/Cargo.toml delete mode 100644 examples/lang/incrementer/src/lib.rs delete mode 100644 examples/lang/shared_vec/.cargo/config delete mode 100644 examples/lang/shared_vec/.gitignore delete mode 100644 examples/lang/shared_vec/.ink/abi_gen/Cargo.toml delete mode 100644 examples/lang/shared_vec/.ink/abi_gen/main.rs delete mode 100644 examples/lang/shared_vec/Cargo.toml delete mode 100644 examples/lang/shared_vec/src/lib.rs diff --git a/examples/lang/delegator/.cargo/config b/examples/lang/delegator/.cargo/config deleted file mode 100644 index 9ed784e6..00000000 --- a/examples/lang/delegator/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] \ No newline at end of file diff --git a/examples/lang/delegator/.gitignore b/examples/lang/delegator/.gitignore deleted file mode 100644 index bf910de1..00000000 --- a/examples/lang/delegator/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock \ No newline at end of file diff --git a/examples/lang/delegator/.ink/abi_gen/Cargo.toml b/examples/lang/delegator/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 46d9eb4d..00000000 --- a/examples/lang/delegator/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "delegator", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/delegator/.ink/abi_gen/main.rs b/examples/lang/delegator/.ink/abi_gen/main.rs deleted file mode 100644 index 9fa36177..00000000 --- a/examples/lang/delegator/.ink/abi_gen/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/delegator/Cargo.toml b/examples/lang/delegator/Cargo.toml deleted file mode 100644 index 35a6a1e1..00000000 --- a/examples/lang/delegator/Cargo.toml +++ /dev/null @@ -1,81 +0,0 @@ -[package] -name = "delegator" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -adder = { path = "adder", default-features = false, features = ["ink-as-dependency"] } -subber = { path = "subber", default-features = false, features = ["ink-as-dependency"] } -accumulator = { path = "accumulator", default-features = false, features = ["ink-as-dependency"] } - -[lib] -name = "delegator" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", - - "adder/std", - "subber/std", - "accumulator/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", - "type-metadata/std", - - "adder/test-env", - "subber/test-env", - "accumulator/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - - "adder/ink-generate-abi", - "subber/ink-generate-abi", - "accumulator/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen", - "accumulator", - "adder", - "subber", -] -exclude = [ - ".ink" -] diff --git a/examples/lang/delegator/README.md b/examples/lang/delegator/README.md deleted file mode 100644 index 8f21e22a..00000000 --- a/examples/lang/delegator/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Delegator Smart Contract - -The delegator smart contract is our showcase for executing other smart contracts on-chain. - -It consists in total of 4 different smart contract: - -- Delegator (root): Delegates calls either to the Adder or Subber smart contract -- Adder: Increases a value in the Accumulator smart contract -- Subber: Decreases a value in the Accumulator smart contract -- Accumulator: Owns a simple `i32` value that can be incremented or decremented - -In order to test this bundle of smart contracts you need to do the following: - -1. Compile all dependencies of the Delegator smart contract using the `./build-all.sh` script. - As usual you will receive their respective Wasm blobs in their respective `target` folders. -1. Go to the delegator directory (root of the example) and run `cargo run -p abi-gen` in order - to generate the ABI file for the Delegator smart contract. - Note: You won't need an ABI file for the other smart contracts since we won't operate on them - using the Polkadot UI. -1. Put the Wasm blobs of Accumulator, Adder, Subber and the Delegator on the chain via `put_code` command. - While doing so note down their respective code hashes that you can inspect by extracting this information - out from the signalling events upon putting the code on the chain. -1. Instantiate the Delegator smart contract given all of the code hashes and a starting value. - The Delegator smart contract will take over the work of instantiating the other smart contracts for you. -1. Now you are able to run the operations provided by the Delegator smart contract. - Namely `delegate` to delegate the call to either the Adder or the Subber to either increase or decrease - the value stored in the Accumulator smart contract respectively and `switch` to switch the currently - delegated-to smart contract. - The initial delegated-to smart contract is the Adder. diff --git a/examples/lang/delegator/accumulator/Cargo.toml b/examples/lang/delegator/accumulator/Cargo.toml deleted file mode 100644 index af13bb86..00000000 --- a/examples/lang/delegator/accumulator/Cargo.toml +++ /dev/null @@ -1,49 +0,0 @@ -[package] -name = "accumulator" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../../core", default-features = false } -ink_model = { path = "../../../../model", default-features = false } -ink_lang = { path = "../../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "accumulator" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", - "type-metadata/std", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] diff --git a/examples/lang/delegator/accumulator/src/lib.rs b/examples/lang/delegator/accumulator/src/lib.rs deleted file mode 100644 index 0faae809..00000000 --- a/examples/lang/delegator/accumulator/src/lib.rs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - memory::format, - storage, -}; -use ink_lang::contract; - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// Holds a simple i32 value that can be incremented and decremented. - struct Accumulator { - /// The current value. - value: storage::Value, - } - - impl Deploy for Accumulator { - /// Initializes the value to the initial value. - fn deploy(&mut self, init_value: i32) { - self.value.set(init_value) - } - } - - impl Accumulator { - /// Mutates the internal value. - pub(external) fn inc(&mut self, by: i32) { - self.value += by; - } - - /// Returns the current state. - pub(external) fn get(&self) -> i32 { - *self.value - } - } -} diff --git a/examples/lang/delegator/adder/Cargo.toml b/examples/lang/delegator/adder/Cargo.toml deleted file mode 100644 index c8de251d..00000000 --- a/examples/lang/delegator/adder/Cargo.toml +++ /dev/null @@ -1,54 +0,0 @@ -[package] -name = "adder" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../../core", default-features = false } -ink_model = { path = "../../../../model", default-features = false } -ink_lang = { path = "../../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -accumulator = { path = "../accumulator", default-features = false, features = ["ink-as-dependency"] } - -[lib] -name = "adder" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", - - "accumulator/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", - - "accumulator/ink-generate-abi", -] -ink-as-dependency = [] diff --git a/examples/lang/delegator/adder/src/lib.rs b/examples/lang/delegator/adder/src/lib.rs deleted file mode 100644 index 7d4a57ea..00000000 --- a/examples/lang/delegator/adder/src/lib.rs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - memory::format, - storage, -}; -use ink_lang::contract; - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// Increments the accumulator's value. - struct Adder { - /// The accumulator to store values. - accumulator: storage::Value, - } - - impl Deploy for Adder { - fn deploy(&mut self, accumulator: AccountId) { - self.accumulator.set(accumulator::Accumulator::from_account_id(accumulator)); - } - } - - impl Adder { - /// Flips the current state of our smart contract. - pub(external) fn inc(&mut self, by: i32) { - self.accumulator.inc(by); - } - } -} diff --git a/examples/lang/delegator/build-all.sh b/examples/lang/delegator/build-all.sh deleted file mode 100755 index f2db0121..00000000 --- a/examples/lang/delegator/build-all.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -pushd accumulator && cargo contract build && popd && -pushd adder && cargo contract build && popd && -pushd subber && cargo contract build && popd && -cargo contract build diff --git a/examples/lang/delegator/src/lib.rs b/examples/lang/delegator/src/lib.rs deleted file mode 100644 index b422bef3..00000000 --- a/examples/lang/delegator/src/lib.rs +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - memory::format, - storage, -}; -use ink_lang::contract; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "ink-generate-abi", derive(type_metadata::Metadata))] -pub enum Which { - Adder, - Subber, -} - -impl ink_core::storage::Flush for Which { - fn flush(&mut self) {} -} - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// Delegates calls to an adder or subber contract to mutate - /// a value in an accumulator contract. - /// - /// In order to deploy the delegator smart contract we first - /// have to manually put the code of the accumulator, adder - /// and subber smart contracts, receive their code hashes from - /// the signalled events and put their code hash into our - /// delegator smart contract. - struct Delegator { - /// Says which of adder or subber is currently in use. - which: storage::Value, - /// The accumulator smart contract. - accumulator: storage::Value, - /// The adder smart contract. - adder: storage::Value, - /// The subber smart contract. - subber: storage::Value, - } - - impl Deploy for Delegator { - /// Initializes the value to the initial value. - fn deploy( - &mut self, - init_value: i32, - accumulator_code_hash: Hash, - adder_code_hash: Hash, - subber_code_hash: Hash, - ) { - self.which.set(Which::Adder); - let total_balance = env.balance(); - let accumulator = accumulator::Accumulator::new(accumulator_code_hash, init_value) - .value(total_balance / 4) - .create() - .expect("failed at instantiating the accumulator contract"); - self.accumulator.set(accumulator.clone()); - self.adder.set( - adder::Adder::new(adder_code_hash, accumulator.account_id()) - .value(total_balance / 4) - .create() - .expect("failed at instantiating the adder contract") - ); - self.subber.set( - subber::Subber::new(subber_code_hash, accumulator.account_id()) - .value(total_balance / 4) - .create() - .expect("failed at instantiating the subber contract") - ); - } - } - - impl Delegator { - /// Delegates the call. - pub(external) fn delegate(&mut self, by: i32) { - match &*self.which { - Which::Adder => self.adder.inc(by), - Which::Subber => self.subber.dec(by), - } - } - - /// Switches the delegator. - pub(external) fn switch(&mut self) { - match *self.which { - Which::Adder => { - *self.which = Which::Subber; - } - Which::Subber => { - *self.which = Which::Adder; - } - } - } - } -} diff --git a/examples/lang/delegator/subber/Cargo.toml b/examples/lang/delegator/subber/Cargo.toml deleted file mode 100644 index ad330795..00000000 --- a/examples/lang/delegator/subber/Cargo.toml +++ /dev/null @@ -1,51 +0,0 @@ -[package] -name = "subber" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../../core", default-features = false } -ink_model = { path = "../../../../model", default-features = false } -ink_lang = { path = "../../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -accumulator = { path = "../accumulator", default-features = false, features = ["ink-as-dependency"] } - -[lib] -name = "subber" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", - "type-metadata/std", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] diff --git a/examples/lang/delegator/subber/src/lib.rs b/examples/lang/delegator/subber/src/lib.rs deleted file mode 100644 index d49a937c..00000000 --- a/examples/lang/delegator/subber/src/lib.rs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - memory::format, - storage, -}; -use ink_lang::contract; - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// Decrements the accumulator's value. - struct Subber { - /// The accumulator to store values. - accumulator: storage::Value, - } - - impl Deploy for Subber { - fn deploy(&mut self, accumulator: AccountId) { - self.accumulator.set(accumulator::Accumulator::from_account_id(accumulator)); - } - } - - impl Subber { - pub(external) fn dec(&mut self, by: i32) { - self.accumulator.inc(-by); - } - } -} diff --git a/examples/lang/erc20/.cargo/config b/examples/lang/erc20/.cargo/config deleted file mode 100644 index 7d5b52df..00000000 --- a/examples/lang/erc20/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] diff --git a/examples/lang/erc20/.ink/abi_gen/Cargo.toml b/examples/lang/erc20/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 0e566c9d..00000000 --- a/examples/lang/erc20/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "erc20", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/erc20/.ink/abi_gen/main.rs b/examples/lang/erc20/.ink/abi_gen/main.rs deleted file mode 100644 index 9fa36177..00000000 --- a/examples/lang/erc20/.ink/abi_gen/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/erc20/Cargo.toml b/examples/lang/erc20/Cargo.toml deleted file mode 100644 index 53ac5acb..00000000 --- a/examples/lang/erc20/Cargo.toml +++ /dev/null @@ -1,62 +0,0 @@ -[package] -name = "erc20" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "erc20" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "type-metadata/std", - "scale/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/erc20/src/lib.rs b/examples/lang/erc20/src/lib.rs deleted file mode 100644 index 3bf8f9ff..00000000 --- a/examples/lang/erc20/src/lib.rs +++ /dev/null @@ -1,237 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - env::DefaultSrmlTypes, - memory::format, - storage, -}; -use ink_lang::contract; -use ink_model::EnvHandler; - -contract! { - #![env = DefaultSrmlTypes] - - // Event deposited when a token transfer occurs - event Transfer { - from: Option, - to: Option, - value: Balance, - } - - // Event deposited when an approval occurs - event Approval { - owner: AccountId, - spender: AccountId, - value: Balance, - } - - /// The storage items for a typical ERC20 token implementation. - struct Erc20 { - /// The total supply. - total_supply: storage::Value, - /// The balance of each user. - balances: storage::HashMap, - /// Balances that are spendable by non-owners: (owner, spender) -> allowed - allowances: storage::HashMap<(AccountId, AccountId), Balance>, - } - - impl Deploy for Erc20 { - fn deploy(&mut self, init_value: Balance) { - self.total_supply.set(init_value); - self.balances.insert(env.caller(), init_value); - env.emit(Transfer { - from: None, - to: Some(env.caller()), - value: init_value - }); - } - } - - impl Erc20 { - /// Returns the total number of tokens in existence. - pub(external) fn total_supply(&self) -> Balance { - let total_supply = *self.total_supply; - env.println(&format!("Erc20::total_supply = {:?}", total_supply)); - total_supply - } - - /// Returns the balance of the given AccountId. - pub(external) fn balance_of(&self, owner: AccountId) -> Balance { - let balance = self.balance_of_or_zero(&owner); - env.println(&format!("Erc20::balance_of(owner = {:?}) = {:?}", owner, balance)); - balance - } - - /// Returns the amount of tokens that an owner allowed to a spender. - pub(external) fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance { - let allowance = self.allowance_or_zero(&owner, &spender); - env.println(&format!( - "Erc20::allowance(owner = {:?}, spender = {:?}) = {:?}", - owner, spender, allowance - )); - allowance - } - - /// Transfers token from the sender to the `to` AccountId. - pub(external) fn transfer(&mut self, to: AccountId, value: Balance) -> bool { - self.transfer_impl(env, env.caller(), to, value) - } - - /// Approve the passed AccountId to spend the specified amount of tokens - /// on the behalf of the message's sender. - pub(external) fn approve(&mut self, spender: AccountId, value: Balance) -> bool { - let owner = env.caller(); - self.allowances.insert((owner, spender), value); - env.emit(Approval { - owner: owner, - spender: spender, - value: value - }); - true - } - - /// Transfer tokens from one AccountId to another. - pub(external) fn transfer_from(&mut self, from: AccountId, to: AccountId, value: Balance) -> bool { - let allowance = self.allowance_or_zero(&from, &env.caller()); - if allowance < value { - return false - } - self.allowances.insert((from, env.caller()), allowance - value); - self.transfer_impl(env, from, to, value) - } - } - - impl Erc20 { - /// Returns the balance of the AccountId or 0 if there is no balance. - fn balance_of_or_zero(&self, of: &AccountId) -> Balance { - *self.balances.get(of).unwrap_or(&0) - } - - /// Returns the allowance or 0 of there is no allowance. - fn allowance_or_zero(&self, owner: &AccountId, spender: &AccountId) -> Balance { - *self.allowances.get(&(*owner, *spender)).unwrap_or(&0) - } - - /// Transfers token from a specified AccountId to another AccountId. - fn transfer_impl(&mut self, env: &mut EnvHandler>, from: AccountId, to: AccountId, value: Balance) -> bool { - let balance_from = self.balance_of_or_zero(&from); - let balance_to = self.balance_of_or_zero(&to); - if balance_from < value { - return false - } - self.balances.insert(from, balance_from - value); - self.balances.insert(to, balance_to + value); - env.emit(Transfer { - from: Some(from), - to: Some(to), - value: value - }); - true - } - } -} - -#[cfg(all(test, feature = "test-env"))] -mod tests { - use super::*; - use ink_core::env; - type Types = ink_core::env::DefaultSrmlTypes; - - #[test] - fn deployment_works() { - let alice = AccountId::from([0x0; 32]); - env::test::set_caller::(alice); - - // Deploy the contract with some `init_value` - let erc20 = Erc20::deploy_mock(1234); - // Check that the `total_supply` is `init_value` - assert_eq!(erc20.total_supply(), 1234); - // Check that `balance_of` Alice is `init_value` - assert_eq!(erc20.balance_of(alice), 1234); - } - - #[test] - fn transfer_works() { - let alice = AccountId::from([0x0; 32]); - let bob = AccountId::from([0x1; 32]); - - env::test::set_caller::(alice); - // Deploy the contract with some `init_value` - let mut erc20 = Erc20::deploy_mock(1234); - // Alice does not have enough funds for this - assert_eq!(erc20.transfer(bob, 4321), false); - // Alice can do this though - assert_eq!(erc20.transfer(bob, 234), true); - // Check Alice and Bob have the expected balance - assert_eq!(erc20.balance_of(alice), 1000); - assert_eq!(erc20.balance_of(bob), 234); - } - - #[test] - fn allowance_works() { - let alice = AccountId::from([0x0; 32]); - let bob = AccountId::from([0x1; 32]); - let charlie = AccountId::from([0x2; 32]); - - env::test::set_caller::(alice); - // Deploy the contract with some `init_value` - let mut erc20 = Erc20::deploy_mock(1234); - // Bob does not have an allowance from Alice's balance - assert_eq!(erc20.allowance(alice, bob), 0); - // Thus, Bob cannot transfer out of Alice's account - env::test::set_caller::(bob); - assert_eq!(erc20.transfer_from(alice, bob, 1), false); - // Alice can approve bob for some of her funds - env::test::set_caller::(alice); - assert_eq!(erc20.approve(bob, 20), true); - // And the allowance reflects that correctly - assert_eq!(erc20.allowance(alice, bob), 20); - // Charlie cannot send on behalf of Bob - env::test::set_caller::(charlie); - assert_eq!(erc20.transfer_from(alice, bob, 10), false); - // Bob cannot transfer more than he is allowed - env::test::set_caller::(bob); - assert_eq!(erc20.transfer_from(alice, charlie, 25), false); - // A smaller amount should work though - assert_eq!(erc20.transfer_from(alice, charlie, 10), true); - // Check that the allowance is updated - assert_eq!(erc20.allowance(alice, bob), 10); - // and the balance transferred to the right person - assert_eq!(erc20.balance_of(charlie), 10); - } - - #[test] - fn events_work() { - let alice = AccountId::from([0x0; 32]); - let bob = AccountId::from([0x1; 32]); - - // No events to start - env::test::set_caller::(alice); - assert_eq!(env::test::emitted_events::().count(), 0); - // Event should be emitted for initial minting - let mut erc20 = Erc20::deploy_mock(1234); - assert_eq!(env::test::emitted_events::().count(), 1); - // Event should be emitted for approvals - assert_eq!(erc20.approve(bob, 20), true); - assert_eq!(env::test::emitted_events::().count(), 2); - // Event should be emitted for transfers - assert_eq!(erc20.transfer(bob, 10), true); - assert_eq!(env::test::emitted_events::().count(), 3); - } -} diff --git a/examples/lang/erc721/.cargo/config b/examples/lang/erc721/.cargo/config deleted file mode 100644 index 7d5b52df..00000000 --- a/examples/lang/erc721/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] diff --git a/examples/lang/erc721/.ink/abi_gen/Cargo.toml b/examples/lang/erc721/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 38f6556b..00000000 --- a/examples/lang/erc721/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "erc721", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/erc721/.ink/abi_gen/main.rs b/examples/lang/erc721/.ink/abi_gen/main.rs deleted file mode 100644 index 9fa36177..00000000 --- a/examples/lang/erc721/.ink/abi_gen/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/erc721/Cargo.toml b/examples/lang/erc721/Cargo.toml deleted file mode 100644 index e0f68d99..00000000 --- a/examples/lang/erc721/Cargo.toml +++ /dev/null @@ -1,62 +0,0 @@ -[package] -name = "erc721" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "erc721" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "type-metadata/std", - "scale/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/erc721/src/lib.rs b/examples/lang/erc721/src/lib.rs deleted file mode 100644 index bfdc7e84..00000000 --- a/examples/lang/erc721/src/lib.rs +++ /dev/null @@ -1,528 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use core::result::Result; -use ink_core::{ - env::DefaultSrmlTypes, - memory::format, - storage, -}; -use ink_lang::contract; -use ink_model; -use scale::{ - Decode, - Encode, -}; - -pub type EnvHandler = ink_model::EnvHandler>; -pub type TokenId = u32; -pub type Counter = u32; - -#[cfg(feature = "ink-generate-abi")] -use type_metadata::Metadata; - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Copy, Clone)] -#[cfg_attr(feature = "ink-generate-abi", derive(Metadata))] -pub enum Error { - NotOwner, - NotApproved, - TokenExists, - TokenNotFound, - CanNotInsert, - CanNotRemove, - CanNotGetCounter, - AccountZeroNotAllowed, -} - -impl Error { - fn to_u32(self) -> u32 { - self as u32 - } -} - -contract! { - #![env = DefaultSrmlTypes] - - /// Event deposited when a token transfer occurs - event Transfer { - from: AccountId, - to: AccountId, - id: TokenId, - } - - /// Event deposited when an approval occurs - event Approval { - owner: AccountId, - to: AccountId, - id: TokenId, - } - - /// The storage items for a typical ERC721 token implementation. - struct Erc721 { - token_owner: storage::HashMap, - token_approvals: storage::HashMap, - owned_tokens_count: storage::HashMap, - } - - impl Deploy for Erc721 { - fn deploy(&mut self) {} - } - - impl Erc721 { - /// External wrapper for `fn balance_of()`. - pub(external) fn get_balance(&self, owner: AccountId) -> u32 { - let balance = self.balance_of(&owner); - env.println(&format!("Erc721::balance_of(owner = {:?}) = {:?}", owner, balance)); - balance - } - - /// External wrapper for `fn owner_of()`. - pub(external) fn get_owner(&self, id: u32) -> AccountId { - let owner = self.owner_of(&id); - env.println(&format!("Erc721::owner_of(token = {:?}) = {:?}", id, owner)); - owner - } - - /// External wrapper for `fn mint()`. - pub(external) fn mint_token(&mut self, to: AccountId, id: u32) -> Result<(), u32> { - self.mint(env, &to, &id)?; - env.println(&format!("Erc721::minted(token = {:?}) = {:?}", id, to)); - Ok(()) - } - - /// External wrapper for `fn transfer_from()`. - pub(external) fn transfer_token(&mut self, from: AccountId, to: AccountId, id: u32) -> Result<(), u32> { - self.transfer_from(env, &from, &to, &id)?; - env.println(&format!("Erc721::transferred(token = {:?}) = {:?}", id, to)); - Ok(()) - } - - /// External wrapper for `fn burn()`. - pub(external) fn burn_token(&mut self, from: AccountId, id: u32) -> Result<(), u32> { - self.burn(env, &from, &id)?; - env.println(&format!("Erc721::burned(token = {:?}) = {:?}", id, from)); - Ok(()) - } - - /// External wrapper for `fn approve()`. - pub(external) fn approve_transfer(&mut self, to: AccountId, id: u32) -> Result<(), u32> { - self.approve(env, &to, &id)?; - env.println(&format!("Erc721::approved(token = {:?}) = {:?}", id, to)); - Ok(()) - } - } - - impl Erc721 { - /// Transfers token `id` `from` the sender to the `to` AccountId. - fn transfer_from(&mut self, env: &EnvHandler, from: &AccountId, to: &AccountId, id: &TokenId) -> Result<(), u32> { - let caller = env.caller(); - if !self.exists(id) { - return Err(Error::TokenNotFound.to_u32()) - }; - if !self.approved_or_owner(&caller, id){ - return Err(Error::NotApproved.to_u32()) - }; - - self.clear_approval(id)?; - self.remove_token_from(from, id)?; - self.add_token_to(to, id)?; - env.emit(Transfer { - from: *from, - to: *to, - id: *id, - }); - Ok(()) - } - - /// Removes existing approval from token `id`. - fn clear_approval(&mut self, id: &TokenId) -> Result<(), u32> { - if !self.token_approvals.contains_key(id) { - return Ok(()) - }; - - match self.token_approvals.remove(id) { - Some(_res) => Ok(()), - None => Err(Error::CanNotRemove.to_u32()) - } - } - - /// Removes token `id` from the owner. - fn remove_token_from(&mut self, from: &AccountId, id: &TokenId) -> Result<(), u32> { - if !self.exists(id) { - return Err(Error::TokenNotFound.to_u32()); - }; - - self.decrease_counter_of(from)?; - self.token_owner - .remove(id) - .ok_or(Error::CanNotRemove.to_u32())?; - Ok(()) - } - - /// Adds the token `id` to the `to` AccountID. - fn add_token_to(&mut self, to: &AccountId, id: &TokenId) -> Result<(), u32> { - if self.exists(id) { - return Err(Error::TokenExists.to_u32()) - }; - if *to == AccountId::from([0x0; 32]) { - return Err(Error::AccountZeroNotAllowed.to_u32()) - }; - - self.increase_counter_of(to)?; - if !self.token_owner.insert(*id, *to).is_none() { - return Err(Error::CanNotInsert.to_u32()) - } - Ok(()) - } - - /// Approve the passed AccountId to transfer the specified token - /// on behalf of the message's sender. - fn approve(&mut self, env: &EnvHandler, to: &AccountId, id: &TokenId) -> Result<(), u32> { - let caller = env.caller(); - if self.owner_of(id) != caller { - return Err(Error::NotOwner.to_u32()) - }; - if *to == AccountId::from([0x0; 32]) { - return Err(Error::AccountZeroNotAllowed.to_u32()) - }; - - if !self.token_approvals.insert(*id, *to).is_none() { - return Err(Error::CanNotInsert.to_u32()) - }; - env.emit(Approval { - owner: caller, - to: *to, - id: *id, - }); - Ok(()) - } - - /// Creates a unique token `id` assigned to the `to` AccountId. - fn mint(&mut self, env: &EnvHandler, to: &AccountId, id: &TokenId) -> Result<(), u32> { - if *to == AccountId::from([0x0; 32]) { - return Err(Error::AccountZeroNotAllowed.to_u32()) - }; - if self.exists(id) { - return Err(Error::TokenExists.to_u32()) - } - - if !self.token_owner.insert(*id, *to).is_none() { - return Err(Error::CanNotInsert.to_u32()) - } - self.increase_counter_of(to)?; - env.emit(Transfer { - from: AccountId::from([0x0; 32]), - to: *to, - id: *id, - }); - Ok(()) - } - - /// Destroys an existing token `id` owned by the caller. - fn burn(&mut self, env: &EnvHandler, from: &AccountId, id: &TokenId)-> Result<(), u32> { - let caller = env.caller(); - if !self.exists(id) { - return Err(Error::TokenNotFound.to_u32()) - }; - if self.owner_of(id) != caller && *from != AccountId::from([0x0; 32]) { - return Err(Error::NotOwner.to_u32()) - }; - - self.clear_approval(id)?; - self.decrease_counter_of(from)?; - self.token_owner - .remove(id) - .ok_or(Error::CanNotRemove.to_u32())?; - env.emit(Transfer { - from: *from, - to: AccountId::from([0x0; 32]), - id: *id, - }); - Ok(()) - } - - /// Increase token counter from the `of` AccountId. - fn increase_counter_of(&mut self, of: &AccountId) -> Result<(), u32> { - if self.balance_of(of) > 0 { - let count = self.owned_tokens_count - .get_mut(of) - .ok_or(Error::CanNotGetCounter.to_u32())?; - *count += 1; - return Ok(()) - } else { - match self.owned_tokens_count.insert(*of, 1) { - Some(_) => Err(Error::CanNotInsert.to_u32()), - None => Ok(()), - } - } - } - - /// Decrease token counter from the `of` AccountId. - fn decrease_counter_of(&mut self, of: &AccountId) -> Result<(), u32> { - let count = self.owned_tokens_count - .get_mut(of) - .ok_or(Error::CanNotGetCounter.to_u32())?; - *count -= 1; - Ok(()) - } - - /// Returns the total number of tokens from an account. - fn balance_of(&self, of: &AccountId) -> u32 { - *self.owned_tokens_count.get(of).unwrap_or(&0u32) - } - - /// Returns the owner of a token or AccountId 0x0 if it does not exists. - fn owner_of(&self, id: &TokenId) -> AccountId { - *self.token_owner.get(id).unwrap_or(&AccountId::from([0x0; 32])) - } - - /// Returns the approved AccountId froma token `id` - /// or AccountId 0x0 if it does not exists. - fn approved_for(&self, id: &TokenId) -> AccountId { - *self.token_approvals.get(id).unwrap_or(&AccountId::from([0x0; 32])) - } - - /// Returns true if the AccountId `from` is the owner of token `id` - /// or it has been approved on behalf of the token `id` owner. - fn approved_or_owner(&self, from: &AccountId, id: &TokenId) -> bool { - *from != AccountId::from([0x0; 32]) && - (*from == self.owner_of(id) || *from == self.approved_for(id)) - } - - /// Returns true if token `id` exists or false if it does not. - fn exists(&self, id: &TokenId) -> bool { - self.token_owner.get(id).is_some() && self.token_owner.contains_key(id) - } - } -} - -#[cfg(all(test, feature = "test-env"))] -mod tests { - use super::*; - use ink_core::env; - type Types = ink_core::env::DefaultSrmlTypes; - type Erc721Test = test::TestableErc721; - - /// Generate testing accounts. - fn generate_accounts(length: u8) -> Option> { - if length > 0 { - let mut accounts: Vec = vec![AccountId::from([0x0; 32]); 1]; - for n in 1..=length { - accounts.push(AccountId::from([n; 32])); - } - Some(accounts) - } else { - None - } - } - - /// Deploys an ERC721 test contract. - fn initialize_erc721(from: AccountId) -> Erc721Test { - env::test::set_caller::(from); - Erc721::deploy_mock() - } - - #[test] - fn deployment_works() { - let accounts = generate_accounts(3).unwrap(); - let erc721 = initialize_erc721(accounts[0]); - // AccountId 0 can not owns tokens. - assert_eq!(erc721.get_balance(accounts[0]), 0); - // Token 0 does not exists. - assert_eq!(erc721.get_owner(0), accounts[0]); - } - - #[test] - fn mint_works() { - let accounts = generate_accounts(2).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Token 1 does not exists. - assert_eq!(erc721.get_owner(1), accounts[0]); - // AccountId 1 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[1]), 0); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 1 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - // Create token Id 2 owned by AccountId 1. - assert_eq!(erc721.mint_token(accounts[1], 2), Ok(())); - // AccountId 1 owns 2 tokens. - assert_eq!(erc721.get_balance(accounts[1]), 2); - // Token Id 2 is owned by AccountId 1. - assert_eq!(erc721.get_owner(2), accounts[1]); - // Create token Id 3. - assert_eq!(erc721.mint_token(accounts[2], 3), Ok(())); - // Account Id 2 owns 1 token. - assert_eq!(erc721.get_balance(accounts[2]), 1); - // Token Id 3 is owned by AccountId 2. - assert_eq!(erc721.get_owner(3), accounts[2]); - } - - #[test] - fn mint_existing_should_fail() { - let accounts = generate_accounts(2).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 1 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - // Cannot create token Id if it exists. - // AccountId 2 cannot own token Id 1. - assert_eq!( - erc721.mint_token(accounts[2], 1), - Err(Error::TokenExists.to_u32()) - ); - // AccountId 2 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[2]), 0); - // AccountId 1 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - } - - #[test] - fn transfer_works() { - let accounts = generate_accounts(2).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 1 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - // Change transaction caller to AccountId 1. - env::test::set_caller::(accounts[1]); - // Transfer token Id 1 from AccountId 1 to AccountId 2. - assert_eq!(erc721.transfer_token(accounts[1], accounts[2], 1), Ok(())); - // AccountId 1 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[1]), 0); - // AccountId 2 owns 1 token. - assert_eq!(erc721.get_balance(accounts[2]), 1); - // AccountId 2 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[2]); - } - - #[test] - fn invalid_transfer_should_fail() { - let accounts = generate_accounts(3).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Transfer token fails if it does not exists. - assert_eq!( - erc721.transfer_token(accounts[1], accounts[2], 2), - Err(Error::TokenNotFound.to_u32()) - ); - // Token Id 2 does not exists. - assert_eq!(erc721.get_owner(2), accounts[0]); - // Create token Id 2. - assert_eq!(erc721.mint_token(accounts[1], 2), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // Token Id 2 is owned by AccountId 1. - assert_eq!(erc721.get_owner(2), accounts[1]); - // Change transaction caller to AccountId 2. - env::test::set_caller::(accounts[2]); - // AccountId 2 cannot transfer not owned tokens. - assert_eq!( - erc721.transfer_token(accounts[1], accounts[2], 2), - Err(Error::NotApproved.to_u32()) - ); - } - - #[test] - fn burn_works() { - let accounts = generate_accounts(3).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 1 owns token Id 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - // Change transaction caller to AccountId 1. - env::test::set_caller::(accounts[1]); - // Transfer Token Id 1 from AccountId 1 to AccountId 2. - assert_eq!(erc721.transfer_token(accounts[1], accounts[2], 1), Ok(())); - // AccountId 1 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[1]), 0); - // AccountId 2 owns token 1. - assert_eq!(erc721.get_balance(accounts[2]), 1); - // Token Id 1 is owned by AccountId 2. - assert_eq!(erc721.get_owner(1), accounts[2]); - // Change transaction caller to AccountId 2. - env::test::set_caller::(accounts[2]); - // Destroy token Id 1. - assert_eq!(erc721.burn_token(accounts[2], 1), Ok(())); - // AccountId 2 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[2]), 0); - } - - #[test] - fn approved_transfer_works() { - let accounts = generate_accounts(3).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // Token Id 1 is owned by AccountId 1. - assert_eq!(erc721.get_owner(1), accounts[1]); - // Change transaction caller to AccountId 1. - env::test::set_caller::(accounts[1]); - // Approve token Id 1 transfer for AccountId 2 on behalf of the owner. - assert_eq!(erc721.approve_transfer(accounts[2], 1), Ok(())); - // Change transaction caller to AccountId 2. - env::test::set_caller::(accounts[2]); - // Transfer token Id 1 from AccountId 1 to AccountId 3. - assert_eq!(erc721.transfer_token(accounts[1], accounts[3], 1), Ok(())); - // TokenId 3 is owned by AccountId 3. - assert_eq!(erc721.get_owner(1), accounts[3]); - // AccountId 1 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[1]), 0); - // AccountId 2 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[2]), 0); - // AccountId 3 owns 1 token. - assert_eq!(erc721.get_balance(accounts[3]), 1); - } - - #[test] - fn not_approved_transfer_should_fail() { - let accounts = generate_accounts(3).unwrap(); - let mut erc721 = initialize_erc721(accounts[0]); - // Create token Id 1. - assert_eq!(erc721.mint_token(accounts[1], 1), Ok(())); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 2 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[2]), 0); - // AccountId 3 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[3]), 0); - // Change transaction caller to AccountId 2. - env::test::set_caller::(accounts[2]); - // AccountId 3 is not approved by AccountId 1. - assert_eq!( - erc721.transfer_token(accounts[1], accounts[3], 1), - Err(Error::NotApproved.to_u32()) - ); - // AccountId 1 owns 1 token. - assert_eq!(erc721.get_balance(accounts[1]), 1); - // AccountId 2 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[2]), 0); - // AccountId 3 does not owns tokens. - assert_eq!(erc721.get_balance(accounts[3]), 0); - } -} diff --git a/examples/lang/events/.cargo/config b/examples/lang/events/.cargo/config deleted file mode 100644 index 9ed784e6..00000000 --- a/examples/lang/events/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] \ No newline at end of file diff --git a/examples/lang/events/.gitignore b/examples/lang/events/.gitignore deleted file mode 100644 index bf910de1..00000000 --- a/examples/lang/events/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock \ No newline at end of file diff --git a/examples/lang/events/.ink/abi_gen/Cargo.toml b/examples/lang/events/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 6c513fd4..00000000 --- a/examples/lang/events/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "events", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/events/.ink/abi_gen/main.rs b/examples/lang/events/.ink/abi_gen/main.rs deleted file mode 100644 index 576885b1..00000000 --- a/examples/lang/events/.ink/abi_gen/main.rs +++ /dev/null @@ -1,8 +0,0 @@ - -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/events/Cargo.toml b/examples/lang/events/Cargo.toml deleted file mode 100644 index 8293b56a..00000000 --- a/examples/lang/events/Cargo.toml +++ /dev/null @@ -1,62 +0,0 @@ -[package] -name = "events" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "events" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/events/src/lib.rs b/examples/lang/events/src/lib.rs deleted file mode 100644 index 7c21afb8..00000000 --- a/examples/lang/events/src/lib.rs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::storage; -use ink_core::env::{ - ContractEnv, - DefaultSrmlTypes, -}; -use ink_lang::contract; - -contract! { - #![env = DefaultSrmlTypes] - - /// Tests emitting of custom defined events. - struct CallCounter { - /// A simple counter for the calls. - count: storage::Value, - } - - impl Deploy for CallCounter { - fn deploy(&mut self) { - self.count.set(0) - } - } - - event IncCalled { current: u32 } - event DecCalled { current: u32 } - - impl CallCounter { - /// Increments the internal counter. - /// - /// # Note - /// - /// Also emits an event. - pub(external) fn inc(&mut self) { - self.count += 1; - env.emit(IncCalled { current: *self.count }); - } - - /// Decrements the internal counter. - /// - /// # Note - /// - /// Also emits an event. - pub(external) fn dec(&mut self) { - self.dec_internal(env); - } - } - - impl CallCounter { - fn dec_internal(&mut self, env: &mut ink_model::EnvHandler>) { - self.count -= 1; - env.emit(DecCalled { current: *self.count }); - } - } -} - -#[cfg(all(test, feature = "test-env"))] -mod tests { - use super::*; - use ink_core::env; - - #[test] - fn it_works() { - let mut contract = CallCounter::deploy_mock(); - assert_eq!(env::test::emitted_events::().count(), 0); - contract.inc(); - assert_eq!(env::test::emitted_events::().count(), 1); - contract.dec(); - assert_eq!(env::test::emitted_events::().count(), 2); - } -} diff --git a/examples/lang/flipper/.cargo/config b/examples/lang/flipper/.cargo/config deleted file mode 100644 index 9ed784e6..00000000 --- a/examples/lang/flipper/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] \ No newline at end of file diff --git a/examples/lang/flipper/.gitignore b/examples/lang/flipper/.gitignore deleted file mode 100644 index bf910de1..00000000 --- a/examples/lang/flipper/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock \ No newline at end of file diff --git a/examples/lang/flipper/.ink/abi_gen/Cargo.toml b/examples/lang/flipper/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 6d66e0a1..00000000 --- a/examples/lang/flipper/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "flipper", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/flipper/.ink/abi_gen/main.rs b/examples/lang/flipper/.ink/abi_gen/main.rs deleted file mode 100644 index 576885b1..00000000 --- a/examples/lang/flipper/.ink/abi_gen/main.rs +++ /dev/null @@ -1,8 +0,0 @@ - -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/flipper/Cargo.toml b/examples/lang/flipper/Cargo.toml deleted file mode 100644 index 68a2fedf..00000000 --- a/examples/lang/flipper/Cargo.toml +++ /dev/null @@ -1,62 +0,0 @@ -[package] -name = "flipper" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "flipper" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/flipper/src/lib.rs b/examples/lang/flipper/src/lib.rs deleted file mode 100644 index 48da5dc4..00000000 --- a/examples/lang/flipper/src/lib.rs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - env::DefaultSrmlTypes, - memory::format, - storage, -}; -use ink_lang::contract; - -contract! { - #![env = DefaultSrmlTypes] - - /// This simple dummy contract has a `bool` value that can - /// alter between `true` and `false` using the `flip` message. - /// Users can retrieve its current state using the `get` message. - struct Flipper { - /// The current state of our flag. - value: storage::Value, - } - - impl Deploy for Flipper { - /// Initializes our state to `false` upon deploying our smart contract. - fn deploy(&mut self) { - self.value.set(false) - } - } - - impl Flipper { - /// Flips the current state of our smart contract. - pub(external) fn flip(&mut self) { - *self.value = !*self.value; - } - - /// Returns the current state. - pub(external) fn get(&self) -> bool { - env.println(&format!("Flipper Value: {:?}", *self.value)); - *self.value - } - } -} - -#[cfg(test)] -mod tests { - use super::Flipper; - - #[test] - fn it_works() { - let mut flipper = Flipper::deploy_mock(); - assert_eq!(flipper.get(), false); - flipper.flip(); - assert_eq!(flipper.get(), true); - } -} diff --git a/examples/lang/incrementer/.cargo/config b/examples/lang/incrementer/.cargo/config deleted file mode 100644 index 9ed784e6..00000000 --- a/examples/lang/incrementer/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] \ No newline at end of file diff --git a/examples/lang/incrementer/.gitignore b/examples/lang/incrementer/.gitignore deleted file mode 100644 index bf910de1..00000000 --- a/examples/lang/incrementer/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock \ No newline at end of file diff --git a/examples/lang/incrementer/.ink/abi_gen/Cargo.toml b/examples/lang/incrementer/.ink/abi_gen/Cargo.toml deleted file mode 100644 index be1383b1..00000000 --- a/examples/lang/incrementer/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "incrementer", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/incrementer/.ink/abi_gen/main.rs b/examples/lang/incrementer/.ink/abi_gen/main.rs deleted file mode 100644 index 576885b1..00000000 --- a/examples/lang/incrementer/.ink/abi_gen/main.rs +++ /dev/null @@ -1,8 +0,0 @@ - -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/incrementer/Cargo.toml b/examples/lang/incrementer/Cargo.toml deleted file mode 100644 index 9539788f..00000000 --- a/examples/lang/incrementer/Cargo.toml +++ /dev/null @@ -1,63 +0,0 @@ -[package] -name = "incrementer" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "incrementer" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", - "type-metadata/std", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/incrementer/src/lib.rs b/examples/lang/incrementer/src/lib.rs deleted file mode 100644 index eba49e33..00000000 --- a/examples/lang/incrementer/src/lib.rs +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{ - memory::format, - storage, -}; -use ink_lang::contract; - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// A simple incrementer contract that can only increment, - /// compare and return its internal value. - struct Incrementer { - /// The current value. - value: storage::Value, - } - - impl Deploy for Incrementer { - /// Initializes the value to the initial value. - fn deploy(&mut self, init_value: u32) { - self.value.set(init_value) - } - } - - impl Incrementer { - /// Flips the current state of our smart contract. - pub(external) fn inc(&mut self, by: u32) { - env.println(&format!("Incrementer::inc by = {:?}", by)); - self.value += by; - } - - /// Returns the current state. - pub(external) fn get(&self) -> u32 { - env.println(&format!("Incrementer::get = {:?}", *self.value)); - *self.value - } - - /// Returns `true` if the internal value is greater than or equal to the provided value. - pub(external) fn compare(&self, with: u32) -> bool { - env.println(&format!("Incrementer::compare self.value >= with = {:?}", *self.value >= with)); - *self.value >= with - } - } -} - -#[cfg(test)] -mod tests { - use super::Incrementer; - - #[test] - fn it_works() { - let mut incrementer = Incrementer::deploy_mock(5); - assert_eq!(incrementer.get(), 5); - incrementer.inc(42); - assert_eq!(incrementer.get(), 47); - incrementer.inc(0); - assert_eq!(incrementer.get(), 47); - } -} diff --git a/examples/lang/shared_vec/.cargo/config b/examples/lang/shared_vec/.cargo/config deleted file mode 100644 index 9ed784e6..00000000 --- a/examples/lang/shared_vec/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[target.wasm32-unknown-unknown] -rustflags = [ - "-C", "link-args=-z stack-size=65536 --import-memory" -] \ No newline at end of file diff --git a/examples/lang/shared_vec/.gitignore b/examples/lang/shared_vec/.gitignore deleted file mode 100644 index bf910de1..00000000 --- a/examples/lang/shared_vec/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock \ No newline at end of file diff --git a/examples/lang/shared_vec/.ink/abi_gen/Cargo.toml b/examples/lang/shared_vec/.ink/abi_gen/Cargo.toml deleted file mode 100644 index 5964dfa9..00000000 --- a/examples/lang/shared_vec/.ink/abi_gen/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "abi-gen" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" -publish = false - -[[bin]] -name = "abi-gen" -path = "main.rs" - -[dependencies] -contract = { path = "../..", package = "shared_vec", features = ["ink-generate-abi"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/examples/lang/shared_vec/.ink/abi_gen/main.rs b/examples/lang/shared_vec/.ink/abi_gen/main.rs deleted file mode 100644 index 576885b1..00000000 --- a/examples/lang/shared_vec/.ink/abi_gen/main.rs +++ /dev/null @@ -1,8 +0,0 @@ - -fn main() -> Result<(), std::io::Error> { - let abi = contract::ink_generate_abi(); - let contents = serde_json::to_string_pretty(&abi)?; - std::fs::create_dir("target").ok(); - std::fs::write("target/abi.json", contents)?; - Ok(()) -} diff --git a/examples/lang/shared_vec/Cargo.toml b/examples/lang/shared_vec/Cargo.toml deleted file mode 100644 index 9b2148ca..00000000 --- a/examples/lang/shared_vec/Cargo.toml +++ /dev/null @@ -1,62 +0,0 @@ -[package] -name = "shared_vec" -version = "0.1.0" -authors = ["Parity Technologies "] -edition = "2018" - -[dependencies] -ink_abi = { path = "../../../abi", default-features = false, features = ["derive"], optional = true } -ink_core = { path = "../../../core", default-features = false } -ink_model = { path = "../../../model", default-features = false } -ink_lang = { path = "../../../lang", default-features = false } - -scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] } -type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true } - -[lib] -name = "shared_vec" -crate-type = [ - # Used for normal contract Wasm blobs. - "cdylib", - # Used for ABI generation. - "rlib", -] - -[features] -default = ["test-env"] -std = [ - "ink_abi/std", - "ink_core/std", - "ink_model/std", - "ink_lang/std", - "scale/std", - "type-metadata/std", -] -test-env = [ - "std", - "ink_core/test-env", - "ink_model/test-env", - "ink_lang/test-env", -] -ink-generate-abi = [ - "std", - "ink_abi", - "type-metadata", - "ink_core/ink-generate-abi", - "ink_lang/ink-generate-abi", -] -ink-as-dependency = [] - -[profile.release] -panic = "abort" -lto = true -opt-level = "z" -overflow-checks = true - -[workspace] -members = [ - ".ink/abi_gen" -] -exclude = [ - ".ink" -] diff --git a/examples/lang/shared_vec/src/lib.rs b/examples/lang/shared_vec/src/lib.rs deleted file mode 100644 index 937d4b6c..00000000 --- a/examples/lang/shared_vec/src/lib.rs +++ /dev/null @@ -1,259 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of ink!. -// -// ink! is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ink! is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ink!. If not, see . - -#![cfg_attr(not(feature = "std"), no_std)] - -use ink_core::{storage, storage::Flush}; -use ink_lang::contract; -use scale::{Encode, Decode}; - -#[cfg(feature = "ink-generate-abi")] -use type_metadata::Metadata; - -/// Access rights to the shared vector. -#[derive(Encode, Decode)] -#[cfg_attr(feature = "ink-generate-abi", derive(Metadata))] -pub struct Access { - pub begin: Option, - pub end: Option, -} - -impl Flush for Access { - fn flush(&mut self) {} -} - -impl Access { - /// Creates unlimited access rights. - pub fn unlimited() -> Self { - Access { begin: None, end: None } - } - - /// Returns `true` if the access is unlimited. - pub fn is_unlimited(&self) -> bool { - self.begin.is_none() && self.end.is_none() - } - - /// Creates new limited access rights. - pub fn new(begin: B, end: E) -> Self - where - B: Into>, - E: Into>, - { - let begin = begin.into(); - let end = end.into(); - assert!(begin <= end); - Access { begin, end } - } - - /// Returns `true` if the given index is within access rights. - pub fn contains(&self, index: u32) -> bool { - let begin = self.begin.unwrap_or(0); - let end = self.end.unwrap_or(core::u32::MAX); - begin <= index && index <= end - } -} - -pub type ErrNo = u32; -pub type Result = core::result::Result; - -const ACCESS_NOT_ALLOWED: ErrNo = 1; -const NOT_REGISTERED: ErrNo = 2; -const OUT_OF_BOUNDS: ErrNo = 3; - -contract! { - #![env = ink_core::env::DefaultSrmlTypes] - - /// A shared vector that is accessiable to a subset of allowed mutators. - struct SharedVec { - /// The allowed mutators. - /// - /// They can operate on a range within the shared vector. - /// The range is defined by `(start,end)` where `start` and `end` - /// refer to the zero-starting index. A value of `None` means - /// that there is no lower or upper bound. - mutators: storage::HashMap, - /// The shared vector. - vec: storage::Vec, - } - - /// Fires whenever a new mutator is registered - /// or when a mutators access rights are changed. - event Register { - /// The mutator. - mutator: AccountId, - /// The begin access index. - begin: Option, - /// The end access index. - end: Option, - } - - /// Fires whenever a mutator pushes the vector successfully. - event Push { - /// The mutator. - mutator: AccountId, - /// The pushed value. - value: i32, - } - - /// Fires whenever a mutator changes the vector. - event Mutate { - /// The index where the change happened. - at: u32, - /// The new value. - value: i32, - } - - impl Deploy for SharedVec { - /// Initializes the value to the initial value. - fn deploy(&mut self) { - self.mutators.insert(env.caller(), Access::unlimited()); - } - } - - impl SharedVec { - /// Returns the users access if registered or an appropriate error. - fn validate_access(&self, mutator: AccountId) -> Result<&Access, u32> { - if let Some(access) = self.mutators.get(&mutator) { - Ok(access) - } else { - Err(NOT_REGISTERED) - } - } - - /// Pushes a new value to the shared vector. - /// - /// # Errors - /// - /// - If the caller does not have unlimited access rights. - pub(external) fn push(&mut self, value: i32) -> Result<(), u32> { - let access = self.validate_access(env.caller())?; - if !access.is_unlimited() { - return Err(ACCESS_NOT_ALLOWED) - } - self.vec.push(value); - env.emit(Push { mutator: env.caller(), value }); - Ok(()) - } - - /// Registers a new user with the given access rights. - /// - /// Can also be used to change access rights of an already existing user. - /// - /// # Errors - /// - /// - If the caller does not have unlimited access rights. - pub(external) fn register( - &mut self, - mutator: AccountId, - begin: Option, - end: Option - ) -> Result<(), u32> { - let access = self.validate_access(env.caller())?; - if !access.is_unlimited() { - return Err(ACCESS_NOT_ALLOWED) - } - self.mutators.insert(mutator, Access::new(begin, end)); - env.emit(Register { mutator, begin, end }); - Ok(()) - } - - /// Sets the value at the given position to the given value. - /// - /// Returns the previous value. - /// - /// # Errors - /// - /// - If the given position is out of bounds. - /// - If the caller does not have the required access rights. - pub(external) fn set(&mut self, at: u32, to: i32) -> Result { - let access = self.validate_access(env.caller())?; - if !access.contains(at) { - return Err(ACCESS_NOT_ALLOWED) - } - let res = self.vec - .replace(at, move || to) - .ok_or(OUT_OF_BOUNDS)?; - env.emit(Mutate { at, value: to }); - Ok(res) - } - - /// Returns the value of the shared vector at the given position - /// or `None` if the access is out of bounds. - pub(external) fn get(&self, at: u32) -> Option { - self.vec.get(at).cloned() - } - - /// Returns the length of the shared vector. - pub(external) fn len(&self) -> u32 { - self.vec.len() - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use ink_core::env; - use std::convert::TryFrom; - use ink_core::env::DefaultSrmlTypes; - - #[test] - fn it_works() { - let alice = AccountId::try_from([0x0; 32]).unwrap(); - let bob = AccountId::try_from([0x1; 32]).unwrap(); - let charly = AccountId::try_from([0x2; 32]).unwrap(); - - env::test::set_caller::(alice); - - let mut shared_vec = SharedVec::deploy_mock(); - assert_eq!(shared_vec.len(), 0); - assert_eq!(shared_vec.push(5), Ok(())); - assert_eq!(shared_vec.len(), 1); - assert_eq!(shared_vec.register(bob, Some(0), Some(1)), Ok(())); - assert_eq!(shared_vec.push(42), Ok(())); - assert_eq!(shared_vec.push(1337), Ok(())); - assert_eq!(shared_vec.push(77), Ok(())); - assert_eq!(shared_vec.len(), 4); - assert_eq!(shared_vec.set(1, 1000), Ok(42)); - assert_eq!(shared_vec.set(2, 2000), Ok(1337)); - assert_eq!(shared_vec.set(5, 3000), Err(OUT_OF_BOUNDS)); - assert_eq!(shared_vec.get(0), Some(5)); - assert_eq!(shared_vec.get(1), Some(1000)); - assert_eq!(shared_vec.get(2), Some(2000)); - assert_eq!(shared_vec.get(3), Some(77)); - - env::test::set_caller::(bob); - - assert_eq!(shared_vec.set(1, 999), Ok(1000)); - assert_eq!(shared_vec.set(2, 1999), Err(ACCESS_NOT_ALLOWED)); - assert_eq!(shared_vec.set(5, 3000), Err(ACCESS_NOT_ALLOWED)); - assert_eq!(shared_vec.register(charly, Some(0), Some(2)), Err(ACCESS_NOT_ALLOWED)); - assert_eq!(shared_vec.get(0), Some(5)); - assert_eq!(shared_vec.get(1), Some(999)); - assert_eq!(shared_vec.get(2), Some(2000)); - assert_eq!(shared_vec.get(3), Some(77)); - - env::test::set_caller::(charly); - - assert_eq!(shared_vec.set(1, 888), Err(NOT_REGISTERED)); - assert_eq!(shared_vec.set(5, 3000), Err(NOT_REGISTERED)); - assert_eq!(shared_vec.register(charly, Some(1), Some(1)), Err(NOT_REGISTERED)); - assert_eq!(shared_vec.get(0), Some(5)); - assert_eq!(shared_vec.get(1), Some(999)); - assert_eq!(shared_vec.get(2), Some(2000)); - assert_eq!(shared_vec.get(3), Some(77)); - } -} -- GitLab