Uniswap / v3-periphery

🦄 🦄 🦄 Peripheral smart contracts for interacting with Uniswap v3
https://uniswap.org
GNU General Public License v2.0
1.17k stars 1.09k forks source link

I can not get correct amountOut by quoteExactInputSingle #248

Closed dwdpbcsf closed 2 years ago

dwdpbcsf commented 2 years ago
const Web3 = require("web3")
const fs = require('fs')

let web3 = new Web3('http://127.0.0.1:8545')

let myABI = JSON.parse(fs.readFileSync('./0_abi/Quoter.abi').toString())

let addrQuoter = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6' // quoter
let addrTokenIn = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' // ETH
let addrTokenOut = '0x6B175474E89094C44Da98b954EedeAC495271d0F' // DAI

let fee = 500 // 0.05%
let amountIn = 1
let sqrtPriceLimitX96 = 0

let myContract = new web3.eth.Contract(myABI, addrQuoter)

myContract.methods.quoteExactInputSingle(
    addrTokenIn,
    addrTokenOut,
    fee,
    amountIn,
    sqrtPriceLimitX96).call((err, res) => {
    if (err) return console.log(err)
    console.log(res)
})

if amountIn == 1, res is 0 if amountIn == 2, res is 2730, close to the real market price if amountIn == 3, res is 5461, ... so the real amountIn is (amountIn - 1) why?

And the liquidity is limited. But as the amountIn become bigger, the amountOut is bigger.

const ethers = require('ethers')
const quoterABI = require('@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json').abi
const tokenAddresses = require('./src/tokenaddress')

const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545')

const quoterAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6' // quoter

const quoterContract = new ethers.Contract(quoterAddress, quoterABI, provider)

const getAmountOut = async () => {
    const amountIn = 1;

    const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(
        tokenAddresses['ETH'],
        tokenAddresses['DAI'],
        500,
        amountIn.toString(),
        0
    );

    console.log(quotedAmountOut.toNumber())
}

getAmountOut().then()

I use the uniswap v3 sdk to do the same thing. (https://docs.uniswap.org/sdk/guides/creating-a-trade) The same problems occur.

If amountIn is 1, the amountOut is 0, and so on.

So, is this a bug?

moodysalem commented 2 years ago

It's not a bug, the fee taken on 1 unit of any token leaves 0 units remaining to swap. You can't swap less than 1 unit. 1 here represents 1 wei of ETH

dwdpbcsf commented 2 years ago
Thanks for your help! I understand. 丁文迪

@.*** | 签名由网易邮箱大师定制

On 02/28/2022 23:37,Moody @.***> wrote:

It's not a bug, the fee taken on 1 unit of any token leaves 0 units remaining to swap. You can't swap less than 1 unit. 1 here represents 1 wei of ETH

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: @.***>

andreyLed commented 2 years ago

Hello. Have some issue with quoteExactInputSingle(). It works for me 50/50. I can't figure out why it doesn't work with some currencies

for example tokenIn: '0x6b175474e89094c44da98b954eedeac495271d0f', tokenOut: '0xbbbbca6a901c926f240b89eacb641d8aec7aeafd', fee: 3000,

failed(( with Error: call revert exception