import {
useTrailsSendTransaction,
dynamic,
deposit,
swap,
lend,
assertCondition,
erc20Utils,
} from '0xtrails'
const morphoMarketId = 'polygon-usdt-bbqusdt0-0xb7c9988d3922f25a336a469f3bb26ca61fe79e24-4626-vault'
const aaveMarketId = 'polygon-usdc-aave-v3-lending'
export function ComposedEarnSendTransactionButton({
recipient,
}: {
recipient: `0x${string}`
}) {
const { sendTransaction, isPending, error } = useTrailsSendTransaction({
actions: [
// a) Deposit 0.1 USDT -> Morpho vault.
deposit({
marketId: morphoMarketId,
amount: '0.1',
}),
// b) Swap whatever USDT is left -> USDC.
swap({
tokenIn: 'USDT',
tokenOut: 'USDC',
amountIn: dynamic(),
fee: '0.3', // pool tier 0.3%
}),
// c) Guard: make sure the swap really gave us at least 0.08 USDC.
assertCondition({
erc20Balance: { token: 'USDC', gte: '0.08' },
}),
// d) Lend all the USDC we just received into Aave.
lend({
marketId: aaveMarketId,
amount: dynamic(),
}),
],
onStatusUpdate: (transactionStates) => {
for (const state of transactionStates) {
console.log('Transaction:', state)
}
},
})
return (
<button
disabled={isPending}
onClick={() =>
sendTransaction({
to: recipient,
tokenAddress: erc20Utils.USDT.addressOn('polygon'),
tokenAmount: '0.2',
})
}
>
{error ? error.message : isPending ? 'Sending...' : 'Execute'}
</button>
)
}