Create2.test.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const { balance, BN, ether, expectRevert, send } = require('@openzeppelin/test-helpers');
  2. const { computeCreate2Address } = require('../helpers/create2');
  3. const { expect } = require('chai');
  4. const Create2Impl = artifacts.require('Create2Impl');
  5. const ERC20Mock = artifacts.require('ERC20Mock');
  6. const ERC1820Implementer = artifacts.require('ERC1820Implementer');
  7. contract('Create2', function (accounts) {
  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. describe('computeAddress', function () {
  20. it('computes the correct contract address', async function () {
  21. const onChainComputed = await this.factory
  22. .computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
  23. const offChainComputed =
  24. computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  25. expect(onChainComputed).to.equal(offChainComputed);
  26. });
  27. it('computes the correct contract address with deployer', async function () {
  28. const onChainComputed = await this.factory
  29. .computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount);
  30. const offChainComputed =
  31. computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
  32. expect(onChainComputed).to.equal(offChainComputed);
  33. });
  34. });
  35. describe('deploy', function () {
  36. it('deploys a ERC1820Implementer from inline assembly code', async function () {
  37. const offChainComputed =
  38. computeCreate2Address(saltHex, ERC1820Implementer.bytecode, this.factory.address);
  39. await this.factory.deployERC1820Implementer(0, saltHex);
  40. expect(ERC1820Implementer.bytecode).to.include((await web3.eth.getCode(offChainComputed)).slice(2));
  41. });
  42. it('deploys a ERC20Mock with correct balances', async function () {
  43. const offChainComputed = computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
  44. await this.factory.deploy(0, saltHex, constructorByteCode);
  45. const erc20 = await ERC20Mock.at(offChainComputed);
  46. expect(await erc20.balanceOf(deployerAccount)).to.be.bignumber.equal(new BN(100));
  47. });
  48. it('deploys a contract with funds deposited in the factory', async function () {
  49. const deposit = ether('2');
  50. await send.ether(deployerAccount, this.factory.address, deposit);
  51. expect(await balance.current(this.factory.address)).to.be.bignumber.equal(deposit);
  52. const onChainComputed = await this.factory
  53. .computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), this.factory.address);
  54. await this.factory.deploy(deposit, saltHex, constructorByteCode);
  55. expect(await balance.current(onChainComputed)).to.be.bignumber.equal(deposit);
  56. });
  57. it('fails deploying a contract in an existent address', async function () {
  58. await this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount });
  59. await expectRevert(
  60. this.factory.deploy(0, saltHex, constructorByteCode, { from: deployerAccount }), 'Create2: Failed on deploy',
  61. );
  62. });
  63. it('fails deploying a contract if the bytecode length is zero', async function () {
  64. await expectRevert(
  65. this.factory.deploy(0, saltHex, '0x', { from: deployerAccount }), 'Create2: bytecode length is zero',
  66. );
  67. });
  68. it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
  69. await expectRevert(
  70. this.factory.deploy(1, saltHex, constructorByteCode, { from: deployerAccount }),
  71. 'Create2: insufficient balance',
  72. );
  73. });
  74. });
  75. });