Skip to main content

AirDrop

The PREDA airdrop contract implements the functionality to use a transaction to invoke the contract function once and send tokens to multiple addresses. In the second for loop of the airdrop() function, the sender sends different amounts of tokens to the recipients. Unlike the Solidity airdrop contract, where the recipients' balances are changed synchronously, the PREDA airdrop implementation uses the realy statement in the for loop to send tokens, which is asynchronous in semantics. This means that the underlying blockchain system has the chance to issue multiple relay transactions to send tokens to corresponding recipients, and these relay transactions can be executed in parallel on a sharding blockchain.

contract Token {
struct payment
{
address to;
bigint amount;
}

@address bigint balance;

@address function bool transfer_n(array<payment> recipients) export
{
bigint total = 0ib;
for (uint32 i = 0u; i<recipients.length(); i++)
{
__debug.assert(recipients[i].amount >= 0ib);
total += recipients[i].amount;
}
if(total <= balance)
{
__debug.print("transfer_n*", recipients.length(), ", total=", total);
balance -= total;
for (uint32 i = 0u32; i<recipients.length(); i++)
{
if(recipients[i].amount>0ib)
{
relay@recipients[i].to (bigint amount = recipients[i].amount){
__debug.print("deposit_n: ", amount);
balance += amount;
}
}
}

return true;
}

return false;
}
}