How to request ETH from MagicSpend++ protocol
This guide will show you how to request ETH from the MagicSpend++ contracts. Currently the testnet tokens are free to withdraw.
Steps
Define imports and create the clients
import { createSmartAccountClient } from "permissionless"
import { Address, Hex, createPublicClient, encodeFunctionData, getContract, http, parseEther, toHex } from "viem"
import { sepolia } from "viem/chains"
import { privateKeyToAccount } from "viem/accounts"
import { MagicSpendWithdrawalManagerAbi } from "./abi/MagicSpendWithdrawalManager";
import { entryPoint07Address } from "viem/account-abstraction";
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { toSimpleSmartAccount } from "permissionless/accounts";
import "dotenv/config"
const RPC_URL = "https://11155111.rpc.thirdweb.com"
const ETH: Address = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
const amount = "0.001"; // 0.001 ETH
const PRIVATE_KEY = process.env.ACCOUNT_PRIVATE_KEY;
if (PRIVATE_KEY === undefined) {
throw new Error("ACCOUNT_PRIVATE_KEY env var is required")
}
const PIMLICO_URL = process.env.PIMLICO_URL;
if (PIMLICO_URL === undefined) {
throw new Error("PIMLICO_URL env var is required")
}
export const publicClient = createPublicClient({
transport: http(RPC_URL),
chain: sepolia,
})
const pimlicoClient = createPimlicoClient({
transport: http(PIMLICO_URL),
chain: sepolia,
entryPoint: {
address: entryPoint07Address,
version: "0.7",
},
})
const sendMagicSpendRequest = async (method: string, params: any[]) => {
const body = {
jsonrpc: "2.0",
method,
params,
id: 1,
};
const response = await fetch(PIMLICO_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
const j = await response.json();
// @ts-ignore
return j.result;
}
// This is the address of the account that owns the stake
const signer = privateKeyToAccount(PRIVATE_KEY as Hex)
console.log(`Signer address: ${signer.address}`)
const simpleAccount = await toSimpleSmartAccount({
client: publicClient,
owner: signer,
entryPoint: {
address: entryPoint07Address,
version: "0.7",
},
});
const smartAccountClient = createSmartAccountClient({
account: simpleAccount,
chain: sepolia,
bundlerTransport: http(PIMLICO_URL, {
timeout: 60_000,
}),
userOperation: {
estimateFeesPerGas: async () => {
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
},
paymaster: pimlicoClient,
})
Fetch MagicSpend contract address
const {
withdrawalManagerAddress,
} = await sendMagicSpendRequest(
'pimlico_getMagicSpendContracts',
[]
)
Sponsor the Withdrawal
const [withdrawal, withdrawalSignature] = await sendMagicSpendRequest(
"pimlico_sponsorMagicSpendWithdrawal",
[{
recipient: simpleAccount.address,
token: ETH,
amount: toHex(parseEther(amount)),
salt: "0x0",
signature: "0x0",
}, null]
)
Execute the Withdrawal
const magicSpendCallData = encodeFunctionData({
abi: MagicSpendWithdrawalManagerAbi,
functionName: 'withdraw',
args: [
withdrawal,
withdrawalSignature,
]
})
// Send user operation and withdraw funds
// You can add subsequent calls after the withdrawal, like "buy NFT on OpenSea for ETH"
const userOpHash = await smartAccountClient.sendUserOperation({
account: simpleAccount,
calls: [
{
to: withdrawalManagerAddress,
value: parseEther("0"),
data: magicSpendCallData,
}
]
})
console.log(`Userop hash: ${userOpHash}`);
const receipt = await pimlicoClient.waitForUserOperationReceipt({
hash: userOpHash
})
console.log(`Transaction hash: ${receipt.receipt.transactionHash}`);