Skip to content

Commit f6ae7da

Browse files
committed
adds single-transaction-aidrop section
1 parent 73e6945 commit f6ae7da

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Although in its infancy, Solidity has had widespread adoption and is used to com
9999
## [Ethereum Quirks](#ethereum-quirks)
100100
* [Keyless Ether](#keyless-eth)
101101
* [One Time Addresses](#one-time-addresses)
102+
* [Single Transaction Airdrops](#single-transaction-airdrops)
102103

103104
## [List of Interesting Crypto Related Hacks/Bugs](#hacks)
104105

@@ -1609,6 +1610,65 @@ along with the signature, i.e. the `v`, `r` and `s` we made up. This will be a v
16091610

16101611
This quirk can also be used to send ether to a large number of people in a trustless manner, as Nick Johnson describes in [How to send Ether to 11,440 people](https://medium.com/@weka/how-to-send-ether-to-11-440-people-187e332566b7).
16111612

1613+
<h3 id="single-transaction-airdrops">Single Transaction Airdrops</h3>
1614+
1615+
An Airdrop refers to the process of distributing tokens amongst a large
1616+
group of people. Traditionally, airdrops have been processed via a large number
1617+
of transactions where each transaction updates either a single or a batch of
1618+
user's balances. This can be costly and strenuous on the Ethereum blockchain.
1619+
There is an alternative method, in which many users balances can be credited
1620+
with tokens using a single transaction.
1621+
1622+
This technique is explained in more detail by its proposer, RicMoo in his post:
1623+
[Merkle Air-Drops: Make Love, Not War](https://blog.ricmoo.com/merkle-air-drops-e6406945584d).
1624+
1625+
The idea is to create a [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree)
1626+
which contains (as leaf nodes) all the addresses and balances of users to be credited tokens.
1627+
This will be done off-chain. The merkle tree can be given out
1628+
publicly (again off-chain). A smart contract can then be created containing the
1629+
root hash of the merkle tree which allows users to submit [merkle-proofs](https://www.quora.com/Cryptography-How-does-a-Merkle-proof-actually-work) to obtain
1630+
their tokens. Thus a single transaction (the one used to create the contract,
1631+
or to simply store the Merkle tree root hash), allows all credited users to redeem
1632+
their airdropped tokens.
1633+
1634+
RicMoo in his [post](https://blog.ricmoo.com/merkle-air-drops-e6406945584d) also provides an example of a function which can accept Merkle Proofs
1635+
and credit a user's balance:
1636+
```solidity
1637+
function redeem(uint256 index, address recipient,
1638+
uint256 amount, bytes32[] merkleProof) public {
1639+
1640+
// Make sure this has not been redeemed
1641+
uint256 redeemedBlock = _redeemed[index / 256];
1642+
uint256 redeemedMask = (uint256(1) << uint256(index % 256));
1643+
require((redeemedBlock & redeemedMask) == 0);
1644+
1645+
// Mark it as redeemed (if we fail, we revert)
1646+
_redeemed[index / 256] = redeemedBlock | redeemedMask;
1647+
1648+
// Compute the merkle root from the merkle proof
1649+
bytes32 node = keccak256(index, recipient, amount);
1650+
uint256 path = index;
1651+
for (uint16 i = 0; i < merkleProof.length; i++) {
1652+
if ((path & 0x01) == 1) {
1653+
node = keccak256(merkleProof[i], node);
1654+
} else {
1655+
node = keccak256(node, merkleProof[i]);
1656+
}
1657+
path /= 2;
1658+
}
1659+
1660+
// Check the resolved merkle proof matches our merkle root
1661+
require(node == _rootHash);
1662+
1663+
// Redeem!
1664+
_balances[recipient] += amount;
1665+
_totalSupply += amount;
1666+
Transfer(0, recipient, amount);
1667+
}
1668+
```
1669+
This function could be built into a token contract to allow future airdrops.
1670+
The only transaction required to credit all user's balances, would be the
1671+
transaction that sets the Merkle tree root.
16121672

16131673

16141674
<h2 id="hacks">List of Interesting Crypto Related Hacks/Bugs</h2>

0 commit comments

Comments
 (0)