Create2.test.js 4.0 KB

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