This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Add SponsorEverythingPaymaster #256
Merged
jacque006
merged 6 commits into
getwax:main
from
semaphore-paymaster:feature/sponsor-everything-paymaster
Jul 3, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70e52a3
Add SponsorEverythingPaymaster contract
jihoonsong 0d2e041
Add a test for SponsorEverythingPaymaster
jihoonsong 0323935
Add README for paymaster contracts
jihoonsong db7cd17
Modify test description
jihoonsong e301294
Explicitly return values after paymaster validates UserOp
jihoonsong 9d93a45
Use const variable to reduce redundancy
jihoonsong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Paymaster | ||
|
||
These are exemplary paymaster contracts. The SponsorEverythingPaymaster contract and its test can serve as references when developing more complex paymasters for your specific use cases. |
33 changes: 33 additions & 0 deletions
33
packages/plugins/src/paymaster/SponsorEverythingPaymaster.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.4 <0.9.0; | ||
|
||
import {IEntryPoint} from 'account-abstraction/interfaces/IEntryPoint.sol'; | ||
|
||
import {BasePaymaster} from 'account-abstraction/core/BasePaymaster.sol'; | ||
import {UserOperationLib} from 'account-abstraction/core/UserOperationLib.sol'; | ||
import {PackedUserOperation} from 'account-abstraction/interfaces/PackedUserOperation.sol'; | ||
|
||
/*////////////////////////////////////////////////////////////////////////// | ||
THIS CONTRACT IS STILL IN ACTIVE DEVELOPMENT. NOT FOR PRODUCTION USE | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
|
||
/// @title This paymaster sponsors everything. | ||
contract SponsorEverythingPaymaster is BasePaymaster { | ||
using UserOperationLib for PackedUserOperation; | ||
|
||
constructor(IEntryPoint _entryPoint) BasePaymaster(_entryPoint) {} | ||
|
||
/** | ||
* Validate a user operation. | ||
* @param userOp - The user operation. | ||
* @param userOpHash - The hash of the user operation. | ||
* @param maxCost - The maximum cost of the user operation. | ||
*/ | ||
function _validatePaymasterUserOp( | ||
PackedUserOperation calldata userOp, | ||
bytes32 userOpHash, | ||
uint256 maxCost | ||
) internal virtual override returns (bytes memory context, uint256 validationData) { | ||
// Validation logic comes here. | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
packages/plugins/test/e2e/SafeSponsorEverythingPaymaster.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||
import { expect } from "chai"; | ||||||
import { ethers } from "ethers"; | ||||||
import { | ||||||
SafeECDSAFactory__factory, | ||||||
SafeECDSAPlugin__factory, | ||||||
SponsorEverythingPaymaster__factory, | ||||||
EntryPoint__factory | ||||||
} from "../../typechain-types"; | ||||||
import receiptOf from "./utils/receiptOf"; | ||||||
import { setupTests } from "./utils/setupTests"; | ||||||
import { createAndSendUserOpWithEcdsaSig } from "./utils/createUserOp"; | ||||||
|
||||||
const oneEther = ethers.parseEther("1"); | ||||||
|
||||||
describe("SafePaymasterPlugin", () => { | ||||||
it("should pass the ERC4337 validation", async () => { | ||||||
const { | ||||||
bundlerProvider, | ||||||
provider, | ||||||
admin, | ||||||
owner, | ||||||
entryPointAddress, | ||||||
deployer, | ||||||
safeSingleton, | ||||||
} = await setupTests(); | ||||||
|
||||||
// Deploy paymaster. | ||||||
const paymaster = await deployer.connectOrDeploy( | ||||||
SponsorEverythingPaymaster__factory, | ||||||
[entryPointAddress], | ||||||
); | ||||||
const paymasterAddress = await paymaster.getAddress(); | ||||||
|
||||||
// Paymaster deposits. | ||||||
await paymaster.deposit({ value: ethers.parseEther("1") }) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor suggestion
Suggested change
Can also do this in a few other spots. |
||||||
|
||||||
const recipient = ethers.Wallet.createRandom(); | ||||||
const transferAmount = ethers.parseEther("1"); | ||||||
const dummySignature = await owner.signMessage("dummy sig"); | ||||||
|
||||||
// Deploy ecdsa plugin | ||||||
const safeECDSAFactory = await deployer.connectOrDeploy( | ||||||
SafeECDSAFactory__factory, | ||||||
[], | ||||||
); | ||||||
|
||||||
const createArgs = [ | ||||||
safeSingleton, | ||||||
entryPointAddress, | ||||||
await owner.getAddress(), | ||||||
0, | ||||||
] satisfies Parameters<typeof safeECDSAFactory.create.staticCall>; | ||||||
|
||||||
const accountAddress = await safeECDSAFactory.create.staticCall( | ||||||
...createArgs, | ||||||
); | ||||||
|
||||||
await receiptOf(safeECDSAFactory.create(...createArgs)); | ||||||
|
||||||
const safeEcdsaPlugin = SafeECDSAPlugin__factory.connect( | ||||||
accountAddress, | ||||||
owner, | ||||||
); | ||||||
|
||||||
// Native tokens for the pre-fund | ||||||
await receiptOf( | ||||||
admin.sendTransaction({ | ||||||
to: accountAddress, | ||||||
value: ethers.parseEther("1"), | ||||||
}), | ||||||
); | ||||||
|
||||||
// Construct userOp | ||||||
const userOpCallData = safeEcdsaPlugin.interface.encodeFunctionData( | ||||||
"execTransaction", | ||||||
[recipient.address, transferAmount, "0x00"], | ||||||
); | ||||||
|
||||||
// Note: factoryParams is not used because we need to create both the safe | ||||||
// proxy and the plugin, and 4337 currently only allows one contract | ||||||
// creation in this step. Since we need an extra step anyway, it's simpler | ||||||
// to do the whole create outside of 4337. | ||||||
const factoryParams = { | ||||||
factory: "0x", | ||||||
factoryData: "0x", | ||||||
}; | ||||||
|
||||||
// Check paymaster balances before and after sending UserOp. | ||||||
const entrypoint = EntryPoint__factory.connect(entryPointAddress, provider) | ||||||
const paymasterBalanceBefore = await entrypoint.balanceOf(paymasterAddress) | ||||||
|
||||||
// Send userOp | ||||||
await createAndSendUserOpWithEcdsaSig( | ||||||
provider, | ||||||
bundlerProvider, | ||||||
owner, | ||||||
accountAddress, | ||||||
factoryParams, | ||||||
userOpCallData, | ||||||
entryPointAddress, | ||||||
dummySignature, | ||||||
paymasterAddress, | ||||||
3e5, | ||||||
"0x" | ||||||
); | ||||||
|
||||||
const paymasterBalanceAfter = await entrypoint.balanceOf(paymasterAddress) | ||||||
|
||||||
expect(paymasterBalanceBefore).greaterThan(paymasterBalanceAfter) | ||||||
expect(await provider.getBalance(recipient.address)).to.equal(oneEther); | ||||||
}); | ||||||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be explicit with return values here. Either:
Or