Create2.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const { contract, accounts, web3 } = require('@openzeppelin/test-environment');
  2. const { balance, BN, ether, expectRevert, send } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const Create2Impl = contract.fromArtifact('Create2Impl');
  5. const ERC20Mock = contract.fromArtifact('ERC20Mock');
  6. const ERC20 = contract.fromArtifact('ERC20');
  7. describe('Create2', function () {
  8. const [deployerAccount] = accounts;
  9. const salt = 'salt message';
  10. const saltHex = web3.utils.soliditySha3(salt);
  11. const constructorByteCode = `${ERC20Mock.bytecode}${web3.eth.abi
  12. .encodeParameters(['address', 'uint256'], [deployerAccount, 100]).slice(2)
  13. }`;
  14. beforeEach(async function () {
  15. this.factory = await Create2Impl.new();
  16. });
  17. it('should compute the correct contract address', async function () {
  18. const onChainComputed = await this.factory
  19. .computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  20. const offChainComputed =
  21. computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  22. expect(onChainComputed).to.equal(offChainComputed);
  23. });
  24. it('should compute the correct contract address with deployer', async function () {
  25. const onChainComputed = await this.factory
  26. .computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount);
  27. const offChainComputed =
  28. computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  29. expect(onChainComputed).to.equal(offChainComputed);
  30. });
  31. it('should deploy a ERC20 from inline assembly code', async function () {
  32. const offChainComputed =
  33. computeCreate2Address(saltHex, ERC20.bytecode, this.factory.address);
  34. await this.factory
  35. .deploy(0, saltHex, ERC20.bytecode, { from: deployerAccount });
  36. expect(ERC20.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  37. });
  38. it('should deploy a ERC20Mock with correct balances', async function () {
  39. const offChainComputed =
  40. computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  41. await this.factory
  42. .deploy(0, saltHex, constructorByteCode, { from: deployerAccount });
  43. const erc20 = await ERC20Mock.at(offChainComputed);
  44. expect(await erc20.balanceOf(deployerAccount)).to.be.bignumber.equal(new BN(100));
  45. });
  46. it('should deploy a contract with funds deposited in the factory', async function () {
  47. const deposit = ether('2');
  48. await send.ether(deployerAccount, this.factory.address, deposit);
  49. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  50. const onChainComputed = await this.factory
  51. .computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), this.factory.address);
  52. await this.factory.deploy(deposit, saltHex, constructorByteCode);
  53. expect(await balance.current(onChainComputed)).to.be.bignumber.equal(deposit);
  54. });
  55. it('should failed deploying a contract in an existent address', async function () {
  56. await this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount });
  57. await expectRevert(
  58. this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount }), 'Create2: Failed on deploy'
  59. );
  60. });
  61. it('should fail deploying a contract if the bytecode length is zero', async function () {
  62. await expectRevert(
  63. this.factory.deploy(0, saltHex, '0x', { from: deployerAccount }), 'Create2: bytecode length is zero'
  64. );
  65. });
  66. it('should fail deploying a contract if factory contract does not have sufficient balance', async function () {
  67. await expectRevert(
  68. this.factory.deploy(1, saltHex, constructorByteCode, { from: deployerAccount }),
  69. 'Create2: insufficient balance'
  70. );
  71. });
  72. });
  73. function computeCreate2Address (saltHex, bytecode, deployer) {
  74. return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
  75. 'ff',
  76. deployer,
  77. saltHex,
  78. web3.utils.soliditySha3(bytecode),
  79. ].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
  80. }