Context.behavior.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. return { contextHelper: await ethers.deployContract('ContextMockCaller', []) };
  6. }
  7. function shouldBehaveLikeRegularContext() {
  8. beforeEach(async function () {
  9. Object.assign(this, await loadFixture(fixture));
  10. });
  11. describe('msgSender', function () {
  12. it('returns the transaction sender when called from an EOA', async function () {
  13. await expect(this.context.connect(this.sender).msgSender()).to.emit(this.context, 'Sender').withArgs(this.sender);
  14. });
  15. it('returns the transaction sender when called from another contract', async function () {
  16. await expect(this.contextHelper.connect(this.sender).callSender(this.context))
  17. .to.emit(this.context, 'Sender')
  18. .withArgs(this.contextHelper);
  19. });
  20. });
  21. describe('msgData', function () {
  22. const args = [42n, 'OpenZeppelin'];
  23. it('returns the transaction data when called from an EOA', async function () {
  24. const callData = this.context.interface.encodeFunctionData('msgData', args);
  25. await expect(this.context.msgData(...args))
  26. .to.emit(this.context, 'Data')
  27. .withArgs(callData, ...args);
  28. });
  29. it('returns the transaction sender when from another contract', async function () {
  30. const callData = this.context.interface.encodeFunctionData('msgData', args);
  31. await expect(this.contextHelper.callData(this.context, ...args))
  32. .to.emit(this.context, 'Data')
  33. .withArgs(callData, ...args);
  34. });
  35. });
  36. }
  37. module.exports = {
  38. shouldBehaveLikeRegularContext,
  39. };