Create2.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { contract, accounts, web3 } = require('@openzeppelin/test-environment');
  2. const { BN, expectRevert } = 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, 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. .methods['computeAddress(bytes32,bytes,address)'](saltHex, constructorByteCode, deployerAccount);
  27. const offChainComputed =
  28. computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  29. expect(onChainComputed).to.equal(offChainComputed);
  30. });
  31. it('should compute the correct contract address with deployer and bytecode hash', async function () {
  32. const onChainComputed = await this.factory
  33. .methods['computeAddress(bytes32,bytes32,address)'](
  34. saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount
  35. );
  36. const offChainComputed =
  37. computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  38. expect(onChainComputed).to.equal(offChainComputed);
  39. });
  40. it('should deploy a ERC20 from inline assembly code', async function () {
  41. const offChainComputed =
  42. computeCreate2Address(saltHex, ERC20.bytecode, this.factory.address);
  43. await this.factory
  44. .deploy(saltHex, ERC20.bytecode, { from: deployerAccount });
  45. expect(ERC20.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  46. });
  47. it('should deploy a ERC20Mock with correct balances', async function () {
  48. const offChainComputed =
  49. computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  50. await this.factory
  51. .deploy(saltHex, constructorByteCode, { from: deployerAccount });
  52. const erc20 = await ERC20Mock.at(offChainComputed);
  53. expect(await erc20.balanceOf(deployerAccount)).to.be.bignumber.equal(new BN(100));
  54. });
  55. it('should failed deploying a contract in an existent address', async function () {
  56. await this.factory.deploy(saltHex, constructorByteCode, { from: deployerAccount });
  57. await expectRevert(
  58. this.factory.deploy(saltHex, constructorByteCode, { from: deployerAccount }), 'Create2: Failed on deploy'
  59. );
  60. });
  61. });
  62. function computeCreate2Address (saltHex, bytecode, deployer) {
  63. return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
  64. 'ff',
  65. deployer,
  66. saltHex,
  67. web3.utils.soliditySha3(bytecode),
  68. ].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
  69. }