Create2.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const { balance, BN, ether, expectRevert, send } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Create2Impl = artifacts.require('Create2Impl');
  4. const ERC20Mock = artifacts.require('ERC20Mock');
  5. const ERC1820Implementer = artifacts.require('ERC1820Implementer');
  6. contract('Create2', function (accounts) {
  7. const [deployerAccount] = accounts;
  8. const salt = 'salt message';
  9. const saltHex = web3.utils.soliditySha3(salt);
  10. const encodedParams = web3.eth.abi.encodeParameters(
  11. ['string', 'string', 'address', 'uint256'],
  12. ['MyToken', 'MTKN', deployerAccount, 100],
  13. ).slice(2);
  14. const constructorByteCode = `${ERC20Mock.bytecode}${encodedParams}`;
  15. beforeEach(async function () {
  16. this.factory = await Create2Impl.new();
  17. });
  18. describe('computeAddress', function () {
  19. it('computes 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('computes 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. });
  34. describe('deploy', function () {
  35. it('deploys a ERC1820Implementer from inline assembly code', async function () {
  36. const offChainComputed =
  37. computeCreate2Address(saltHex, ERC1820Implementer.bytecode, this.factory.address);
  38. await this.factory.deployERC1820Implementer(0, saltHex);
  39. expect(ERC1820Implementer.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  40. });
  41. it('deploys a ERC20Mock with correct balances', async function () {
  42. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  43. await this.factory.deploy(0, saltHex, constructorByteCode);
  44. const erc20 = await ERC20Mock.at(offChainComputed);
  45. expect(await erc20.balanceOf(deployerAccount)).to.be.bignumber.equal(new BN(100));
  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 onChainComputed = await this.factory
  52. .computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), this.factory.address);
  53. await this.factory.deploy(deposit, saltHex, constructorByteCode);
  54. expect(await balance.current(onChainComputed)).to.be.bignumber.equal(deposit);
  55. });
  56. it('fails deploying a contract in an existent address', async function () {
  57. await this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount });
  58. await expectRevert(
  59. this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount }), 'Create2: Failed on deploy',
  60. );
  61. });
  62. it('fails deploying a contract if the bytecode length is zero', async function () {
  63. await expectRevert(
  64. this.factory.deploy(0, saltHex, '0x', { from: deployerAccount }), 'Create2: bytecode length is zero',
  65. );
  66. });
  67. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  68. await expectRevert(
  69. this.factory.deploy(1, saltHex, constructorByteCode, { from: deployerAccount }),
  70. 'Create2: insufficient balance',
  71. );
  72. });
  73. });
  74. });
  75. function computeCreate2Address (saltHex, bytecode, deployer) {
  76. return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
  77. 'ff',
  78. deployer,
  79. saltHex,
  80. web3.utils.soliditySha3(bytecode),
  81. ].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
  82. }