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

fund pkp when minting #71

Merged
merged 4 commits into from
Feb 20, 2025
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
42 changes: 41 additions & 1 deletion routes/auth/mintAndFetch.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -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,
});
Expand Down
11 changes: 11 additions & 0 deletions tests/routes/auth/mintAndFetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
23 changes: 22 additions & 1 deletion utils/receipt.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -24,3 +26,22 @@ export async function getTokenIdFromTransferEvent(

return transferEventLog.topics[3];
}

export async function getPKPEthAddressFromPKPMintedEvent(
receipt: providers.TransactionReceipt,
): Promise<string> {
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);
}