From 77552280c779cf5f712944d1bc5de312756f1cde Mon Sep 17 00:00:00 2001 From: Danilo Tuler Date: Tue, 30 Jul 2024 11:43:34 -0300 Subject: [PATCH] feat(paymaster): allow second execution of service --- .../src/helpers/abi.ts | 13 ++++ .../src/helpers/verifyingPaymasters.ts | 64 +++++++++++-------- .../mock-verifying-paymaster/src/index.ts | 3 +- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/packages/mock-verifying-paymaster/src/helpers/abi.ts b/packages/mock-verifying-paymaster/src/helpers/abi.ts index 9b77a3c5..b2b242b7 100644 --- a/packages/mock-verifying-paymaster/src/helpers/abi.ts +++ b/packages/mock-verifying-paymaster/src/helpers/abi.ts @@ -100,6 +100,19 @@ export const VERIFYING_PAYMASTER_V07_ABI = [ ], stateMutability: "nonpayable", }, + { + inputs: [], + name: "getDeposit", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, { type: "function", name: "getHash", diff --git a/packages/mock-verifying-paymaster/src/helpers/verifyingPaymasters.ts b/packages/mock-verifying-paymaster/src/helpers/verifyingPaymasters.ts index ac218fb1..d0149d4b 100644 --- a/packages/mock-verifying-paymaster/src/helpers/verifyingPaymasters.ts +++ b/packages/mock-verifying-paymaster/src/helpers/verifyingPaymasters.ts @@ -47,14 +47,6 @@ export const setupVerifyingPaymasterV07 = async ( chain: await getChain(), }); - await walletClient - .sendTransaction({ - to: DETERMINISTIC_DEPLOYER, - data, - }) - .then((hash) => publicClient.waitForTransactionReceipt({ hash })) - .then(() => console.log("deployed VerifyingPaymaster v0.7")); - const address = getContractAddress({ opcode: "CREATE2", from: DETERMINISTIC_DEPLOYER, @@ -62,17 +54,31 @@ export const setupVerifyingPaymasterV07 = async ( bytecode: slice(data, 32), }); + if ((await publicClient.getCode({ address })) === undefined) { + await walletClient + .sendTransaction({ + to: DETERMINISTIC_DEPLOYER, + data, + }) + .then((hash) => publicClient.waitForTransactionReceipt({ hash })) + .then(() => console.log("deployed VerifyingPaymaster v0.7")); + } + const verifyingPaymaster = getContract({ address, abi: VERIFYING_PAYMASTER_V07_ABI, client: walletClient, }); - await verifyingPaymaster.write - .deposit({ - value: parseEther("50"), - }) - .then(() => console.log("Funded VerifyingPaymaster V0.7")); + const requiredDeposit = parseEther("50"); + const currentDeposit = await verifyingPaymaster.read.getDeposit(); + if (currentDeposit < requiredDeposit) { + await verifyingPaymaster.write + .deposit({ + value: requiredDeposit - currentDeposit, + }) + .then(() => console.log("Funded VerifyingPaymaster V0.7")); + } return verifyingPaymaster; }; @@ -87,14 +93,6 @@ export const setupVerifyingPaymasterV06 = async ( chain: await getChain(), }); - await walletClient - .sendTransaction({ - to: DETERMINISTIC_DEPLOYER, - data, - }) - .then((hash) => publicClient.waitForTransactionReceipt({ hash })) - .then(() => console.log("deployed VerifyingPaymaster v0.6")); - const address = getContractAddress({ opcode: "CREATE2", from: DETERMINISTIC_DEPLOYER, @@ -102,17 +100,31 @@ export const setupVerifyingPaymasterV06 = async ( bytecode: slice(data, 32), }); + if ((await publicClient.getCode({ address })) === undefined) { + await walletClient + .sendTransaction({ + to: DETERMINISTIC_DEPLOYER, + data, + }) + .then((hash) => publicClient.waitForTransactionReceipt({ hash })) + .then(() => console.log("deployed VerifyingPaymaster v0.6")); + } + const verifyingPaymaster = getContract({ address, abi: VERIFYING_PAYMASTER_V06_ABI, client: walletClient, }); - await verifyingPaymaster.write - .deposit({ - value: parseEther("50"), - }) - .then(() => console.log("Funded VerifyingPaymaster V0.6")); + const requiredDeposit = parseEther("50"); + const currentDeposit = await verifyingPaymaster.read.getDeposit(); + if (currentDeposit < requiredDeposit) { + await verifyingPaymaster.write + .deposit({ + value: requiredDeposit - currentDeposit, + }) + .then(() => console.log("Funded VerifyingPaymaster V0.6")); + } return verifyingPaymaster; }; diff --git a/packages/mock-verifying-paymaster/src/index.ts b/packages/mock-verifying-paymaster/src/index.ts index 42cc91c3..f2977e15 100644 --- a/packages/mock-verifying-paymaster/src/index.ts +++ b/packages/mock-verifying-paymaster/src/index.ts @@ -49,7 +49,8 @@ const main = async () => { return reply.code(200).send({ message: "pong" }); }); - await app.listen({ host: "0.0.0.0", port: 3000 }); + const service = await app.listen({ host: "0.0.0.0", port: 3000 }); + console.log(`Service ready: ${service}`); }; main();