Fail to swap ETH to dai using uniswap sdk with web3.js

Viewed 1713

I am coming across an issue swapping weth to dai using uniswap Node.js SDK with web3.js.

My code is below:

const dai = await Fetcher.fetchTokenData(net, daiAddress);
const weth = await Fetcher.fetchTokenData(net, wethAddress);
const pair = await Fetcher.fetchPairData(dai, weth);
const route = new Route([pair], weth);
console.log(route.midPrice.toSignificant(6))
console.log(route.midPrice.invert().toSignificant(6))

const trade = new Trade(route, new TokenAmount(weth, "1000000000000000000"), TradeType.EXACT_INPUT)
console.log(trade.executionPrice.toSignificant(6))

const slippageTollerance = new Percent("10", "10000")
const amountOutMin = trade.minimumAmountOut(slippageTollerance)
// const amountOutMin = web3.current.utils.toBN(amountOutMinRaw)
const path = [weth.address, dai.address]

const deadline = Math.floor(Date.now() / 1000 ) + 60*20
const amountIn = trade.inputAmount.raw
const to = ""

uniSwapRouter.current = new web3.current.eth.Contract(
  uniRouterAbi,
  "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
);  
const tx = await uniSwapRouter.current.methods.swapExactETHForTokens(
  // amountIn,
  1,
  to,
  path,
  deadline
).send({
  from:accounts[0],
  gasLimit: 8000000,  
  gasPrice: web3.current.utils.toWei('100', 'Gwei'), 
  value: web3.current.utils.toWei('1', 'Ether') 
})
console.log(tx.hash)

And it turns error:

Unhandled Rejection (Error): expected array value (argument="path", value="", code=INVALID_ARGUMENT, version=abi/5.0.7)

Anyone helps let me know which part I mess up. I have been struggling with it for two days.

I am running it on kovan testnet and I am sure that I have some DAI(11.647) & ETH(2.4845) in my kovan metamask wallet.

1 Answers

I think I found out where the thing gone wrong. The deadline might be too short to go through the transaction. It turned out ok after expending the deadline.

Related