@alchemy/aa-core
Version:
viem based SDK that enables interactions with ERC-4337 Smart Accounts. ABIs are based off the definitions generated in @account-abstraction/contracts
119 lines • 5.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSimpleSmartAccount = void 0;
const viem_1 = require("viem");
const SimpleAccountAbi_v6_js_1 = require("../abis/SimpleAccountAbi_v6.js");
const SimpleAccountAbi_v7_js_1 = require("../abis/SimpleAccountAbi_v7.js");
const SimpleAccountFactoryAbi_js_1 = require("../abis/SimpleAccountFactoryAbi.js");
const bundlerClient_js_1 = require("../client/bundlerClient.js");
const index_js_1 = require("../entrypoint/index.js");
const account_js_1 = require("../errors/account.js");
const defaults_js_1 = require("../utils/defaults.js");
const base_js_1 = require("./base.js");
const schema_js_1 = require("./schema.js");
const smartContractAccount_js_1 = require("./smartContractAccount.js");
class SimpleSmartContractAccount extends base_js_1.BaseSmartContractAccount {
constructor(params) {
(0, schema_js_1.SimpleSmartAccountParamsSchema)().parse(params);
const client = (0, bundlerClient_js_1.createBundlerClient)({
transport: params.transport,
chain: params.chain,
});
super({ ...params, rpcClient: client });
Object.defineProperty(this, "index", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "entryPointVersion", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.signer = params.signer;
this.index = params.salt ?? 0n;
this.entryPointVersion =
params.entryPointVersion ?? index_js_1.defaultEntryPointVersion;
}
getDummySignature() {
return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
}
async encodeExecute(target, value, data) {
return (0, viem_1.encodeFunctionData)({
abi: this.entryPointVersion === "0.6.0"
? SimpleAccountAbi_v6_js_1.SimpleAccountAbi_v6
: SimpleAccountAbi_v7_js_1.SimpleAccountAbi_v7,
functionName: "execute",
args: [target, value, data],
});
}
async encodeBatchExecute(txs) {
const [targets, datas] = txs.reduce((accum, curr) => {
accum[0].push(curr.target);
accum[1].push(curr.data);
return accum;
}, [[], []]);
return (0, viem_1.encodeFunctionData)({
abi: this.entryPointVersion === "0.6.0"
? SimpleAccountAbi_v6_js_1.SimpleAccountAbi_v6
: SimpleAccountAbi_v7_js_1.SimpleAccountAbi_v7,
functionName: "executeBatch",
args: [targets, datas],
});
}
signMessage(msg) {
return this.signer.signMessage(typeof msg === "string" && !(0, viem_1.isHex)(msg) ? msg : { raw: msg });
}
async getAccountInitCode() {
return (0, viem_1.concatHex)([
this.factoryAddress,
(0, viem_1.encodeFunctionData)({
abi: SimpleAccountFactoryAbi_js_1.SimpleAccountFactoryAbi,
functionName: "createAccount",
args: [await this.signer.getAddress(), this.index],
}),
]);
}
}
async function createSimpleSmartAccount({ chain, entryPoint = (0, index_js_1.getEntryPoint)(chain), factoryAddress = (0, defaults_js_1.getDefaultSimpleAccountFactoryAddress)(chain, entryPoint.version), ...params }) {
if (!params.signer)
throw new account_js_1.AccountRequiresOwnerError("SimpleAccount");
const simpleAccount = new SimpleSmartContractAccount((0, schema_js_1.SimpleSmartAccountParamsSchema)().parse({
chain,
entryPointAddress: entryPoint.address,
factoryAddress,
...params,
}));
const parsedParams = (0, schema_js_1.SimpleSmartAccountParamsSchema)().parse({
chain,
entryPointAddress: entryPoint.address,
entryPointVersion: entryPoint.version,
factoryAddress,
...params,
});
const base = await (0, smartContractAccount_js_1.toSmartContractAccount)({
source: "SimpleAccount",
transport: params.transport,
chain,
encodeBatchExecute: simpleAccount.encodeBatchExecute.bind(simpleAccount),
encodeExecute: (tx) => simpleAccount.encodeExecute.bind(simpleAccount)(tx.target, tx.value ?? 0n, tx.data),
entryPoint,
getAccountInitCode: async () => {
if (parsedParams.initCode)
return parsedParams.initCode;
return simpleAccount.getAccountInitCode();
},
getDummySignature: simpleAccount.getDummySignature.bind(simpleAccount),
signMessage: ({ message }) => simpleAccount.signMessage(typeof message === "string" ? message : message.raw),
signTypedData: simpleAccount.signTypedData.bind(simpleAccount),
accountAddress: parsedParams.accountAddress,
});
return {
...base,
getSigner: () => simpleAccount.getSigner(),
};
}
exports.createSimpleSmartAccount = createSimpleSmartAccount;
//# sourceMappingURL=simple.js.map