Create2.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const { balance, ether, expectEvent, expectRevert, send } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { computeCreate2Address } = require('../helpers/create');
  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 = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  24. expect(onChainComputed).to.equal(offChainComputed);
  25. });
  26. it('computes the correct contract address with deployer', async function () {
  27. const onChainComputed = await this.factory.$computeAddress(
  28. saltHex,
  29. web3.utils.keccak256(constructorByteCode),
  30. deployerAccount,
  31. );
  32. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  33. expect(onChainComputed).to.equal(offChainComputed);
  34. });
  35. });
  36. describe('deploy', function () {
  37. it('deploys a contract without constructor', async function () {
  38. const offChainComputed = computeCreate2Address(saltHex, ConstructorLessContract.bytecode, this.factory.address);
  39. expectEvent(await this.factory.$deploy(0, saltHex, ConstructorLessContract.bytecode), 'return$deploy', {
  40. addr: offChainComputed,
  41. });
  42. expect(ConstructorLessContract.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  43. });
  44. it('deploys a contract with constructor arguments', async function () {
  45. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  46. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy', {
  47. addr: offChainComputed,
  48. });
  49. const instance = await VestingWallet.at(offChainComputed);
  50. expect(await instance.owner()).to.be.equal(other);
  51. });
  52. it('deploys a contract with funds deposited in the factory', async function () {
  53. const deposit = ether('2');
  54. await send.ether(deployerAccount, this.factory.address, deposit);
  55. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  56. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  57. expectEvent(await this.factory.$deploy(deposit, saltHex, constructorByteCode), 'return$deploy', {
  58. addr: offChainComputed,
  59. });
  60. expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
  61. });
  62. it('fails deploying a contract in an existent address', async function () {
  63. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy');
  64. // TODO: Make sure it actually throws "Create2FailedDeployment".
  65. // For some unknown reason, the revert reason sometimes return:
  66. // `revert with unrecognized return data or custom error`
  67. await expectRevert.unspecified(this.factory.$deploy(0, saltHex, constructorByteCode));
  68. });
  69. it('fails deploying a contract if the bytecode length is zero', async function () {
  70. await expectRevertCustomError(this.factory.$deploy(0, saltHex, '0x'), 'Create2EmptyBytecode', []);
  71. });
  72. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  73. await expectRevertCustomError(
  74. this.factory.$deploy(1, saltHex, constructorByteCode),
  75. 'Create2InsufficientBalance',
  76. [0, 1],
  77. );
  78. });
  79. });
  80. });