import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});
async function sendDelegatedTransaction(
credentials: DelegationCredentials,
to: string,
value: bigint
) {
const fromAddress = await getWalletAddress(credentials.walletId);
const nonce = await publicClient.getTransactionCount({
address: fromAddress,
});
const gasEstimate = await publicClient.estimateGas({
account: fromAddress,
to,
value,
});
const feeData = await publicClient.estimateFeesPerGas();
const transaction: TransactionSerializable = {
to,
value,
chainId: mainnet.id,
nonce,
gasLimit: gasEstimate,
maxFeePerGas: feeData.maxFeePerGas!,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas!,
};
const signedTx = await delegatedSignTransaction(delegatedClient, {
walletId: credentials.walletId,
walletApiKey: credentials.walletApiKey,
keyShare: credentials.keyShare,
transaction,
});
const txHash = await publicClient.sendRawTransaction({
serializedTransaction: signedTx as `0x${string}`,
});
return txHash;
}