Last active
May 1, 2020 15:23
-
-
Save erin-koen/7ee2a1678f67258aebfcc9f2981a454f to your computer and use it in GitHub Desktop.
The Melon Bot's Price Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async getPrice(baseCurrency: TokenDefinition, quoteCurrency: TokenDefinition, baseQuantity: BigNumber) { | |
// Every uniswap exchange is WETH/someToken, and identified by the non-weth token | |
const exchangeToken = baseCurrency.symbol === 'WETH' ? quoteCurrency : baseCurrency; | |
// call the method to find the address | |
const exchangeAddress = await this.uniswapFactoryContract.getExchange(exchangeToken.address); | |
// instantiate the exchange contract | |
const exchange = new UniswapExchange(this.environment, exchangeAddress); | |
// call the correct method to get the price. If the base currency is WETH, you want to go ETH => token and vice versa | |
const quoteQuantity = | |
baseCurrency.symbol === 'WETH' | |
? await exchange.getEthToTokenInputPrice(baseQuantity) // quantity passed is in WETH if you're trying to sell WETH for MLN | |
: await exchange.getTokenToEthInputPrice(baseQuantity); // quantity passed is in MLN if you're trying to sell MLN for WETH | |
// price will be important if you're doing any TA. My magicFunction doesn't use it but I've included it anyway. | |
const priceInBase = quoteQuantity.dividedBy(baseQuantity); | |
const priceInQuote = new BigNumber(1).dividedBy(priceInBase); | |
return { | |
baseCurrency: baseCurrency, | |
quoteCurrency: quoteCurrency, | |
priceInBase: priceInBase, | |
priceInQuote: priceInQuote, | |
sizeInBase: baseQuantity, | |
sizeInQuote: quoteQuantity, | |
exchangeAddress: exchangeAddress, | |
} as PriceQueryResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment