Fetching a quote
Let's fetch a quote for executing a chain abstracted batch of transactions
Fetching a quote requires the client to specify the following inputs:
walletAddress: The user's EVM or Solana wallet address
chainId: Chain ID of the target chain
amount: How much they want to spend on the target chain
type: The type of quote the user is fetching. Learn more about quote types here.
settlementToken: The token the user will settle in (ex. USDC or SOL)
spendingTokenAddress: The token the user wants to borrow and spend on the target chain
transactions: A list of transaction objects representing the actions the user wants to execute on the target chain. These could be any type of transaction including transfers, swaps, depositing to yield protocols, buying an NFT, etc.
Importing libraries
import axios from 'axios';
Defining types
enum QuoteType {
AMOUNT_IN = 'AMOUNT_IN',
AMOUNT_OUT = 'AMOUNT_OUT'
}
type Transaction = {
to: string; // destination contract/address (0x-prefixed)
data: string; // calldata (0x-prefixed)
value: string; // value in wei (as string)
};
type GetQuoteParams = {
// Solution inputs
walletAddress: string; // registered EVM address or Solana Address
chainId: number; // chain ID
amount: string; // smallest units, e.g. "200000" for 0.2 USDC
type: QuoteType; // borrow mode (AMOUNT_IN or AMOUNT_OUT)
settlementToken?: string | null; // 'SOL' for SOL settlement, USDC by default
spendingTokenAddress?: string | null; // USDC by default, specify address of token on the chain corresponding to the given chainId
// New: explicit call bundle the solver should execute
transactions?: Transaction[]; // [{ to, data, value }, ...]
};
type QuoteResponse = type Quote = {
chainId: number;
tokenAddress: string;
// Amount the user pays from source chains
settlementAmount: string;
// Expected amount of tokens the user will have on the target chain after borrowing
expectedAmountOut: string;
// Map of chain IDs to amounts to be settled from the corresponding chain
settlementPlan: Record<string, string>;
};
Setting up request headers
const headers = {
'Content-Type': 'application/json',
'Authorization': process.env.ENCLAVE_API_KEY,
};
Calling the quote API
export async function getQuote(params: GetQuoteParams): Promise<QuoteResponse> {
const {
userId,
walletAddress,
outputNetwork,
amount,
type,
settlementToken = null, // USDC by default
spendingTokenAddress = null, // USDC by default
transactions = [],
} = params;
const { data } = await axios.post(
`https://api.enclave.money/magicspend/quote`,
{
userId,
walletAddress,
outputNetwork,
amount,
type,
settlementToken,
spendingToken,
transactions, // [{ to, data, value }]
},
{
headers: headers
}
);
const {
expectedAmountOut,
settlementAmount,
settlementPlan
}
console.log(`User receives: ${expectedAmountOut}`);
console.log(`User pays: ${settlementAmount}`);
console.log(`Payment split between networks: ${settlementPlan}`);
return data
}
Last updated