Skip to content

Commit

Permalink
method to withdraw excess funds as discussed (plus extend test for 2n…
Browse files Browse the repository at this point in the history
…d tx: temp)
  • Loading branch information
livingrockrises committed Dec 11, 2023
1 parent ff71663 commit e383a65
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
12 changes: 12 additions & 0 deletions contracts/sponsorship/SponsorshipPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ contract SponsorshipPaymaster is

address public feeCollector;

uint256 internal totalDeductables = 0;

constructor(
address _owner,
IEntryPoint _entryPoint,
Expand Down Expand Up @@ -184,6 +186,14 @@ contract SponsorshipPaymaster is
emit GasWithdrawn(msg.sender, withdrawAddress, amount);
}

function withdrawExceessFunds(address payable withdrawAddress) public nonReentrant onlyOwner {
if (withdrawAddress == address(0)) revert CanNotWithdrawToZeroAddress();
uint256 excess = entryPoint.balanceOf(address(this)) - totalDeductables;
if (excess > 0) {
entryPoint.withdrawTo(withdrawAddress, excess);
}
}

/**
* @dev This method is called by the off-chain service, to sign the request.
* It is called on-chain from the validatePaymasterUserOp, to validate the signature.
Expand Down Expand Up @@ -269,6 +279,8 @@ contract SponsorshipPaymaster is
unaccountedEPGasOverhead *
effectiveGasPrice;

totalDeductables += balToDeduct;

uint256 costIncludingPremium = (balToDeduct * dynamicMarkup) /
PRICE_DENOMINATOR;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ describe("EntryPoint with VerifyingPaymaster Singleton", function () {
it("succeed with valid signature", async () => {
const fundingId = await offchainSigner.getAddress();

// Review this is only ideal for first tx for a paymasterId
await sponsorshipPaymaster
.connect(deployer)
.setUnaccountedEPGasOverhead(35500);
.setUnaccountedEPGasOverhead(57735);

await sponsorshipPaymaster.depositFor(fundingId, {
value: ethers.utils.parseEther("1"),
Expand Down Expand Up @@ -291,6 +292,64 @@ describe("EntryPoint with VerifyingPaymaster Singleton", function () {
expect(feeCollectorBalanceAfter).to.be.equal(
paymasterIdBalanceDiff.mul(BigNumber.from(1)).div(BigNumber.from(11))
);

// Second transaction same funding id
/* const userOp2 = await fillAndSign(
{
sender: walletAddress,
verificationGasLimit: 200000,
},
walletOwner,
entryPoint,
"nonce"
);
const hash2 = await sponsorshipPaymaster.getHash(
userOp2,
fundingId,
MOCK_VALID_UNTIL,
MOCK_VALID_AFTER,
dynamicMarkup
);
const sig2 = await offchainSigner.signMessage(arrayify(hash2));
const userOp3 = await fillAndSign(
{
...userOp2,
paymasterAndData: hexConcat([
paymasterAddress,
ethers.utils.defaultAbiCoder.encode(
["address", "uint48", "uint48", "uint32"],
[fundingId, MOCK_VALID_UNTIL, MOCK_VALID_AFTER, dynamicMarkup]
),
sig2,
]),
},
walletOwner,
entryPoint,
"nonce"
);
const signatureWithModuleAddress2 = ethers.utils.defaultAbiCoder.encode(
["bytes", "address"],
[userOp3.signature, ecdsaModule.address]
);
userOp3.signature = signatureWithModuleAddress2;
const tx2 = await entryPoint.handleOps(
[userOp3],
await offchainSigner.getAddress(),
{
type: 2,
maxFeePerGas: userOp3.maxFeePerGas,
maxPriorityFeePerGas: userOp3.maxPriorityFeePerGas,
}
);
const receipt2 = await tx2.wait();
console.log(
"effective gas price ",
receipt2.effectiveGasPrice.toString()
);
console.log("gas used VPM V2 ", receipt2.gasUsed.toString()); */
});
});

Expand Down

0 comments on commit e383a65

Please sign in to comment.