Create2.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { balance, ether, expectEvent, expectRevert, send } = require('@openzeppelin/test-helpers');
  4. const { expectRevertCustomError } = require('../helpers/customError');
  5. const Create2 = artifacts.require('$Create2');
  6. const VestingWallet = artifacts.require('VestingWallet');
  7. // This should be a contract that:
  8. // - has no constructor arguments
  9. // - has no immutable variable populated during construction
  10. const ConstructorLessContract = Create2;
  11. contract('Create2', function (accounts) {
  12. const [deployerAccount, other] = accounts;
  13. const salt = 'salt message';
  14. const saltHex = web3.utils.soliditySha3(salt);
  15. const encodedParams = web3.eth.abi.encodeParameters(['address', 'uint64', 'uint64'], [other, 0, 0]).slice(2);
  16. const constructorByteCode = `${VestingWallet.bytecode}${encodedParams}`;
  17. beforeEach(async function () {
  18. this.factory = await Create2.new();
  19. });
  20. describe('computeAddress', function () {
  21. it('computes the correct contract address', async function () {
  22. const onChainComputed = await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  23. const offChainComputed = ethers.getCreate2Address(
  24. this.factory.address,
  25. saltHex,
  26. ethers.keccak256(constructorByteCode),
  27. );
  28. expect(onChainComputed).to.equal(offChainComputed);
  29. });
  30. it('computes the correct contract address with deployer', async function () {
  31. const onChainComputed = await this.factory.$computeAddress(
  32. saltHex,
  33. web3.utils.keccak256(constructorByteCode),
  34. deployerAccount,
  35. );
  36. const offChainComputed = ethers.getCreate2Address(
  37. deployerAccount,
  38. saltHex,
  39. ethers.keccak256(constructorByteCode),
  40. );
  41. expect(onChainComputed).to.equal(offChainComputed);
  42. });
  43. });
  44. describe('deploy', function () {
  45. it('deploys a contract without constructor', async function () {
  46. const offChainComputed = ethers.getCreate2Address(
  47. this.factory.address,
  48. saltHex,
  49. ethers.keccak256(ConstructorLessContract.bytecode),
  50. );
  51. expectEvent(await this.factory.$deploy(0, saltHex, ConstructorLessContract.bytecode), 'return$deploy', {
  52. addr: offChainComputed,
  53. });
  54. expect(ConstructorLessContract.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  55. });
  56. it('deploys a contract with constructor arguments', async function () {
  57. const offChainComputed = ethers.getCreate2Address(
  58. this.factory.address,
  59. saltHex,
  60. ethers.keccak256(constructorByteCode),
  61. );
  62. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy', {
  63. addr: offChainComputed,
  64. });
  65. const instance = await VestingWallet.at(offChainComputed);
  66. expect(await instance.owner()).to.be.equal(other);
  67. });
  68. it('deploys a contract with funds deposited in the factory', async function () {
  69. const deposit = ether('2');
  70. await send.ether(deployerAccount, this.factory.address, deposit);
  71. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  72. const offChainComputed = ethers.getCreate2Address(
  73. this.factory.address,
  74. saltHex,
  75. ethers.keccak256(constructorByteCode),
  76. );
  77. expectEvent(await this.factory.$deploy(deposit, saltHex, constructorByteCode), 'return$deploy', {
  78. addr: offChainComputed,
  79. });
  80. expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
  81. });
  82. it('fails deploying a contract in an existent address', async function () {
  83. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy');
  84. // TODO: Make sure it actually throws "Create2FailedDeployment".
  85. // For some unknown reason, the revert reason sometimes return:
  86. // `revert with unrecognized return data or custom error`
  87. await expectRevert.unspecified(this.factory.$deploy(0, saltHex, constructorByteCode));
  88. });
  89. it('fails deploying a contract if the bytecode length is zero', async function () {
  90. await expectRevertCustomError(this.factory.$deploy(0, saltHex, '0x'), 'Create2EmptyBytecode', []);
  91. });
  92. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  93. await expectRevertCustomError(
  94. this.factory.$deploy(1, saltHex, constructorByteCode),
  95. 'Create2InsufficientBalance',
  96. [0, 1],
  97. );
  98. });
  99. });
  100. });