Git Product home page Git Product logo

loot-box's Introduction

Loot Boxes

CircleCI Coverage Status

Loot Boxes are special addresses that are associated to an ERC721. Only the NFT owner can withdraw tokens from the Loot Box address.

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:

  1. The Loot Box Controller computes the Loot Box address for any ERC721.
  2. Anyone can transfer tokens to the Loot Box address
  3. 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

loot-box's People

Contributors

coin-monster avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.