Skip to content

Instantly share code, notes, and snippets.

@bibekblockchain
Created February 14, 2022 10:43
Show Gist options
  • Save bibekblockchain/3eb94c0ef5c6920e0399f65fc9294b7a to your computer and use it in GitHub Desktop.
Save bibekblockchain/3eb94c0ef5c6920e0399f65fc9294b7a to your computer and use it in GitHub Desktop.
public sale for checkerchain
#![no_std]
elrond_wasm::imports!();
const EGLD_DECIMALS_VALUE: u64 = 100000000;
#[elrond_wasm::contract]
pub trait CHECKERSale {
/// Necessary configuration when deploying:
/// `max_amount` - max amount of EGLD that can be used to buy $MUNCHKIN.
/// `min_amount` - min amount of EGLD that can be used to buy $MUNCHKIN.
/// `initial_price` - price for $MUNCHKIN token in EGLD (how much $MUNCHKIN for 1 EGLD)
/// `token_id` - $MUNCHKIN token ID.
#[init]
fn init(
&self,
max_amount: BigUint,
min_amount: BigUint,
initial_price: BigUint,
checker_token_id: TokenIdentifier,
) -> SCResult<()> {
require!(&max_amount > BigUint::from(0), "Max amount cannot be set to zero!");
require!(&min_amount > BigUint::from(0), "Min amount cannot be set to zero!");
require!(&initial_price > BigUint::from(0), "Initial price cannot be set to zero!");
require!(
checker_token_id.is_egld() || checker_token_id.is_valid_esdt_identifier(),
"Invalid token provided"
);
let caller: Address = self.blockchain().get_caller();
self.set_owner(&caller);
self.price().set(&initial_price);
self.max_amount().set(&max_amount);
self.min_amount().set(&min_amount);
self.sale_token_id().set(&checker_token_id);
Ok(())
}
// endpoints
/// User sends some tokens to the contract in order to exchange it for $Munchkin
/// Optional `_data` argument is ignored.
#[payable("EGLD")]
#[endpoint]
fn buy(
&self,
#[payment_amount] payment_amount: BigUint,
) -> SCResult<()> {
require!(
payment_amount <= self.max_amount().get(),
"The payment is too high"
);
require!(
payment_amount >= self.min_amount().get(),
"The payment is too low"
);
let balance = self.blockchain().get_sc_balance(&self.sale_token_id().get(), 0);
require!(
balance > 0,
"No more token to sale."
);
let current_price = self.price().get();
let one_egld = BigUint::from(EGLD_DECIMALS_VALUE);
let result_edst_token_amount = ( &current_price * &payment_amount ) / one_egld;
require!(
balance > result_edst_token_amount,
"Not enough tokens for sale."
);
//send the ESDT token amount to the user
let caller = self.blockchain().get_caller();
let token_id = self.sale_token_id().get();
self.send()
.direct(&caller, &token_id, 0, &result_edst_token_amount, b"Munchkin sale successful :).");
Ok(())
}
/// Optional `_data` argument is ignored.
#[payable("*")]
#[endpoint]
fn deposit(
&self,
#[payment_amount] _payment_amount: BigUint,
) -> SCResult<()> {
Ok(())
}
// storage
#[storage_set("owner")]
fn set_owner(&self, address: &Address);
#[view]
#[storage_get("owner")]
fn get_owner(&self) -> Address;
#[view(getSaleToken)]
#[storage_mapper("saleTokenId")]
fn sale_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
#[view(getMaxAmount)]
#[storage_mapper("maxAmount")]
fn max_amount(&self) -> SingleValueMapper<BigUint>;
#[view(getMinAmount)]
#[storage_mapper("minAmount")]
fn min_amount(&self) -> SingleValueMapper<BigUint>;
#[view(getIncreaseAmount)]
#[storage_mapper("increaseAmount")]
fn increase_amount(&self) -> SingleValueMapper<BigUint>;
#[view(getPrice)]
#[storage_mapper("price")]
fn price(&self) -> SingleValueMapper<BigUint>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment