@reservoir0x/relay-sdk
Version:
Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.
92 lines • 3.36 kB
JavaScript
import {} from 'axios';
import { axios } from '../utils/axios.js';
import { zeroAddress } from 'viem';
import prepareCallTransaction from '../utils/prepareCallTransaction.js';
import { isSimulateContractRequest, APIError, adaptViemWallet } from '../utils/index.js';
import { isViemWalletClient } from '../utils/viemWallet.js';
import { getClient, RelayClient } from '../client.js';
import { getDeadAddress } from '../constants/address.js';
const getDefaultQuoteParameters = async (client, toChainId, chainId, recipient, adaptedWallet) => {
const fromChain = client.chains.find((chain) => chain.id === chainId);
const toChain = client.chains.find((chain) => chain.id === toChainId);
const originDeadAddress = fromChain
? getDeadAddress(fromChain.vmType, fromChain.id)
: undefined;
const destinationDeadAddress = toChain
? getDeadAddress(toChain.vmType, toChain.id)
: undefined;
let caller;
if (adaptedWallet) {
caller = await adaptedWallet.address();
}
return {
user: caller || originDeadAddress || zeroAddress,
recipient: recipient
? recipient
: caller || destinationDeadAddress || zeroAddress
};
};
/**
* Method to get a quote
* @param data - {@link GetQuoteParameters}
*/
export async function getQuote(parameters, includeDefaultParameters) {
const { toChainId, toCurrency, wallet, chainId, currency, tradeType, amount = '0', recipient, options, txs, user } = parameters;
const client = getClient();
if (!client.baseApiUrl || !client.baseApiUrl.length) {
throw new ReferenceError('RelayClient missing api url configuration');
}
let adaptedWallet;
if (wallet) {
adaptedWallet = isViemWalletClient(wallet)
? adaptViemWallet(wallet)
: wallet;
}
let preparedTransactions;
if (txs && txs.length > 0) {
preparedTransactions = txs.map((tx) => {
if (isSimulateContractRequest(tx)) {
return prepareCallTransaction(tx);
}
return tx;
});
}
let defaultParameters = undefined;
if (includeDefaultParameters) {
defaultParameters = await getDefaultQuoteParameters(client, toChainId, chainId, recipient, adaptedWallet);
}
if (!user && !defaultParameters?.user) {
throw new Error('User is required');
}
if (!recipient && !defaultParameters?.recipient) {
throw new Error('Recipient is required');
}
const query = {
user: includeDefaultParameters
? defaultParameters?.user
: user,
destinationCurrency: toCurrency,
destinationChainId: toChainId,
originCurrency: currency,
originChainId: chainId,
amount,
recipient: includeDefaultParameters
? defaultParameters?.recipient
: recipient,
tradeType,
referrer: client.source || undefined,
txs: preparedTransactions,
...options
};
const request = {
url: `${client.baseApiUrl}/quote`,
method: 'post',
data: query
};
const res = await axios.request(request);
if (res.status !== 200) {
throw new APIError(res?.data?.message, res.status, res.data);
}
return { ...res.data, request };
}
//# sourceMappingURL=getQuote.js.map