Comments (3)
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.
Thanks @haydenadams ! Seems straight forward from here.
from ethereum-dex-prices-service.
resolved by #12
from ethereum-dex-prices-service.
Related Issues (17)
- Add support for The Ocean HOT 2
- Introduce new "metadata" field to the result data
- UnhandledPromiseRejectionWarning: Could not connect to socket HOT 7
- Add unit tests for Uniswap HOT 3
- Discussion: Support of the Standard Relayer API (Contract Addresses vs. Symbols) HOT 12
- DEXes APIs HOT 1
- Server Crashing due to timeout HOT 3
- Add support for EthFinex HOT 3
- cli.js require(main) error HOT 1
- Refactor Ethfinex class HOT 1
- Eth2Dai closed HOT 2
- Oasis moving contracts HOT 1
- Refactor Eth2dai class as OasisTrade class
- Fix RadarRelay class HOT 1
- Add support for ForkDelta HOT 1
- Discussion: Addition of unit tests and/or integration tests HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ethereum-dex-prices-service.