@account-kit/infra
Version:
adapters for @aa-sdk/core for interacting with alchemy services
89 lines • 3.94 kB
JavaScript
import { ChainNotFoundError, clientHeaderTrack, createSmartAccountClient, isSmartAccountWithSigner, } from "@aa-sdk/core";
import {} from "viem";
import { alchemy, convertHeadersToObject, } from "../alchemyTransport.js";
import { getDefaultUserOperationFeeOptions } from "../defaults.js";
import { alchemyFeeEstimator } from "../middleware/feeEstimator.js";
import { alchemyGasAndPaymasterAndDataMiddleware } from "../middleware/gasManager.js";
import { alchemyUserOperationSimulator } from "../middleware/userOperationSimulator.js";
import { alchemyActions, } from "./decorators/smartAccount.js";
import { headersUpdate } from "../alchemyTrackerHeaders.js";
export function getSignerTypeHeader(account) {
return { "Alchemy-Aa-Sdk-Signer": account.getSigner().signerType };
}
/**
* Creates an Alchemy smart account client using the provided configuration options, including account details, gas manager configuration, and custom middleware.
*
* @example
* ```ts
* import { createAlchemySmartAccountClient, alchemy } from "@account-kit/infra";
* import { sepolia } from "@account-kit/infra/chain";
*
* const client = createAlchemySmartAccountClient({
* chain: sepolia,
* transport: alchemy({ apiKey: "your-api-key" }),
* });
* ```
*
* @param {AlchemySmartAccountClientConfig} config The configuration for creating the Alchemy smart account client
* @returns {AlchemySmartAccountClient} An instance of `AlchemySmartAccountClient` configured based on the provided options
*/
export function createAlchemySmartAccountClient(config) {
if (!config.chain) {
throw new ChainNotFoundError();
}
const feeOptions = config.opts?.feeOptions ?? getDefaultUserOperationFeeOptions(config.chain);
const scaClient = createSmartAccountClient({
account: config.account,
transport: config.transport,
chain: config.chain,
type: "AlchemySmartAccountClient",
opts: {
...config.opts,
feeOptions,
},
feeEstimator: config.feeEstimator ?? alchemyFeeEstimator(config.transport),
gasEstimator: config.gasEstimator,
customMiddleware: async (struct, args) => {
if (isSmartAccountWithSigner(args.account)) {
config.transport.updateHeaders(getSignerTypeHeader(args.account));
}
return config.customMiddleware
? config.customMiddleware(struct, args)
: struct;
},
...(config.policyId
? alchemyGasAndPaymasterAndDataMiddleware({
policyId: config.policyId,
policyToken: config.policyToken,
transport: config.transport,
gasEstimatorOverride: config.gasEstimator,
feeEstimatorOverride: config.feeEstimator,
})
: {}),
userOperationSimulator: config.useSimulation
? alchemyUserOperationSimulator(config.transport)
: undefined,
signUserOperation: config.signUserOperation,
addBreadCrumb(breadcrumb) {
const oldConfig = config.transport.config;
const dynamicFetchOptions = config.transport.dynamicFetchOptions;
const newTransport = alchemy({ ...oldConfig });
newTransport.updateHeaders(headersUpdate(breadcrumb)(convertHeadersToObject(dynamicFetchOptions?.headers)));
return createAlchemySmartAccountClient({
...config,
transport: newTransport,
});
},
})
.extend(alchemyActions)
.extend((client_) => ({
addBreadcrumb(breadcrumb) {
return clientHeaderTrack(client_, breadcrumb);
},
}));
if (config.account && isSmartAccountWithSigner(config.account)) {
config.transport.updateHeaders(getSignerTypeHeader(config.account));
}
return scaClient;
}
//# sourceMappingURL=smartAccountClient.js.map