diff --git a/routes/auth/mintAndFetch.ts b/routes/auth/mintAndFetch.ts index fa40c8d..829bfda 100644 --- a/routes/auth/mintAndFetch.ts +++ b/routes/auth/mintAndFetch.ts @@ -1,13 +1,21 @@ import { Request } from "express"; import { Response } from "express-serve-static-core"; import { ParsedQs } from "qs"; -import { getPKPsForAuthMethod, mintPKP } from "../../lit"; +import { + getPkpEthAddress, + getPKPsForAuthMethod, + getSigner, + mintPKP, +} from "../../lit"; import { AuthMethodVerifyToFetchResponse, FetchRequest, MintNextAndAddAuthMethodsRequest, MintNextAndAddAuthMethodsResponse, } from "../../models"; +import { ethers } from "ethers"; +import { getPKPEthAddressFromPKPMintedEvent } from "../../utils/receipt"; +import { Sequencer } from "../../lib/sequencer"; export async function mintNextAndAddAuthMethodsHandler( req: Request< @@ -29,6 +37,38 @@ export async function mintNextAndAddAuthMethodsHandler( console.info("Minted PKP", { requestId: mintTx.hash, }); + const signer = getSigner(); + const receipt = await signer.provider.waitForTransaction(mintTx.hash!); + const pkpEthAddress = await getPKPEthAddressFromPKPMintedEvent(receipt); + + // send 0.001 eth to the pkp to fund it. + // we will replace this with EIP2771 funding once we have that working + const sequencer = Sequencer.Instance; + Sequencer.Wallet = signer; + const gasToFund = ethers.utils.parseEther("0.001"); + + const gasFundingTxn = await sequencer.wait({ + action: (...args) => { + const paramsToFn = Object.assign( + {}, + ...args, + ) as ethers.providers.TransactionRequest; + return signer.sendTransaction(paramsToFn); + }, + params: [{ to: pkpEthAddress, value: gasToFund }], + transactionData: {}, + }); + console.log("gasFundingTxn", gasFundingTxn); + // wait for confirmation + await gasFundingTxn.wait(); + + // const tx = await ( + // await getSigner() + // ).sendTransaction({ + // to: mintTx.to, + // value: ethers.utils.parseEther("0.001"), + // }); + // await tx.wait(); return res.status(200).json({ requestId: mintTx.hash, }); diff --git a/tests/routes/auth/mintAndFetch.test.ts b/tests/routes/auth/mintAndFetch.test.ts index fb14dfd..633520e 100644 --- a/tests/routes/auth/mintAndFetch.test.ts +++ b/tests/routes/auth/mintAndFetch.test.ts @@ -169,5 +169,16 @@ describe("mintNextAndAddAuthMethods Integration Tests", () => { // Verify that the random address owns the NFT const owner = await pkpNft.ownerOf(tokenId); expect(owner.toLowerCase()).toBe(sendToAddress.toLowerCase()); + + // get PKP eth address from the PKP NFT contract + const pkpEthAddress = await pkpNft.getEthAddress(tokenId); + expect(pkpEthAddress).toBeDefined(); + + // check that the pkp has 0.001 eth + const pkpBalance = await provider.getBalance(pkpEthAddress); + console.log("pkpBalance", pkpBalance); + expect(pkpBalance.toHexString()).toBe( + ethers.utils.parseEther("0.001").toHexString(), + ); }, 30000); // Increase timeout to 30s since we're waiting for real transactions }); diff --git a/utils/receipt.ts b/utils/receipt.ts index a411f2f..59f907d 100644 --- a/utils/receipt.ts +++ b/utils/receipt.ts @@ -1,4 +1,6 @@ -import { providers } from "ethers"; +import config from "../config"; +import { ethers, providers } from "ethers"; +import { getPkpNftContract } from "../lit"; const TRANSFER_EVENT_SIGNATURE = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"; @@ -24,3 +26,22 @@ export async function getTokenIdFromTransferEvent( return transferEventLog.topics[3]; } + +export async function getPKPEthAddressFromPKPMintedEvent( + receipt: providers.TransactionReceipt, +): Promise { + const pkpNft = getPkpNftContract(config.network); + const mintEvent = receipt.logs.find((log) => { + try { + return pkpNft.interface.parseLog(log).name === "PKPMinted"; + } catch { + return false; + } + }); + if (!mintEvent) { + throw new Error("No PKPMinted event found in receipt"); + } + const pkpPubkey = pkpNft.interface.parseLog(mintEvent).args.pubkey; + + return ethers.utils.computeAddress(pkpPubkey); +}