UNPKG

@reservoir0x/relay-sdk

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

69 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getQuote = void 0; const axios_js_1 = require("../utils/axios.js"); const viem_1 = require("viem"); const prepareCallTransaction_js_1 = require("../utils/prepareCallTransaction.js"); const index_js_1 = require("../utils/index.js"); const viemWallet_js_1 = require("../utils/viemWallet.js"); const client_js_1 = require("../client.js"); const address_js_1 = require("../constants/address.js"); async function getQuote(parameters) { const { toChainId, toCurrency, wallet, chainId, currency, tradeType, amount = '0', recipient, options, txs } = parameters; const client = (0, client_js_1.getClient)(); if (!client.baseApiUrl || !client.baseApiUrl.length) { throw new ReferenceError('RelayClient missing api url configuration'); } let adaptedWallet; let caller; if (wallet) { adaptedWallet = (0, viemWallet_js_1.isViemWalletClient)(wallet) ? (0, index_js_1.adaptViemWallet)(wallet) : wallet; caller = await adaptedWallet.address(); } let preparedTransactions; if (txs && txs.length > 0) { preparedTransactions = txs.map((tx) => { if ((0, index_js_1.isSimulateContractRequest)(tx)) { return (0, prepareCallTransaction_js_1.default)(tx); } return tx; }); } const fromChain = client.chains.find((chain) => chain.id === chainId); const toChain = client.chains.find((chain) => chain.id === toChainId); const originDeadAddress = fromChain ? (0, address_js_1.getDeadAddress)(fromChain.vmType, fromChain.id) : undefined; const destinationDeadAddress = toChain ? (0, address_js_1.getDeadAddress)(toChain.vmType, toChain.id) : undefined; const query = { user: caller || originDeadAddress || viem_1.zeroAddress, destinationCurrency: toCurrency, destinationChainId: toChainId, originCurrency: currency, originChainId: chainId, amount, recipient: recipient ? recipient : caller || destinationDeadAddress || viem_1.zeroAddress, tradeType, referrer: client.source || undefined, txs: preparedTransactions, ...options }; const request = { url: `${client.baseApiUrl}/quote`, method: 'post', data: query }; const res = await axios_js_1.axios.request(request); if (res.status !== 200) { throw new index_js_1.APIError(res?.data?.message, res.status, res.data); } return { ...res.data, request }; } exports.getQuote = getQuote; //# sourceMappingURL=getQuote.js.map