Skip to content

Commit bf3593e

Browse files
committed
use @OpenZeppelin/contracts/hardhat/common-contracts
1 parent 602c301 commit bf3593e

File tree

8 files changed

+37
-45
lines changed

8 files changed

+37
-45
lines changed

hardhat.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require('hardhat-exposed');
1717
require('solidity-coverage');
1818
require('solidity-docgen');
1919
require('./hardhat/remappings');
20+
require('@openzeppelin/contracts/hardhat/common-contracts.js');
2021

2122
module.exports = {
2223
solidity: {

test/account/Account.behavior.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint } = require('hardhat');
22
const { expect } = require('chai');
33
const { setBalance } = require('@nomicfoundation/hardhat-network-helpers');
44

@@ -12,7 +12,7 @@ function shouldBehaveLikeAnAccountBase() {
1212
describe('entryPoint', function () {
1313
it('should return the canonical entrypoint', async function () {
1414
await this.mock.deploy();
15-
expect(await this.mock.entryPoint()).to.equal(this.entrypoint.target);
15+
expect(await this.mock.entryPoint()).to.equal(entrypoint);
1616
});
1717
});
1818

@@ -43,7 +43,7 @@ function shouldBehaveLikeAnAccountBase() {
4343

4444
describe('when the caller is the canonical entrypoint', function () {
4545
beforeEach(async function () {
46-
this.entrypointAsSigner = await impersonate(this.entrypoint.target);
46+
this.entrypointAsSigner = await impersonate(entrypoint.target);
4747
});
4848

4949
it('should return SIG_VALIDATION_SUCCESS if the signature is valid', async function () {
@@ -102,8 +102,8 @@ function shouldBehaveLikeAnAccountBase() {
102102
})
103103
.then(op => this.signUserOp(op));
104104

105-
const prevAccountBalance = await ethers.provider.getBalance(this.mock.target);
106-
const prevEntrypointBalance = await ethers.provider.getBalance(this.entrypoint.target);
105+
const prevAccountBalance = await ethers.provider.getBalance(this.mock);
106+
const prevEntrypointBalance = await ethers.provider.getBalance(entrypoint);
107107
const amount = ethers.parseEther('0.1');
108108

109109
const tx = await this.mock
@@ -113,10 +113,8 @@ function shouldBehaveLikeAnAccountBase() {
113113
const receipt = await tx.wait();
114114
const callerFees = receipt.gasUsed * tx.gasPrice;
115115

116-
expect(await ethers.provider.getBalance(this.mock.target)).to.equal(prevAccountBalance - amount);
117-
expect(await ethers.provider.getBalance(this.entrypoint.target)).to.equal(
118-
prevEntrypointBalance + amount - callerFees,
119-
);
116+
expect(await ethers.provider.getBalance(this.mock)).to.equal(prevAccountBalance - amount);
117+
expect(await ethers.provider.getBalance(entrypoint)).to.equal(prevEntrypointBalance + amount - callerFees);
120118
});
121119
});
122120
});
@@ -126,11 +124,11 @@ function shouldBehaveLikeAnAccountBase() {
126124
await this.mock.deploy();
127125
await setBalance(this.other.address, ethers.parseEther('1'));
128126

129-
const prevBalance = await ethers.provider.getBalance(this.mock.target);
127+
const prevBalance = await ethers.provider.getBalance(this.mock);
130128
const amount = ethers.parseEther('0.1');
131-
await this.other.sendTransaction({ to: this.mock.target, value: amount });
129+
await this.other.sendTransaction({ to: this.mock, value: amount });
132130

133-
expect(await ethers.provider.getBalance(this.mock.target)).to.equal(prevBalance + amount);
131+
expect(await ethers.provider.getBalance(this.mock)).to.equal(prevBalance + amount);
134132
});
135133
});
136134
}
@@ -192,7 +190,7 @@ function shouldBehaveLikeAccountHolder() {
192190

193191
await token.connect(owner).safeTransferFrom(owner, this.mock, tokenId);
194192

195-
expect(await token.ownerOf(tokenId)).to.equal(this.mock.target);
193+
expect(await token.ownerOf(tokenId)).to.equal(this.mock);
196194
});
197195
});
198196
});
@@ -202,8 +200,8 @@ function shouldBehaveLikeAnAccountBaseExecutor({ deployable = true } = {}) {
202200
describe('executeUserOp', function () {
203201
beforeEach(async function () {
204202
await setBalance(this.mock.target, ethers.parseEther('1'));
205-
expect(await ethers.provider.getCode(this.mock.target)).to.equal('0x');
206-
this.entrypointAsSigner = await impersonate(this.entrypoint.target);
203+
expect(await ethers.provider.getCode(this.mock)).to.equal('0x');
204+
this.entrypointAsSigner = await impersonate(entrypoint.target);
207205
});
208206

209207
it('should revert if the caller is not the canonical entrypoint or the account itself', async function () {
@@ -244,8 +242,8 @@ function shouldBehaveLikeAnAccountBaseExecutor({ deployable = true } = {}) {
244242
.then(op => op.addInitCode())
245243
.then(op => this.signUserOp(op));
246244

247-
await expect(this.entrypoint.connect(this.entrypointAsSigner).handleOps([operation.packed], this.beneficiary))
248-
.to.emit(this.entrypoint, 'AccountDeployed')
245+
await expect(entrypoint.handleOps([operation.packed], this.beneficiary))
246+
.to.emit(entrypoint, 'AccountDeployed')
249247
.withArgs(operation.hash(), this.mock, this.factory, ethers.ZeroAddress)
250248
.to.emit(this.target, 'MockFunctionCalledExtra')
251249
.withArgs(this.mock, 17);
@@ -268,8 +266,7 @@ function shouldBehaveLikeAnAccountBaseExecutor({ deployable = true } = {}) {
268266

269267
operation.signature = '0x00';
270268

271-
await expect(this.entrypoint.connect(this.entrypointAsSigner).handleOps([operation.packed], this.beneficiary))
272-
.to.be.reverted;
269+
await expect(entrypoint.handleOps([operation.packed], this.beneficiary)).to.be.reverted;
273270
});
274271
});
275272
}
@@ -294,7 +291,7 @@ function shouldBehaveLikeAnAccountBaseExecutor({ deployable = true } = {}) {
294291
.then(op => this.signUserOp(op));
295292

296293
expect(await this.mock.getNonce()).to.equal(0);
297-
await expect(this.entrypoint.connect(this.entrypointAsSigner).handleOps([operation.packed], this.beneficiary))
294+
await expect(entrypoint.handleOps([operation.packed], this.beneficiary))
298295
.to.emit(this.target, 'MockFunctionCalledExtra')
299296
.withArgs(this.mock, 42);
300297
expect(await this.mock.getNonce()).to.equal(1);

test/account/AccountECDSA.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint } = require('hardhat');
22
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
33
const { ERC4337Helper } = require('../helpers/erc4337');
44
const { PackedUserOperation } = require('../helpers/eip712-types');
@@ -32,7 +32,7 @@ async function fixture() {
3232
};
3333

3434
const signUserOp = async userOp => {
35-
const typedOp = Object.assign(userOp.packed, { entrypoint: env.entrypoint.target });
35+
const typedOp = Object.assign(userOp.packed, { entrypoint: entrypoint.target });
3636
userOp.signature = await signer.signTypedData(domain, { PackedUserOperation }, typedOp);
3737
return userOp;
3838
};

test/account/AccountERC7702.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint } = require('hardhat');
22
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
33
const { ERC4337Helper } = require('../helpers/erc4337');
44
const { PackedUserOperation } = require('../helpers/eip712-types');
@@ -32,7 +32,7 @@ async function fixture() {
3232
};
3333

3434
const signUserOp = async userOp => {
35-
const typedOp = Object.assign(userOp.packed, { entrypoint: env.entrypoint.target });
35+
const typedOp = Object.assign(userOp.packed, { entrypoint: entrypoint.target });
3636
userOp.signature = await signer.signTypedData(domain, { PackedUserOperation }, typedOp);
3737
return userOp;
3838
};

test/account/AccountP256.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint } = require('hardhat');
22
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
33
const { ERC4337Helper } = require('../helpers/erc4337');
44
const { NonNativeSigner, P256SigningKey } = require('../helpers/signers');
@@ -38,7 +38,7 @@ async function fixture() {
3838
};
3939

4040
const signUserOp = async userOp => {
41-
const typedOp = Object.assign(userOp.packed, { entrypoint: env.entrypoint.target });
41+
const typedOp = Object.assign(userOp.packed, { entrypoint: entrypoint.target });
4242
userOp.signature = await signer.signTypedData(domain, { PackedUserOperation }, typedOp);
4343
return userOp;
4444
};

test/account/AccountRSA.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint } = require('hardhat');
22
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
33
const { ERC4337Helper } = require('../helpers/erc4337');
44
const { NonNativeSigner, RSASHA256SigningKey } = require('../helpers/signers');
@@ -38,7 +38,7 @@ async function fixture() {
3838
};
3939

4040
const signUserOp = async userOp => {
41-
const typedOp = Object.assign(userOp.packed, { entrypoint: env.entrypoint.target });
41+
const typedOp = Object.assign(userOp.packed, { entrypoint: entrypoint.target });
4242
userOp.signature = await signer.signTypedData(domain, { PackedUserOperation }, typedOp);
4343
return userOp;
4444
};

test/helpers/erc4337.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const { ethers } = require('hardhat');
1+
const { ethers, entrypoint, senderCreator } = require('hardhat');
22
const { setCode } = require('@nomicfoundation/hardhat-network-helpers');
33

44
const { UserOperation } = require('@openzeppelin/contracts/test/helpers/erc4337');
5-
const { deployEntrypoint } = require('@openzeppelin/contracts/test/helpers/erc4337-entrypoint');
65

76
const parseInitCode = initCode => ({
87
factory: '0x' + initCode.replace(/0x/, '').slice(0, 40),
@@ -13,24 +12,20 @@ const parseInitCode = initCode => ({
1312
class ERC4337Helper {
1413
constructor() {
1514
this.cache = new Map();
16-
this.envAsPromise = Promise.all([
17-
deployEntrypoint(),
18-
ethers.provider.getNetwork(),
19-
ethers.deployContract('Create2Mock'),
20-
]).then(([{ entrypoint, sendercreator }, { chainId }, factory]) => ({
21-
entrypoint,
22-
sendercreator,
23-
chainId,
24-
factory,
25-
}));
15+
this.envAsPromise = Promise.all([ethers.provider.getNetwork(), ethers.deployContract('Create2Mock')]).then(
16+
([{ chainId }, factory]) => ({
17+
chainId,
18+
factory,
19+
}),
20+
);
2621
}
2722

2823
async wait() {
2924
return (this.env = await this.envAsPromise);
3025
}
3126

3227
async newAccount(name, extraArgs = [], params = {}) {
33-
const { factory, sendercreator } = await this.wait();
28+
const { factory } = await this.wait();
3429

3530
if (!this.cache.has(name)) {
3631
await ethers.getContractFactory(name).then(factory => this.cache.set(name, factory));
@@ -48,7 +43,7 @@ class ERC4337Helper {
4843
factory.interface.encodeFunctionData('$deploy', [0, params.salt ?? ethers.randomBytes(32), tx.data]),
4944
)
5045
.then(deployCode => ethers.concat([factory.target, deployCode]));
51-
const instance = await sendercreator.createSender
46+
const instance = await senderCreator.createSender
5247
.staticCall(initCode)
5348
.then(address => accountFactory.attach(address));
5449
return new SmartAccount(instance, initCode, this);
@@ -57,7 +52,6 @@ class ERC4337Helper {
5752

5853
async fillUserOp(userOp) {
5954
if (!userOp.nonce) {
60-
const { entrypoint } = await this.wait();
6155
userOp.nonce = await entrypoint.getNonce(userOp.sender, 0);
6256
}
6357
if (ethers.isAddressable(userOp.paymaster)) {
@@ -128,7 +122,7 @@ class UserOperationWithContext extends UserOperation {
128122
}
129123

130124
hash() {
131-
const { entrypoint, chainId } = this.params.sender.helper.env;
125+
const { chainId } = this.params.sender.helper.env;
132126
return super.hash(entrypoint, chainId);
133127
}
134128
}

0 commit comments

Comments
 (0)