Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faucet sc init commit #115

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions contracts/faucet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions contracts/faucet/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions contracts/faucet/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta_lib::cli_main::<faucet::AbiProvider>();
}
3 changes: 3 additions & 0 deletions contracts/faucet/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
73 changes: 73 additions & 0 deletions contracts/faucet/src/faucet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::{derive_imports::*, imports::*};

pub type Signature<M> = ManagedByteArray<M, ED25519_SIGNATURE_BYTE_LEN>;
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<Self::Api>) {
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<Self::Api>,
signature: &Signature<Self::Api>,
) {
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::Api>) {
self.signer().set(&signer);
}

#[storage_mapper("payments")]
fn payments(&self) -> VecMapper<EsdtTokenPayment<Self::Api>>;

#[storage_mapper("signer")]
fn signer(&self) -> SingleValueMapper<ManagedAddress<Self::Api>>;

#[storage_mapper("claimers")]
fn claimers(&self) -> WhitelistMapper<ManagedAddress<Self::Api>>;
}
198 changes: 198 additions & 0 deletions contracts/faucet/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions contracts/faucet/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 = ["."]
Loading
Loading