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

@powerpool/balancer-v2-pool-weighted

v2.0.2

Published

Balancer V2 Weighted Pools

Downloads

1

Readme

Balancer V2 Weighted Pools

NPM Package

This package contains the source code for Balancer V2 Weighted Pools, that is, Pools that swap tokens by enforcing a Constant Weighted Product invariant.

The two basic flavors currently in existence are WeightedPool (basic twenty token version) and WeightedPool2Tokens (limited to two tokens, but supporting price oracles).

The smart directory contains a number of 'smart' variants, which automatically update some of their attributes to support more complex use cases. Examples are LiquidityBootstrappingPool for auction-like mechanisms, and ManagedPool for managed portfolios.

| :warning: | Managed Pools are still undergoing development and may contain bugs and/or change significantly. | | --------- | :-------------------------------------------------------------------------------------------------- |

Other useful contracts include WeightedMath, which implements the low level calculations required for swaps, joins, exits and price calculations, and IPriceOracle, used to make price oracle queries.

Overview

Installation

$ npm install @balancer-labs/v2-pool-weighted

Usage

This package can be used in multiple ways, including interacting with already deployed Pools, performing local testing, or even creating new Pool types that also use the Constant Weighted Product invariant.

To get the address of deployed contracts in both mainnet and various test networks, see v2-deployments.

Sample contract that performs an action conditionally using a Pool as a price oracle:

pragma solidity ^0.7.0;

import "@balancer-labs/v2-pool-weighted/contracts/IPriceOracle.sol";

contract SimpleOracleQuery {
    IPriceOracle private constant oracle = "0x0b09deA16768f0799065C475bE02919503cB2a35"; // WETH-DAI Pool

    function performAction() external {
      IPriceOracle.OracleAverageQuery[] memory queries = new IPriceOracle.OracleAverageQuery[](1);

      // Average price over the last hour - note that the oracle must be fully initialized
      queries[0] = IPriceOracle.OracleAverageQuery({
        variable: IPriceOracle.Variable.PAIR_PRICE,
        secs: 3600,
        ago: 0
      });

      uint256[] memory results = oracle.getTimeWeightedAverage(queries);
      if (results[0] >= 4000) {
        ...
      } else {
        ...
      }
    }
}

Sample Weighted Pool that computes weights dynamically on every swap, join and exit:

pragma solidity ^0.7.0;

import '@balancer-labs/v2-pool-weighted/contracts/BaseWeightedPool.sol';

contract DynamicWeightedPool is BaseWeightedPool {
    uint256 private immutable _creationTime;

    constructor() {
        _creationTime = block.timestamp;
    }

    function _getNormalizedWeightsAndMaxWeightIndex() internal view override returns (uint256[] memory) {
        uint256[] memory weights = new uint256[](2);

        // Change weights from 50-50 to 30-70 one month after deployment
        if (block.timestamp < (_creationTime + 1 month)) {
          weights[0] = 0.5e18;
          weights[1] = 0.5e18;
        } else {
          weights[0] = 0.3e18;
          weights[1] = 0.7e18;
        }

        return (weights, 1);
    }

    ...
}

Licensing

GNU General Public License Version 3 (GPL v3).