Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@base-org/account": "^2.5.1",
"@coinbase/wallet-sdk": "^4.3.6",
"@electric-sql/pglite": "^0.3.15",
"@lifi/intent": "file:../intent.ts",
"@lifi/intent": "0.1.4",
"@metamask/sdk": "^0.34.0",
"@safe-global/safe-apps-provider": "~0.18.6",
"@safe-global/safe-apps-sdk": "^9.1.0",
Expand Down
22 changes: 13 additions & 9 deletions src/lib/components/GetQuote.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script lang="ts">
import { IntentApi } from "@lifi/intent";
import type { AppTokenContext } from "$lib/appTypes";
import { resolveDemoQuoteParams } from "$lib/libraries/demoQuote";
import { interval } from "rxjs";
import { isAddress } from "viem";

let {
exclusiveFor = $bindable(),
useExclusiveForQuoteRequest = false,
use11Demo = false,
integratorKey = "",
inputTokens,
outputTokens = $bindable(),
account,
Expand All @@ -15,30 +17,32 @@
}: {
exclusiveFor: string;
useExclusiveForQuoteRequest?: boolean;
use11Demo?: boolean;
integratorKey?: string;
inputTokens: AppTokenContext[];
outputTokens: AppTokenContext[];
account: () => `0x${string}`;
mainnet: boolean;
useProductionApi: boolean | null;
} = $props();

const toRawAddress = (value: string): `0x${string}` | undefined =>
isAddress(value, { strict: false }) ? (value as `0x${string}`) : undefined;

const intentApi = $derived(new IntentApi(useProductionApi ?? mainnet));

async function getQuoteAndSet() {
try {
const requestedExclusiveFor = useExclusiveForQuoteRequest
? [toRawAddress(exclusiveFor)].filter(
(value): value is `0x${string}` => value !== undefined
)
: undefined;
const { exclusiveFor: requestedExclusiveFor, integratorKey: requestedIntegratorKey } =
resolveDemoQuoteParams({
use11Demo,
integratorKey,
useExclusiveForQuoteRequest,
exclusiveFor
});

const response = await intentApi.getQuotes({
user: account(),
userChainId: inputTokens[0].token.chainId,
exclusiveFor: requestedExclusiveFor,
integratorKey: requestedIntegratorKey,
inputs: inputTokens.map(({ token, amount }) => {
return {
sender: account(),
Expand Down
4 changes: 4 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const MULTICHAIN_INPUT_SETTLER_COMPACT =
export const ALWAYS_OK_ALLOCATOR = "281773970620737143753120258" as const;
export const POLYMER_ALLOCATOR = "116450367070547927622991121" as const; // 0x02ecC89C25A5DCB1206053530c58E002a737BD11 signing by 0x934244C8cd6BeBDBd0696A659D77C9BDfE86Efe6
export const COIN_FILLER = "0x0000000000eC36B683C2E6AC89e9A75989C22a2e" as const;
// LI.FI solver used for the 1:1 stablecoin demo. In demo mode the quote request
// omits exclusiveFor, but the on-chain intent is still made exclusive to this
// solver so it fills 1:1 via its quick fallback.
export const DEMO_EXCLUSIVE_SOLVER = "0x94807fE4300D15909C1a4fd39f76c61D68aee11E" as const;
export const WORMHOLE_ORACLE: Partial<Record<number, `0x${string}`>> = {
[ethereum.id]: "0x0000000000000000000000000000000000000000",
[arbitrum.id]: "0x0000000000000000000000000000000000000000",
Expand Down
41 changes: 41 additions & 0 deletions src/lib/libraries/demoQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isAddress } from "viem";

export type DemoQuoteParams = {
/** Solver addresses to request exclusivity for, or undefined for none. */
exclusiveFor: `0x${string}`[] | undefined;
/** Integrator key to send as the X-Integrator-Key header, or undefined. */
integratorKey: string | undefined;
};

const toRawAddress = (value: string): `0x${string}` | undefined =>
isAddress(value, { strict: false }) ? (value as `0x${string}`) : undefined;

/**
* Resolves the exclusivity and integrator-key parameters for a quote request.
*
* In 1:1 demo mode the integrator key is sent as a header and `exclusiveFor` is
* never set, so the request carries no `metadata.exclusiveFor` (the integrator
* key alone drives the 1:1 quote). Outside demo mode, behavior follows the manual
* "Lock Exclusive" field.
*/
export function resolveDemoQuoteParams(opts: {
use11Demo: boolean;
integratorKey: string;
useExclusiveForQuoteRequest: boolean;
exclusiveFor: string;
}): DemoQuoteParams {
if (opts.use11Demo) {
return {
exclusiveFor: undefined,
integratorKey: opts.integratorKey ? opts.integratorKey : undefined
};
}

const exclusiveFor = opts.useExclusiveForQuoteRequest
? [toRawAddress(opts.exclusiveFor)].filter(
(value): value is `0x${string}` => value !== undefined
)
: undefined;

return { exclusiveFor, integratorKey: undefined };
}
36 changes: 34 additions & 2 deletions src/lib/screens/IssueIntent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import FormControl from "$lib/components/ui/FormControl.svelte";
import ScreenFrame from "$lib/components/ui/ScreenFrame.svelte";
import SectionCard from "$lib/components/ui/SectionCard.svelte";
import { POLYMER_ALLOCATOR, formatTokenAmount, getChainName } from "$lib/config";
import {
DEMO_EXCLUSIVE_SOLVER,
POLYMER_ALLOCATOR,
formatTokenAmount,
getChainName
} from "$lib/config";
import { IntentFactory, escrowApprove } from "$lib/libraries/intentFactory";
import { CompactLib } from "$lib/libraries/compactLib";
import store from "$lib/state.svelte";
Expand Down Expand Up @@ -39,7 +44,11 @@

const intentOptions = $derived.by(
(): AppCreateIntentOptions => ({
exclusiveFor: resolveExclusiveFor(store.exclusiveFor),
// In 1:1 demo mode the quote omits exclusiveFor, but the on-chain intent
// must still be exclusive to the demo solver so it fills 1:1.
exclusiveFor: store.use11Demo
? DEMO_EXCLUSIVE_SOLVER
: resolveExclusiveFor(store.exclusiveFor),
inputTokens: store.inputTokens,
outputTokens: store.outputTokens,
verifier: store.verifier,
Expand Down Expand Up @@ -226,6 +235,8 @@
<GetQuote
bind:exclusiveFor={store.exclusiveFor}
useExclusiveForQuoteRequest={store.useExclusiveForQuoteRequest}
use11Demo={store.use11Demo}
integratorKey={store.integratorKey}
mainnet={store.mainnet}
useProductionApi={store.useProductionApi}
inputTokens={store.inputTokens}
Expand Down Expand Up @@ -343,6 +354,27 @@
Lock Exclusive
</label>
</div>
<div class="flex min-w-0 items-center gap-1">
<label
class="flex items-center gap-1 text-[11px] font-semibold whitespace-nowrap text-gray-500"
>
<input
type="checkbox"
class="h-3.5 w-3.5 rounded border-gray-300 text-sky-600 focus:ring-sky-300"
bind:checked={store.use11Demo}
/>
1:1 demo
</label>
{#if store.use11Demo}
<FormControl
type="text"
size="sm"
className="flex-1"
placeholder="X-Integrator-Key"
bind:value={store.integratorKey}
/>
{/if}
</div>
</div>
</SectionCard>

Expand Down
5 changes: 5 additions & 0 deletions src/lib/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ class Store {
exclusiveFor: string = $state("");
recipient: string = $state("");
useExclusiveForQuoteRequest = $state(false);
// 1:1 stablecoin demo: when on, quote requests send the X-Integrator-Key header
// and never set metadata.exclusiveFor (the integrator key drives the quote), but
// the on-chain intent is still made exclusive to the demo solver so it fills 1:1.
use11Demo = $state(false);
integratorKey: string = $state("");

invalidateWalletReadCache(scope: "all" | "balance" | "allowance" | "compact" = "all") {
if (scope === "all" || scope === "balance") invalidateRpcPrefix("balance:");
Expand Down
74 changes: 74 additions & 0 deletions tests/unit/demoQuote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from "bun:test";
import { resolveDemoQuoteParams } from "../../src/lib/libraries/demoQuote";

describe("resolveDemoQuoteParams", () => {
it("never sets exclusiveFor and sends the integrator key in demo mode", () => {
const result = resolveDemoQuoteParams({
use11Demo: true,
integratorKey: "demo-key",
useExclusiveForQuoteRequest: false,
exclusiveFor: ""
});

expect(result.exclusiveFor).toBeUndefined();
expect(result.integratorKey).toBe("demo-key");
});

it("ignores the manual exclusive field while in demo mode", () => {
const result = resolveDemoQuoteParams({
use11Demo: true,
integratorKey: "demo-key",
useExclusiveForQuoteRequest: true,
exclusiveFor: "0x1111111111111111111111111111111111111111"
});

expect(result.exclusiveFor).toBeUndefined();
});

it("omits the integrator key when none is provided in demo mode", () => {
const result = resolveDemoQuoteParams({
use11Demo: true,
integratorKey: "",
useExclusiveForQuoteRequest: false,
exclusiveFor: ""
});

expect(result.exclusiveFor).toBeUndefined();
expect(result.integratorKey).toBeUndefined();
});

it("never sends an integrator key when demo mode is off", () => {
const result = resolveDemoQuoteParams({
use11Demo: false,
integratorKey: "demo-key",
useExclusiveForQuoteRequest: false,
exclusiveFor: ""
});

expect(result.integratorKey).toBeUndefined();
expect(result.exclusiveFor).toBeUndefined();
});

it("uses the manual exclusive address when locked and not in demo mode", () => {
const addr = "0x1111111111111111111111111111111111111111";
const result = resolveDemoQuoteParams({
use11Demo: false,
integratorKey: "",
useExclusiveForQuoteRequest: true,
exclusiveFor: addr
});

expect(result.exclusiveFor).toEqual([addr]);
});

it("filters out an invalid manual exclusive address", () => {
const result = resolveDemoQuoteParams({
use11Demo: false,
integratorKey: "",
useExclusiveForQuoteRequest: true,
exclusiveFor: "not-an-address"
});

expect(result.exclusiveFor).toEqual([]);
});
});
Loading