@uniblock/launcher-airdrop v0.0.1-8
@uniblock/launcher-airdrop
This is a wrapper for the contract methods of uniblock airdrop. Before you begin, please create a uniblock account and get a uniblock api key here
1. Set up
Follow this step to add @uniblock/launcher-airdrop to your repository:
npm i @uniblock/launcher-airdrop
or
yarn add @uniblock/launcher-airdrop
2. General Overview
The Uniblock airdrop contract can help user temporarily store their token to let their recipients receive their tokens in a cryptographically secure way. The contract uses merkle proof to handle participate in the airdrop. This package is created to simplify the usage of the airdrop contract. This makes the task of dealing with blockchain contracts easier for the developers. This means developers can call any contract methods in the same way as they call apis from their backend, thus allowing their applications to obtain the functionality of airdrop contract they deployed. For more details about this package, take a look at the documentation
3. Quick Guide
This guide will guide you through the flow of interacting with the airdrop contract:
Step 1: Initialize the SDK:
To initialize the SDK, you must first create an airdrop contract through the Uniblock dashboard. Then you have to provide a Uniblock API key which can be found after creating the project on the site. The Uniblock API key can be seen inside the created project page. Then collect the contract address of your deployed contract and chain ID and pass them into the initialize function with the Uniblock API key:
import { initializeAirDrop } from "@uniblock/launcher-airdrop"
const uniblockApiKey = <YOUR_UNIBLOCK_API_KEY>
const contractAddress = <YOUR_AIRDROP_CONTRACT_ADDRESS>
const chainId = <YOUR_CHAIN_ID>
const option = {
rpc: <YOUR_RPC_URL> // You can assign your own rpc url
}
const airdropSdk = await initializeAirDrop(uniblockApiKey, {contractAddress, chainId}, option)
// or if you don't have your own rpc url, you can use uniblock sdk default url
// and use the following code:
// const airdropSdk = await initializeAirDrop(uniblockApiKey, {contractAddress, chainId})
Note:
You can create your airdrop contract through the uniblock dashboard and get the contract address here
Step 2: Create a signer instance
We will need to first create a signer:
browser environment (we are using metamask in this example)
const ethereum = (window as any).ethereum; const accounts = await ethereum.request({ method: "eth_requestAccounts", }); const provider = new ethers.providers.Web3Provider(ethereum) const signer = provider.getSigner(accounts[0]);
nodejs environment:
const provider = new ethers.providers.JsonRpcProvider("RPC_URL", 5); // needs the RPC Url of the specific chain, those urls can be found on chainlist: // https://chainlist.org/chain/5 const signer = new ethers.Wallet("WALLET_PRIVATE_KEY", provider);
Step 3: Ensure allowance
Next, we need to ensure the supplied token contract allow airdrop contract to transfer fee during the call of registerTree. This can be done by going to the etherscan to call increaseAllowance method by providing the spender
address with the airdrop contract address if the token contract is verified or run the following code:
const allowance = 100 // set allowance to be 100 for example
await airdrop.increaseAllowance(<ERC20_CONTRACT_ADDRESS>, BigNumber.from(allowance), signer)
Step 4: Create an airdrop
For users who want to start an airdrop in the contracts. They would run the follow code:
// step1: get tree root
const calculateERC20Tree = (recipients, amounts) => {
if (recipients.length !== amounts.length) {
throw new BadRequestException('Recipients and amounts mismatched length');
}
const treeValues = recipients.map((recipient, i) => [recipient, amounts[i]]);
const tree = StandardMerkleTree.of(treeValues, ['address', 'uint256']);
return { tree: JSON.stringify(tree.dump()), root: tree.root };
};
const recipients = [<RECIPIENT_ADDRESS>]
const amounts = [3]
const {tree, root} = calculateERC20Tree(recipients, amounts)
// step2: run register tree method to store token:
const tokenAddress = <YOUR_CRYPTO_TOKEN>
const supply = sum(amounts) + await airdrop.fee()
// startTime and endTime are Unix time epoch where current time is between startTime and endTime
await airdrop.registerTree(tokenAddress, supply, startTime, endTime, root)
// step3: Get the id of the airdrop you just created
const id = await airdrop.getAirdropCount()
Step 5: Claim the airdrop
To allow users to claim their tokens, run the following after they initialized the sdk instance:
// step1: pass the tree from the code snippet above to obtain the proof
const getUserAirdrop = (storedTree, recipient) => {
const tree = StandardMerkleTree.load(storedTree);
for (const [i, [address, amount]] of tree.entries()) {
if (address === recipient) {
return {
recipient,
amount,
proof: tree.getProof(i),
};
}
}
throw new NotFoundException('User not found in Airdrop');
};
// variable tree is obtained from calculateERC20Tree in the above code snippet
const {proof} = getUserAirdrop(JSON.parse(tree), recipients)
// step2: call claim
await airdrop.claim(
walletAddress,// walletAddress must be an address from recipients in above snippet
id, // id is the get from getAirdropCount in the above code snippet
amount, // if walletAddress is the i^{th} element of recipients, then amount is amounts[i] in above code snippet
proof,
signer
)