npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

token-basket

v0.1.2

Published

ERC20 (BEP20) token representing a fixed set of other tokens with predefined weights

Downloads

7

Readme

TokenBasket

NPM Package License

ERC20 (BEP20) token representing a fixed set of other tokens with predefined weights

Overview

TokenBasket is for the crypto market what ETF is for the stock market. It works like a wrapper for token collections, making them much more convenient and cost-effective to transfer.

Let's imagine a following TokenBasket:

name: Example Token Basket
symbol: ETB
holdings:
  - Wrapped Ether (WETH), weight: 2
  - Binance Coin (BNB), weight: 3

Each unit of ETB (Example Token Basket) is pegged to 2 units of WETH and 3 units of BNB (you need to deposit 2 WETH + 3 BNB in order to mint 1 ETB, analogically you will be rewarded 2 WETH + 3 BNB for burning 1 ETB).

Lifecycle and gas usage estimation

Deployment

TokenBasket constructor takes 5 parameters:

  1. token basket name
  2. token basket symbol
  3. number of decimal places
  4. array of holdings (addresses of underlying token contracts)
  5. array of holding weights in the basket

That's how Example Token Basket could be deployed:

pragma solidity ^0.8.0;

import "token-basket/contracts/TokenBasket.sol";

contract ExampleTokenBasket is TokenBasket {
  constructor () TokenBasket ("Example Token Basket", "ETB", 18
      [ 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, // WETH address
      0xB8c77482e45F1F44dE1745F52C74426C631bDD52 ], // BNB address
      [ 2, 3 ]) {
  }
}
EXAMPLE_TOKEN_BASKET_ADDRESS = new ExampleTokenBasket();

Gas usage for the deployment can be roughly estimated as 1,187,000 + 42,000 per holding (compiler optimized for 2000 runs) so in above case it would be around 1,271,000.

Minting

Prerequisite for minting is an approval to transfer underlying holdings (amount to be minted multiplied by the holding weight) from the minter's address to the TokenBasket contract address.

Approvals necessary to mint 50 units (with 18 decimal places) of Example Token Basket would look like this from the minter perspective:

approve(EXAMPLE_TOKEN_BASKET_ADDRESS, 2 * 50 * 10**18); // for WETH address
approve(EXAMPLE_TOKEN_BASKET_ADDRESS, 3 * 50 * 10**18); // for BNB address

Approval gas usage depends only on underlying holdings. Usually it's up to 45,000 per holding so in above case it would be around 90,000.

Actual mint function takes only one argument: amount of units to be minted. For 50 units with 18 decimal places it would be:

mint(50 * 10**18); // for EXAMPLE_TOKEN_BASKET_ADDRESS

Under the hood, this function transfers approved holdings to the TokenBasket contract address and increases minter's balance accordingly. Gas usage: usually up to 66,000 + up to 45,000 per holding, so around 156,000 for the above case.

Transfer

Transfers and operations related to allowance are intended to be the most optimized. They shouldn't use more gas than analogical operations for other simple ERC20 (BEP20) tokens, usually between 25,000 and 65,000.

Burning

Burning process is analogical to minting process, burn function takes only one argument: amount of units to be burned. For 50 units with 18 decimal places it would be:

burn(50 * 10**18); // for EXAMPLE_TOKEN_BASKET_ADDRESS

It decreases burner's balance and transfers underlying tokens to his address. Gas usage: around 37,000 + between 21,000 and 36,000 per holding, so up to 109,000 for Example Token Basket.

Gas usage optimization

Gas usage can be slightly optimized by storing holdings and weights in a constant array. In order to achieve that, instead of calling TokenBasket constructor, new contract (extending AbstractTokenBasket) needs to be created. This can save around 50,000 gas per holding during the deployment.

However, this is not a recommended solution as it involves some code changes which may be error prone.

Projects using TokenBasket

TBD

License

TokenBasket is released under the MIT License.