diff --git a/Cargo.lock b/Cargo.lock index 095c0284..1871fb89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,23 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" +[[package]] +name = "faucet" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "multiversx-sc-modules", + "multiversx-sc-scenario", +] + +[[package]] +name = "faucet-meta" +version = "0.0.0" +dependencies = [ + "faucet", + "multiversx-sc-meta-lib", +] + [[package]] name = "fiat-crypto" version = "0.2.9" diff --git a/Cargo.toml b/Cargo.toml index 5fb5dd18..3320c8d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,8 @@ members = [ "contracts/factorial/meta", "contracts/fair-launch", "contracts/fair-launch/meta", + "contracts/faucet", + "contracts/faucet/meta", "contracts/fractional-nfts", "contracts/fractional-nfts/meta", "contracts/lottery-esdt", diff --git a/contracts/faucet/Cargo.toml b/contracts/faucet/Cargo.toml new file mode 100644 index 00000000..24c4b01d --- /dev/null +++ b/contracts/faucet/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "faucet" +version = "0.0.0" +publish = false +edition = "2021" +authors = ["you"] + +[lib] +path = "src/faucet.rs" + +[dependencies.multiversx-sc] +version = "0.53.2" + +[dependencies.multiversx-sc-modules] +version = "0.53.2" + +[dev-dependencies.multiversx-sc-scenario] +version = "0.53.2" diff --git a/contracts/faucet/meta/Cargo.toml b/contracts/faucet/meta/Cargo.toml new file mode 100644 index 00000000..3c401f8d --- /dev/null +++ b/contracts/faucet/meta/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "faucet-meta" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.faucet] +path = ".." + +[dependencies.multiversx-sc-meta-lib] +version = "0.53.2" +default-features = false diff --git a/contracts/faucet/meta/src/main.rs b/contracts/faucet/meta/src/main.rs new file mode 100644 index 00000000..cb017c9e --- /dev/null +++ b/contracts/faucet/meta/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + multiversx_sc_meta_lib::cli_main::(); +} diff --git a/contracts/faucet/multiversx.json b/contracts/faucet/multiversx.json new file mode 100644 index 00000000..73655396 --- /dev/null +++ b/contracts/faucet/multiversx.json @@ -0,0 +1,3 @@ +{ + "language": "rust" +} \ No newline at end of file diff --git a/contracts/faucet/src/faucet.rs b/contracts/faucet/src/faucet.rs new file mode 100644 index 00000000..db16e873 --- /dev/null +++ b/contracts/faucet/src/faucet.rs @@ -0,0 +1,73 @@ +#![no_std] + +#[allow(unused_imports)] +use multiversx_sc::{derive_imports::*, imports::*}; + +pub type Signature = ManagedByteArray; +pub const ED25519_SIGNATURE_BYTE_LEN: usize = 64; + +/// An empty contract. To be used as a template when starting a new contract from scratch. +#[multiversx_sc::contract] +pub trait Faucet { + #[init] + fn init(&self) {} + + #[upgrade] + fn upgrade(&self) {} + + #[only_owner] + #[payable("*")] + #[endpoint(deposit)] + fn deposit(&self, max_users: u64) { + let payments = self.call_value().all_esdt_transfers(); + for payment in payments.iter() { + let amount_per_user = payment.amount / max_users; + self.payments().push( &EsdtTokenPayment::new(payment.token_identifier, payment.token_nonce, amount_per_user)); + } + } + + #[endpoint(claim)] + fn claim(&self, signature: &Signature) { + let caller = self.blockchain().get_caller(); + let claimers_mapper = self.claimers(); + require!(!claimers_mapper.contains(&caller), "Caller already claimed"); + + self.verify_signature(&caller, signature); + self.claimers().add(&caller); + + let mut payments = ManagedVec::new(); + for payment in self.payments().iter() { + payments.push(payment); + } + + self.tx().to(ToCaller).with_multi_token_transfer(payments).transfer(); + } + + fn verify_signature( + &self, + caller: &ManagedAddress, + signature: &Signature, + ) { + let signer = self.signer().get(); + self.crypto().verify_ed25519( + signer.as_managed_buffer(), + caller.as_managed_buffer(), + signature.as_managed_buffer(), + ); + } + + #[only_owner] + #[endpoint(setSigner)] + fn set_signer(&self, signer: ManagedAddress) { + self.signer().set(&signer); + } + + #[storage_mapper("payments")] + fn payments(&self) -> VecMapper>; + + #[storage_mapper("signer")] + fn signer(&self) -> SingleValueMapper>; + + #[storage_mapper("claimers")] + fn claimers(&self) -> WhitelistMapper>; +} diff --git a/contracts/faucet/wasm/Cargo.lock b/contracts/faucet/wasm/Cargo.lock new file mode 100644 index 00000000..56e06a46 --- /dev/null +++ b/contracts/faucet/wasm/Cargo.lock @@ -0,0 +1,198 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "faucet" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "multiversx-sc-modules", +] + +[[package]] +name = "faucet-wasm" +version = "0.0.0" +dependencies = [ + "faucet", + "multiversx-sc-wasm-adapter", +] + +[[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.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "multiversx-sc" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75ea89a26f0aacda21437a8ae5ccfbefab99d8191942b3d2eddbcbf84f9866d7" +dependencies = [ + "bitflags", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", + "unwrap-infallible", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "007d7a5a8534e5dc9128cb8f15a65a21dd378e135c6016c7cd1491cd012bc8cb" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", + "unwrap-infallible", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffba1dce273ed5b61ee1b90aeea5c8c744617d0f12624f620768c144d83e753" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c17fdf90fafca2f19085ae67b0502d9f71bf8ab1be3c83808eb88e02a8c18b9" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-modules" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daeb48acbd39255868a3241798df2f85050f0ae8d82d6417bd2cd0e30a241855" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20659915a4377d375c46d7f237e810053a03f7e084fad6362dd5748a7233defb" +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.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +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.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unwrap-infallible" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "151ac09978d3c2862c4e39b557f4eceee2cc72150bc4cb4f16abf061b6e381fb" diff --git a/contracts/faucet/wasm/Cargo.toml b/contracts/faucet/wasm/Cargo.toml new file mode 100644 index 00000000..8b20a063 --- /dev/null +++ b/contracts/faucet/wasm/Cargo.toml @@ -0,0 +1,34 @@ +# Code generated by the multiversx-sc build system. DO NOT EDIT. + +# ########################################## +# ############## AUTO-GENERATED ############# +# ########################################## + +[package] +name = "faucet-wasm" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] + +[profile.release] +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" +overflow-checks = false + +[profile.dev] +panic = "abort" + +[dependencies.faucet] +path = ".." + +[dependencies.multiversx-sc-wasm-adapter] +version = "0.53.2" + +[workspace] +members = ["."] diff --git a/contracts/faucet/wasm/src/lib.rs b/contracts/faucet/wasm/src/lib.rs new file mode 100644 index 00000000..b471a0c2 --- /dev/null +++ b/contracts/faucet/wasm/src/lib.rs @@ -0,0 +1,29 @@ +// Code generated by the multiversx-sc build system. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +// Init: 1 +// Upgrade: 1 +// Endpoints: 3 +// Async Callback (empty): 1 +// Total number of exported functions: 6 + +#![no_std] + +multiversx_sc_wasm_adapter::allocator!(); +multiversx_sc_wasm_adapter::panic_handler!(); + +multiversx_sc_wasm_adapter::endpoints! { + faucet + ( + init => init + upgrade => upgrade + deposit => deposit + claim => claim + setSigner => set_signer + ) +} + +multiversx_sc_wasm_adapter::async_callback_empty! {}