Create2.test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, 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(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(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 failed deploying a contract in an existent address', async function () {
  47. await this.factory.deploy(saltHex, constructorByteCode, { from: deployerAccount });
  48. await expectRevert(
  49. this.factory.deploy(saltHex, constructorByteCode, { from: deployerAccount }), 'Create2: Failed on deploy'
  50. );
  51. });
  52. });
  53. function computeCreate2Address (saltHex, bytecode, deployer) {
  54. return web3.utils.toChecksumAddress(`0x${web3.utils.sha3(`0x${[
  55. 'ff',
  56. deployer,
  57. saltHex,
  58. web3.utils.soliditySha3(bytecode),
  59. ].map(x => x.replace(/0x/, '')).join('')}`).slice(-40)}`);
  60. }