Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@gnosis.pm/dx-price-oracle

Package Overview
Dependencies
Maintainers
12
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gnosis.pm/dx-price-oracle

A reliable price oracle based on the DutchX protocol

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
12
Created
Source

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!

Keywords

FAQs

Package last updated on 23 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc