UNPKG

@parifi/sdk

Version:

Parifi SDK with common utility functions

164 lines (157 loc) 5.7 kB
// src/relayers/gelato/gelato-function.ts import { GelatoRelay } from "@gelatonetwork/relay-sdk"; var executeTxUsingGelato = async (targetContractAddress, chainId, gelatoKey, encodedTxData, gelatoGasLimit) => { const request = { chainId: BigInt(chainId.toString()), target: targetContractAddress, data: encodedTxData }; const relay = new GelatoRelay(); const relayOptions = { gasLimit: gelatoGasLimit || BigInt(5e6) }; const { taskId } = await relay.sponsoredCall(request, gelatoKey || "", relayOptions); return taskId; }; var checkGelatoTaskStatus = async (taskId) => { const relay = new GelatoRelay(); const txStatus = await relay.getTaskStatus(taskId); return txStatus; }; // src/relayers/gelato/index.ts var Gelato = class { constructor(gelatoConfig, rpcConfig) { this.gelatoConfig = gelatoConfig; this.rpcConfig = rpcConfig; this.checkGelatoTaskStatus = (taskId) => { return checkGelatoTaskStatus(taskId); }; } async executeTxUsingGelato(targetContractAddress, encodedTxData, gelatoGasLimit) { return await executeTxUsingGelato( targetContractAddress, this.rpcConfig.chainId, this.gelatoConfig?.apiKey, encodedTxData, gelatoGasLimit ); } }; // src/relayers/pimlico/index.ts import "dotenv/config"; // src/relayers/pimlico/utils.ts import "dotenv/config"; import { arbitrum } from "viem/chains"; import { ENTRYPOINT_ADDRESS_V07, createSmartAccountClient } from "permissionless"; import { privateKeyToSimpleSmartAccount } from "permissionless/accounts"; import { createPublicClient, http } from "viem"; import { createPimlicoBundlerClient, createPimlicoPaymasterClient } from "permissionless/clients/pimlico"; // src/common/constants.ts import { Decimal } from "decimal.js"; var PRECISION_MULTIPLIER = new Decimal("10000"); var DEVIATION_PRECISION_MULTIPLIER = new Decimal(10).pow(12); var SECONDS_IN_A_YEAR = new Decimal(365 * 24 * 60 * 60); var MAX_FEE = new Decimal(1e7); var WAD = new Decimal(10).pow(18); var DECIMAL_10 = new Decimal(10); var DECIMAL_ZERO = new Decimal(0); var BIGINT_ZERO = BigInt(0); var ONE_GWEI = 1e9; var DEFAULT_GAS_PRICE = 2 * ONE_GWEI; var FACTORY_ADDRESS_SIMPLE_ACCOUNT = "0x91E60e0613810449d098b0b5Ec8b51A0FE8c8985"; // src/common/helpers.ts import { formatEther, parseEther } from "viem"; // src/relayers/pimlico/utils.ts var getPimlicoSmartAccountClient = async (pimlicoConfig, rpcConfig, privateKey) => { const apiKey = pimlicoConfig.apiKey ?? ""; const viemChain = getViemChainById(rpcConfig.chainId); const chainId = rpcConfig.chainId; const paymasterUrl = `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${apiKey}`; const publicClient = createPublicClient({ transport: http(rpcConfig.rpcEndpointUrl) }); const paymasterClient = createPimlicoPaymasterClient({ transport: http(paymasterUrl), entryPoint: ENTRYPOINT_ADDRESS_V07 }); const account = await privateKeyToSimpleSmartAccount(publicClient, { privateKey, entryPoint: ENTRYPOINT_ADDRESS_V07, factoryAddress: FACTORY_ADDRESS_SIMPLE_ACCOUNT }); const bundlerUrl = `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${apiKey}`; const bundlerClient = createPimlicoBundlerClient({ transport: http(bundlerUrl), entryPoint: ENTRYPOINT_ADDRESS_V07 }); const smartAccountClient = createSmartAccountClient({ account, entryPoint: ENTRYPOINT_ADDRESS_V07, chain: viemChain, bundlerTransport: http(bundlerUrl), middleware: { gasPrice: async () => { return (await bundlerClient.getUserOperationGasPrice()).fast; }, sponsorUserOperation: paymasterClient.sponsorUserOperation } }); return smartAccountClient; }; var executeTxUsingPimlico = async (smartAccountClient, targetContractAddress, txData) => { const txHash = await smartAccountClient.sendTransaction({ to: targetContractAddress, value: 0n, data: txData }); return { txHash }; }; var getViemChainById = (chainId) => { if (chainId === 42161) { return arbitrum; } }; // src/relayers/pimlico/index.ts import { generatePrivateKey } from "viem/accounts"; var Pimlico = class { constructor(pimlicoConfig, rpcConfig, subgraphConfig) { this.pimlicoConfig = pimlicoConfig; this.rpcConfig = rpcConfig; this.subgraphConfig = subgraphConfig; //////////////////////////////////////////////////////////////// //////////////////// PUBLIC FUNCTIONS //////////////////// //////////////////////////////////////////////////////////////// this.executeTxUsingPimlico = async (targetContractAddress, txData) => { return await executeTxUsingPimlico(this.smartAccountClient, targetContractAddress, txData); }; this.isInitialized = false; this.smartAccountClient = {}; } //////////////////////////////////////////////////////////////// ////////////////////// INITIALIZER /////////////////////// //////////////////////////////////////////////////////////////// async initPimlico() { if (this.isInitialized) { console.log("Pimlico relayer already initialized"); return; } if (this.pimlicoConfig?.apiKey === void 0 || this.rpcConfig.rpcEndpointUrl === void 0) { console.log("Invalid config for Pimlico"); return; } const privateKey = (process.env.PRIVATE_KEY || this.pimlicoConfig.password) ?? (() => { const pk = generatePrivateKey(); this.pimlicoConfig.password = pk; return pk; })(); this.smartAccountClient = await getPimlicoSmartAccountClient(this.pimlicoConfig, this.rpcConfig, privateKey); this.isInitialized = true; } }; export { Gelato, Pimlico }; //# sourceMappingURL=index.mjs.map