Executing a transaction
Step 1 — Build ERC‑20 Transfer Transaction Calldata
import { erc20Abi, encodeFunctionData, parseUnits } from 'viem';
const chainId = 137;
const usdc = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
const recipient = '0x1111111111111111111111111111111111111111';
// 1. Convert 1.0 USDC to base units (6 decimals)
const amount = parseUnits('1.0', 6);
// 2. Encode transfer(to, amount) calldata
const encodedData = encodeFunctionData({
abi: erc20Abi,
functionName: 'transfer',
args: [recipient, amount],
});import { Interface, parseUnits } from 'ethers';
const chainId = 137;
const usdc = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
const recipient = '0x1111111111111111111111111111111111111111';
const erc20 = new Interface([
'function transfer(address to, uint256 amount) returns (bool)'
]);
const amount = parseUnits('1.0', 6); // 1 USDC -> 1_000_000
const encodedData = erc20.encodeFunctionData('transfer', [recipient, amount]);Step 2 — Build the chain abstracted transaction (with quote type + token addresses)
import axios from 'axios';
const chainId = 137; // Polygon
// Token the user settles in
const settlementToken = 'USDC';
// Token the user spends
const spendingTokenAddress = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
// For a transfer, spending and settlement tokens are both USDC.
// Use 'AMOUNT_OUT' to send an exact recipient amount; 'AMOUNT_IN' to spend an exact input amount.
const quoteType = 'AMOUNT_OUT'; // or 'AMOUNT_IN'
const amount = '1000000'; // Formatted amount (USDC uses 6 decimals)
const { data: transactionData } = await axios.post('https://api.enclave.money/magicspend/build-transaction', {
chainId,
transactions: [{
to: usdc
// encodedData comes from Step 1 (ERC-20 transfer calldata built via viem/ethers)
data: encodedData
}],
quoteType, // 'AMOUNT_OUT' | 'AMOUNT_IN'
amount, // '1.0' => 1 USDC
spendingTokenAddress, // token user spends
settlementToken // token user settle in (ex. 'USDC' or 'SOL')
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': process.env.ENCLAVE_API_KEY,
}
});
const {
userOpHash,
authorizations,
settlementPlan,
expiryTimestamp,
transactionId
} = transactionData;Token
Identifier
Supported Networks
Step 3 — Initialize Privy and sign the user operation and authorizations
Step 4 — Sign EIP‑7702 authorization(s)
Step 5 - Submitting the transaction
Step 6 - Checking the status of the transaction
Status Code
Description
Last updated