redeemLocal.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { getEndpointIdByName } = require("@layerzerolabs/lz-sdk")
  2. module.exports = async function (taskArgs, hre) {
  3. let accounts = await ethers.getSigners()
  4. let owner = accounts[0] // me
  5. console.log(`owner: ${owner.address}`)
  6. // get the destination chainId
  7. const srcChainId = getEndpointIdByName(hre.network.name)
  8. const dstChainId = getEndpointIdByName(taskArgs.targetNetwork)
  9. // factory / router
  10. const factory = await ethers.getContract("Factory")
  11. const router = await ethers.getContract("Router")
  12. // get the token from the router
  13. let Pool = await ethers.getContractFactory("Pool")
  14. let poolData = await factory.getPool(taskArgs.poolId) // return stg lp address
  15. let pool = await Pool.attach(poolData)
  16. let tokenAddr = await pool.token()
  17. let withdrawLpQty = taskArgs.qty
  18. if (withdrawLpQty == 0) {
  19. withdrawLpQty = await pool.balanceOf(owner.address)
  20. }
  21. console.log(`${hre.network.name}[${srcChainId}] redeemLocal poolId:${taskArgs.poolId} tokenAddr: ${tokenAddr}`)
  22. console.log(` -> dstChainId: ${dstChainId} , removing ${taskArgs.qty} LP`)
  23. //return
  24. let tx = await (
  25. await router.redeemLocal(
  26. dstChainId,
  27. taskArgs.poolId, // source pool id
  28. taskArgs.dstPoolId,
  29. owner.address, // refund address
  30. withdrawLpQty, // amount LP to remove corresponds to the liquidity to remove
  31. owner.address, // to
  32. { dstGasForCall: 300000, dstNativeAmount: 0, dstNativeAddr: "0x" },
  33. { value: ethers.utils.parseEther("2") } // send native value for the underlying message cost
  34. )
  35. ).wait(1)
  36. console.log(`-💦 redeemLocal qty: ${withdrawLpQty} | tx: ${tx.transactionHash}`)
  37. }