@parifi/sdk
Version:
Parifi SDK with common utility functions
127 lines (123 loc) • 4.98 kB
JavaScript
;
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/pimlico/utils.ts
var utils_exports = {};
__export(utils_exports, {
executeBatchTxsUsingPimlico: () => executeBatchTxsUsingPimlico,
executeTxUsingPimlico: () => executeTxUsingPimlico,
getPimlicoSmartAccountClient: () => getPimlicoSmartAccountClient,
getViemChainById: () => getViemChainById
});
module.exports = __toCommonJS(utils_exports);
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 executeBatchTxsUsingPimlico = async (smartAccountClient, targetContractAddresses, txDatas) => {
const batchTxDatas = [];
if (targetContractAddresses.length != txDatas.length) {
throw new Error("Target contract and data lengths do not match");
}
for (let index = 0; index < targetContractAddresses.length; index++) {
batchTxDatas.push({
to: targetContractAddresses[index],
value: 0n,
data: txDatas[index]
});
}
const txHash = await smartAccountClient.sendTransactions({
transactions: batchTxDatas
});
return { txHash };
};
var getViemChainById = (chainId) => {
if (chainId === 42161) {
return import_chains.arbitrum;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
executeBatchTxsUsingPimlico,
executeTxUsingPimlico,
getPimlicoSmartAccountClient,
getViemChainById
});
//# sourceMappingURL=utils.js.map