registerTree()

The registerTree() function is a method to register a new airdrop on the contract by specifying its token type, supply amount, start and end times, Merkle tree root, and the wallet address signing the transaction.

Step 1: Create Signer instance

  • 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);
const signer = new ethers.Wallet("WALLET_PRIVATE_KEY", provider);

Step 2: Call registerTree method

Register an airdrop to this contract

Parameter

ArgumentTypeDescription
tokenAddressstringThe token type of the supply stored in the airdrop.
supplynumberThe amount of tokens stored in the airdrop.
startTimenumberStart unix timestamp (in seconds) of the airdrop.
endTimenumberEnd unix timestamp (in seconds) of the airdrop.
rootstringThe root of the Merkle tree.
signerSignerThe instance to represent wallet address.

Response

FieldTypeDescription
typenumberThe

Example

Before calling the actual registerTree, make sure to have enough allowance from the fee token of airdrop contract for airdrop contract and the transfer token. If not, developers can always use the following lines to ensure sufficient allowance:

// increase allowance for fee token
const amount = await airdrop.getFee()
const feeToken = await airdrop.getFeeToken()
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
if(feeToken !== ZERO_ADDRESS){
    const allowance = await airdrop.getAllowance(feeToken, await signer.getAddress())
    if(allowance.lt(amount)){
        const feeIncrementAllowance = amount.sub(allowance).toString()
        await airdrop.increaseAllowance(feeToken, feeIncrementAllowance, signer)
    }
}


// increase allowance for the transferred token
const amounts = [3];
const tokenAddress = <YOUR_CRYPTO_TOKEN>
const supply = amounts.reduce((acc, curr) => acc + curr)
// assume amounts store the amount of token for each recipient in an airdrop
if(tokenAddress !== ZERO_ADDRESS){
    const allowance = await airdrop.getAllowance(tokenAddress, await signer.getAddress())
    if(allowance.lt(supply)){
        const transferIncrementAllowance = supply.sub(allowance).toString()
        await airdrop.increaseAllowance(tokenAddress, transferIncrementAllowance, signer)
    }
}

Then call the registerTree method:

const supply = (tokenAddress === feeToken ? amount + await airdrop.fee() : amount)
await airdrop.registerTree(tokenAddress, supply, startTime, endTime, root, signer)
// see how to obtain root value in the quickstart guide