Create2.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { balance, ether, expectEvent, expectRevert, send } = require('@openzeppelin/test-helpers');
  2. const { computeCreate2Address } = require('../helpers/create2');
  3. const { expect } = require('chai');
  4. const Create2 = artifacts.require('$Create2');
  5. const VestingWallet = artifacts.require('VestingWallet');
  6. const ERC1820Implementer = artifacts.require('$ERC1820Implementer');
  7. contract('Create2', function (accounts) {
  8. const [ deployerAccount, other ] = accounts;
  9. const salt = 'salt message';
  10. const saltHex = web3.utils.soliditySha3(salt);
  11. const encodedParams = web3.eth.abi.encodeParameters(
  12. [ 'address', 'uint64', 'uint64' ],
  13. [ other, 0, 0 ],
  14. ).slice(2);
  15. const constructorByteCode = `${VestingWallet.bytecode}${encodedParams}`;
  16. beforeEach(async function () {
  17. this.factory = await Create2.new();
  18. });
  19. describe('computeAddress', function () {
  20. it('computes the correct contract address', async function () {
  21. const onChainComputed =
  22. await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  23. const offChainComputed =
  24. computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  25. expect(onChainComputed).to.equal(offChainComputed);
  26. });
  27. it('computes the correct contract address with deployer', async function () {
  28. const onChainComputed =
  29. await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount);
  30. const offChainComputed =
  31. computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  32. expect(onChainComputed).to.equal(offChainComputed);
  33. });
  34. });
  35. describe('deploy', function () {
  36. it('deploys a ERC1820Implementer from inline assembly code', async function () {
  37. const offChainComputed = computeCreate2Address(saltHex, ERC1820Implementer.bytecode, this.factory.address);
  38. expectEvent(
  39. await this.factory.$deploy(0, saltHex, ERC1820Implementer.bytecode),
  40. 'return$deploy',
  41. { addr: offChainComputed },
  42. );
  43. expect(ERC1820Implementer.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  44. });
  45. it('deploys a contract with constructor arguments', async function () {
  46. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  47. expectEvent(
  48. await this.factory.$deploy(0, saltHex, constructorByteCode),
  49. 'return$deploy',
  50. { addr: offChainComputed },
  51. );
  52. expect(await VestingWallet.at(offChainComputed).then(instance => instance.beneficiary())).to.be.equal(other);
  53. });
  54. it('deploys a contract with funds deposited in the factory', async function () {
  55. const deposit = ether('2');
  56. await send.ether(deployerAccount, this.factory.address, deposit);
  57. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  58. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  59. expectEvent(
  60. await this.factory.$deploy(deposit, saltHex, constructorByteCode),
  61. 'return$deploy',
  62. { addr: offChainComputed },
  63. );
  64. expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
  65. });
  66. it('fails deploying a contract in an existent address', async function () {
  67. expectEvent(
  68. await this.factory.$deploy(0, saltHex, constructorByteCode),
  69. 'return$deploy',
  70. );
  71. await expectRevert(
  72. this.factory.$deploy(0, saltHex, constructorByteCode),
  73. 'Create2: Failed on deploy',
  74. );
  75. });
  76. it('fails deploying a contract if the bytecode length is zero', async function () {
  77. await expectRevert(
  78. this.factory.$deploy(0, saltHex, '0x'),
  79. 'Create2: bytecode length is zero',
  80. );
  81. });
  82. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  83. await expectRevert(
  84. this.factory.$deploy(1, saltHex, constructorByteCode),
  85. 'Create2: insufficient balance',
  86. );
  87. });
  88. });
  89. });