Skip to content

Commit

Permalink
fix: gas estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed Dec 11, 2024
1 parent 98e975b commit c385383
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/validators/SessionKeyValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ contract SessionKeyValidator is IModuleValidator {
(SessionLib.SessionSpec, uint64[])
);
require(spec.signer != address(0), "Invalid signer (empty)");
bytes32 sessionHash = keccak256(abi.encode(spec));
// this generally throws instead of returning false
sessions[sessionHash].validate(transaction, spec, periodIds);
(address recoveredAddress, ECDSA.RecoverError recoverError) = ECDSA.tryRecover(signedHash, transactionSignature);
if (recoverError != ECDSA.RecoverError.NoError || recoveredAddress == address(0)) {
return false;
}
require(recoveredAddress == spec.signer, "Invalid signer (mismatch)");
bytes32 sessionHash = keccak256(abi.encode(spec));
// this generally throws instead of returning false
sessions[sessionHash].validate(transaction, spec, periodIds);
return true;
}

Expand Down
16 changes: 7 additions & 9 deletions test/SessionKeyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,11 @@ class SessionTester {

async sendTxSuccess(txRequest: ethers.TransactionLike = {}) {
this.aaTransaction = {
...await this.aaTxTemplate(true),
...await this.aaTxTemplate(await this.periodIds(txRequest.to!, txRequest.data?.slice(0, 10))),
...txRequest,
};
const estimatedGas = await provider.estimateGas(this.aaTransaction);
this.aaTransaction.gasLimit = BigInt(Math.ceil(Number(estimatedGas) * 1.1));
logInfo(`\`sessionTx\` gas estimated: ${await provider.estimateGas(this.aaTransaction)}`);
this.aaTransaction.gasLimit = await provider.estimateGas(this.aaTransaction);
logInfo(`\`sessionTx\` gas estimated: ${this.aaTransaction.gasLimit}`);

const signedTransaction = await this.sessionAccount.signTransaction(this.aaTransaction);
const tx = await provider.broadcastTransaction(signedTransaction);
Expand All @@ -265,7 +264,7 @@ class SessionTester {

async sendTxFail(tx: ethers.TransactionLike = {}) {
this.aaTransaction = {
...await this.aaTxTemplate(true),
...await this.aaTxTemplate(await this.periodIds(tx.to!, tx.data?.slice(0, 10))),
gasLimit: 100_000_000n,
...tx,
};
Expand Down Expand Up @@ -320,8 +319,7 @@ class SessionTester {
};
}

async aaTxTemplate(forSession: boolean = false) {
const numberOfConstraints = this.session.callPolicies.map((policy) => policy.constraints.length);
async aaTxTemplate(periodIds?: number[]) {
return {
type: 113,
from: this.proxyAccountAddress,
Expand All @@ -332,14 +330,14 @@ class SessionTester {
gasPrice: await provider.getGasPrice(),
customData: {
gasPerPubdata: utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
customSignature: forSession ? abiCoder.encode(
customSignature: periodIds ? abiCoder.encode(
["bytes", "address", "bytes"],
[
ethers.zeroPadValue("0x1b", 65),
await fixtures.getSessionKeyModuleAddress(),
abiCoder.encode(
[sessionSpecAbi, "uint64[]"],
[this.session, new Array(2 + Math.max(0, ...numberOfConstraints)).fill(0)],
[this.session, periodIds],
),
],
) : undefined,
Expand Down

0 comments on commit c385383

Please sign in to comment.