Build: get paid by QR code
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.0qr-checkout.ts
/**
* Get paid by QR code: the merchant compiles a deposit intent, and the response
* carries a `fundEscrow` action anyone can send. Render it as a QR; once a
* customer funds the escrow from any wallet, a solver swaps the payment to the
* merchant's token and delivers at least the committed floor.
*
* const { flow, run } = buildFlow(sdk, inputs);
* const result = await flow.compile(run); // result.actions holds fundEscrow
*/
import {
type Address,
type ComposeRunInput,
type ComposeSdk,
resources,
} from '@lifi/composer-sdk';
export interface QrCheckoutInputs {
chainId: number;
/** The ERC-20 the customer pays with. */
payToken: Address;
/** What the customer pays, in `payToken`'s smallest unit. */
amount: bigint;
merchantToken: Address;
merchant: Address;
slippageBps: number;
}
export const buildFlow = (
sdk: ComposeSdk,
inputs: QrCheckoutInputs,
name = 'tpl:qr-checkout@1'
) => {
const { chainId, payToken, merchantToken, merchant } = inputs;
// The leg a solver runs from the funded escrow. The swap's slippage floor is
// what the settlement contract enforces before paying the merchant.
const leg = sdk.flow(chainId, {
name: `${name}:swap`,
inputs: { funding: resources.erc20(payToken, chainId) },
});
leg.lifi.swap('swap', {
bind: { amountIn: leg.inputs.funding },
config: {
resourceOut: resources.erc20(merchantToken, chainId),
slippage: inputs.slippageBps / 10_000,
},
});
const flow = sdk.flow(chainId, {
name,
inputs: { payment: resources.erc20(payToken, chainId) },
});
flow.continuation.settle('settle', {
bind: { input: flow.inputs.payment },
config: {
provider: 'intent-factory',
// The escrow transfer is a separate action any wallet can send.
funding: 'external',
outcomes: [{ token: merchantToken, destination: merchant, chainId }],
validity: { expiresAtMs: Date.now() + 60 * 60 * 1000 },
continuationFlow: leg.build(),
},
});
// External funding takes a plain amount: the customer, not the signer, pays.
const run = {
signer: merchant,
inputs: { payment: inputs.amount },
} satisfies ComposeRunInput;
return { flow, run };
};Call it
import { createComposeSdk } from '@lifi/composer-sdk';
import { buildFlow } from './qr-checkout';
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:qr-checkout@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.
Token you receiveFixed by you
Where it landsFixed by you
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