Metamask-extension: Have price estimation fall back to dexes

Created on 10 Jan 2019  路  8Comments  路  Source: MetaMask/metamask-extension

Currently we just hit the balanc3 api then call it a day. To give estimations of increasingly small tokens, it would be cool to check dexes for conversion rates.

I think uniswap would be a good MVP of this.

It sure would be nice if there were a uniswap exchange on every network we connected to. They could even have the same address:
https://twitter.com/PhABCD/status/1082703455943315456

L11-integration

All 8 comments

@danfinlay I think this is a great idea - let me know if I can help in any way. Uniswap is being efficiently arbitraged, so it acts as a good price oracle. Setting this up should be easy.

For a given token:

exchangeAddress = UniswapFactory.getExchange(tokenAddress)

ethBal = web3.eth.getBalance(exchangeAddress)
tokenBal = TokenContract.balanceOf(exchangeAddress)

// ERC20 / ETH rate
rate = tokenBal/ethBal 

Then do the same for DAI to get the USD rate of ETH and you're done! To make sure you're always getting a good rate, only do this for tokens with more than 10 ETH in liquidity.

@haydenadams Would you consider deploying UniSwap contracts to the same addresses on different network via CREATE2 post Constantinople fork?

Would be awesome. But requires a redeploy of Uniswap - so it would have to be bundled in with other significant upgrades to justify.

What networks are you thinking of?

It would be easy to hard-code to point to the current contract for the main chain, but for "the world of coming other chains", would be great to have a fallback address that is deterministic and trustworthy via create2.

Other networks include:

  • rootstock
  • xdai
  • poa
  • etc

And more are really being created all the time.

@haydenadams A nice compromise would be to not upgrade the mainnet contract, but have all the other "copies" deployed using CREATE2. So only 1/N would be different, or (N-1) / N would be the same. That would already be an improvement until uniswap V2.

@danfinlay gotcha, yeah I like the idea. Will look into it!

@PhABC seems like a reasonable solution

One concern:

If token price estimation fell back to Uniswap, we would probably want some visual indication of the market depth, otherwise phishers would be sure to prop up their scam coins with tiny collaterals.

Has anyone built a uniswap market depth component yet?

@danfinlay Doing this in web3 is super easy. Uniswap maintains a 50-50 value of ETH and the erc20 token being sold in the relevant exchange contract. Checking the ETH balance is the best way of finding the depth.

Finding the MKR depth is as simple as:

mkrExchangeAddress = factoryContract.getExchange(mkrAddress)
// Measured in ETH
mkrDepth = web3.eth.getBalance(mkrExchangeAddress)

If you want the depth measured relative to DAI, just get the ETH<>DAI exchange rate on Uniswap. That pair has plenty of liquidity ($225k as of today).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kumavis picture kumavis  路  3Comments

johnerfx picture johnerfx  路  4Comments

rossbulat picture rossbulat  路  3Comments

estebanmino picture estebanmino  路  3Comments

BassBauman picture BassBauman  路  3Comments