LPTokenERC20.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const { MockProvider } = require("@ethereum-waffle/provider")
  2. const { expect } = require("chai")
  3. const { ethers } = require("hardhat")
  4. const { getApprovalDigest, getAddr } = require("./util/helpers")
  5. const { ecsign } = require("ethereumjs-util")
  6. const { deployNew } = require("./util/helpers")
  7. const { ZERO_ADDRESS } = require("./util/constants")
  8. const { BigNumber } = require("ethers")
  9. describe("LPTokenERC20:", function () {
  10. let lzEndpoint, initialSupply, owner, alice, bob, stargateTokenOwner, stargateTokenSpender, lpTokenErc20, chainId
  11. before(async function () {
  12. ;({ owner, bob, alice } = await getAddr(ethers))
  13. chainId = 1
  14. initialSupply = ethers.utils.parseEther("1000000000")
  15. })
  16. beforeEach(async function () {
  17. lzEndpoint = await deployNew("LZEndpointMock", [chainId])
  18. lpTokenErc20 = await deployNew("LPTokenERC20", ["X", "X*LP"])
  19. stargateTokenOwner = await deployNew("StargateToken", ["Stargate", "STG", lzEndpoint.address, chainId, initialSupply])
  20. stargateTokenSpender = await deployNew("StargateToken", ["Stargate", "STGLFG", lzEndpoint.address, chainId, initialSupply])
  21. })
  22. it("approve() - emit event / set", async function () {
  23. const amount = ethers.utils.parseEther("100")
  24. await expect(lpTokenErc20.approve(stargateTokenSpender.address, amount)).to.emit(lpTokenErc20, "Approval")
  25. })
  26. it("increaseApproval()", async function () {
  27. const amount = 3664
  28. expect(await lpTokenErc20.allowance(owner.address, alice.address)).to.equal(0)
  29. await lpTokenErc20.increaseAllowance(alice.address, amount)
  30. expect(await lpTokenErc20.allowance(owner.address, alice.address)).to.equal(3664)
  31. })
  32. it("decreaseApproval()", async function () {
  33. const amount = 3664
  34. expect(await lpTokenErc20.allowance(owner.address, alice.address)).to.equal(0)
  35. await lpTokenErc20.increaseAllowance(alice.address, amount)
  36. expect(await lpTokenErc20.allowance(owner.address, alice.address)).to.equal(3664)
  37. await lpTokenErc20.decreaseAllowance(alice.address, amount / 2)
  38. expect(await lpTokenErc20.allowance(owner.address, alice.address)).to.equal(amount / 2)
  39. })
  40. it("permit() - reverts if deadline is before block timestamp", async function () {
  41. await expect(
  42. lpTokenErc20.permit(
  43. ZERO_ADDRESS,
  44. ZERO_ADDRESS,
  45. 0,
  46. 1,
  47. 1,
  48. "0x0000000000000000000000000000000000000000000000000000000000000000",
  49. "0x0000000000000000000000000000000000000000000000000000000000000000"
  50. )
  51. ).to.revertedWith("Bridge: EXPIRED")
  52. })
  53. it("permit()", async function () {
  54. const TEST_AMOUNT = ethers.utils.parseEther("10")
  55. const provider = new MockProvider({
  56. ganacheOptions: {
  57. hardfork: "istanbul",
  58. mnemonic: "horn horn horn horn horn horn horn horn horn horn horn horn",
  59. gasLimit: 9999999,
  60. },
  61. })
  62. const [wallet, other] = provider.getWallets()
  63. const nonce = await lpTokenErc20.nonces(wallet.address)
  64. const deadline = ethers.constants.MaxUint256
  65. const digest = await getApprovalDigest(
  66. lpTokenErc20,
  67. { owner: wallet.address, spender: other.address, value: TEST_AMOUNT },
  68. nonce,
  69. deadline
  70. )
  71. const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"))
  72. await expect(
  73. lpTokenErc20.permit(wallet.address, other.address, TEST_AMOUNT, deadline, v, ethers.utils.hexlify(r), ethers.utils.hexlify(s))
  74. )
  75. .to.emit(lpTokenErc20, "Approval")
  76. .withArgs(wallet.address, other.address, TEST_AMOUNT)
  77. expect(await lpTokenErc20.allowance(wallet.address, other.address)).to.eq(TEST_AMOUNT)
  78. expect(await lpTokenErc20.nonces(wallet.address)).to.eq(BigNumber.from(1))
  79. })
  80. it("permit() - reverts with invalid signature", async function () {
  81. const TEST_AMOUNT = ethers.utils.parseEther("10")
  82. const provider = new MockProvider({
  83. ganacheOptions: {
  84. hardfork: "istanbul",
  85. mnemonic: "horn horn horn horn horn horn horn horn horn horn horn horn",
  86. gasLimit: 9999999,
  87. },
  88. })
  89. const [wallet, other] = provider.getWallets()
  90. const nonce = await lpTokenErc20.nonces(wallet.address)
  91. const deadline = ethers.constants.MaxUint256
  92. const digest = await getApprovalDigest(
  93. lpTokenErc20,
  94. { owner: wallet.address, spender: other.address, value: TEST_AMOUNT },
  95. nonce,
  96. deadline
  97. )
  98. const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"))
  99. // pass the wrong "owner" into permit()
  100. await expect(
  101. lpTokenErc20.permit(bob.address, other.address, TEST_AMOUNT, deadline, v, ethers.utils.hexlify(r), ethers.utils.hexlify(s))
  102. ).to.revertedWith("Bridge: INVALID_SIGNATURE")
  103. })
  104. })