Clones.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const shouldBehaveLikeClone = require('./Clones.behaviour');
  3. const ClonesMock = artifacts.require('ClonesMock');
  4. contract('Clones', function (accounts) {
  5. describe('clone', function () {
  6. shouldBehaveLikeClone(async (implementation, initData, opts = {}) => {
  7. const factory = await ClonesMock.new();
  8. const receipt = await factory.clone(implementation, initData, { value: opts.value });
  9. const address = receipt.logs.find(({ event }) => event === 'NewInstance').args.instance;
  10. return { address };
  11. });
  12. });
  13. describe('cloneDeterministic', function () {
  14. shouldBehaveLikeClone(async (implementation, initData, opts = {}) => {
  15. const salt = web3.utils.randomHex(32);
  16. const factory = await ClonesMock.new();
  17. const receipt = await factory.cloneDeterministic(implementation, salt, initData, { value: opts.value });
  18. const address = receipt.logs.find(({ event }) => event === 'NewInstance').args.instance;
  19. return { address };
  20. });
  21. it('address already used', async function () {
  22. const implementation = web3.utils.randomHex(20);
  23. const salt = web3.utils.randomHex(32);
  24. const factory = await ClonesMock.new();
  25. // deploy once
  26. expectEvent(
  27. await factory.cloneDeterministic(implementation, salt, '0x'),
  28. 'NewInstance',
  29. );
  30. // deploy twice
  31. await expectRevert(
  32. factory.cloneDeterministic(implementation, salt, '0x'),
  33. 'ERC1167: create2 failed',
  34. );
  35. });
  36. it('address prediction', async function () {
  37. const implementation = web3.utils.randomHex(20);
  38. const salt = web3.utils.randomHex(32);
  39. const factory = await ClonesMock.new();
  40. const predicted = await factory.predictDeterministicAddress(implementation, salt);
  41. expectEvent(
  42. await factory.cloneDeterministic(implementation, salt, '0x'),
  43. 'NewInstance',
  44. { instance: predicted },
  45. );
  46. });
  47. });
  48. });