Build: pay an invoice from any chain
Three steps from here to your app. Run it from your backend, your frontend or your agent.
1
Get your API key
Create a key on the LI.FI portal. It ties usage and your fees to your app.
Get API key ↗2
Add the flow
Install
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0invoice-payment.ts
/**
* Pay a Request Network invoice in USDC on Base from USDC on another chain,
* with one signature. A LI.FI Intents order delivers exactly the invoice total
* into an escrow on Base, and a solver pays it through Request's fee proxy;
* unspent USDC returns to the payer, and an unfilled escrow refunds them.
*
* const { flow, run } = buildFlow(sdk, inputs);
* const result = await flow.compile(run);
*/
import {
type Address,
type ComposeRunInput,
type ComposeSdk,
materialisers,
raw,
resources,
} from '@lifi/composer-sdk';
const BASE = 8453;
const BASE_USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
// Request Network's ERC20FeeProxy on Base: its event marks the invoice paid.
const FEE_PROXY = '0x1892196E80C4c17ea5100Da765Ab48c1fE2Fb814';
export interface InvoicePaymentInputs {
sourceChainId: number;
sourceUsdc: Address;
/** USDC the payer deposits; must cover what the solver asks for the total. */
amountIn: bigint;
payer: Address;
/** The invoice terms, in USDC base units on Base. */
invoice: {
payee: Address;
amount: bigint;
fee: bigint;
feeAddress: Address;
paymentReference: `0x${string}`;
};
}
export const buildFlow = (
sdk: ComposeSdk,
inputs: InvoicePaymentInputs,
name = 'tpl:invoice-payment@1'
) => {
const { invoice, payer, sourceChainId, sourceUsdc, amountIn } = inputs;
const baseUsdc = resources.erc20(BASE_USDC, BASE);
const total = `${invoice.amount + invoice.fee}`;
// The leg a solver runs on Base: `exact` binds the escrow to this one call.
const leg = sdk.flow(BASE, {
name: `${name}:pay`,
inputs: { funding: baseUsdc },
});
leg.core.call('pay', {
resource: leg.inputs.funding,
bind: {
tokenAddress: raw.literal('address', BASE_USDC),
to: raw.literal('address', invoice.payee),
amount: raw.literal('uint256', `${invoice.amount}`),
paymentReference: raw.literal('bytes', invoice.paymentReference),
feeAmount: raw.literal('uint256', `${invoice.fee}`),
feeAddress: raw.literal('address', invoice.feeAddress),
},
config: {
target: FEE_PROXY,
functionSignature:
'function transferFromWithReferenceAndFee(address tokenAddress, address to, uint256 amount, bytes paymentReference, uint256 feeAmount, address feeAddress)',
approvals: [{ spender: FEE_PROXY }],
},
});
const flow = sdk.flow(sourceChainId, {
name,
inputs: { usdc: resources.erc20(sourceUsdc, sourceChainId) },
});
// No `destination`: the solver delivers exactly the total into the escrow.
const order = flow.lifiIntents.order('order', {
bind: { amountIn: flow.inputs.usdc },
config: { resourceOut: baseUsdc, exactAmountOut: total },
});
flow.continuation.settle('settle', {
bind: { input: order.amountOut },
config: {
provider: 'intent-factory',
exact: true,
outcomes: [],
refund: payer,
validity: { expiresAtMs: Date.now() + 60 * 60 * 1000 },
continuationFlow: leg.build(),
},
});
const run = {
signer: payer,
inputs: { usdc: materialisers.directDeposit({ amount: amountIn }) },
// Unspent USDC returns to the payer. The payment leaves the flow, which
// the price-impact gate would count as a loss.
sweepTo: payer,
maxPriceImpactBps: 0,
} satisfies ComposeRunInput;
return { flow, run };
};Call it
import { createComposeSdk } from '@lifi/composer-sdk';
import { buildFlow } from './invoice-payment';
const sdk = createComposeSdk({
baseUrl: 'https://composer.li.quest',
apiKey: process.env.LIFI_API_KEY!,
});
// inputs: the values your user picks; see the module's inputs interface.
const { flow, run } = buildFlow(sdk, inputs);
const result = await flow.compile({ ...run, simulationPolicy: 'allow-revert' });
// Show the outputs' amount.minimum to the user, then have them sign
// result.transaction.The full working module, not an excerpt. It names the flow tpl:invoice-payment@1 so LI.FI can see which template it came from.
3
Choose what you control
Fix these once, or pass them in from your user on every call.
InvoicePassed by your app
Pay withPassed by the payer
Before you go live
Need it changed?
A protocol we don't cover yet, custom logic, or a launch with a partner. We build these with you.
Talk to the team