Context.behavior.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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())
  14. .to.emit(this.context, 'Sender')
  15. .withArgs(this.sender.address);
  16. });
  17. it('returns the transaction sender when called from another contract', async function () {
  18. await expect(this.contextHelper.connect(this.sender).callSender(this.context))
  19. .to.emit(this.context, 'Sender')
  20. .withArgs(this.contextHelper.target);
  21. });
  22. });
  23. describe('msgData', function () {
  24. const args = [42n, 'OpenZeppelin'];
  25. it('returns the transaction data when called from an EOA', async function () {
  26. const callData = this.context.interface.encodeFunctionData('msgData', args);
  27. await expect(this.context.msgData(...args))
  28. .to.emit(this.context, 'Data')
  29. .withArgs(callData, ...args);
  30. });
  31. it('returns the transaction sender when from another contract', async function () {
  32. const callData = this.context.interface.encodeFunctionData('msgData', args);
  33. await expect(this.contextHelper.callData(this.context, ...args))
  34. .to.emit(this.context, 'Data')
  35. .withArgs(callData, ...args);
  36. });
  37. });
  38. }
  39. module.exports = {
  40. shouldBehaveLikeRegularContext,
  41. };