Giter Club home page Giter Club logo

spot-price-aggregator's Introduction

Spot Price Aggregator

Build Status Coverage Status

The 1inch spot price aggregator is a set of smart contracts that extract price data for tokens traded on DEXes from the blockchain. To avoid price manipulations within a transaction, the spot price aggregator should ONLY be used off-chain. DO NOT use it on-chain. For off-chain usage see Examples section below.

Wrappers

To handle wrapped tokens, such as wETH, cDAI, aDAI etc., the 1inch spot price aggregator uses custom wrapper smart contracts that wrap/unwrap tokens at the current wrapping exchange rate.

Connectors

If no direct liquidity pair exists between two tokens, the spot price aggregator calculates rates for those coins using another token that has pairs with both of them โ€“ a connector token.

Supported Deployments

Ethereum Mainnet

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Binance Smart Chain (BSC, BNB)

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Polygon (Matic)

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Optimism (Optimistic)

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Arbitrum

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Avalanche (Avax)

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Gnosis (xDai)

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Fantom

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Aurora

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Klaytn

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

zkSync

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Base

Supported DEXes
Supported wrappers
Supported connectors
Prev oracle versions (legacy)

Examples

Oracle Deployment Guide

This section provides a comprehensive guide on deploying an oracle, including the nuances of script parameters and additional setup steps. Follow these steps to ensure accurate and reliable oracle deployment for price data retrieval.

Step 1: Surveying DEX Liquidity

  1. Identify DEXes with Sufficient Liquidity: Begin by surveying the network for Decentralized Exchanges (DEXes) that offer sufficient liquidity. This ensures the oracle can retrieve reliable and accurate price data.

Step 2: Selection of DEXes

  1. Select Supported DEXes: Choose DEXes that are supported by SpotPriceAggregator or are forks of supported protocols. Supported DEXes can be found in the contracts/oracles/ directory of the project.

Step 3: Network Configuration

  1. Configure the Network Settings:
    • Skip this step if your network is supported. This can be checked by observing whether the network is mentioned (registered or not) during a test run, visible in the console output. This verification can be done also by reviewing the registerAll method in the Networks class. If your network is listed there, it's considered supported, and no further action is required for registration in this step.
    • Update the Hardhat settings file to configure the network.
    • Utilize the Networks class from solidity-utils for network registration.
    • Example configuration snippet:
      ...
      const { Networks } = require('@1inch/solidity-utils/hardhat-setup');
      const net = new Networks(true, 'mainnet', true);
      net.register(your_network_name, networkId, process.env.YOURNETWORK_RPC_URL, process.env.YOURNETWORK_PRIVATE_KEY, etherscan_network_name, process.env.YOURNETWORK_ETHERSCAN_KEY);
      const networks = net.networks;
      const etherscan = net.etherscan;
      ...

Step 4: Environment Variables

  1. Set Environment Variables: Define necessary environment variables in the .env file located at the project root. Include variables such as YOURNETWORK_RPC_URL, YOURNETWORK_PRIVATE_KEY, and YOURNETWORK_ETHERSCAN_KEY with appropriate values:

    • YOURNETWORK_RPC_URL: The RPC URL for accessing your network's node. This URL can support the HTTP header 'auth-key'. To use this header, append the header value to the URL using the | symbol. For example: http://localhost:8545|HeaderValue. This format allows you to authenticate requests to your node.

    • YOURNETWORK_PRIVATE_KEY: Your account's private key, which should be entered without the 0x prefix. This key is used for deploying contracts and executing transactions on the network.

    • YOURNETWORK_ETHERSCAN_KEY: The API key for an Etherscan-like blockchain explorer that supports your network. This key is necessary for verifying and publishing your contract's source code. Ensure you register for an API key with a compatible explorer service for your network.

Step 5: Deploying Oracles

  1. Deploy Oracles:
    • Use the deploy script located at deploy/commands/simple-deploy.js. You can find a description of the script and how to use it in the scripts description.
    • Configure the PARAMS object for each protocol you wish to deploy an oracle for. The parameters include:
      • contractName: Name of the contract from the contracts/oracles/ directory.
      • args: Arguments required by the contract (See contract's constructor).
      • deploymentName: A name for your deployment, which will be used to create a file in the deployments/ directory.
    • Ensure the skip flag is set to false to proceed with deployment.
    • Example command for deployment: yarn && yarn deploy <your_network_name>.

Step 6: Deploying Wrappers

  1. Deploy Wrappers:
    • Follow similar steps as step 5 to deploy necessary wrappers and MultiWrapper. You can find different wrappers in the contracts/wrappers/ directory. After MultiWrapper is deployed, it will be possible to edit these lists of wrappers.

Step 7: Deploying OffchainOracle

  1. Deploy OffchainOracle:
    • Follow similar steps as step 5 to deploy the OffchainOracle. Make sure to include the deployed oracles (from step 5), MultiWrapper with wrappers (from step 6) and specifying the tokens you wish to use as connectors for price discovery. After OffchainOracle is deployed, it will be possible to edit these lists of oracles and connectors.

Support and Assistance

For any questions or further assistance, don't hesitate to reach out (for example via issue). This guide aims to facilitate your oracle deployment process, ensuring a smooth and reliable setup.

spot-price-aggregator's People

Contributors

belactriple9 avatar byshape avatar coolhill avatar krboktv avatar pavelkurmacheff avatar pzixel avatar sevenswen avatar simplyoptimistic avatar typicalbuster avatar zodenode avatar zumzoom avatar zzomrot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spot-price-aggregator's Issues

Add Support for Curve Pools

Task Description
1inch offchain oracle calculates the price forecast based on weighted average prices from different sources.
It is necessary to connect Curve pools as one of the price sources. To check if the result is correct, use the assumption that price extracted from the Curve pools shouldn't differ from the average weighted price calculated by off-chain oracle w/o Curve pools more than 1000WEI

PR Submission requiments

  • Smart contract implementation in solidity code
  • Tests covering smart contract code and demonstrating that code is working correctly.

Sources implementation examples
https://github.com/1inch/offchain-oracle/blob/master/contracts/oracles/UniswapOracle.sol
https://github.com/1inch/offchain-oracle/blob/master/contracts/oracles/MooniswapOracle.sol

Tests examples
https://github.com/1inch/offchain-oracle/blob/master/test/UniswapOracle.js
https://github.com/1inch/offchain-oracle/blob/master/test/MooniswapOracle.js

Support for linea

Hey,

Awesome library.

Do you plan to add support for linea prices?

Thanks,
JP

Why, in some cases, a spot price aggregator cannot return the price of a token with high liquidity on a well-known exchange. And another time it may return the price of a token with a liquidity of $200

For example, if we try to fetch price of WSM (0x62694d43ccb9b64e76e38385d15e325c7712a735), it listed on Uniswap, or EVDC(0x93749e69560efe1ad6661903e47df538492c50a4), listed on pancake we will get this answer.

{
  "0x62694d43ccb9b64e76e38385d15e325c7712a735": "0",
  "0x93749e69560efe1ad6661903e47df538492c50a4": "0"
}

But if we try it with some tokens with very low liquidity, for example BOTAI (0x20b2d9f094b7561a4abbcf8a2b88938b779c986d), liquidity in which does not exceed $200, we have correct answer

{
"0x20b2d9f094b7561a4abbcf8a2b88938b779c986d": "0.00002475886904925149",
}

I also leave curl here for you to check it up

curl -X GET \
      "https://api.1inch.dev/price/v1.1/56/0x93749e69560efe1ad6661903e47df538492c50a4,0x62694d43ccb9b64e76e38385d15e325c7712a735,0x20b2d9f094b7561a4abbcf8a2b88938b779c986d?currency=USD" \
      -H "Authorization: Bearer " \ # put key here
      -H "accept: application/json" \
      -H "content-type: application/json" \
      

RPC timeout while doing multicall on `aggregator` on local forks

I may be doing this wrong but I think there is an issue when performing multicall aggregator contract. The weird part is while performing a single call to just the getRate or getRateToEth functions it works sometimes, but takes 4-5 mins. But when it comes to multicall ganache/hardhat all make Time out error.

Poc:

  • Performed multicall on mainnet which works just fine
    image

  • Mainnet-fork using anvil
    image

My environment

  • OS: Linux: Debian
  • Framework: brownie & apeworx

getRate on BSC for EOS returns the wrong price

Referring to 0x0AdDd25a91563696D8567Df78D5A01C9a991F9B8 deployed on BSC.

Screenshot 2023-12-26 at 20 52 45

When calling the getRate function with EOS(0x56b6fb708fc5732dec1afc8d8556423a2edccbd6) and USDT(0x55d398326f99059fF775485246999027B3197955) as the src and dst tokens, the rate returned is ~$360,000 while it is supposed to be ~$0.84.

Which price does spot-price-aggregator returns, last, ask or else?

I fetch price data of coin AGI (0x818835503F55283cd51A4399f595e295A9338753) using price for requiested tokens(https://portal.1inch.dev/documentation/spot-price/swagger). And 1inch api return this
image
At the same time on 1inch web price is a bit different
image
The price of last deal is also different
image
Moreover, when it comes to pancake, the price of LP tokens is also distinct. Consequently, I find it challenging to determine which price the requested tokens will return. I believe that the difference in the USDT-USD price has a minor impact, as on the Binance Smart Chain (BSC), USDT currently equals 0.9998 USD, and the difference is relatively small at present. It's worth noting that in all my price calculations, I've consistently used the USDT price for AGI, always considering it as 1 USDT. Additionally, it's possible that the quantity of coins for the swap could influence the price, although this is not the case here. Furthermore, this matter has already been addressed in an open ticket.

I also want to mention that I picked AGI cause it only has one pair on defi (on pancake with USDT). Thats why the situation is not clear to me. How can 3 sources receiving the price from the same path have 3 different numbers

Add Support for Bancor Pools

Task Description
1inch offchain oracle calculates the price forecast based on weighted average prices from different sources.
It is necessary to connect Bancor pools as one of the price sources.
To check if the result is correct, use the assumption that price extracted from the Bancor pool shouldn't differ from the average weighted price calculated by off-chain oracle w/o Bancor pools more than 1000WEI

PR Submission requiments

  • Smart contract implementation in solidity code
  • Tests covering smart contract code and demonstrating that code is working correctly.

Sources implementation examples
https://github.com/1inch/offchain-oracle/blob/master/contracts/oracles/UniswapOracle.sol
https://github.com/1inch/offchain-oracle/blob/master/contracts/oracles/MooniswapOracle.sol

Tests examples
https://github.com/1inch/offchain-oracle/blob/master/test/UniswapOracle.js
https://github.com/1inch/offchain-oracle/blob/master/test/MooniswapOracle.js

Add price impact for getRate

  • Some token prices may be wrong in consideration of the liquidity available.
  • The impact rate is important to know the price error difference.
  • The impact can be used to as a flag for further use cases.

Issue with Uniswap V3 Gas Limit Estimation

I've checked a few times and the current deployed build is over estimating the gas limit of Uniswap V3 single swap transactions by 30%. I've checked swapping 0.01 Eth to DAI. I'm not sure if this exists on other Dex's estimations but it should be looked into. And hopefully I'm in the right place.

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.