Account.behavior.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { ethers, entrypoint } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { impersonate } = require('../helpers/account');
  4. const { SIG_VALIDATION_SUCCESS, SIG_VALIDATION_FAILURE } = require('../helpers/erc4337');
  5. const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
  6. function shouldBehaveLikeAccountCore() {
  7. describe('entryPoint', function () {
  8. it('should return the canonical entrypoint', async function () {
  9. await this.mock.deploy();
  10. await expect(this.mock.entryPoint()).to.eventually.equal(entrypoint.v08);
  11. });
  12. });
  13. describe('validateUserOp', function () {
  14. beforeEach(async function () {
  15. await this.other.sendTransaction({ to: this.mock.target, value: ethers.parseEther('1') });
  16. await this.mock.deploy();
  17. this.userOp ??= {};
  18. });
  19. it('should revert if the caller is not the canonical entrypoint', async function () {
  20. // empty operation (does nothing)
  21. const operation = await this.mock.createUserOp(this.userOp).then(op => this.signUserOp(op));
  22. await expect(this.mock.connect(this.other).validateUserOp(operation.packed, operation.hash(), 0))
  23. .to.be.revertedWithCustomError(this.mock, 'AccountUnauthorized')
  24. .withArgs(this.other);
  25. });
  26. describe('when the caller is the canonical entrypoint', function () {
  27. beforeEach(async function () {
  28. this.mockFromEntrypoint = this.mock.connect(await impersonate(entrypoint.v08.target));
  29. });
  30. it('should return SIG_VALIDATION_SUCCESS if the signature is valid', async function () {
  31. // empty operation (does nothing)
  32. const operation = await this.mock.createUserOp(this.userOp).then(op => this.signUserOp(op));
  33. expect(await this.mockFromEntrypoint.validateUserOp.staticCall(operation.packed, operation.hash(), 0)).to.eq(
  34. SIG_VALIDATION_SUCCESS,
  35. );
  36. });
  37. it('should return SIG_VALIDATION_FAILURE if the signature is invalid', async function () {
  38. // empty operation (does nothing)
  39. const operation = await this.mock.createUserOp(this.userOp);
  40. operation.signature = (await this.invalidSig?.()) ?? '0x00';
  41. expect(await this.mockFromEntrypoint.validateUserOp.staticCall(operation.packed, operation.hash(), 0)).to.eq(
  42. SIG_VALIDATION_FAILURE,
  43. );
  44. });
  45. it('should pay missing account funds for execution', async function () {
  46. // empty operation (does nothing)
  47. const operation = await this.mock.createUserOp(this.userOp).then(op => this.signUserOp(op));
  48. const value = 42n;
  49. await expect(
  50. this.mockFromEntrypoint.validateUserOp(operation.packed, operation.hash(), value),
  51. ).to.changeEtherBalances([this.mock, entrypoint.v08], [-value, value]);
  52. });
  53. });
  54. });
  55. describe('fallback', function () {
  56. it('should receive ether', async function () {
  57. await this.mock.deploy();
  58. const value = 42n;
  59. await expect(this.other.sendTransaction({ to: this.mock, value })).to.changeEtherBalances(
  60. [this.other, this.mock],
  61. [-value, value],
  62. );
  63. });
  64. });
  65. }
  66. function shouldBehaveLikeAccountHolder() {
  67. describe('onReceived', function () {
  68. beforeEach(async function () {
  69. await this.mock.deploy();
  70. });
  71. shouldSupportInterfaces(['ERC1155Receiver']);
  72. describe('onERC1155Received', function () {
  73. const ids = [1n, 2n, 3n];
  74. const values = [1000n, 2000n, 3000n];
  75. const data = '0x12345678';
  76. beforeEach(async function () {
  77. this.token = await ethers.deployContract('$ERC1155', ['https://somedomain.com/{id}.json']);
  78. await this.token.$_mintBatch(this.other, ids, values, '0x');
  79. });
  80. it('receives ERC1155 tokens from a single ID', async function () {
  81. await this.token.connect(this.other).safeTransferFrom(this.other, this.mock, ids[0], values[0], data);
  82. await expect(
  83. this.token.balanceOfBatch(
  84. ids.map(() => this.mock),
  85. ids,
  86. ),
  87. ).to.eventually.deep.equal(values.map((v, i) => (i == 0 ? v : 0n)));
  88. });
  89. it('receives ERC1155 tokens from a multiple IDs', async function () {
  90. await expect(
  91. this.token.balanceOfBatch(
  92. ids.map(() => this.mock),
  93. ids,
  94. ),
  95. ).to.eventually.deep.equal(ids.map(() => 0n));
  96. await this.token.connect(this.other).safeBatchTransferFrom(this.other, this.mock, ids, values, data);
  97. await expect(
  98. this.token.balanceOfBatch(
  99. ids.map(() => this.mock),
  100. ids,
  101. ),
  102. ).to.eventually.deep.equal(values);
  103. });
  104. });
  105. describe('onERC721Received', function () {
  106. const tokenId = 1n;
  107. beforeEach(async function () {
  108. this.token = await ethers.deployContract('$ERC721', ['Some NFT', 'SNFT']);
  109. await this.token.$_mint(this.other, tokenId);
  110. });
  111. it('receives an ERC721 token', async function () {
  112. await this.token.connect(this.other).safeTransferFrom(this.other, this.mock, tokenId);
  113. await expect(this.token.ownerOf(tokenId)).to.eventually.equal(this.mock);
  114. });
  115. });
  116. });
  117. }
  118. module.exports = { shouldBehaveLikeAccountCore, shouldBehaveLikeAccountHolder };