import {
useTrailsSendTransaction,
dynamic,
deposit,
swap,
lend,
assertCondition,
erc20Utils,
} from '0xtrails'
const morphoMarketId = 'polygon-usdt-bbqusdt0-0xb7c9988d3922f25a336a469f3bb26ca61fe79e24-4626-vault'
const fluidMarketId = 'polygon-usdc-fusdc-0x571d456b578fdc34e26e6d636736ed7c0cdb9d89-4626-vault'
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 Fluid.
lend({
marketId: fluidMarketId,
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: '200000',
})
}
>
{error ? error.message : isPending ? 'Sending...' : 'Execute'}
</button>
)
}