swap.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. task("swap", "swap using stargate")
  2. .addParam("router", "The stargate Router address (locally)")
  3. .addParam("factory", "The stargate Factory address (locally)")
  4. .addParam("poolId", "the poolId")
  5. .addParam("dstChainId", "the destination chainId")
  6. .addParam("qty", "the quantity to swap")
  7. .addParam("minQtyOut", "the minimum qty you want to get out")
  8. .setAction(async (taskArgs) => {
  9. console.log(`taskArgs: ${JSON.stringify(taskArgs)}`)
  10. let accounts = await ethers.getSigners()
  11. let owner = accounts[0] // me
  12. console.log(`owner: ${owner.address}`)
  13. let Factory = await ethers.getContractFactory("Factory")
  14. let factory = await Factory.attach(taskArgs.factory)
  15. let Router = await ethers.getContractFactory("Router")
  16. let router = await Router.attach(taskArgs.router)
  17. // get the token address from the router for the pool id so we know the address to approve
  18. let Pool = await ethers.getContractFactory("Pool")
  19. let poolData = await factory.getPool(taskArgs.poolId)
  20. let pool = await Pool.attach(poolData)
  21. let poolTokenAddr = await pool.token()
  22. console.log(`swap() poolTokenAddr: ${poolTokenAddr}`)
  23. let MockToken = await ethers.getContractFactory("MockToken") // erc20
  24. let mockToken = await MockToken.attach(poolTokenAddr)
  25. await (await mockToken.approve(router.address, taskArgs.qty)).wait(1)
  26. let tx = await (
  27. await router.swap(
  28. taskArgs.dstChainId,
  29. taskArgs.poolId,
  30. owner.address,
  31. taskArgs.qty,
  32. taskArgs.minQtyOut,
  33. 0,
  34. owner.address,
  35. "0x",
  36. { value: ethers.utils.parseEther("0.1") } // guess a value high enough , it refunds extra
  37. )
  38. ).wait()
  39. console.log(`tx.transactionHash: ${tx.transactionHash}`)
  40. })