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:

  1. walletAddress: The user's EVM or Solana wallet address

  2. chainId: Chain ID of the target chain

  3. amount: How much they want to spend on the target chain

  4. type: The type of quote the user is fetching. Learn more about quote types here.

  5. settlementToken: The token the user will settle in (ex. USDC or SOL)

  6. spendingTokenAddress: The token the user wants to borrow and spend on the target chain

  7. 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
}

Note on specifying spending amount: The client specifies how much of a given token the user wants to spend. In the case where are user has 5$ on Base, but wants to lend a total of 10$ to a vault on Base, the client would set the amount field to 10$. Enclave will lend the user the 5$ deficit and fees will only be charged on the amount the user ends up borrowing.

Last updated