Create2.test.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // This should be a contract that:
  7. // - has no constructor arguments
  8. // - has no immutable variable populated during construction
  9. const ConstructorLessContract = Create2;
  10. contract('Create2', function (accounts) {
  11. const [deployerAccount, other] = accounts;
  12. const salt = 'salt message';
  13. const saltHex = web3.utils.soliditySha3(salt);
  14. const encodedParams = web3.eth.abi.encodeParameters(['address', 'uint64', 'uint64'], [other, 0, 0]).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 = await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  22. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  23. expect(onChainComputed).to.equal(offChainComputed);
  24. });
  25. it('computes the correct contract address with deployer', async function () {
  26. const onChainComputed = await this.factory.$computeAddress(
  27. saltHex,
  28. web3.utils.keccak256(constructorByteCode),
  29. deployerAccount,
  30. );
  31. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  32. expect(onChainComputed).to.equal(offChainComputed);
  33. });
  34. });
  35. describe('deploy', function () {
  36. it('deploys a contract without constructor', async function () {
  37. const offChainComputed = computeCreate2Address(saltHex, ConstructorLessContract.bytecode, this.factory.address);
  38. expectEvent(await this.factory.$deploy(0, saltHex, ConstructorLessContract.bytecode), 'return$deploy', {
  39. addr: offChainComputed,
  40. });
  41. expect(ConstructorLessContract.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  42. });
  43. it('deploys a contract with constructor arguments', async function () {
  44. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  45. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy', {
  46. addr: offChainComputed,
  47. });
  48. expect(await VestingWallet.at(offChainComputed).then(instance => instance.beneficiary())).to.be.equal(other);
  49. });
  50. it('deploys a contract with funds deposited in the factory', async function () {
  51. const deposit = ether('2');
  52. await send.ether(deployerAccount, this.factory.address, deposit);
  53. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  54. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  55. expectEvent(await this.factory.$deploy(deposit, saltHex, constructorByteCode), 'return$deploy', {
  56. addr: offChainComputed,
  57. });
  58. expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
  59. });
  60. it('fails deploying a contract in an existent address', async function () {
  61. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy');
  62. await expectRevert(this.factory.$deploy(0, saltHex, constructorByteCode), 'Create2: Failed on deploy');
  63. });
  64. it('fails deploying a contract if the bytecode length is zero', async function () {
  65. await expectRevert(this.factory.$deploy(0, saltHex, '0x'), 'Create2: bytecode length is zero');
  66. });
  67. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  68. await expectRevert(this.factory.$deploy(1, saltHex, constructorByteCode), 'Create2: insufficient balance');
  69. });
  70. });
  71. });