Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(relay-kit): Improve Safe4337Pack paymaster types #902

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ describe('Safe4337Pack', () => {
},
paymasterOptions: {
isSponsored: true,
paymasterUrl: fixtures.PAYMASTER_URL,
paymasterAddress: fixtures.PAYMASTER_ADDRESS
paymasterUrl: fixtures.PAYMASTER_URL
}
})

Expand All @@ -440,7 +439,7 @@ describe('Safe4337Pack', () => {
safe: fixtures.SAFE_ADDRESS_v1_4_1,
entryPoint: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
initCode: '0x',
paymasterAndData: '0x0000000000325602a77416A16136FDafd04b299f',
paymasterAndData: '0x',
callData: constants.INTERFACES.encodeFunctionData('executeUserOp', [
transferUSDC.to,
transferUSDC.value,
Expand All @@ -463,9 +462,9 @@ describe('Safe4337Pack', () => {
options: {
safeAddress: fixtures.SAFE_ADDRESS_v1_4_1
},
// @ts-expect-error - An error will be thrown
paymasterOptions: {
isSponsored: true,
paymasterAddress: fixtures.PAYMASTER_ADDRESS
isSponsored: true
}
})

Expand Down
31 changes: 18 additions & 13 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
Safe4337Options,
UserOperationReceipt,
UserOperationWithPayload,
PaymasterOptions
PaymasterOptions,
ERC20PaymasterOption
} from './types'
import {
DEFAULT_SAFE_VERSION,
Expand Down Expand Up @@ -200,10 +201,10 @@ export class Safe4337Pack extends RelayKitBasePack<{
let deploymentTo = addModulesLibAddress
let deploymentData = INTERFACES.encodeFunctionData('enableModules', [[safe4337ModuleAddress]])

const { isSponsored, paymasterTokenAddress } = paymasterOptions || {}

const isApproveTransactionRequired =
!!paymasterOptions && !isSponsored && !!paymasterTokenAddress
!!paymasterOptions &&
!paymasterOptions.isSponsored &&
!!paymasterOptions.paymasterTokenAddress

if (isApproveTransactionRequired) {
const { paymasterAddress, amountToApprove = MAX_ERC20_AMOUNT_TO_APPROVE } = paymasterOptions
Expand All @@ -216,7 +217,7 @@ export class Safe4337Pack extends RelayKitBasePack<{
}

const approveToPaymasterTransaction = {
to: paymasterTokenAddress,
to: paymasterOptions.paymasterTokenAddress,
data: INTERFACES.encodeFunctionData('approve', [paymasterAddress, amountToApprove]),
value: '0',
operation: OperationType.Call // Call for approve
Expand Down Expand Up @@ -389,20 +390,21 @@ export class Safe4337Pack extends RelayKitBasePack<{
}: Safe4337CreateTransactionProps): Promise<EthSafeOperation> {
const safeAddress = await this.protocolKit.getAddress()
const nonce = await this.#getSafeNonceFromEntrypoint(safeAddress)

const { amountToApprove, validUntil, validAfter, feeEstimator } = options

if (amountToApprove) {
if (!this.#paymasterOptions || !this.#paymasterOptions.paymasterTokenAddress) {
const paymasterOptions = this.#paymasterOptions as ERC20PaymasterOption

if (!paymasterOptions.paymasterTokenAddress) {
throw new Error('Paymaster must be initialized')
}

const paymasterAddress = this.#paymasterOptions.paymasterAddress
const paymasterTokenAddress = this.#paymasterOptions.paymasterTokenAddress

const approveToPaymasterTransaction = {
to: paymasterTokenAddress,
data: INTERFACES.encodeFunctionData('approve', [paymasterAddress, amountToApprove]),
to: paymasterOptions.paymasterTokenAddress,
data: INTERFACES.encodeFunctionData('approve', [
paymasterOptions.paymasterAddress,
amountToApprove
]),
value: '0',
operation: OperationType.Call // Call for approve
}
Expand All @@ -422,7 +424,10 @@ export class Safe4337Pack extends RelayKitBasePack<{
})
: this.#encodeExecuteUserOpCallData(transactions[0])

const paymasterAndData = this.#paymasterOptions?.paymasterAddress || '0x'
const paymasterAndData =
this.#paymasterOptions && 'paymasterAddress' in this.#paymasterOptions
? this.#paymasterOptions.paymasterAddress
: '0x'

const userOperation: UserOperation = {
sender: safeAddress,
Expand Down
14 changes: 10 additions & 4 deletions packages/relay-kit/src/packs/safe-4337/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ type PredictedSafeOptions = {
saltNonce?: string
}

export type PaymasterOptions = {
paymasterUrl?: string
isSponsored?: boolean
export type SponsoredPaymasterOption = {
isSponsored: true
paymasterUrl: string
sponsorshipPolicyId?: string
}

export type ERC20PaymasterOption = {
isSponsored?: false
paymasterAddress: string
paymasterTokenAddress?: string
paymasterTokenAddress: string
amountToApprove?: bigint
}

export type PaymasterOptions = SponsoredPaymasterOption | ERC20PaymasterOption | undefined

export type Safe4337InitOptions = {
provider: SafeProviderConfig['provider']
signer?: SafeProviderConfig['signer']
Expand Down
Loading