📦

Airdrop

In this page, we will go over, explain and review the INK! smart contract that associated with the airdrop program and all of its functions.
The only users the eligable to collect the airdrop tokens are the airdrop event winners.

Airdrop contract source code:

Imports:

We are importing openbrush::{contracts::{traits::psp22::PSP22Ref,} in order to make cross contract calls to PSP22 object, we use this import to reference PSP22 token and last we import ink::storage::Mapping; in order to initialize the contract's hashmaps.
use openbrush::{
contracts::{
traits::psp22::PSP22Ref,
},
};
use ink::storage::Mapping;

Contract's struct:

"manager" : Stores the contracts deployer address.
"panx_psp22" : Stores PANX PSP22 token address in order to reference the cross contract calls.
"collected_airdrop" : Hashmap used to store the statuses of users whom which collected the airdrop or not.

Functions:

new (constructor)

Constructor function, used to initialize the airdrop smart contract and to deploy it to the network. We pass "panx_contract" as the PANX smart contract address in order to interact and transfer PANX tokens to the airdrop participants.
#[ink(constructor)]
pub fn new(panx_contract:AccountId) -> Self {
let panx_psp22 = panx_contract;
let manager = Self::env().caller();
let collected_airdrop = Mapping::default();
Self{
manager,
panx_psp22,
collected_airdrop
}
}

collect_50_tokens

Function to collect 50 PANX tokens as a airdrop winner. We check if the caller didnt collect the airdrop yet (status 0):
if self.collected_airdrop.get(&self.env().caller()).unwrap_or(0) != 0 {
panic!(
"Caller already redeemed the airdrop, cannot redeem again."
)
}
And after successful transfer, we set the user "collected_airdrop" status to 1
#[ink(message)]
pub fn collect_50_tokens(&mut self) {
//making sure account didnt redeem airdrop yet
if self.collected_airdrop.get(&self.env().caller()).unwrap_or(0) != 0 {
panic!(
"Caller already redeemed the airdrop, cannot redeem again."
)
}
let tokens_to_transfer:Balance;
match 50u128.checked_mul(10u128.pow(12)) {
Some(result) => {
tokens_to_transfer = result;
}
None => {
panic!("overflow!");
}
};
//transfers the airdrop tokens to caller
PSP22Ref::transfer(&self.panx_psp22, self.env().caller(), tokens_to_transfer,ink::prelude:vec![]).unwrap_or_else(|error| {
panic!(
"Failed to transfer PSP22 tokens to caller : {:?}",
error
)
});
//make sure to change his collected airdrop status to 1 to prevent the user to call it again
self.collected_airdrop.insert(self.env().caller(),&1);
}

collect_500_tokens

Function to collect 50 PANX tokens as a airdrop winner. We check if the caller didnt collect the airdrop yet (status 0):
if self.collected_airdrop.get(&self.env().caller()).unwrap_or(0) != 0 {
panic!(
"Caller already redeemed the airdrop, cannot redeem again."
)
}
And after successful transfer, we set the user "collected_airdrop" status to 1
#[ink(message)]
pub fn collect_500_tokens(&mut self) {
//making sure account didnt redeem airdrop yet
if self.collected_airdrop.get(&self.env().caller()).unwrap_or(0) != 0 {
panic!(
"Caller already redeemed the airdrop, cannot redeem again."
)
}
let tokens_to_transfer:Balance;
match 500.checked_mul(10u128.pow(12)) {
Some(result) => {
tokens_to_transfer = result;
}
None => {
panic!("overflow!");
}
};
//transfers the airdrop tokens to caller
PSP22Ref::transfer(&self.panx_psp22, self.env().caller(), tokens_to_transfer, ink_prelude::vec![]).unwrap_or_else(|error| {
panic!(
"Failed to transfer PSP22 tokens to caller : {:?}",
error
)
});
//make sure to change his collected airdrop status to 1 to prevent the user to call it again
self.collected_airdrop.insert(self.env().caller(),&1);
}
///funtion to get airdrop contract PANX reserve
#[ink(message)]
pub fn get_airdrop_contract_panx_reserve(&self)-> Balance {
let balance:Balance = PSP22Ref::balance_of(&self.panx_psp22, Self::env().account_id());
balance
}

user_airdrop_collection_status

Function to fetch the caller's current airdrop collection status (0 = didnt collect, 1 = did collect)
#[ink(message)]
pub fn user_airdrop_collection_status(&mut self,account:AccountId)->i64 {
let airdrop_status = self.collected_airdrop.get(account).unwrap_or(0);
airdrop_status
}