Skip to content

Commit

Permalink
chore: fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Feb 20, 2024
1 parent 57f818d commit 77bb2c5
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 56 deletions.
36 changes: 18 additions & 18 deletions tests/LSP17ContractExtension/LSP17ExtendableTokens.behaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
it('should revert with NoExtensionForFunctionSignature error', async () => {
await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: notExistingFunctionSignature,
}),
)
Expand All @@ -172,7 +172,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
const amountSent = 200;
await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: notExistingFunctionSignature,
value: amountSent,
}),
Expand Down Expand Up @@ -202,7 +202,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
// different sender
await expect(
context.accounts[1].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: checkMsgVariableFunctionSignature,
value: value,
}),
Expand All @@ -225,7 +225,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
sender.sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: checkMsgVariableFunctionSignature,
value: 100, // different value
}),
Expand Down Expand Up @@ -263,7 +263,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
sender.sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: checkMsgVariableFunctionSignature,
value: 100, // different value
}),
Expand All @@ -282,7 +282,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
// different sender
await expect(
context.accounts[1].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: checkMsgVariableFunctionSignature,
value: value,
}),
Expand All @@ -297,7 +297,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
abiCoder.encode(['address', 'uint256'], [sender.address, value]).substring(2);

await sender.sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: checkMsgVariableFunctionSignature,
value: value,
});
Expand Down Expand Up @@ -329,7 +329,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: revertStringFunctionSignature,
value: 0,
}),
Expand Down Expand Up @@ -358,13 +358,13 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
sender.sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: revertCustomFunctionSelector,
value: 0,
}),
)
.to.be.revertedWithCustomError(revertCustomExtension, 'RevertWithAddresses')
.withArgs(sender.address, context.contract.address);
.withArgs(sender.address, await context.contract.getAddress());
});
});

Expand All @@ -384,7 +384,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
it('should pass and emit the event on the extension', async () => {
await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: emitEventFunctionSelector,
value: 0,
}),
Expand All @@ -406,7 +406,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
it('should pass and return the name correctly', async () => {
const returnValue = await provider.call({
from: context.accounts[0].address,
to: context.contract.address,
to: await context.contract.getAddress(),
data: nameFunctionSelector,
});

Expand All @@ -428,7 +428,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
it('should pass and return the age correctly', async () => {
const returnValue = await provider.call({
from: context.accounts[0].address,
to: context.contract.address,
to: await context.contract.getAddress(),
data: ageFunctionSelector,
});

Expand Down Expand Up @@ -459,7 +459,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
abiCoder.encode(['uint256'], [amountTransferred]).substring(2);

await context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: transferFunctionSignature,
});

Expand All @@ -486,7 +486,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
expect(balanceBefore).to.equal(0);

await context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
value: 100,
data: buyFunctionSelector,
});
Expand Down Expand Up @@ -530,7 +530,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: reenterAccountFunctionSignature,
}),
).to.not.emit(emitEventExtension, 'EventEmittedInExtension');
Expand Down Expand Up @@ -558,7 +558,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte

await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: reenterAccountFunctionSignature,
value: 0,
}),
Expand Down Expand Up @@ -601,7 +601,7 @@ export const shouldBehaveLikeLSP17 = (buildContext: () => Promise<LSP17TestConte
it('should revert', async () => {
await expect(
context.accounts[0].sendTransaction({
to: context.contract.address,
to: await context.contract.getAddress(),
data: '0x01',
}),
).to.be.revertedWithCustomError(context.contract, 'InvalidFunctionSelector');
Expand Down
23 changes: 13 additions & 10 deletions tests/LSP17Extensions/helpers/Create2Factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// from: https://github.com/Arachnid/deterministic-deployment-proxy
import { BigNumberish, JsonRpcProvider, Signer, toBeHex } from 'ethers';
import { BigNumberish, JsonRpcProvider, Signer, ethers, toBeHex } from 'ethers';
import { getBytes, concat, zeroPadValue, keccak256 } from 'ethers';
import { Provider } from '@ethersproject/providers';
import { TransactionRequest } from '@ethersproject/abstract-provider';
Expand Down Expand Up @@ -51,24 +51,25 @@ export class Create2Factory {
data: this.getDeployTransactionCallData(initCode, salt),
};
if (gasLimit === 'estimate') {
gasLimit = await this.signer.estimateGas(deployTx);
gasLimit = await (await this.signer).estimateGas(deployTx);
}

if (gasLimit === undefined) {
gasLimit =
gasLimit = ethers.toBigInt(
getBytes(initCode)
.map((x) => (x === 0 ? 4 : 16))
.reduce((sum, x) => sum + x) +
(200 * initCode.length) / 2 + // actual is usually somewhat smaller (only deposited code, not entire constructor)
6 * Math.ceil(initCode.length / 64) + // hash price. very minor compared to deposit costs
32000 +
21000;
(200 * initCode.length) / 2 + // actual is usually somewhat smaller (only deposited code, not entire constructor)
6 * Math.ceil(initCode.length / 64) + // hash price. very minor compared to deposit costs
32000 +
21000,
);

// deployer requires some extra gas
gasLimit = Math.floor((gasLimit * 64) / 63);
gasLimit = ethers.toBigInt(Math.floor((ethers.toNumber(gasLimit) * 64) / 63));
}

const ret = await this.signer.sendTransaction({ ...deployTx, gasLimit });
const ret = await (await this.signer).sendTransaction({ ...deployTx, gasLimit });
await ret.wait();
if ((await this.provider.getCode(addr).then((code) => code.length)) === 2) {
throw new Error('failed to deploy');
Expand Down Expand Up @@ -102,7 +103,9 @@ export class Create2Factory {
if (await this._isFactoryDeployed()) {
return;
}
await (signer ?? this.signer).sendTransaction({
await (
await (signer ?? this.signer)
).sendTransaction({
to: Create2Factory.factoryDeployer,
value: BigInt(Create2Factory.factoryDeploymentFee),
});
Expand Down
31 changes: 17 additions & 14 deletions tests/LSP17Extensions/helpers/UserOp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBytes, dataSlice, keccak256 } from 'ethers';
import { getBytes, dataSlice, keccak256, BytesLike } from 'ethers';
import { Wallet } from 'ethers';
import { AddressZero, callDataCost } from './utils';
import { ecsign, toRpcSig, keccak256 as keccak256_buffer } from 'ethereumjs-util';
Expand All @@ -7,6 +7,7 @@ import { EntryPoint } from '@account-abstraction/contracts';
import { ethers } from 'ethers';
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
import * as typ from './solidityTypes';
import { ethers as hreEther } from 'hardhat';

export interface UserOperation {
sender: typ.address;
Expand Down Expand Up @@ -41,14 +42,14 @@ export function packUserOp(op: UserOperation, forSignature = true): string {
[
op.sender,
op.nonce,
keccak256(op.initCode),
keccak256(op.callData),
keccak256(op.initCode as BytesLike),
keccak256(op.callData as BytesLike),
op.callGasLimit,
op.verificationGasLimit,
op.preVerificationGas,
op.maxFeePerGas,
op.maxPriorityFeePerGas,
keccak256(op.paymasterAndData),
keccak256(op.paymasterAndData as BytesLike),
],
);
} else {
Expand Down Expand Up @@ -163,10 +164,10 @@ export async function fillUserOp(
entryPoint?: EntryPoint,
): Promise<UserOperation> {
const op1 = { ...op };
const provider = entryPoint?.provider;
const provider = hreEther.provider;
if (op.initCode != null) {
const initAddr = dataSlice(op1.initCode, 0, 20);
const initCallData = dataSlice(op1.initCode, 20);
const initAddr = dataSlice(op1.initCode as BytesLike, 0, 20);
const initCallData = dataSlice(op1.initCode as BytesLike, 20);
if (op1.nonce == null) op1.nonce = 0;
if (op1.sender == null) {
if (initAddr.toLowerCase() === Create2Factory.contractAddress.toLowerCase()) {
Expand All @@ -176,7 +177,7 @@ export async function fillUserOp(
} else {
if (provider == null) throw new Error('no entrypoint/provider');
op1.sender = await entryPoint.callStatic
.getSenderAddress(op1.initCode)
.getSenderAddress(op1.initCode as BytesLike)
.catch((e) => e.errorArgs.sender);
}
}
Expand Down Expand Up @@ -208,17 +209,17 @@ export async function fillUserOp(
const gasEtimated = await provider.estimateGas({
from: entryPoint?.target,
to: op1.sender,
data: op1.callData,
data: ethers.hexlify(op1.callData as BytesLike),
});

op1.callGasLimit = gasEtimated;
}
if (op1.maxFeePerGas == null) {
if (provider == null) throw new Error('must have entryPoint to autofill maxFeePerGas');
const block = await provider.getBlock('latest');
op1.maxFeePerGas = block.baseFeePerGas.add(
op1.maxPriorityFeePerGas ?? DefaultsForUserOp.maxPriorityFeePerGas,
);
op1.maxFeePerGas =
block.baseFeePerGas +
BigInt(op1.maxPriorityFeePerGas ?? DefaultsForUserOp.maxPriorityFeePerGas);
}

if (op1.maxPriorityFeePerGas == null) {
Expand All @@ -237,11 +238,13 @@ export async function fillAndSign(
signer: SignerWithAddress,
entryPoint?: EntryPoint,
): Promise<UserOperation> {
const provider = entryPoint?.provider;
const provider = hreEther.provider;
const op2 = await fillUserOp(op, signer, entryPoint);

const chainId = await provider.getNetwork().then((net) => net.chainId);
const message = getBytes(getUserOpHash(op2, entryPoint.target as string, chainId));
const message = getBytes(
getUserOpHash(op2, entryPoint.target as string, ethers.toNumber(chainId)),
);

return {
...op2,
Expand Down
4 changes: 2 additions & 2 deletions tests/LSP17Extensions/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function callDataCost(data: string): number {
}

export async function deployEntryPoint(provider = ethers.provider): Promise<EntryPoint> {
const create2factory = new Create2Factory(provider);
const create2factory = new Create2Factory(provider as any);
const addr = await create2factory.deploy(
EntryPoint__factory.bytecode,
0,
process.env.COVERAGE != null ? 20e6 : 8e6,
process.env.COVERAGE != null ? BigInt(20e6) : BigInt(8e6),
);
return EntryPoint__factory.connect(addr, await provider.getSigner());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/LSP6KeyManager/LSP6KeyManager.behaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const shouldInitializeLikeLSP6 = (buildContext: () => Promise<LSP6TestCon
});

it('should be linked to the right ERC725 account contract', async () => {
const account = await context.keyManager.target();
const account = await context.keyManager['target()'].staticCall();
expect(account).to.equal(await context.universalProfile.getAddress());
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/LSP6KeyManager/LSP6KeyManagerInit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('LSP6KeyManager with proxy', () => {
const accounts = await ethers.getSigners();
const keyManagerBaseContract = await new LSP6KeyManagerInit__factory(accounts[0]).deploy();

const linkedTarget = await keyManagerBaseContract.target();
const linkedTarget = await keyManagerBaseContract['target()'].staticCall();
expect(linkedTarget).to.equal(ethers.ZeroAddress);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/LSP6KeyManager/Relay/MultiChannelNonce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const shouldBehaveLikeMultiChannelNonce = (buildContext: () => Promise<LS

describe('when calling `getNonce(...)` with a channel ID greater than 2 ** 128', () => {
it('should revert', async () => {
const channelId = ethers.toBigInt(2 ** 129);
const channelId = ethers.toBigInt('0xffffffffffffffffffffffffffffffffff');

await expect(context.keyManager.getNonce(signer.address, channelId)).to.be.revertedWithPanic;
});
Expand Down
4 changes: 2 additions & 2 deletions tests/LSP6KeyManager/SetData/PermissionSetData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ export const shouldBehaveLikePermissionSetData = (buildContext: () => Promise<LS
const aliceUniversalProfilePayload =
aliceContext.universalProfile.interface.encodeFunctionData('execute', [
OPERATION_TYPES.CALL,
bobContext.keyManager.getAddress(),
await bobContext.keyManager.getAddress(),
0,
bobKeyManagerPayload,
]);
Expand Down Expand Up @@ -805,7 +805,7 @@ export const shouldBehaveLikePermissionSetData = (buildContext: () => Promise<LS
const aliceUniversalProfilePayload =
aliceContext.universalProfile.interface.encodeFunctionData('execute', [
OPERATION_TYPES.CALL,
bobContext.keyManager.getAddress(),
await bobContext.keyManager.getAddress(),
0,
bobKeyManagerPayload,
]);
Expand Down
Loading

0 comments on commit 77bb2c5

Please sign in to comment.