Skip to content

Commit

Permalink
alchemy no paymaster code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed Jul 17, 2024
1 parent 05091b1 commit 42f2d4b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/4337-gas-metering/alchemy/alchemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ if (usePaymaster) {
sponsoredUserOperation.callGasLimit = rvGas?.callGasLimit
sponsoredUserOperation.verificationGasLimit = rvGas?.verificationGasLimit

const weiToSend = parseEther('1')
const weiToSend = parseEther('0.5')
let safeETHBalance = await publicClient.getBalance({
address: senderAddress,
})
Expand Down
43 changes: 28 additions & 15 deletions examples/4337-gas-metering/alchemy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ export type gasData = {
preVerificationGas: string
callGasLimit: string
verificationGasLimit: string
paymasterVerificationGasLimit: string | null
}

export const serializeValuesToBigInt = (obj: Record<string, string>): Record<string, bigint> => {
export const serializeValuesToBigInt = <T extends Record<string, string>, K extends keyof T = never>(
obj: T,
excludeFields: K[] = [] as K[],
): {
[P in keyof T]: P extends K ? T[P] : bigint
} => {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
acc[key] = BigInt(value)
if (excludeFields.includes(key as K)) {
acc[key as keyof T] = value as keyof T extends K ? T[K & keyof T] : bigint
} else {
acc[key as keyof T] = BigInt(value) as keyof T extends K ? T[K & keyof T] : bigint
}
return acc
},
{} as Record<string, bigint>,
{} as {
[P in keyof T]: P extends K ? T[P] : bigint
},
)
}

Expand Down Expand Up @@ -75,7 +87,7 @@ export const getGasValuesFromAlchemyPaymaster = async (
})
console.log('\nReceived Paymaster Data from Alchemy.')

return serializeValuesToBigInt(suoData)
return serializeValuesToBigInt(suoData, ['paymasterAndData'])
}

export const getMaxPriorityFeePerGasFromAlchemy = async (chain: string, apiKey: string): Promise<bigint> => {
Expand All @@ -97,8 +109,6 @@ export const getMaxPriorityFeePerGasFromAlchemy = async (chain: string, apiKey:
}
return json
})

console.log({ responseValues })
console.log('\nReceived Fee Data from Alchemy.')

let rvFee
Expand Down Expand Up @@ -148,7 +158,7 @@ export const getGasValuesFromAlchemy = async (
callGasLimit: undefined,
verificationGasLimit: undefined,
preVerificationGas: undefined,
maxFeePerGas: sponsoredUserOperation.maxFeePerGas.toString(16),
maxFeePerGas: addHexPrefix(sponsoredUserOperation.maxFeePerGas.toString(16)),
maxPriorityFeePerGas: addHexPrefix(sponsoredUserOperation.maxPriorityFeePerGas.toString(16)),
nonce: addHexPrefix(sponsoredUserOperation.nonce.toString(16)),
},
Expand All @@ -167,14 +177,17 @@ export const getGasValuesFromAlchemy = async (
})

console.log('\nReceived Gas Data from Alchemy.')
console.log(responseValues)
let rvGas
let rvGas: gasData | undefined
if (responseValues && responseValues['result']) {
rvGas = responseValues['result'] as gasData
} else {
throw new Error('No gas data found')
}

if (!rvGas.paymasterVerificationGasLimit) {
rvGas.paymasterVerificationGasLimit = '0x0'
}
// @ts-expect-error I don't know why but the types are not correct
return serializeValuesToBigInt(rvGas)
}

Expand All @@ -197,7 +210,7 @@ export const submitUserOperationAlchemy = async (
callGasLimit: addHexPrefix(sponsoredUserOperation.callGasLimit.toString(16)),
verificationGasLimit: addHexPrefix(sponsoredUserOperation.verificationGasLimit.toString(16)),
preVerificationGas: addHexPrefix(sponsoredUserOperation.preVerificationGas.toString(16)),
maxFeePerGas: sponsoredUserOperation.maxFeePerGas.toString(16),
maxFeePerGas: addHexPrefix(sponsoredUserOperation.maxFeePerGas.toString(16)),
maxPriorityFeePerGas: addHexPrefix(sponsoredUserOperation.maxPriorityFeePerGas.toString(16)),
nonce: addHexPrefix(sponsoredUserOperation.nonce.toString(16)),
},
Expand Down Expand Up @@ -244,15 +257,15 @@ export const submitUserOperationAlchemy = async (
if (json.error && 'message' in json.error) {
throw new Error(json.error.message)
}
receipt = json
receipt = json['result']
})
runOnce = false
}

if (receipt['result'] && receipt['result']['receipt']['transactionHash']) {
console.log('\nTransaction Link: https://' + chain + '.etherscan.io/tx/' + receipt['result']['receipt']['transactionHash'])
const actualGasUsed = fromHex(receipt['result']['actualGasUsed'], 'number')
const gasUsed = fromHex(responseValues['result']['receipt']['gasUsed'], 'number')
if (receipt && receipt['receipt']['transactionHash']) {
console.log('\nTransaction Link: https://' + chain + '.etherscan.io/tx/' + receipt['transactionHash'])
const actualGasUsed = fromHex(receipt['actualGasUsed'], 'number')
const gasUsed = fromHex(receipt['receipt']['gasUsed'], 'number')
console.log(`\nGas Used (Account or Paymaster): ${actualGasUsed}`)
console.log(`Gas Used (Transaction): ${gasUsed}\n`)
}
Expand Down

0 comments on commit 42f2d4b

Please sign in to comment.