import { isBrdgError } from '@kernlog/brdg-sdk';
import type { Hex } from 'viem';
import { account, brdg, signEvmStep } from './evm';
async function main() {
// 1. Quote: 25 USDC on Base to USDC on Arbitrum
const quote = await brdg.getQuote({
fromChain: 'base',
toChain: 'arbitrum',
fromToken: 'USDC',
toToken: 'USDC',
amountAtomic: '25000000',
sender: account.address,
recipient: account.address,
});
if (!quote.best) throw new Error(`no executable route: ${quote.rejected.map((r) => r.code).join(', ')}`);
console.log(`${quote.best.venue} delivers ${quote.best.amountOutAtomic} in ~${quote.best.estimatedSeconds}s`);
// 2. Build the best quote
const build = await brdg.buildTransfer({ decisionId: quote.decisionId });
// 3. Sign every step in order. `main` is the bridge transaction; any other step is an approval.
let mainHash: Hex | undefined;
for (const step of build.steps) {
const hash = await signEvmStep(step);
if (step.step === 'main') mainHash = hash;
}
if (!mainHash) throw new Error('build carried no main step');
// 4. Report the bridge transaction's hash
await brdg.submitTransfer(build.transferId, { txHash: mainHash, step: 'main' });
// 5. Wait for delivery
const transfer = await brdg.waitForTransfer(build.transferId, {
onUpdate: (t) => console.log(t.status),
});
console.log(transfer.status, transfer.destinationTxHash);
}
main().catch((error) => {
if (isBrdgError(error)) console.error(error.code, error.message, error.details);
else console.error(error);
process.exit(1);
});