Skip to content

Commit

Permalink
Add API url as env
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlovdog committed Jan 5, 2025
1 parent d7e0eae commit 190e226
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/app/transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const ETH_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
// This is a dummy private key for testing - DO NOT use in production
const DUMMY_KEY = "0x1234567890123456789012345678901234567890123456789012345678901234";

if (!process.env.NEXT_PUBLIC_PIMLICO_API_URL) {
throw new Error("NEXT_PUBLIC_PIMLICO_API_URL is not set");
}

export default function Transfer() {
const [apiKey, setApiKey] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -26,6 +30,11 @@ export default function Transfer() {
const [selectedChain, setSelectedChain] = useState<typeof sepolia | typeof baseSepolia | typeof arbitrumSepolia>(sepolia);
const chains = [sepolia, baseSepolia, arbitrumSepolia];

const getPimlicoUrl = (chainId: number) => {
if (!apiKey) throw new Error("API key is required");
return `${process.env.NEXT_PUBLIC_PIMLICO_API_URL}v2/${chainId}/rpc?apikey=${apiKey}`;
};

useEffect(() => {
const loadApiKey = async () => {
const key = await pimlicoStorage.getApiKey();
Expand Down Expand Up @@ -67,17 +76,17 @@ export default function Transfer() {
console.log("Request params:", params);

setTransferStatus("Requesting withdrawal data...");
const response = await fetch(`https://api.pimlico.io/v2/${selectedChain.id}/rpc?apikey=${apiKey}`, {
const response = await fetch(getPimlicoUrl(selectedChain.id), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "pimlico_sponsorMagicSpendWithdrawal",
params: [params, null],
id: 1
}),
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "pimlico_sponsorMagicSpendWithdrawal",
params: [params, null],
id: 1
}),
});

const data = await response.json();
Expand All @@ -100,7 +109,7 @@ export default function Transfer() {
});

const paymasterClient = createPimlicoClient({
transport: http(`https://api.pimlico.io/v2/${selectedChain.id}/rpc?apikey=${apiKey}`),
transport: http(getPimlicoUrl(selectedChain.id)),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
Expand All @@ -127,7 +136,7 @@ export default function Transfer() {
account: safeAccount,
chain: selectedChain,
paymaster: paymasterClient,
bundlerTransport: http(`https://api.pimlico.io/v2/${selectedChain.id}/rpc?apikey=${apiKey}`),
bundlerTransport: http(getPimlicoUrl(selectedChain.id)),
userOperation: {
estimateFeesPerGas: async () =>
(await paymasterClient.getUserOperationGasPrice()).fast,
Expand Down

0 comments on commit 190e226

Please sign in to comment.