From e88b76dadc62d2bcb3ad25c38b12292fee36ac5a Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 19 Oct 2023 09:52:31 +0300 Subject: [PATCH 1/3] contract from template --- Cargo.lock | 17 ++ Cargo.toml | 2 + chain-factory/.gitignore | 7 + chain-factory/Cargo.toml | 18 +++ chain-factory/meta/Cargo.toml | 14 ++ chain-factory/meta/src/main.rs | 3 + chain-factory/multiversx.json | 3 + chain-factory/mxsc-template.toml | 18 +++ chain-factory/src/lib.rs | 9 ++ chain-factory/wasm/Cargo.lock | 219 +++++++++++++++++++++++++ chain-factory/wasm/Cargo.toml | 27 ++++ chain-factory/wasm/src/lib.rs | 28 ++++ esdt-safe/.gitignore | 4 - esdt-safe/wasm/Cargo.lock | 263 +++++++++++++++++++++++++++++++ 14 files changed, 628 insertions(+), 4 deletions(-) create mode 100644 chain-factory/.gitignore create mode 100644 chain-factory/Cargo.toml create mode 100644 chain-factory/meta/Cargo.toml create mode 100644 chain-factory/meta/src/main.rs create mode 100644 chain-factory/multiversx.json create mode 100644 chain-factory/mxsc-template.toml create mode 100644 chain-factory/src/lib.rs create mode 100644 chain-factory/wasm/Cargo.lock create mode 100644 chain-factory/wasm/Cargo.toml create mode 100644 chain-factory/wasm/src/lib.rs create mode 100644 esdt-safe/wasm/Cargo.lock diff --git a/Cargo.lock b/Cargo.lock index c0c120b5..03ac3ef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,6 +304,23 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chain-factory" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "multiversx-sc-scenario", + "num-bigint", +] + +[[package]] +name = "chain-factory-meta" +version = "0.0.0" +dependencies = [ + "chain-factory", + "multiversx-sc-meta", +] + [[package]] name = "cipher" version = "0.4.4" diff --git a/Cargo.toml b/Cargo.toml index fea941dd..389cadef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,7 @@ [workspace] members = [ + "chain-factory", + "chain-factory/meta", "esdt-safe", "esdt-safe/meta", ] diff --git a/chain-factory/.gitignore b/chain-factory/.gitignore new file mode 100644 index 00000000..2c76bc98 --- /dev/null +++ b/chain-factory/.gitignore @@ -0,0 +1,7 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +*/target/ + +# The mxpy output +/output*/ diff --git a/chain-factory/Cargo.toml b/chain-factory/Cargo.toml new file mode 100644 index 00000000..cb74c255 --- /dev/null +++ b/chain-factory/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "chain-factory" +version = "0.0.0" +authors = [ "you",] +edition = "2018" +publish = false + +[lib] +path = "src/lib.rs" + +[dev-dependencies] +num-bigint = "0.4.2" + +[dependencies.multiversx-sc] +version = "=0.43.5" + +[dev-dependencies.multiversx-sc-scenario] +version = "=0.43.5" diff --git a/chain-factory/meta/Cargo.toml b/chain-factory/meta/Cargo.toml new file mode 100644 index 00000000..091c6c63 --- /dev/null +++ b/chain-factory/meta/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "chain-factory-meta" +version = "0.0.0" +edition = "2018" +publish = false +authors = [ "you",] + +[dev-dependencies] + +[dependencies.chain-factory] +path = ".." + +[dependencies.multiversx-sc-meta] +version = "=0.43.5" diff --git a/chain-factory/meta/src/main.rs b/chain-factory/meta/src/main.rs new file mode 100644 index 00000000..732e0b9f --- /dev/null +++ b/chain-factory/meta/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + multiversx_sc_meta::cli_main::(); +} diff --git a/chain-factory/multiversx.json b/chain-factory/multiversx.json new file mode 100644 index 00000000..73655396 --- /dev/null +++ b/chain-factory/multiversx.json @@ -0,0 +1,3 @@ +{ + "language": "rust" +} \ No newline at end of file diff --git a/chain-factory/mxsc-template.toml b/chain-factory/mxsc-template.toml new file mode 100644 index 00000000..441addd7 --- /dev/null +++ b/chain-factory/mxsc-template.toml @@ -0,0 +1,18 @@ +name = "empty" +contract_trait = "EmptyContract" +src_file = "empty.rs" +rename_pairs = [ + [ + "blockchain.set_current_dir_from_workspace(\"contracts/examples/empty\");", + "// blockchain.set_current_dir_from_workspace(\"relative path to your workspace, if applicable\");", + ], +] +files_include = [ + "meta", + "scenarios", + "src", + "tests", + "wasm/Cargo.toml", + "Cargo.toml", + "multiversx.json", +] diff --git a/chain-factory/src/lib.rs b/chain-factory/src/lib.rs new file mode 100644 index 00000000..88cc50c6 --- /dev/null +++ b/chain-factory/src/lib.rs @@ -0,0 +1,9 @@ +#![no_std] + +multiversx_sc::imports!(); + +#[multiversx_sc::contract] +pub trait ChainFactoryContract { + #[init] + fn init(&self) {} +} diff --git a/chain-factory/wasm/Cargo.lock b/chain-factory/wasm/Cargo.lock new file mode 100644 index 00000000..77e68358 --- /dev/null +++ b/chain-factory/wasm/Cargo.lock @@ -0,0 +1,219 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", +] + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chain-factory" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "chain-factory-wasm" +version = "0.0.0" +dependencies = [ + "chain-factory", + "multiversx-sc-wasm-adapter", +] + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0" + +[[package]] +name = "multiversx-sc" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adbdd41a744caa562646c6d593df35caed84889e0d35df8b9ad8efc45457b5bd" +dependencies = [ + "bitflags", + "hashbrown", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f1e15b46c17b87c0c7cdd79b041a4abd7f3a2b45f3c993f6ce38c0f233e82b6" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7bc0762cd6d88f8bc54805bc652b042a61cd7fbc2d0a325010f088b78fb2ac" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebdaec412a272d8fd6668bc0b4b8674a5e03148aa72041a4705d502b86f4d9ce" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2befd4545b5b47adf1df89313b76a2282873fd9163b70badd727d8dfce83c588" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/chain-factory/wasm/Cargo.toml b/chain-factory/wasm/Cargo.toml new file mode 100644 index 00000000..d32b76eb --- /dev/null +++ b/chain-factory/wasm/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "chain-factory-wasm" +version = "0.0.0" +edition = "2018" +publish = false +authors = [ "you",] + +[lib] +crate-type = [ "cdylib",] + +[workspace] +members = [ ".",] + +[dev-dependencies] + +[profile.release] +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" + +[dependencies.chain-factory] +path = ".." + +[dependencies.multiversx-sc-wasm-adapter] +version = "=0.43.5" diff --git a/chain-factory/wasm/src/lib.rs b/chain-factory/wasm/src/lib.rs new file mode 100644 index 00000000..530281cc --- /dev/null +++ b/chain-factory/wasm/src/lib.rs @@ -0,0 +1,28 @@ +// Code generated by the multiversx-sc multi-contract system. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +// Init: 1 +// Endpoints: 0 +// Async Callback (empty): 1 +// Total number of exported functions: 2 + +#![no_std] + +// Configuration that works with rustc < 1.73.0. +// TODO: Recommended rustc version: 1.73.0 or newer. +#![feature(lang_items)] + +multiversx_sc_wasm_adapter::allocator!(); +multiversx_sc_wasm_adapter::panic_handler!(); + +multiversx_sc_wasm_adapter::endpoints! { + chain_factory + ( + init => init + ) +} + +multiversx_sc_wasm_adapter::async_callback_empty! {} diff --git a/esdt-safe/.gitignore b/esdt-safe/.gitignore index f00d026a..9494cb14 100644 --- a/esdt-safe/.gitignore +++ b/esdt-safe/.gitignore @@ -3,9 +3,5 @@ /target/ */target/ -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - # The mxpy output output diff --git a/esdt-safe/wasm/Cargo.lock b/esdt-safe/wasm/Cargo.lock new file mode 100644 index 00000000..5c933480 --- /dev/null +++ b/esdt-safe/wasm/Cargo.lock @@ -0,0 +1,263 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", +] + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bls-signature" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "esdt-safe" +version = "0.0.0" +dependencies = [ + "bls-signature", + "max-bridged-amount-module", + "multiversx-sc", + "multiversx-sc-modules", + "transaction", + "tx-batch-module", +] + +[[package]] +name = "esdt-safe-wasm" +version = "0.0.0" +dependencies = [ + "esdt-safe", + "multiversx-sc-wasm-adapter", +] + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0" + +[[package]] +name = "max-bridged-amount-module" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adbdd41a744caa562646c6d593df35caed84889e0d35df8b9ad8efc45457b5bd" +dependencies = [ + "bitflags", + "hashbrown", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f1e15b46c17b87c0c7cdd79b041a4abd7f3a2b45f3c993f6ce38c0f233e82b6" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7bc0762cd6d88f8bc54805bc652b042a61cd7fbc2d0a325010f088b78fb2ac" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebdaec412a272d8fd6668bc0b4b8674a5e03148aa72041a4705d502b86f4d9ce" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-modules" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45a02a1c14d05986661eff35beab26f5e72cdc44753b1f415eff64d6c4e4bc98" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.43.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2befd4545b5b47adf1df89313b76a2282873fd9163b70badd727d8dfce83c588" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "transaction" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "tx-batch-module" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" From b12a5c87675a45ab7b9ec588bfae1321b085c613 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 19 Oct 2023 11:10:44 +0300 Subject: [PATCH 2/3] chain factory sc --- chain-factory/mxsc-template.toml | 18 ---------- chain-factory/src/factory.rs | 51 ++++++++++++++++++++++++++ chain-factory/src/lib.rs | 30 ++++++++++++++-- chain-factory/src/slash.rs | 61 ++++++++++++++++++++++++++++++++ chain-factory/wasm/src/lib.rs | 10 ++++-- 5 files changed, 148 insertions(+), 22 deletions(-) delete mode 100644 chain-factory/mxsc-template.toml create mode 100644 chain-factory/src/factory.rs create mode 100644 chain-factory/src/slash.rs diff --git a/chain-factory/mxsc-template.toml b/chain-factory/mxsc-template.toml deleted file mode 100644 index 441addd7..00000000 --- a/chain-factory/mxsc-template.toml +++ /dev/null @@ -1,18 +0,0 @@ -name = "empty" -contract_trait = "EmptyContract" -src_file = "empty.rs" -rename_pairs = [ - [ - "blockchain.set_current_dir_from_workspace(\"contracts/examples/empty\");", - "// blockchain.set_current_dir_from_workspace(\"relative path to your workspace, if applicable\");", - ], -] -files_include = [ - "meta", - "scenarios", - "src", - "tests", - "wasm/Cargo.toml", - "Cargo.toml", - "multiversx.json", -] diff --git a/chain-factory/src/factory.rs b/chain-factory/src/factory.rs new file mode 100644 index 00000000..45c2f531 --- /dev/null +++ b/chain-factory/src/factory.rs @@ -0,0 +1,51 @@ +multiversx_sc::imports!(); + +#[multiversx_sc::module] +pub trait FactoryModule { + // TODO: Typed args + #[payable("EGLD")] + #[endpoint(deploySovereignChainConfigContract)] + fn deploy_sovereign_chain_config_contract(&self, args: MultiValueEncoded) { + let payment_amount = self.call_value().egld_value().clone_value(); + let deploy_cost = self.deploy_cost().get(); + require!(payment_amount == deploy_cost, "Invalid payment amount"); + + let mut serialized_args = ManagedArgBuffer::new(); + for arg in args { + serialized_args.push_arg(arg); + } + + // TODO: add caller as admin + // let caller = self.blockchain().get_caller(); + + // TODO: Typed call based on proxy + let source_address = self.chain_config_template().get(); + let gas_left = self.blockchain().get_gas_left(); + let metadata = + CodeMetadata::PAYABLE_BY_SC | CodeMetadata::UPGRADEABLE | CodeMetadata::READABLE; + let (sc_address, _) = self.send_raw().deploy_from_source_contract( + gas_left, + &BigUint::zero(), + &source_address, + metadata, + &serialized_args, + ); + let _ = self.all_deployed_contracts().insert(sc_address); + } + + #[only_owner] + #[endpoint(blacklistSovereignChainSc)] + fn blacklist_sovereign_chain_sc(&self, sc_address: ManagedAddress) { + let _ = self.all_deployed_contracts().swap_remove(&sc_address); + } + + #[view(getDeployCost)] + #[storage_mapper("deployCost")] + fn deploy_cost(&self) -> SingleValueMapper; + + #[storage_mapper("chainConfigTemplate")] + fn chain_config_template(&self) -> SingleValueMapper; + + #[storage_mapper("allDeployedContracts")] + fn all_deployed_contracts(&self) -> UnorderedSetMapper; +} diff --git a/chain-factory/src/lib.rs b/chain-factory/src/lib.rs index 88cc50c6..13c9785f 100644 --- a/chain-factory/src/lib.rs +++ b/chain-factory/src/lib.rs @@ -2,8 +2,34 @@ multiversx_sc::imports!(); +pub mod factory; +pub mod slash; + #[multiversx_sc::contract] -pub trait ChainFactoryContract { +pub trait ChainFactoryContract: factory::FactoryModule + slash::SlashModule { #[init] - fn init(&self) {} + fn init( + &self, + validators_contract_address: ManagedAddress, + chain_config_template: ManagedAddress, + deploy_cost: BigUint, + ) { + self.require_sc_address(&validators_contract_address); + self.require_sc_address(&chain_config_template); + + self.validators_contract_address() + .set(validators_contract_address); + self.chain_config_template().set(chain_config_template); + self.deploy_cost().set(deploy_cost); + } + + #[endpoint] + fn upgrade(&self) {} + + fn require_sc_address(&self, address: &ManagedAddress) { + require!( + !address.is_zero() && self.blockchain().is_smart_contract(address), + "Invalid SC address" + ); + } } diff --git a/chain-factory/src/slash.rs b/chain-factory/src/slash.rs new file mode 100644 index 00000000..4776f197 --- /dev/null +++ b/chain-factory/src/slash.rs @@ -0,0 +1,61 @@ +multiversx_sc::imports!(); + +pub type DestAmountPairs = MultiValueEncoded, BigUint>>; + +mod validators_contract_proxy { + use super::DestAmountPairs; + + multiversx_sc::imports!(); + + #[multiversx_sc::proxy] + pub trait ValidatorsContractProxy { + #[endpoint] + fn slash(&self, validator_address: ManagedAddress, value: BigUint); + + #[endpoint(distrubteSlashed)] + fn distribute_slashed(&self, dest_amount_pairs: DestAmountPairs); + } +} + +#[multiversx_sc::module] +pub trait SlashModule: crate::factory::FactoryModule { + #[endpoint] + fn slash(&self, validator_address: ManagedAddress, value: BigUint) { + let caller = self.blockchain().get_caller(); + self.require_deployed_sc(&caller); + + let validators_contract_address = self.validators_contract_address().get(); + let _: IgnoreValue = self + .validator_proxy(validators_contract_address) + .slash(validator_address, value) + .execute_on_dest_context(); + } + + #[endpoint(distrubteSlashed)] + fn distribute_slashed(&self, dest_amount_pairs: DestAmountPairs) { + let caller = self.blockchain().get_caller(); + self.require_deployed_sc(&caller); + + let validators_contract_address = self.validators_contract_address().get(); + let _: IgnoreValue = self + .validator_proxy(validators_contract_address) + .distribute_slashed(dest_amount_pairs) + .execute_on_dest_context(); + } + + fn require_deployed_sc(&self, address: &ManagedAddress) { + require!( + self.all_deployed_contracts().contains(address), + "Only deployed contracts may call this endpoint" + ); + } + + #[proxy] + fn validator_proxy( + &self, + sc_address: ManagedAddress, + ) -> validators_contract_proxy::Proxy; + + #[storage_mapper("validatorsContractAddress")] + fn validators_contract_address(&self) -> SingleValueMapper; +} diff --git a/chain-factory/wasm/src/lib.rs b/chain-factory/wasm/src/lib.rs index 530281cc..e4f3af1d 100644 --- a/chain-factory/wasm/src/lib.rs +++ b/chain-factory/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 0 +// Endpoints: 6 // Async Callback (empty): 1 -// Total number of exported functions: 2 +// Total number of exported functions: 8 #![no_std] @@ -22,6 +22,12 @@ multiversx_sc_wasm_adapter::endpoints! { chain_factory ( init => init + upgrade => upgrade + deploySovereignChainConfigContract => deploy_sovereign_chain_config_contract + blacklistSovereignChainSc => blacklist_sovereign_chain_sc + getDeployCost => deploy_cost + slash => slash + distrubteSlashed => distribute_slashed ) } From 1a29c5db713d34470b8575110f9230799a2149eb Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 19 Oct 2023 11:14:17 +0300 Subject: [PATCH 3/3] typo --- chain-factory/src/slash.rs | 4 ++-- chain-factory/wasm/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/chain-factory/src/slash.rs b/chain-factory/src/slash.rs index 4776f197..2e9e8705 100644 --- a/chain-factory/src/slash.rs +++ b/chain-factory/src/slash.rs @@ -12,7 +12,7 @@ mod validators_contract_proxy { #[endpoint] fn slash(&self, validator_address: ManagedAddress, value: BigUint); - #[endpoint(distrubteSlashed)] + #[endpoint(distributeSlashed)] fn distribute_slashed(&self, dest_amount_pairs: DestAmountPairs); } } @@ -31,7 +31,7 @@ pub trait SlashModule: crate::factory::FactoryModule { .execute_on_dest_context(); } - #[endpoint(distrubteSlashed)] + #[endpoint(distributeSlashed)] fn distribute_slashed(&self, dest_amount_pairs: DestAmountPairs) { let caller = self.blockchain().get_caller(); self.require_deployed_sc(&caller); diff --git a/chain-factory/wasm/src/lib.rs b/chain-factory/wasm/src/lib.rs index e4f3af1d..7364f9bb 100644 --- a/chain-factory/wasm/src/lib.rs +++ b/chain-factory/wasm/src/lib.rs @@ -27,7 +27,7 @@ multiversx_sc_wasm_adapter::endpoints! { blacklistSovereignChainSc => blacklist_sovereign_chain_sc getDeployCost => deploy_cost slash => slash - distrubteSlashed => distribute_slashed + distributeSlashed => distribute_slashed ) }