|
| 1 | +// Env example: to be set in the .env file |
| 2 | +/* |
| 3 | +REGISTRAR_PRIVATE_KEY=your-eth-private-key |
| 4 | +L2_RPC_URL=https://sepolia.era.zksync.dev |
| 5 | +PAYMASTER_TEST_ADDR=0x76f03aD8AA376385e45221d45371DeB51D597c43 |
| 6 | +L2_CHAIN_ID=300 |
| 7 | +ZYFI_BASE_URL=https://api.zyfi.org/api/ |
| 8 | +ZYFI_SPONSORED=erc20_sponsored_paymaster/v1 |
| 9 | +ZYFI_API_KEY=your-zyfi-api-key |
| 10 | +FEE_TOKEN_ADDR=0xb4B74C2BfeA877672B938E408Bae8894918fE41C |
| 11 | +GAS_LIMIT=1000000 |
| 12 | +*/ |
| 13 | +// Usage example: source .env && npx ts-node src/testPaymaster.ts 0x2E7F3926Ae74FDCDcAde2c2AB50990C5daFD42bD alex |
| 14 | + |
| 15 | +import { Interface, getAddress, Contract } from "ethers"; |
| 16 | +import { Provider as L2Provider, Wallet } from "zksync-ethers"; |
| 17 | + |
| 18 | +// L2 Contract |
| 19 | +export const PAYMASTER_TEST_INTERFACE = new Interface([ |
| 20 | + "function register(address to, string memory name)", |
| 21 | +]); |
| 22 | + |
| 23 | +import dotenv from "dotenv"; |
| 24 | +dotenv.config(); |
| 25 | + |
| 26 | +const to: string = getAddress(process.argv[2]); |
| 27 | +const name: string = process.argv[3]; |
| 28 | +const privateKey = process.env.REGISTRAR_PRIVATE_KEY!; |
| 29 | +const l2Provider = new L2Provider(process.env.L2_RPC_URL!); |
| 30 | +const l2Wallet = new Wallet(privateKey, l2Provider); |
| 31 | +const paymasterTestAddress = getAddress(process.env.PAYMASTER_TEST_ADDR!); |
| 32 | + |
| 33 | +const paymasterTestContract = new Contract( |
| 34 | + paymasterTestAddress, |
| 35 | + PAYMASTER_TEST_INTERFACE, |
| 36 | + l2Wallet, |
| 37 | +); |
| 38 | +const zyfiSponsoredUrl = process.env.ZYFI_BASE_URL |
| 39 | + ? new URL(process.env.ZYFI_SPONSORED!, process.env.ZYFI_BASE_URL) |
| 40 | + : null; |
| 41 | + |
| 42 | +type ZyfiSponsoredRequest = { |
| 43 | + chainId: number; |
| 44 | + feeTokenAddress: string; |
| 45 | + gasLimit: string; |
| 46 | + isTestnet: boolean; |
| 47 | + checkNft: boolean; |
| 48 | + txData: { |
| 49 | + from: string; |
| 50 | + to: string; |
| 51 | + value: string; |
| 52 | + data: string; |
| 53 | + }; |
| 54 | + sponsorshipRatio: number; |
| 55 | + replayLimit: number; |
| 56 | +}; |
| 57 | + |
| 58 | +interface ZyfiSponsoredResponse { |
| 59 | + txData: { |
| 60 | + chainId: number; |
| 61 | + from: string; |
| 62 | + to: string; |
| 63 | + value: string; |
| 64 | + data: string; |
| 65 | + customData: { |
| 66 | + paymasterParams: { |
| 67 | + paymaster: string; |
| 68 | + paymasterInput: string; |
| 69 | + }; |
| 70 | + gasPerPubdata: number; |
| 71 | + }; |
| 72 | + maxFeePerGas: string; |
| 73 | + gasLimit: number; |
| 74 | + }; |
| 75 | + gasLimit: string; |
| 76 | + gasPrice: string; |
| 77 | + tokenAddress: string; |
| 78 | + tokenPrice: string; |
| 79 | + feeTokenAmount: string; |
| 80 | + feeTokenDecimals: string; |
| 81 | + feeUSD: string; |
| 82 | + markup: string; |
| 83 | + expirationTime: string; |
| 84 | + expiresIn: string; |
| 85 | + maxNonce: string; |
| 86 | + protocolAddress: string; |
| 87 | + sponsorshipRatio: string; |
| 88 | + warnings: string[]; |
| 89 | +} |
| 90 | + |
| 91 | +async function fetchZyfiSponsored( |
| 92 | + request: ZyfiSponsoredRequest, |
| 93 | +): Promise<ZyfiSponsoredResponse> { |
| 94 | + console.log(`zyfiSponsoredUrl: ${zyfiSponsoredUrl}`); |
| 95 | + const response = await fetch(zyfiSponsoredUrl!, { |
| 96 | + method: "POST", |
| 97 | + headers: { |
| 98 | + "Content-Type": "application/json", |
| 99 | + "X-API-KEY": process.env.ZYFI_API_KEY!, |
| 100 | + }, |
| 101 | + body: JSON.stringify(request), |
| 102 | + }); |
| 103 | + |
| 104 | + if (!response.ok) { |
| 105 | + throw new Error(`Failed to fetch zyfi sponsored`); |
| 106 | + } |
| 107 | + const sponsoredResponse = (await response.json()) as ZyfiSponsoredResponse; |
| 108 | + |
| 109 | + return sponsoredResponse; |
| 110 | +} |
| 111 | + |
| 112 | +const zyfiRequestTemplate: ZyfiSponsoredRequest = { |
| 113 | + chainId: Number(process.env.L2_CHAIN_ID!), |
| 114 | + feeTokenAddress: process.env.FEE_TOKEN_ADDR!, |
| 115 | + gasLimit: process.env.GAS_LIMIT!, |
| 116 | + isTestnet: process.env.L2_CHAIN_ID === "300", |
| 117 | + checkNft: false, |
| 118 | + txData: { |
| 119 | + from: l2Wallet.address, |
| 120 | + to: paymasterTestAddress, |
| 121 | + data: "0x0", |
| 122 | + value: "0", |
| 123 | + }, |
| 124 | + sponsorshipRatio: 100, |
| 125 | + replayLimit: 5, |
| 126 | +}; |
| 127 | + |
| 128 | +async function main(to: string, name: string): Promise<void> { |
| 129 | + let response; |
| 130 | + if (zyfiSponsoredUrl) { |
| 131 | + const encodedRegister = PAYMASTER_TEST_INTERFACE.encodeFunctionData( |
| 132 | + "register", |
| 133 | + [to, name], |
| 134 | + ); |
| 135 | + const zyfiRequest: ZyfiSponsoredRequest = { |
| 136 | + ...zyfiRequestTemplate, |
| 137 | + txData: { |
| 138 | + ...zyfiRequestTemplate.txData, |
| 139 | + data: encodedRegister, |
| 140 | + }, |
| 141 | + }; |
| 142 | + const zyfiResponse = await fetchZyfiSponsored(zyfiRequest); |
| 143 | + console.log(`ZyFi response: ${JSON.stringify(zyfiResponse)}`); |
| 144 | + |
| 145 | + response = await l2Wallet.sendTransaction(zyfiResponse.txData); |
| 146 | + } else { |
| 147 | + response = await paymasterTestContract.register(to, name); |
| 148 | + } |
| 149 | + |
| 150 | + const receipt = await response.wait(); |
| 151 | + if (receipt.status !== 1) { |
| 152 | + throw new Error("Transaction failed"); |
| 153 | + } |
| 154 | + console.log(`Transaction hash: ${receipt.hash}`); |
| 155 | +} |
| 156 | + |
| 157 | +main(to, name); |
0 commit comments