'Decentralized Exchanges Quotes: how to retrieve real time quotes?

Is there any API key / solidity function that would allow me to retrieve real time quotes from exchanges? I already tried 0x API and DEX.AG but both are quite slow (1/2 calls per sec).



Solution 1:[1]

Yes, it is not hard but you will have to put in some work. Lets say you have 1 ETH and you want to know how much DAI the exchange will give you for it.

Example:Uniswap v2 Router:

https://etherscan.io/address/0xf164fc0ec4e93095b804a4795bbe1e041497b92a

Under 'Contracts' you can see the function getAmountsOut (Copied here to make it easier to understand)

function getAmountsOut(uint amountIn, address[] memory path) public view override returns (uint[] memory amounts) {
    return UniswapV2Library.getAmountsOut(factory, amountIn, path);
}

This 'getAmountsOut' function will accept an integer amountIn, and two addresses called path.

If we input 1 for amountIn, and two addresses

WETH, DAI

[0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, 0x6b175474e89094c44da98b954eedeac495271d0f]

the function will return the 'amounts' which is what we asked for which as of the time of writing this is 1695 DAI for 1 WETH.

Here are a couple of tutorials THAT I DID NOT WRITE that should help you get started.

UNISWAP v2: https://soliditydeveloper.com/uniswap2

MORE UNISWAP V2: https://vomtom.at/how-to-use-uniswap-v2-as-a-developer/

Solution 2:[2]

For automated market-makers like Uniswap, you can directly use their SDK.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mikko Ohtamaa