@parifi/sdk
Version:
Parifi SDK with common utility functions
99 lines (96 loc) • 3.47 kB
JavaScript
// 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 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 arbitrum;
}
};
export {
executeBatchTxsUsingPimlico,
executeTxUsingPimlico,
getPimlicoSmartAccountClient,
getViemChainById
};
//# sourceMappingURL=utils.mjs.map