@pooltogether/loot-box
v1.1.0
Published
Loot Boxes are Ethereum smart contracts that can be traded like NFTs and plundered by the owner.
Downloads
30
Readme
Loot Boxes
Loot Boxes are Ethereum smart contracts that can be traded like NFTs and plundered for tokens.
Supported tokens:
- ERC20
- ERC721
- ERC777 (only if it implements ERC20)
- ERC1155
Setup
You can use NPM to install the package:
$ npm install @pooltogether/loot-box
Or you can find the address and ABIs on the Networks page of the PoolTogether documentation.
Usage
LootBoxController: A contract that associates an address with an ERC721 and allows anyone to "plunder" the address: withdrawing desired tokens to the ERC721 owner's address.
The user flow is like so:
- The Loot Box Controller computes the Loot Box address for any ERC721.
- Anyone can transfer tokens to the Loot Box address
- Anyone may call the Loot Box Controller to transfer tokens from the Loot Box address to the current owner of the ERC721.
When the Loot Box controller withdraws tokens from a Loot Box, it's actually using CREATE2 to create a minimal proxy instance of a Loot Box. After the tokens are transferred, the Loot Box contract is destroyed. This cheap deployment and immediate destruction of the contract minimizes the gas overhead.
API
LootBoxController
computeAddress(address erc721, uint256 tokenId)
Computes the address of the Loot Box for the given ERC721. Returns the address of the Loot Box.
Note that no contract will exist there. The Loot Box contract is created and destroyed in a single tx.
plunder(
address erc721,
uint256 tokenId,
address[] calldata erc20s,
WithdrawERC721[] calldata erc721s,
WithdrawERC1155[] calldata erc1155s
)
Transfers all of the given tokens in the Loot Box to the owner of the ERC721.
erc20s
is an array of ERC20 token addresses. The full balance of each will be transferred to the ERC721 owner.
erc721s
is an array of WithdrawERC721
objects:
struct WithdrawERC721 {
address token;
uint256[] tokenIds;
}
erc1155s
is an array of WithdrawERC1155
objects:
struct WithdrawERC1155 {
address token;
uint256[] ids;
uint256[] amounts;
bytes data;
}
The gas usage of plunder
is quite reasonable. When withdrawing 2 ERC20s, 1 ERC721s and 1 ERC1155 the gas cost is ~211k. This is based on the standard OpenZeppelin token implementations.
Development
First clone the repository then run:
$ yarn
Now run the tests:
$ yarn test
Run code coverage:
$ yarn coverage
Run gas checks:
# start a node
$ yarn start-gas
# run the tests with gas reporter
$ yarn gas