EIP7702Utils.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { ethers, config } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. // [NOTE]
  5. //
  6. // ethers.getSigners() returns object than cannot currently send type-4 transaction, or sign authorization. Therefore,
  7. // we have to instantiate the eoa AND the relayer manually using ethers 6.14.0 wallets. This can be improved when
  8. // @nomicfoundation/hardhat-ethers starts instantiating signers with 7702 support.
  9. const relayAuthorization = authorization =>
  10. ethers.Wallet.fromPhrase(config.networks.hardhat.accounts.mnemonic, ethers.provider).sendTransaction({
  11. to: ethers.ZeroAddress,
  12. authorizationList: [authorization],
  13. gasLimit: 46_000n,
  14. });
  15. const fixture = async () => {
  16. const eoa = ethers.Wallet.createRandom(ethers.provider);
  17. const mock = await ethers.deployContract('$EIP7702Utils');
  18. return { eoa, mock };
  19. };
  20. describe('EIP7702Utils', function () {
  21. beforeEach(async function () {
  22. Object.assign(this, await loadFixture(fixture));
  23. });
  24. describe('fetchDelegate', function () {
  25. it('EOA without delegation', async function () {
  26. await expect(this.mock.$fetchDelegate(this.eoa)).to.eventually.equal(ethers.ZeroAddress);
  27. });
  28. it('EOA with delegation', async function () {
  29. // set delegation
  30. await this.eoa.authorize({ address: this.mock }).then(relayAuthorization);
  31. await expect(this.mock.$fetchDelegate(this.eoa)).to.eventually.equal(this.mock);
  32. });
  33. it('EOA with revoked delegation', async function () {
  34. // set delegation
  35. await this.eoa.authorize({ address: this.mock }).then(relayAuthorization);
  36. // reset delegation
  37. await this.eoa.authorize({ address: ethers.ZeroAddress }).then(relayAuthorization);
  38. await expect(this.mock.$fetchDelegate(this.eoa)).to.eventually.equal(ethers.ZeroAddress);
  39. });
  40. it('other smart contract', async function () {
  41. await expect(this.mock.$fetchDelegate(this.mock)).to.eventually.equal(ethers.ZeroAddress);
  42. });
  43. });
  44. });