Create2.test.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(['address', 'uint64', 'uint64'], [other, 0, 0]).slice(2);
  12. const constructorByteCode = `${VestingWallet.bytecode}${encodedParams}`;
  13. beforeEach(async function () {
  14. this.factory = await Create2.new();
  15. });
  16. describe('computeAddress', function () {
  17. it('computes the correct contract address', async function () {
  18. const onChainComputed = await this.factory.$computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  19. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  20. expect(onChainComputed).to.equal(offChainComputed);
  21. });
  22. it('computes the correct contract address with deployer', async function () {
  23. const onChainComputed = await this.factory.$computeAddress(
  24. saltHex,
  25. web3.utils.keccak256(constructorByteCode),
  26. deployerAccount,
  27. );
  28. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  29. expect(onChainComputed).to.equal(offChainComputed);
  30. });
  31. });
  32. describe('deploy', function () {
  33. it('deploys a ERC1820Implementer from inline assembly code', async function () {
  34. const offChainComputed = computeCreate2Address(saltHex, ERC1820Implementer.bytecode, this.factory.address);
  35. expectEvent(await this.factory.$deploy(0, saltHex, ERC1820Implementer.bytecode), 'return$deploy', {
  36. addr: offChainComputed,
  37. });
  38. expect(ERC1820Implementer.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  39. });
  40. it('deploys a contract with constructor arguments', async function () {
  41. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  42. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy', {
  43. addr: offChainComputed,
  44. });
  45. expect(await VestingWallet.at(offChainComputed).then(instance => instance.beneficiary())).to.be.equal(other);
  46. });
  47. it('deploys a contract with funds deposited in the factory', async function () {
  48. const deposit = ether('2');
  49. await send.ether(deployerAccount, this.factory.address, deposit);
  50. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  51. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  52. expectEvent(await this.factory.$deploy(deposit, saltHex, constructorByteCode), 'return$deploy', {
  53. addr: offChainComputed,
  54. });
  55. expect(await balance.current(offChainComputed)).to.be.bignumber.equal(deposit);
  56. });
  57. it('fails deploying a contract in an existent address', async function () {
  58. expectEvent(await this.factory.$deploy(0, saltHex, constructorByteCode), 'return$deploy');
  59. await expectRevert(this.factory.$deploy(0, saltHex, constructorByteCode), 'Create2: Failed on deploy');
  60. });
  61. it('fails deploying a contract if the bytecode length is zero', async function () {
  62. await expectRevert(this.factory.$deploy(0, saltHex, '0x'), 'Create2: bytecode length is zero');
  63. });
  64. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  65. await expectRevert(this.factory.$deploy(1, saltHex, constructorByteCode), 'Create2: insufficient balance');
  66. });
  67. });
  68. });