Does anyone know a reliable way of fetching price of a token from AMMs, without the interference of flashloan manipulations onto a contract?

Dec 16, 2021, 10:32 PM
Without using an oracle too...
Dec 16, 2021, 10:33 PM
the price at any given time is simply the pair's balance of the token in question divided by the pair's balance of the other token
or vice versa
Dec 16, 2021, 10:42 PM
not balance, but reserves
Dec 16, 2021, 11:00 PM
yes
Dec 16, 2021, 11:00 PM
Which can be manipulated by a flashloan. I know how to get the price from pair, but need some reliability
Dec 16, 2021, 11:11 PM
Aren't flash loans paid back within the same transaction? And even during that, the reserves aren't affected, only the balances. So if you need a correct price even inside a flashloaned transaction, you can still use reserves
Reserves get updated at end of the tx
Dec 16, 2021, 11:17 PM
I have a different usecase
Token price is taken for a reward calculation purpose
Dec 16, 2021, 11:19 PM
Ok I'm unsure why a flash loan happening in one transaction would matter
Dec 16, 2021, 11:20 PM
My attack scenario is
Take bnb flashloan and buy token x
Call a function in my contract
Fetching token x price from manipulated pool
Gets huge amount of reward than normal
Sell x token and repay bnb flashloan
Wanna avoid this
Dec 16, 2021, 11:21 PM
Reserves aren't affected by flash loan during the transaction...only at the end (after the loan is paid back)
Dec 16, 2021, 11:22 PM
Flashloan is not taken in token, rather bnb or some stable coin
Which is then used to purchase the token
Which will affect the reserve
Edited so that it makes more sense
How does 1inch calculate price. If I pull price from their aggregator, will that be more reliable?
Dec 16, 2021, 11:26 PM
Ok that makes more sense. Maybe you need a twap then
Dec 16, 2021, 11:26 PM
Umm what's a twap?
Dec 16, 2021, 11:27 PM
Time weighted average price
I believe uniswap v2 contracts have a simple one built in
Dec 16, 2021, 11:27 PM
Haha that's what I am doing right now
Using 15 data points to average
Dec 16, 2021, 11:27 PM
Or if you want to use reserves, just maybe use reserves from a block in the recent past, then the flash loan attack doesn't seem possible?
Dec 16, 2021, 11:28 PM
Ooh they do? Have some docs pointing to that?
Dec 16, 2021, 11:28 PM
I think so...on mobile right now but I recall seeing it
Dec 16, 2021, 11:28 PM
Which cannot be accessed in solidity unfortunately
Dec 16, 2021, 11:29 PM
Well if you fetch/store it you can...
I.e. piggybacking off some other transactions perhaps
Just a thought, Ive seen similar strategies
Dec 16, 2021, 11:29 PM
Thought of that, but my contract won't have much traffic. Setting up a bot would be costly too
Dec 16, 2021, 11:30 PM
Yeah on eth it will suck for sure
Dec 16, 2021, 11:31 PM
Exactly. Couple 1000 bucks per day
No thanks xD
Dec 16, 2021, 11:31 PM
Other option is setting up a chainlink price feed I suppose
Dec 16, 2021, 11:31 PM
Thought of that too, but I have multiple tokens whose price should be fetched. So I have to pay chainlink to make oracles for each of them...
Don't think that's ideal
I'll look into this more. As of now I'm calculating manually, so if there is an already built in method, I'll just pick that
Thanks for your input @coffeeconverter :)
Dec 16, 2021, 11:38 PM
No prob yeah hopefully that works
Dec 16, 2021, 11:40 PM
Yes
Look at the white paper
They call it out. Might also be in their online docs, bht it’s for sure in their white paper
Dec 16, 2021, 11:48 PM

© 2024 Draquery.com All rights reserved.