@reservoir0x/relay-sdk
Version:
Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.
83 lines • 3.35 kB
JavaScript
import { getClient } from '../client.js';
import { executeSteps, adaptViemWallet, getCurrentStepData, safeStructuredClone } from '../utils/index.js';
import {} from 'viem';
import { isViemWalletClient } from '../utils/viemWallet.js';
import { isDeadAddress } from '../constants/address.js';
/**
* Execute crosschain using Relay
* @param data.quote A Relay quote retrieved using {@link getQuote}
* @param data.depositGasLimit A gas limit to use in base units (wei, etc)
* @param data.wallet Wallet object that adheres to the AdaptedWakket interface or a viem WalletClient
* @param data.onProgress Callback to update UI state as execution progresses
* @param abortController Optional AbortController to cancel the execution
*/
export function execute(data) {
const { quote, wallet, depositGasLimit, onProgress } = data;
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;
}
try {
if (!adaptedWallet) {
throw new Error('AdaptedWallet is required to execute steps');
}
// Instantiate a new abort controller
const abortController = new AbortController();
const chainId = quote.details?.currencyIn?.currency?.chainId;
if (chainId === undefined) {
throw new Error('Missing chainId from quote');
}
if (isDeadAddress(quote?.details?.recipient)) {
throw new Error('Recipient should never be burn address');
}
if (isDeadAddress(quote?.details?.sender)) {
throw new Error('Sender should never be burn address');
}
const { request, ...restOfQuote } = quote;
const _quote = safeStructuredClone(restOfQuote);
// Build the promise that carries out the execution
const executionPromise = new Promise((resolve, reject) => {
executeSteps(chainId, request, adaptedWallet, ({ steps, fees, breakdown, details, refunded, error }) => {
if (abortController.signal.aborted) {
console.log('Relay SDK: Execution aborted, skipping progress callback');
return;
}
const { currentStep, currentStepItem, txHashes } = getCurrentStepData(steps);
onProgress?.({
steps,
fees,
breakdown,
details,
currentStep,
currentStepItem,
txHashes,
refunded,
error
});
}, _quote, depositGasLimit
? {
deposit: {
gasLimit: depositGasLimit
}
}
: undefined)
.then((data) => {
resolve({ data, abortController });
})
.catch(reject);
});
executionPromise.abortController = abortController;
return executionPromise;
}
catch (err) {
console.error(err);
throw err;
}
}
//# sourceMappingURL=execute.js.map