Skip to content

Commit

Permalink
ERC-1155 Example (#800)
Browse files Browse the repository at this point in the history
* Add basic contract skeleton

* Add dummy ERC-1155 trait implementations

* Implement `balance_of` method

* First attempt at `balance_of_batch` implementation

I'm not sure if the output format is correct, need to read the docs
more closely

* Implement simple token transfer

* Flatten balances BTreeMap

* Clean up account usage in tests

* Implement approval mechanism

* Fix bug when sending tokens to an account with zero balance

* Check approvals before sending tokens

* Suppress warnings

* Appease Clippy

* Add crude support for token transfers to smart contracts

* Simplify check for smart contract-ness

* Handle receiving tokens as a smart contract

* Implement `safe_transfer_from` method

* Only do approval and recipient checks during in batch transfers

* I was wrong about the compiler's cleverness...

* Add documentation about interface

* Make better use of some imports

* Disallow owners from approving themselves for token tranfers

* Allow creating and minting tokens

* Derive default for storage struct

* Add note on on-chain panic

* Remove `with_balances` constructor

It wasn't ERC-1155 compliant (no transfer events emitted) and it
also leaked the internal structure of how balances were tracked.

* RustFmt with Nightly

Not sure I like some of the decisions though...

* Tag on_received messages with selectors

* Add missing event

* Index topics in events

* Remove note on BTreeSet usage

Can't figure out how to get tests to compile with it.

* Stop panicking on cross-contract call error

However, this is only because I have no feedback on why this call
is actually failing. This behaviour should be added back.

* Nightly RustFmt

* Fix RustDoc links

* Remove inline questions

* Remove unused `data` argument from `create/mint`

* Rename magic value contants

* Remove data argument from `mint/create` tests

* Use entry API when decreasing account balance

* Extract approvals pairs into struct

This is better in terms of type safety and ease of use

* Improve some of the panic messages

* Cache calls to `self.env().caller()`

* Allow `TransferSingle` events to contain Optional addresses

This slightly deviates from the spec which says we should use the `0x00`
address during certain operations, but this is more idiomatic Rust.

* Add logging around calls to `onERC1155Received`

* Improve debug message when receiving cross-contract results

* Move warning lints to specific lines of code

* Format code

* Remove backticks from URLs

Co-authored-by: Michael Müller <[email protected]>

* Fix comment wording/typo

* Add expected panic messages to tests

* Move imports related to x-contract calls closer to use site

* Change selector bytes to hex for the humans

* Remove incorrect comment about off-chain environment testing

* Add documentation for `TokenId`

This will make sure that it doesn't show up as `u128` in
the generated docs.

* Nightly RustFmt

* Uppercase selector bytes

* Don't repeat `erc_1155` in `Erc1155TokenReceiver` methods

* Nightly RustFmt

* Appease the spellchecker

* Use Environment typedef

* Allow tests to run in stable and experimental off-chain envs

* Add explanation as to why we don't accept tokens

* Return `Result` when minting tokens

* Allow (most) errors to be handled gracefully by caller

* Nightly RustFmt

* Add shorthand zero-address to allowed spelling list

* Run tests with `--features ink-experimental-engine` in CI

* Perform batch balance checks before trying to transfer tokens

* Move smart contract transfer checks to their own helper function

* Appease Clippy

* Make `ensure` macro definition more explicit

Co-authored-by: Robin Freyler <[email protected]>

* Iterate over values instead of references

Co-authored-by: Robin Freyler <[email protected]>

* Iterate over references again

* Return a value from `on_batch_received`

* Don't collect into intermediate Vec

* Wrap 0x00 in code blocks

This way the spellchecker will ignore it and we
can avoid adding it to our dictionary.

Co-authored-by: Michael Müller <[email protected]>
Co-authored-by: Robin Freyler <[email protected]>
  • Loading branch information
3 people authored Jul 8, 2021
1 parent 4e2c7c3 commit afc4871
Show file tree
Hide file tree
Showing 5 changed files with 876 additions and 0 deletions.
1 change: 1 addition & 0 deletions .config/cargo_spellcheck.dic
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Polkadot
RPC
SHA
UI
URI
Wasm
Wasm32
WebAssembly
Expand Down
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ examples-test-experimental-engine:
# We test only the examples for which the tests have already been migrated to
# use the experimental engine.
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/erc20/Cargo.toml
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/erc1155/Cargo.toml
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/contract-terminate/Cargo.toml
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/contract-transfer/Cargo.toml

Expand Down
9 changes: 9 additions & 0 deletions examples/erc1155/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 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
38 changes: 38 additions & 0 deletions examples/erc1155/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "erc1155"
version = "3.0.0-rc3"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
ink_primitives = { version = "3.0.0-rc3", path = "../../crates/primitives", default-features = false }
ink_metadata = { version = "3.0.0-rc3", path = "../../crates/metadata", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0.0-rc3", path = "../../crates/env", default-features = false, features = ["ink-debug"] }
ink_storage = { version = "3.0.0-rc3", path = "../../crates/storage", default-features = false }
ink_lang = { version = "3.0.0-rc3", path = "../../crates/lang", default-features = false }
ink_prelude = { version = "3.0.0-rc3", path = "../../crates/prelude", default-features = false }

scale = { package = "parity-scale-codec", version = "2.1", default-features = false, features = ["derive"] }
scale-info = { version = "0.6", default-features = false, features = ["derive"], optional = true }

[lib]
name = "erc1155"
path = "lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"ink_prelude/std",
"scale/std",
"scale-info",
"scale-info/std",
]
ink-as-dependency = []
ink-experimental-engine = ["ink_env/ink-experimental-engine"]
Loading

0 comments on commit afc4871

Please sign in to comment.