Giter VIP home page Giter VIP logo

Comments (3)

haydenadams avatar haydenadams commented on August 22, 2024 1

If you guys are open to using web3 directly, prices are very easy. There is a mapping of tokenAddress -> exchangeAddress which you can get from the now finished directory API endpoint, or can just pull all NewExchange events. Or just call getExchange(tokenAddress) on the factory.

If I want to calculate the cost of buying/selling DAI, all you need is the ETH and DAI balance of the DAI exchange contract:

ethReserve = web3.eth.getBalance(daiExchangeAddress)
erc20Reserve = daiContract.balanceOf(daiExchangeAddress)

then you just plug into either the buy or sell formula, as implemented in the contract:

/*
If you're selling ERC20 for ETH, sold reserve is erc20Reserve, boughtReserve is ethReserve
If you're selling ETH for ERC20, sold reserve is ethReserve, boughtReserve is erc20Reserve
*/
function sellPrice(amountSold, soldReserve, boughtReserve) {
   numerator = amountSold * boughtReserve * 997
   denominator = soldReserve * 1000 + amountSold * 997
   amountBought = numerator / denominator
}

/*
If you're buying ERC20 with ETH, sold reserve is ethReserve, boughtReserve is erc20Reserve
If you're buying ETH with ERC20, sold reserve is erc20Reserve, boughtReserve is ethReserve
*/
function buyPrice(amountBought, soldReserve, boughtReserve) {
   numerator = amountBought * soldReserve * 1000
   denominator = (boughtReserve - amountBought) * 997
   amountSold = numerator / denominator + 1
}

from ethereum-dex-prices-service.

perich avatar perich commented on August 22, 2024

Thanks @haydenadams ! Seems straight forward from here.

from ethereum-dex-prices-service.

perich avatar perich commented on August 22, 2024

resolved by #12

from ethereum-dex-prices-service.

Related Issues (17)

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.