UNPKG

@parifi/sdk

Version:

Parifi SDK with common utility functions

190 lines (181 loc) 7.14 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/relayers/index.ts var relayers_exports = {}; __export(relayers_exports, { Gelato: () => Gelato, Pimlico: () => Pimlico }); module.exports = __toCommonJS(relayers_exports); // src/relayers/gelato/gelato-function.ts var import_relay_sdk = require("@gelatonetwork/relay-sdk"); var executeTxUsingGelato = async (targetContractAddress, chainId, gelatoKey, encodedTxData, gelatoGasLimit) => { const request = { chainId: BigInt(chainId.toString()), target: targetContractAddress, data: encodedTxData }; const relay = new import_relay_sdk.GelatoRelay(); const relayOptions = { gasLimit: gelatoGasLimit || BigInt(5e6) }; const { taskId } = await relay.sponsoredCall(request, gelatoKey || "", relayOptions); return taskId; }; var checkGelatoTaskStatus = async (taskId) => { const relay = new import_relay_sdk.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 var import_config2 = require("dotenv/config"); // src/relayers/pimlico/utils.ts var import_config = require("dotenv/config"); var import_chains = require("viem/chains"); var import_permissionless = require("permissionless"); var import_accounts = require("permissionless/accounts"); var import_viem2 = require("viem"); var import_pimlico = require("permissionless/clients/pimlico"); // src/common/constants.ts var import_decimal = require("decimal.js"); var PRECISION_MULTIPLIER = new import_decimal.Decimal("10000"); var DEVIATION_PRECISION_MULTIPLIER = new import_decimal.Decimal(10).pow(12); var SECONDS_IN_A_YEAR = new import_decimal.Decimal(365 * 24 * 60 * 60); var MAX_FEE = new import_decimal.Decimal(1e7); var WAD = new import_decimal.Decimal(10).pow(18); var DECIMAL_10 = new import_decimal.Decimal(10); var DECIMAL_ZERO = new import_decimal.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 var import_viem = require("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 = (0, import_viem2.createPublicClient)({ transport: (0, import_viem2.http)(rpcConfig.rpcEndpointUrl) }); const paymasterClient = (0, import_pimlico.createPimlicoPaymasterClient)({ transport: (0, import_viem2.http)(paymasterUrl), entryPoint: import_permissionless.ENTRYPOINT_ADDRESS_V07 }); const account = await (0, import_accounts.privateKeyToSimpleSmartAccount)(publicClient, { privateKey, entryPoint: import_permissionless.ENTRYPOINT_ADDRESS_V07, factoryAddress: FACTORY_ADDRESS_SIMPLE_ACCOUNT }); const bundlerUrl = `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${apiKey}`; const bundlerClient = (0, import_pimlico.createPimlicoBundlerClient)({ transport: (0, import_viem2.http)(bundlerUrl), entryPoint: import_permissionless.ENTRYPOINT_ADDRESS_V07 }); const smartAccountClient = (0, import_permissionless.createSmartAccountClient)({ account, entryPoint: import_permissionless.ENTRYPOINT_ADDRESS_V07, chain: viemChain, bundlerTransport: (0, import_viem2.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 import_chains.arbitrum; } }; // src/relayers/pimlico/index.ts var import_accounts2 = require("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 = (0, import_accounts2.generatePrivateKey)(); this.pimlicoConfig.password = pk; return pk; })(); this.smartAccountClient = await getPimlicoSmartAccountClient(this.pimlicoConfig, this.rpcConfig, privateKey); this.isInitialized = true; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Gelato, Pimlico }); //# sourceMappingURL=index.js.map