@0xfacet/sdk
Version:
A toolkit for Facet blockchain integration.
62 lines (61 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendFacetTransaction = sendFacetTransaction;
const actions_1 = require("viem/actions");
const utils_1 = require("viem/utils");
const utils_2 = require("../utils");
/**
* Sends a transaction through the Facet protocol.
*
* This function builds a Facet transaction using the provided parameters and sends it using
* the viem client. It handles the complexities of creating L2 transactions on the Facet network.
*
* @template chain - The chain type parameter
* @template account - The account type parameter
* @template request - The request type parameter
* @template chainOverride - Optional chain override type parameter
*
* @param client - The viem client instance used to interact with the blockchain
* @param parameters - The transaction parameters, following viem's SendTransactionParameters format
* @param parameters.data - The transaction calldata
* @param parameters.to - The recipient address
* @param parameters.value - The amount of ether to send
* @param parameters.mineBoost - Optional hex to increase FCT mining amount
*
* @returns A promise that resolves to the transaction hash
*
* @throws Will throw and properly format any errors that occur during transaction sending
*
* @example
* const hash = await sendFacetTransaction(client, {
* to: '0x...',
* value: parseEther('0.1'),
* data: '0x...',
* mineBoost: '0x1' // Optional: increase FCT mining amount
* });
*/
async function sendFacetTransaction(client, parameters) {
try {
const chain = (parameters.chain ?? client.chain);
const accountOrAddress = parameters.account ?? client.account;
const account = accountOrAddress ? (0, utils_1.parseAccount)(accountOrAddress) : null;
const { facetTransactionHash } = await (0, utils_2.sendRawFacetTransaction)(chain.id, account.address, {
data: parameters.data,
to: parameters.to,
value: parameters.value,
mineBoost: parameters.mineBoost,
}, ({ chainId, ...l1Transaction }) => (0, actions_1.sendTransaction)(client, {
...l1Transaction,
chain,
account,
}));
return facetTransactionHash;
}
catch (err) {
throw (0, utils_1.getTransactionError)(err, {
...parameters,
account: client.account || null,
chain: parameters.chain || undefined,
});
}
}