addLiquidity.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. task("addLiquidity", "add liquidity to stargate instance")
  2. .addParam("poolId", "the poolId")
  3. .addParam("qty", "the qty of tokens to add")
  4. .setAction(async (taskArgs, hre) => {
  5. let accounts = await ethers.getSigners()
  6. let owner = accounts[0]
  7. // factory / router
  8. const factory = await ethers.getContract("Factory")
  9. const router = await ethers.getContract("Router")
  10. // get the token from the router
  11. let Pool = await ethers.getContractFactory("Pool")
  12. let poolData = await factory.getPool(taskArgs.poolId) // return stg lp address
  13. let pool = await Pool.attach(poolData)
  14. let tokenAddr = await pool.token()
  15. console.log(`${hre.network.name} > addLiquidity poolId:${taskArgs.poolId} tokenAddr: ${tokenAddr}`)
  16. let MockToken = await ethers.getContractFactory("MockToken")
  17. let mockToken = await MockToken.attach(tokenAddr)
  18. let balance = await mockToken.balanceOf(owner.address)
  19. let allowance = await mockToken.allowance(owner.address, router.address)
  20. let amountInTokenDecimals = ethers.utils.parseUnits(taskArgs.qty, await mockToken.decimals())
  21. console.log(`amountInTokenDecimals: ${amountInTokenDecimals}`)
  22. await (await mockToken.mint(owner.address, amountInTokenDecimals)).wait(1)
  23. if (balance == 0 || balance < allowance) {
  24. await (await mockToken.mint(owner.address, amountInTokenDecimals)).wait(1)
  25. balance = await mockToken.balanceOf(owner.address)
  26. console.log(` (now) we have this much to addLiquidity: ${balance}`)
  27. }
  28. let t = await mockToken.approve(router.address, amountInTokenDecimals) // give stargate router approval for this qty
  29. await t.wait(1)
  30. let tx = await (await router.addLiquidity(taskArgs.poolId, amountInTokenDecimals, owner.address)).wait(1)
  31. console.log(`💦 addLiquidity | tx: ${tx.transactionHash}`)
  32. })