StargateToken.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const { expect } = require("chai")
  2. const { ethers } = require("hardhat")
  3. const CONFIG = require("../constants/config.json")
  4. const { callAsContract } = require("../test/util/helpers")
  5. const { ZERO_ADDRESS } = require("./util/constants")
  6. const { getAddr, deployNew, encodeParams } = require("./util/helpers")
  7. describe("StargateToke:", function () {
  8. let owner, alice, badUser1, fakeContract, lzEndpoint
  9. let chainId, chainIdB, nonce, symbol, decimals, name, initSupplyEndpoint, stargateToken, lzVersion
  10. before(async function () {
  11. ;({ owner, alice, badUser1, fakeContract } = await getAddr(ethers))
  12. name = CONFIG.stargateToken.name
  13. symbol = CONFIG.stargateToken.name
  14. decimals = 18
  15. chainId = 1
  16. chainIdB = 2
  17. nonce = 1
  18. lzVersion = 1
  19. initSupplyEndpoint = ethers.utils.parseEther("1000000000")
  20. })
  21. beforeEach(async function () {
  22. lzEndpoint = await deployNew("LZEndpointMock", [chainId])
  23. initSupplyEndpoint = ethers.utils.parseEther("1000000000")
  24. stargateToken = await deployNew("StargateToken", [name, symbol, lzEndpoint.address, chainId, initSupplyEndpoint])
  25. })
  26. it("name()", async function () {
  27. expect(await stargateToken.name()).to.equal(name)
  28. })
  29. it("symbol()", async function () {
  30. expect(await stargateToken.symbol()).to.equal(symbol)
  31. })
  32. it("decimals()", async function () {
  33. expect(await stargateToken.decimals()).to.equal(decimals)
  34. })
  35. it("constructor() - mints to deployer", async function () {
  36. expect(await stargateToken.balanceOf(owner.address)).to.equal(initSupplyEndpoint)
  37. })
  38. // it("mint() reverts when called by non Owner ", async function () {
  39. // await expect(
  40. // stargateToken.connect(bob).mint(alice.address, 1)
  41. // ).to.be.revertedWith("Ownable: caller is not the owner");
  42. // });
  43. it("renounceOwnership() - doesnt affect ownership", async function () {
  44. expect(await stargateToken.owner()).to.equal(owner.address)
  45. await stargateToken.renounceOwnership()
  46. expect(await stargateToken.owner()).to.equal(owner.address)
  47. })
  48. it("lzReceive() - can mint to an address once wired", async function () {
  49. const qty = ethers.utils.parseEther("1")
  50. const payload = encodeParams(["bytes", "uint256"], [alice.address, qty.toString()])
  51. // deploy an oft contract that is not master by using the non 'mainEndpoint' chain id on deployment
  52. const StargateToken = await ethers.getContractFactory("StargateToken")
  53. const stargateTokenB = await StargateToken.deploy(name, symbol, lzEndpoint.address, chainIdB, initSupplyEndpoint)
  54. // should revert before we wire
  55. await expect(
  56. callAsContract(stargateTokenB, lzEndpoint.address, "lzReceive(uint16,bytes,uint64,bytes)", [
  57. chainIdB,
  58. stargateTokenB.address,
  59. nonce,
  60. payload,
  61. ])
  62. ).to.be.revertedWith("OFT: invalid source sending contract")
  63. expect(await stargateTokenB.balanceOf(alice.address)).to.equal(0)
  64. // set the destination contract address to enable this chain to receive from this address
  65. await stargateTokenB.setDestination(chainIdB, stargateTokenB.address)
  66. expect(await stargateTokenB.dstContractLookup(chainIdB)).to.equal(stargateTokenB.address.toLowerCase())
  67. // can now receive due to wiring
  68. await callAsContract(stargateTokenB, lzEndpoint.address, "lzReceive(uint16,bytes,uint64,bytes)", [
  69. chainIdB,
  70. stargateTokenB.address,
  71. nonce,
  72. payload,
  73. ])
  74. expect(await stargateTokenB.balanceOf(alice.address)).to.equal(qty)
  75. })
  76. it("setConfig() - reverts when non owner", async function () {
  77. const payload = encodeParams(["uint16", "address"], [1, ZERO_ADDRESS])
  78. await expect(stargateToken.connect(alice).setConfig(lzVersion, chainId, 1, payload)).to.be.revertedWith(
  79. "Ownable: caller is not the owner"
  80. )
  81. })
  82. it("setDestination() - reverts with non owner", async function () {
  83. await expect(stargateToken.connect(alice).setDestination(1, stargateToken.address)).to.be.revertedWith(
  84. "Ownable: caller is not the owner"
  85. )
  86. })
  87. it("sendTokens()", async function () {
  88. const qty = ethers.utils.parseEther("1")
  89. const txParameters = "0x"
  90. // initial values
  91. expect(await stargateToken.totalSupply()).to.equal(initSupplyEndpoint)
  92. expect(await stargateToken.balanceOf(owner.address)).to.equal(initSupplyEndpoint)
  93. expect(await stargateToken.balanceOf(alice.address)).to.equal(0)
  94. // wire
  95. await stargateToken.setDestination(chainId, stargateToken.address)
  96. await lzEndpoint.setDestLzEndpoint(stargateToken.address, lzEndpoint.address)
  97. await stargateToken.connect(owner).sendTokens(chainId, alice.address, qty, ZERO_ADDRESS, txParameters, {
  98. value: ethers.utils.parseEther("0.1"),
  99. })
  100. expect(await stargateToken.balanceOf(owner.address)).to.equal(initSupplyEndpoint.sub(qty))
  101. expect(await stargateToken.balanceOf(alice.address)).to.equal(qty)
  102. })
  103. it("pauseSendTokens() - reverts with non owner", async function () {
  104. await expect(stargateToken.connect(alice).pauseSendTokens(true)).to.be.revertedWith("Ownable: caller is not the owner")
  105. })
  106. it("pauseSendTokens()", async function () {
  107. expect(await stargateToken.paused()).to.equal(false)
  108. await stargateToken.connect(owner).pauseSendTokens(true)
  109. expect(await stargateToken.paused()).to.equal(true)
  110. await stargateToken.connect(owner).pauseSendTokens(false)
  111. expect(await stargateToken.paused()).to.equal(false)
  112. })
  113. it("pauseSendTokens() - cant transfer", async function () {
  114. await stargateToken.connect(owner).pauseSendTokens(true)
  115. const qty = ethers.utils.parseEther("1")
  116. const txParameters = "0x"
  117. // wire
  118. await stargateToken.setDestination(chainId, stargateToken.address)
  119. await lzEndpoint.setDestLzEndpoint(stargateToken.address, lzEndpoint.address)
  120. // cant send
  121. await expect(
  122. stargateToken.connect(owner).sendTokens(chainId, alice.address, qty, ZERO_ADDRESS, txParameters, {
  123. value: ethers.utils.parseEther("0.1"),
  124. })
  125. ).to.be.revertedWith("OFT: sendTokens() is currently paused")
  126. await expect(
  127. stargateToken.connect(alice).sendTokens(chainId, alice.address, qty, ZERO_ADDRESS, txParameters, {
  128. value: ethers.utils.parseEther("0.1"),
  129. })
  130. ).to.be.revertedWith("OFT: sendTokens() is currently paused")
  131. // unpause
  132. await stargateToken.connect(owner).pauseSendTokens(false)
  133. // can send
  134. await expect(
  135. stargateToken.connect(owner).sendTokens(chainId, alice.address, qty, ZERO_ADDRESS, txParameters, {
  136. value: ethers.utils.parseEther("0.1"),
  137. })
  138. ).to.not.be.revertedWith
  139. await expect(
  140. stargateToken.connect(alice).sendTokens(chainId, alice.address, qty, ZERO_ADDRESS, txParameters, {
  141. value: ethers.utils.parseEther("0.1"),
  142. })
  143. ).to.not.be.revertedWith
  144. })
  145. })