Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get current price of a pair #1

Open
soltrac opened this issue Feb 25, 2021 · 2 comments
Open

How to get current price of a pair #1

soltrac opened this issue Feb 25, 2021 · 2 comments

Comments

@soltrac
Copy link

soltrac commented Feb 25, 2021

Hello,

I'm very new to uniswap sdk. I'm trying to figure out with this SDK. Given a contract with the pair, how can I know the current price?

Thanks

@juanfranblanco
Copy link
Member

https://uniswap.org/docs/v2/advanced-topics/pricing/
https://uniswap.org/docs/v2/smart-contracts/pair/

@lunarmoon26
Copy link

Here is my code:

            int precision = 10000000;
            var getAmountsOutFunctionMessage = new GetAmountsOutFunction()
            {
                AmountIn = precision,
                Path = GetPath() // List<>{tokenAddress, ..., usdtAddress}
            };
            var getAmountsOutHandler = web3.Eth.GetContractQueryHandler<GetAmountsOutFunction>();
            var amounts = await getAmountsOutHandler
                .QueryAsync<List<BigInteger>>(
                routerAddress),
                getAmountsOutFunctionMessage);

            var ratio = BigDecimal.Pow(10, usdtDecimals - tokenDecimals);

            var priceUsd = amounts.Last() / ratio / precision;

Remember to get the decimal difference of the token to USDT, it's important

also the precision should be optimal, any smaller number will cause the query hang if the token price is too small.

excuse for bad design pattern, many of those variables can be reused. I generally just extract some key steps from my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants