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

@gnosis.pm/dx-price-oracle

v0.2.1

Published

A reliable price oracle based on the DutchX protocol

Downloads

12

Readme

Build Status

DutchX Price Oracle

Contract to get an onchain reliable price from DutchX protocol.

The oracle exposes a simple function that will return the price for any ERC20 traded on the DutchX (See implementation).

function getPrice(address token)
        public
        view
        returns (uint num, uint den)

This function will return a fraction (num/den), using the following logic:

  • 0/0:
    • If the token it's not listed in DutchX
    • If the token it's not whitelisted in the DutchX
    • If there's not enough liquidity. Basically, it requires that there should be auctions running continuosly in the DutchX. More formally, it will return 0/0 if there is less than 9 auctions in the last 4.5 days.
  • num/den: Median of the last 9 auctions. In other words, if we order the last 9 auctions by price, we take the 5th value.

In the following example, we get the price for RDN (0x255Aa6DF07540Cb5d3d297f0D0D4D84cb52bc8e6):

It provides also a parametrized function, to allow to change the behaviour of the price oracle (See implementation):

function getPriceCustom(
        address token,
        uint time,
        bool requireWhitelisted,
        uint maximumTimePeriod,
        uint numberOfAuctions
    )
        public
        view
        returns (uint num, uint den)

Note: the contract is only safe for odd numbers of auctions. See here.

The next image shows an example, on how to get the price using:

  • RDN Token: token = 0x255Aa6DF07540Cb5d3d297f0D0D4D84cb52bc8e6
  • Check the current price: time = 0
  • Return only prices for whitelisted tokens: requireWhitelisted = true
  • Return only a price if the auctions runned within the last 1.5 days: maximumTimePeriod = 1296000
  • Use 3 auctions to get the median: numberOfAuctions = 3

Deployed price oracle

Rinkeby:

Mainnet:

Local development

# Install dependencies
yarn install

# Compile contracts and inject network info
yarn restore

# Print the network info for every contract
yarn networks

Migrate contracts

For a local ganache:

# Run ganache
npx ganache-cli

# Migrate
yarn migrate

For other networks:

# Rinkeby
PK=<private_key> yarn migrate --network rinkeby

# Mainnet
PK=<private_key> yarn migrate --network mainnet

Create a new version

# Generate version and tag it
npm version <new-version-number>

# Publish version into git
git push && git push --tags

# Publish version into npm
npm publish --access=public

Validate the contract

# Flatten contract
npx truffle-flattener contracts/DutchXPriceOracle.sol > build/DutchXPriceOracle-EtherScan.sol
npx truffle-flattener contracts/WhitelistPriceOracle.sol > build/WhitelistPriceOracle-EtherScan.sol

Validate the contract:

Tests

Tests use a mock contract to imitate the behavior of the DutchX. We generate 50 auctions with random prices and clearing times, and then test each contract fn on that model.

The recommended ways to run the tests is:

npx truffle test

or

npx truffle test --log (to get console logs)

Have fun!