@edel-finance/swap-widget 0.0.1 → 0.1.4-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -0
- package/dist/adapters/angular.d.ts +32 -0
- package/dist/adapters/angular.js +84 -0
- package/dist/adapters/solid.d.ts +56 -0
- package/dist/adapters/solid.js +41 -0
- package/dist/adapters/svelte.d.ts +26 -0
- package/dist/adapters/svelte.js +45 -0
- package/dist/adapters/vue.d.ts +58 -0
- package/dist/adapters/vue.js +124 -0
- package/dist/embed.d.ts +58 -5
- package/dist/embed.js +211 -31
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/widget.d.ts +4 -0
- package/dist/widget.js +5 -1
- package/package.json +28 -5
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# `@edel-finance/swap-widget`
|
|
2
|
+
|
|
3
|
+
Embeddable Swap Widget for **Edel Markets** (MainNet and TestNet), powered by `@edel-finance/sdk` with Alpine Forest II dark/light theming, Shadow DOM CSS isolation, RFQ slippage presets (`1.0%` / `2.0%` / `3.0%` + Custom), and live 5-step WebSocket settlement tracking.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @edel-finance/swap-widget @edel-finance/sdk react react-dom
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
All framework subpath adapters (`vue`, `svelte`, `angular`, `solid`) have **zero runtime dependency** on the target framework and isolate styles inside an open Shadow DOM root.
|
|
12
|
+
|
|
13
|
+
## React
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { EdelSwapWidget } from "@edel-finance/swap-widget";
|
|
17
|
+
|
|
18
|
+
export function SwapPage() {
|
|
19
|
+
return (
|
|
20
|
+
<EdelSwapWidget
|
|
21
|
+
network="mainnet"
|
|
22
|
+
integratorId="your-integrator-id"
|
|
23
|
+
defaultFrom="CC"
|
|
24
|
+
defaultTo="USDCx"
|
|
25
|
+
defaultMaxSlippageBps={100}
|
|
26
|
+
theme="dark"
|
|
27
|
+
onSwapComplete={(order) => console.log("Settled:", order.swapId)}
|
|
28
|
+
/>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Vue >= 3
|
|
34
|
+
|
|
35
|
+
```vue
|
|
36
|
+
<script setup lang="ts">
|
|
37
|
+
import { h } from "vue";
|
|
38
|
+
import { createVueSwapWidget } from "@edel-finance/swap-widget/vue";
|
|
39
|
+
|
|
40
|
+
const EdelSwapWidget = createVueSwapWidget(h);
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<template>
|
|
44
|
+
<EdelSwapWidget
|
|
45
|
+
network="mainnet"
|
|
46
|
+
integrator-id="your-integrator-id"
|
|
47
|
+
default-from="CC"
|
|
48
|
+
default-to="USDCx"
|
|
49
|
+
theme="dark"
|
|
50
|
+
@swap-complete="(order) => console.log('Settled:', order.swapId)"
|
|
51
|
+
/>
|
|
52
|
+
</template>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Or use the zero-boilerplate directive `vEdelSwapWidget`:
|
|
56
|
+
|
|
57
|
+
```vue
|
|
58
|
+
<div v-edel-swap-widget="{ network: 'mainnet', theme: 'dark', integratorId: 'loop' }" />
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Svelte >= 5
|
|
62
|
+
|
|
63
|
+
```svelte
|
|
64
|
+
<script lang="ts">
|
|
65
|
+
import { edelSwapWidget } from "@edel-finance/swap-widget/svelte";
|
|
66
|
+
|
|
67
|
+
let theme = $state<"dark" | "light">("dark");
|
|
68
|
+
</script>
|
|
69
|
+
|
|
70
|
+
<div
|
|
71
|
+
use:edelSwapWidget={{
|
|
72
|
+
network: "mainnet",
|
|
73
|
+
integratorId: "your-integrator-id",
|
|
74
|
+
defaultFrom: "CC",
|
|
75
|
+
defaultTo: "USDCx",
|
|
76
|
+
theme,
|
|
77
|
+
}}
|
|
78
|
+
></div>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
For Svelte 5 `$effect` rune mounting, use `mountSvelteSwapWidget(containerEl, props)`.
|
|
82
|
+
|
|
83
|
+
## Angular >= 17
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from "@angular/core";
|
|
87
|
+
import { EdelSwapWidgetAngularController } from "@edel-finance/swap-widget/angular";
|
|
88
|
+
|
|
89
|
+
@Component({
|
|
90
|
+
selector: "app-edel-swap",
|
|
91
|
+
standalone: true,
|
|
92
|
+
template: `<div #swapHost></div>`,
|
|
93
|
+
})
|
|
94
|
+
export class EdelSwapComponent implements AfterViewInit, OnDestroy {
|
|
95
|
+
@ViewChild("swapHost", { static: true }) swapHost!: ElementRef<HTMLElement>;
|
|
96
|
+
private readonly controller = new EdelSwapWidgetAngularController();
|
|
97
|
+
|
|
98
|
+
ngAfterViewInit(): void {
|
|
99
|
+
this.controller.attach(this.swapHost.nativeElement, {
|
|
100
|
+
network: "mainnet",
|
|
101
|
+
integratorId: "your-integrator-id",
|
|
102
|
+
defaultFrom: "CC",
|
|
103
|
+
defaultTo: "USDCx",
|
|
104
|
+
theme: "dark",
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
ngOnDestroy(): void {
|
|
109
|
+
this.controller.destroy();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
For Angular `CUSTOM_ELEMENTS_SCHEMA` templates with `<edel-swap-widget>`, call `registerEdelSwapWidgetElement()` and bind typed listeners with `bindEdelSwapWidgetEvents(element, handlers)`.
|
|
115
|
+
|
|
116
|
+
## SolidJS
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
import { onCleanup } from "solid-js";
|
|
120
|
+
import { createSolidSwapWidget } from "@edel-finance/swap-widget/solid";
|
|
121
|
+
|
|
122
|
+
export function SwapPanel() {
|
|
123
|
+
const widget = createSolidSwapWidget({
|
|
124
|
+
network: "mainnet",
|
|
125
|
+
integratorId: "your-integrator-id",
|
|
126
|
+
defaultFrom: "CC",
|
|
127
|
+
defaultTo: "USDCx",
|
|
128
|
+
theme: "dark",
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
onCleanup(() => widget.cleanup());
|
|
132
|
+
return <div ref={widget.ref} />;
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Shadow DOM Web Component (`<edel-swap-widget>`)
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { registerEdelSwapWidgetElement } from "@edel-finance/swap-widget";
|
|
140
|
+
|
|
141
|
+
registerEdelSwapWidgetElement();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
```html
|
|
145
|
+
<edel-swap-widget
|
|
146
|
+
network="mainnet"
|
|
147
|
+
integrator-id="your-integrator-id"
|
|
148
|
+
from="CC"
|
|
149
|
+
to="USDCx"
|
|
150
|
+
max-slippage-bps="100"
|
|
151
|
+
theme="dark"
|
|
152
|
+
></edel-swap-widget>
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Dispatched bubbling, composed `CustomEvent`s:
|
|
156
|
+
|
|
157
|
+
- `edel-swap-quote`: `SwapQuote`
|
|
158
|
+
- `edel-swap-complete`: `SwapOrder`
|
|
159
|
+
- `edel-swap-error`: `{ message: string; error: Error }`
|
|
160
|
+
- `edel-swap-slippage-change`: `{ maxSlippageBps: number }`
|
|
161
|
+
|
|
162
|
+
## 3-Line Standalone iframe Embed
|
|
163
|
+
|
|
164
|
+
```html
|
|
165
|
+
<iframe src="https://app.edel.finance/embed/swap?network=mainnet&from=CC&to=USDCx&theme=dark" title="Edel Swap Widget"
|
|
166
|
+
width="100%" height="560" allow="clipboard-write"
|
|
167
|
+
style="border:0;border-radius:18px;overflow:hidden;max-width:440px;"></iframe>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Interactive Storybook
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pnpm --filter @edel-finance/swap-widget storybook
|
|
174
|
+
pnpm --filter @edel-finance/swap-widget build-storybook
|
|
175
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SwapOrder, SwapQuote } from "@edel-finance/sdk";
|
|
2
|
+
import { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, type EdelSwapWidgetErrorDetail, type EdelSwapWidgetSlippageDetail, type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement } from "../embed.js";
|
|
3
|
+
import type { EdelSwapWidgetProps } from "../widget.js";
|
|
4
|
+
export interface EdelSwapWidgetAngularEventHandlers {
|
|
5
|
+
onQuote?: (quote: SwapQuote) => void;
|
|
6
|
+
onComplete?: (order: SwapOrder) => void;
|
|
7
|
+
onError?: (detail: EdelSwapWidgetErrorDetail) => void;
|
|
8
|
+
onSlippageChange?: (detail: EdelSwapWidgetSlippageDetail) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Typed controller for Angular >= 17 components (compatible with `AfterViewInit`,
|
|
12
|
+
* `OnChanges`, `OnDestroy`, or signal-based `viewChild` + `effect()`).
|
|
13
|
+
* Zero runtime dependency on `@angular/core`.
|
|
14
|
+
*/
|
|
15
|
+
export declare class EdelSwapWidgetAngularController {
|
|
16
|
+
private mounted;
|
|
17
|
+
private readonly defaultOptions;
|
|
18
|
+
constructor(options?: MountEdelSwapWidgetOptions);
|
|
19
|
+
get isMounted(): boolean;
|
|
20
|
+
attach(nativeElement: HTMLElement, props?: EdelSwapWidgetProps, options?: MountEdelSwapWidgetOptions): void;
|
|
21
|
+
update(nextProps: Partial<EdelSwapWidgetProps>): void;
|
|
22
|
+
getProps(): EdelSwapWidgetProps | null;
|
|
23
|
+
destroy(): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Binds strongly-typed CustomEvent listeners (`edel-swap-quote`, `edel-swap-complete`,
|
|
27
|
+
* `edel-swap-error`, `edel-swap-slippage-change`) on an `<edel-swap-widget>` element
|
|
28
|
+
* and returns a single unsubscribe callback for `ngOnDestroy` / `DestroyRef.onDestroy`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function bindEdelSwapWidgetEvents(element: EventTarget, handlers: EdelSwapWidgetAngularEventHandlers): () => void;
|
|
31
|
+
export type { EdelSwapWidgetProps } from "../widget.js";
|
|
32
|
+
export { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, type EdelSwapWidgetErrorDetail, type EdelSwapWidgetSlippageDetail, type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement, };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
2
|
+
/**
|
|
3
|
+
* Typed controller for Angular >= 17 components (compatible with `AfterViewInit`,
|
|
4
|
+
* `OnChanges`, `OnDestroy`, or signal-based `viewChild` + `effect()`).
|
|
5
|
+
* Zero runtime dependency on `@angular/core`.
|
|
6
|
+
*/
|
|
7
|
+
export class EdelSwapWidgetAngularController {
|
|
8
|
+
mounted = null;
|
|
9
|
+
defaultOptions;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.defaultOptions = options;
|
|
12
|
+
}
|
|
13
|
+
get isMounted() {
|
|
14
|
+
return this.mounted !== null;
|
|
15
|
+
}
|
|
16
|
+
attach(nativeElement, props = {}, options = {}) {
|
|
17
|
+
if (this.mounted) {
|
|
18
|
+
this.mounted.update(props);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
this.mounted = mountEdelSwapWidget(nativeElement, props, {
|
|
22
|
+
shadowDom: true,
|
|
23
|
+
dispatchEvents: true,
|
|
24
|
+
...this.defaultOptions,
|
|
25
|
+
...options,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
update(nextProps) {
|
|
29
|
+
this.mounted?.update(nextProps);
|
|
30
|
+
}
|
|
31
|
+
getProps() {
|
|
32
|
+
return this.mounted ? this.mounted.getProps() : null;
|
|
33
|
+
}
|
|
34
|
+
destroy() {
|
|
35
|
+
this.mounted?.unmount();
|
|
36
|
+
this.mounted = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Binds strongly-typed CustomEvent listeners (`edel-swap-quote`, `edel-swap-complete`,
|
|
41
|
+
* `edel-swap-error`, `edel-swap-slippage-change`) on an `<edel-swap-widget>` element
|
|
42
|
+
* and returns a single unsubscribe callback for `ngOnDestroy` / `DestroyRef.onDestroy`.
|
|
43
|
+
*/
|
|
44
|
+
export function bindEdelSwapWidgetEvents(element, handlers) {
|
|
45
|
+
const cleanups = [];
|
|
46
|
+
if (handlers.onQuote) {
|
|
47
|
+
const onQuote = handlers.onQuote;
|
|
48
|
+
const listener = (event) => {
|
|
49
|
+
onQuote(event.detail);
|
|
50
|
+
};
|
|
51
|
+
element.addEventListener(EDEL_SWAP_WIDGET_EVENTS.quote, listener);
|
|
52
|
+
cleanups.push(() => element.removeEventListener(EDEL_SWAP_WIDGET_EVENTS.quote, listener));
|
|
53
|
+
}
|
|
54
|
+
if (handlers.onComplete) {
|
|
55
|
+
const onComplete = handlers.onComplete;
|
|
56
|
+
const listener = (event) => {
|
|
57
|
+
onComplete(event.detail);
|
|
58
|
+
};
|
|
59
|
+
element.addEventListener(EDEL_SWAP_WIDGET_EVENTS.complete, listener);
|
|
60
|
+
cleanups.push(() => element.removeEventListener(EDEL_SWAP_WIDGET_EVENTS.complete, listener));
|
|
61
|
+
}
|
|
62
|
+
if (handlers.onError) {
|
|
63
|
+
const onError = handlers.onError;
|
|
64
|
+
const listener = (event) => {
|
|
65
|
+
onError(event.detail);
|
|
66
|
+
};
|
|
67
|
+
element.addEventListener(EDEL_SWAP_WIDGET_EVENTS.error, listener);
|
|
68
|
+
cleanups.push(() => element.removeEventListener(EDEL_SWAP_WIDGET_EVENTS.error, listener));
|
|
69
|
+
}
|
|
70
|
+
if (handlers.onSlippageChange) {
|
|
71
|
+
const onSlippageChange = handlers.onSlippageChange;
|
|
72
|
+
const listener = (event) => {
|
|
73
|
+
onSlippageChange(event.detail);
|
|
74
|
+
};
|
|
75
|
+
element.addEventListener(EDEL_SWAP_WIDGET_EVENTS.slippageChange, listener);
|
|
76
|
+
cleanups.push(() => element.removeEventListener(EDEL_SWAP_WIDGET_EVENTS.slippageChange, listener));
|
|
77
|
+
}
|
|
78
|
+
return () => {
|
|
79
|
+
for (const cleanup of cleanups) {
|
|
80
|
+
cleanup();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, mountEdelSwapWidget, registerEdelSwapWidgetElement, };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { EdelNetwork } from "@edel-finance/sdk";
|
|
2
|
+
import { type EdelSwapWidgetEventMap, type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget } from "../embed.js";
|
|
3
|
+
import type { SwapWidgetTheme } from "../theme.js";
|
|
4
|
+
import type { EdelSwapWidgetProps } from "../widget.js";
|
|
5
|
+
export interface SolidSwapWidgetHandle {
|
|
6
|
+
/** Pass directly to Solid's `ref={widget.ref}` attribute. */
|
|
7
|
+
ref(el: HTMLElement): void;
|
|
8
|
+
/** Push reactive prop updates from `createEffect(() => widget.update(props))`. */
|
|
9
|
+
update(nextProps: Partial<EdelSwapWidgetProps>): void;
|
|
10
|
+
/** Unmount the widget inside `onCleanup(() => widget.cleanup())`. */
|
|
11
|
+
cleanup(): void;
|
|
12
|
+
/** Access the underlying mounted widget instance, if attached. */
|
|
13
|
+
getInstance(): MountedEdelSwapWidget | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a typed SolidJS widget handle backed by `mountEdelSwapWidget` with Shadow DOM
|
|
17
|
+
* CSS isolation and zero runtime dependency on `solid-js`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createSolidSwapWidget(initialProps?: EdelSwapWidgetProps, mountOptions?: MountEdelSwapWidgetOptions): SolidSwapWidgetHandle;
|
|
20
|
+
/**
|
|
21
|
+
* SolidJS `use:edelSwapWidget={props}` directive backed by `mountEdelSwapWidget`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function edelSwapWidget(el: HTMLElement, accessor: () => EdelSwapWidgetProps, mountOptions?: MountEdelSwapWidgetOptions): MountedEdelSwapWidget;
|
|
24
|
+
export interface EdelSwapWidgetSolidIntrinsicAttributes {
|
|
25
|
+
network?: EdelNetwork;
|
|
26
|
+
theme?: SwapWidgetTheme;
|
|
27
|
+
"integrator-id"?: string;
|
|
28
|
+
"partner-id"?: string;
|
|
29
|
+
from?: string;
|
|
30
|
+
to?: string;
|
|
31
|
+
"max-slippage-bps"?: number | string;
|
|
32
|
+
"integrator-fee-bps"?: number | string;
|
|
33
|
+
"recipient-address"?: string;
|
|
34
|
+
"on:edel-swap-quote"?: (event: EdelSwapWidgetEventMap["edel-swap-quote"]) => void;
|
|
35
|
+
"on:edel-swap-complete"?: (event: EdelSwapWidgetEventMap["edel-swap-complete"]) => void;
|
|
36
|
+
"on:edel-swap-error"?: (event: EdelSwapWidgetEventMap["edel-swap-error"]) => void;
|
|
37
|
+
"on:edel-swap-slippage-change"?: (event: EdelSwapWidgetEventMap["edel-swap-slippage-change"]) => void;
|
|
38
|
+
}
|
|
39
|
+
export interface SolidSwapWidgetJSXIntrinsicElements {
|
|
40
|
+
"edel-swap-widget": EdelSwapWidgetSolidIntrinsicAttributes;
|
|
41
|
+
}
|
|
42
|
+
export interface SolidSwapWidgetDirectives {
|
|
43
|
+
edelSwapWidget: EdelSwapWidgetProps;
|
|
44
|
+
}
|
|
45
|
+
declare global {
|
|
46
|
+
namespace JSX {
|
|
47
|
+
interface IntrinsicElements {
|
|
48
|
+
"edel-swap-widget": EdelSwapWidgetSolidIntrinsicAttributes;
|
|
49
|
+
}
|
|
50
|
+
interface Directives {
|
|
51
|
+
edelSwapWidget: EdelSwapWidgetProps;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
56
|
+
export type { EdelSwapWidgetProps } from "../widget.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mountEdelSwapWidget, } from "../embed.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a typed SolidJS widget handle backed by `mountEdelSwapWidget` with Shadow DOM
|
|
4
|
+
* CSS isolation and zero runtime dependency on `solid-js`.
|
|
5
|
+
*/
|
|
6
|
+
export function createSolidSwapWidget(initialProps = {}, mountOptions = {}) {
|
|
7
|
+
let instance = null;
|
|
8
|
+
let pendingProps = { ...initialProps };
|
|
9
|
+
return {
|
|
10
|
+
ref(el) {
|
|
11
|
+
if (instance) {
|
|
12
|
+
instance.unmount();
|
|
13
|
+
}
|
|
14
|
+
instance = mountEdelSwapWidget(el, pendingProps, {
|
|
15
|
+
shadowDom: true,
|
|
16
|
+
...mountOptions,
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
update(nextProps) {
|
|
20
|
+
pendingProps = { ...pendingProps, ...nextProps };
|
|
21
|
+
instance?.update(nextProps);
|
|
22
|
+
},
|
|
23
|
+
cleanup() {
|
|
24
|
+
instance?.unmount();
|
|
25
|
+
instance = null;
|
|
26
|
+
},
|
|
27
|
+
getInstance() {
|
|
28
|
+
return instance;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* SolidJS `use:edelSwapWidget={props}` directive backed by `mountEdelSwapWidget`.
|
|
34
|
+
*/
|
|
35
|
+
export function edelSwapWidget(el, accessor, mountOptions = {}) {
|
|
36
|
+
return mountEdelSwapWidget(el, accessor(), {
|
|
37
|
+
shadowDom: true,
|
|
38
|
+
...mountOptions,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export { EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type MountEdelSwapWidgetOptions } from "../embed.js";
|
|
2
|
+
import type { EdelSwapWidgetProps } from "../widget.js";
|
|
3
|
+
export interface SvelteActionReturn<TParams> {
|
|
4
|
+
update?: (params: TParams) => void;
|
|
5
|
+
destroy?: () => void;
|
|
6
|
+
}
|
|
7
|
+
export type SvelteSwapWidgetAction = (node: HTMLElement, props?: EdelSwapWidgetProps) => SvelteActionReturn<EdelSwapWidgetProps>;
|
|
8
|
+
/**
|
|
9
|
+
* Factory for a typed Svelte action (`use:edelSwapWidget={props}`) with custom mount options.
|
|
10
|
+
* Zero runtime dependency on `svelte`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createSvelteSwapWidgetAction(mountOptions?: MountEdelSwapWidgetOptions): SvelteSwapWidgetAction;
|
|
13
|
+
/**
|
|
14
|
+
* Svelte >= 5 action for mounting the Edel Swap Widget inside a Shadow DOM host.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* `<div use:edelSwapWidget={{ network: "mainnet", theme: "dark", integratorId: "loop" }}></div>`
|
|
18
|
+
*/
|
|
19
|
+
export declare const edelSwapWidget: SvelteSwapWidgetAction;
|
|
20
|
+
/**
|
|
21
|
+
* Svelte 5 `$effect`-friendly mount helper. Returns a cleanup callback directly
|
|
22
|
+
* compatible with `$effect(() => mountSvelteSwapWidget(containerEl, props))`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function mountSvelteSwapWidget(node: HTMLElement | null | undefined, props?: EdelSwapWidgetProps, mountOptions?: MountEdelSwapWidgetOptions): () => void;
|
|
25
|
+
export { type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
26
|
+
export type { EdelSwapWidgetProps } from "../widget.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { mountEdelSwapWidget, } from "../embed.js";
|
|
2
|
+
/**
|
|
3
|
+
* Factory for a typed Svelte action (`use:edelSwapWidget={props}`) with custom mount options.
|
|
4
|
+
* Zero runtime dependency on `svelte`.
|
|
5
|
+
*/
|
|
6
|
+
export function createSvelteSwapWidgetAction(mountOptions = {}) {
|
|
7
|
+
return function edelSwapWidgetAction(node, props = {}) {
|
|
8
|
+
const mounted = mountEdelSwapWidget(node, props, {
|
|
9
|
+
shadowDom: true,
|
|
10
|
+
...mountOptions,
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
update(nextProps) {
|
|
14
|
+
mounted.update(nextProps);
|
|
15
|
+
},
|
|
16
|
+
destroy() {
|
|
17
|
+
mounted.unmount();
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Svelte >= 5 action for mounting the Edel Swap Widget inside a Shadow DOM host.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* `<div use:edelSwapWidget={{ network: "mainnet", theme: "dark", integratorId: "loop" }}></div>`
|
|
27
|
+
*/
|
|
28
|
+
export const edelSwapWidget = createSvelteSwapWidgetAction();
|
|
29
|
+
/**
|
|
30
|
+
* Svelte 5 `$effect`-friendly mount helper. Returns a cleanup callback directly
|
|
31
|
+
* compatible with `$effect(() => mountSvelteSwapWidget(containerEl, props))`.
|
|
32
|
+
*/
|
|
33
|
+
export function mountSvelteSwapWidget(node, props = {}, mountOptions = {}) {
|
|
34
|
+
if (!node) {
|
|
35
|
+
return () => { };
|
|
36
|
+
}
|
|
37
|
+
const mounted = mountEdelSwapWidget(node, props, {
|
|
38
|
+
shadowDom: true,
|
|
39
|
+
...mountOptions,
|
|
40
|
+
});
|
|
41
|
+
return () => {
|
|
42
|
+
mounted.unmount();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export { mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SwapOrder, SwapQuote } from "@edel-finance/sdk";
|
|
2
|
+
import { type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget } from "../embed.js";
|
|
3
|
+
import type { EdelSwapWidgetProps } from "../widget.js";
|
|
4
|
+
export type EdelSwapWidgetVueProps = EdelSwapWidgetProps;
|
|
5
|
+
export interface EdelSwapWidgetVueEmits {
|
|
6
|
+
quote: [quote: SwapQuote];
|
|
7
|
+
swapComplete: [order: SwapOrder];
|
|
8
|
+
orderSubmitted: [order: SwapOrder];
|
|
9
|
+
error: [error: Error];
|
|
10
|
+
slippageChange: [maxSlippageBps: number];
|
|
11
|
+
}
|
|
12
|
+
export type VueRenderFactory = (type: string, props?: Record<string, unknown> | null, children?: unknown) => unknown;
|
|
13
|
+
export interface VueWidgetInstanceContext {
|
|
14
|
+
$el?: HTMLElement | null;
|
|
15
|
+
$props?: Partial<EdelSwapWidgetProps>;
|
|
16
|
+
$emit?: (event: string, ...args: unknown[]) => void;
|
|
17
|
+
_edelWidget?: MountedEdelSwapWidget | null;
|
|
18
|
+
}
|
|
19
|
+
export interface VueSwapWidgetComponentDefinition {
|
|
20
|
+
name: string;
|
|
21
|
+
props: readonly (keyof EdelSwapWidgetProps)[];
|
|
22
|
+
emits: readonly (keyof EdelSwapWidgetVueEmits)[];
|
|
23
|
+
mounted(this: VueWidgetInstanceContext): void;
|
|
24
|
+
updated(this: VueWidgetInstanceContext): void;
|
|
25
|
+
beforeUnmount(this: VueWidgetInstanceContext): void;
|
|
26
|
+
render?: () => unknown;
|
|
27
|
+
template?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a typed Vue >= 3 component backed by `mountEdelSwapWidget` with Shadow DOM
|
|
31
|
+
* CSS isolation and zero runtime dependency on `vue`.
|
|
32
|
+
*
|
|
33
|
+
* Pass Vue's `h` render helper for runtime-only builds (Vite default):
|
|
34
|
+
* `const SwapWidget = createVueSwapWidget(h);`
|
|
35
|
+
*/
|
|
36
|
+
export declare function createVueSwapWidget(h?: VueRenderFactory, mountOptions?: MountEdelSwapWidgetOptions): VueSwapWidgetComponentDefinition;
|
|
37
|
+
/**
|
|
38
|
+
* Ready-to-register Vue >= 3 component definition.
|
|
39
|
+
* For runtime-only Vite builds (without the Vue template compiler), prefer
|
|
40
|
+
* `createVueSwapWidget(h)` or the `vEdelSwapWidget` directive.
|
|
41
|
+
*/
|
|
42
|
+
export declare const EdelSwapWidgetVue: VueSwapWidgetComponentDefinition;
|
|
43
|
+
export interface VueDirectiveBinding<T> {
|
|
44
|
+
value?: T;
|
|
45
|
+
}
|
|
46
|
+
export interface VueSwapWidgetDirective {
|
|
47
|
+
mounted(el: HTMLElement, binding: VueDirectiveBinding<EdelSwapWidgetProps>): void;
|
|
48
|
+
updated(el: HTMLElement, binding: VueDirectiveBinding<EdelSwapWidgetProps>): void;
|
|
49
|
+
unmounted(el: HTMLElement): void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates a Vue >= 3 custom directive (`v-edel-swap-widget="props"`) that works in
|
|
53
|
+
* both runtime-only and full Vue builds with zero dependency on `vue`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createVueSwapWidgetDirective(mountOptions?: MountEdelSwapWidgetOptions): VueSwapWidgetDirective;
|
|
56
|
+
export declare const vEdelSwapWidget: VueSwapWidgetDirective;
|
|
57
|
+
export { type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
|
58
|
+
export type { EdelSwapWidgetProps } from "../widget.js";
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { mountEdelSwapWidget, } from "../embed.js";
|
|
2
|
+
const VUE_PROP_NAMES = [
|
|
3
|
+
"integratorId",
|
|
4
|
+
"partnerId",
|
|
5
|
+
"theme",
|
|
6
|
+
"network",
|
|
7
|
+
"defaultFrom",
|
|
8
|
+
"defaultTo",
|
|
9
|
+
"defaultFromAsset",
|
|
10
|
+
"defaultToAsset",
|
|
11
|
+
"defaultMaxSlippageBps",
|
|
12
|
+
"recipientAddress",
|
|
13
|
+
"integratorFeeBps",
|
|
14
|
+
"client",
|
|
15
|
+
"initialQuote",
|
|
16
|
+
"initialOrder",
|
|
17
|
+
"onQuote",
|
|
18
|
+
"onSlippageChange",
|
|
19
|
+
"onSwapComplete",
|
|
20
|
+
"onOrderSubmitted",
|
|
21
|
+
"onError",
|
|
22
|
+
"style",
|
|
23
|
+
];
|
|
24
|
+
const VUE_EMIT_NAMES = [
|
|
25
|
+
"quote",
|
|
26
|
+
"swapComplete",
|
|
27
|
+
"orderSubmitted",
|
|
28
|
+
"error",
|
|
29
|
+
"slippageChange",
|
|
30
|
+
];
|
|
31
|
+
function buildVueWidgetProps(ctx) {
|
|
32
|
+
const raw = ctx.$props ?? {};
|
|
33
|
+
const emit = ctx.$emit;
|
|
34
|
+
return {
|
|
35
|
+
...raw,
|
|
36
|
+
onQuote: (quote) => {
|
|
37
|
+
raw.onQuote?.(quote);
|
|
38
|
+
emit?.("quote", quote);
|
|
39
|
+
},
|
|
40
|
+
onSwapComplete: (order) => {
|
|
41
|
+
raw.onSwapComplete?.(order);
|
|
42
|
+
emit?.("swapComplete", order);
|
|
43
|
+
},
|
|
44
|
+
onOrderSubmitted: (order) => {
|
|
45
|
+
raw.onOrderSubmitted?.(order);
|
|
46
|
+
emit?.("orderSubmitted", order);
|
|
47
|
+
},
|
|
48
|
+
onError: (error) => {
|
|
49
|
+
raw.onError?.(error);
|
|
50
|
+
emit?.("error", error);
|
|
51
|
+
},
|
|
52
|
+
onSlippageChange: (maxSlippageBps) => {
|
|
53
|
+
raw.onSlippageChange?.(maxSlippageBps);
|
|
54
|
+
emit?.("slippageChange", maxSlippageBps);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a typed Vue >= 3 component backed by `mountEdelSwapWidget` with Shadow DOM
|
|
60
|
+
* CSS isolation and zero runtime dependency on `vue`.
|
|
61
|
+
*
|
|
62
|
+
* Pass Vue's `h` render helper for runtime-only builds (Vite default):
|
|
63
|
+
* `const SwapWidget = createVueSwapWidget(h);`
|
|
64
|
+
*/
|
|
65
|
+
export function createVueSwapWidget(h, mountOptions = {}) {
|
|
66
|
+
const component = {
|
|
67
|
+
name: "EdelSwapWidget",
|
|
68
|
+
props: VUE_PROP_NAMES,
|
|
69
|
+
emits: VUE_EMIT_NAMES,
|
|
70
|
+
mounted() {
|
|
71
|
+
if (!this.$el)
|
|
72
|
+
return;
|
|
73
|
+
this._edelWidget = mountEdelSwapWidget(this.$el, buildVueWidgetProps(this), {
|
|
74
|
+
shadowDom: true,
|
|
75
|
+
...mountOptions,
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
updated() {
|
|
79
|
+
this._edelWidget?.update(buildVueWidgetProps(this));
|
|
80
|
+
},
|
|
81
|
+
beforeUnmount() {
|
|
82
|
+
this._edelWidget?.unmount();
|
|
83
|
+
this._edelWidget = null;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
if (h) {
|
|
87
|
+
component.render = () => h("div", { class: "edel-swap-widget-vue-host" });
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
component.template = '<div class="edel-swap-widget-vue-host"></div>';
|
|
91
|
+
}
|
|
92
|
+
return component;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Ready-to-register Vue >= 3 component definition.
|
|
96
|
+
* For runtime-only Vite builds (without the Vue template compiler), prefer
|
|
97
|
+
* `createVueSwapWidget(h)` or the `vEdelSwapWidget` directive.
|
|
98
|
+
*/
|
|
99
|
+
export const EdelSwapWidgetVue = createVueSwapWidget();
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Vue >= 3 custom directive (`v-edel-swap-widget="props"`) that works in
|
|
102
|
+
* both runtime-only and full Vue builds with zero dependency on `vue`.
|
|
103
|
+
*/
|
|
104
|
+
export function createVueSwapWidgetDirective(mountOptions = {}) {
|
|
105
|
+
const instances = new WeakMap();
|
|
106
|
+
return {
|
|
107
|
+
mounted(el, binding) {
|
|
108
|
+
const mounted = mountEdelSwapWidget(el, binding.value ?? {}, {
|
|
109
|
+
shadowDom: true,
|
|
110
|
+
...mountOptions,
|
|
111
|
+
});
|
|
112
|
+
instances.set(el, mounted);
|
|
113
|
+
},
|
|
114
|
+
updated(el, binding) {
|
|
115
|
+
instances.get(el)?.update(binding.value ?? {});
|
|
116
|
+
},
|
|
117
|
+
unmounted(el) {
|
|
118
|
+
instances.get(el)?.unmount();
|
|
119
|
+
instances.delete(el);
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export const vEdelSwapWidget = createVueSwapWidgetDirective();
|
|
124
|
+
export { mountEdelSwapWidget, registerEdelSwapWidgetElement, } from "../embed.js";
|
package/dist/embed.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type { EdelNetwork } from "@edel-finance/sdk";
|
|
2
|
-
import
|
|
1
|
+
import type { EdelNetwork, SwapOrder, SwapQuote } from "@edel-finance/sdk";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { type SwapWidgetTheme } from "./theme.js";
|
|
4
|
+
import { type EdelSwapWidgetProps } from "./widget.js";
|
|
3
5
|
export interface SwapWidgetEmbedOptions {
|
|
4
6
|
/** Target Edel network (MainNet or TestNet). Defaults to "mainnet". */
|
|
5
7
|
network?: EdelNetwork;
|
|
@@ -17,6 +19,50 @@ export interface SwapWidgetEmbedOptions {
|
|
|
17
19
|
embedOrigin?: string;
|
|
18
20
|
}
|
|
19
21
|
export declare const SWAP_WIDGET_ORIGINS: Record<EdelNetwork, string>;
|
|
22
|
+
export declare const EDEL_SWAP_WIDGET_TAG: "edel-swap-widget";
|
|
23
|
+
export declare const EDEL_SWAP_WIDGET_EVENTS: {
|
|
24
|
+
readonly quote: "edel-swap-quote";
|
|
25
|
+
readonly complete: "edel-swap-complete";
|
|
26
|
+
readonly error: "edel-swap-error";
|
|
27
|
+
readonly slippageChange: "edel-swap-slippage-change";
|
|
28
|
+
};
|
|
29
|
+
export interface EdelSwapWidgetErrorDetail {
|
|
30
|
+
message: string;
|
|
31
|
+
error: Error;
|
|
32
|
+
}
|
|
33
|
+
export interface EdelSwapWidgetSlippageDetail {
|
|
34
|
+
maxSlippageBps: number;
|
|
35
|
+
}
|
|
36
|
+
export interface EdelSwapWidgetEventMap {
|
|
37
|
+
"edel-swap-quote": CustomEvent<SwapQuote>;
|
|
38
|
+
"edel-swap-complete": CustomEvent<SwapOrder>;
|
|
39
|
+
"edel-swap-error": CustomEvent<EdelSwapWidgetErrorDetail>;
|
|
40
|
+
"edel-swap-slippage-change": CustomEvent<EdelSwapWidgetSlippageDetail>;
|
|
41
|
+
}
|
|
42
|
+
export interface MountedEdelSwapWidget {
|
|
43
|
+
/** Update widget props without remounting the underlying React root. */
|
|
44
|
+
update(nextProps: Partial<EdelSwapWidgetProps>): void;
|
|
45
|
+
/** Unmount the widget and release DOM/React resources. */
|
|
46
|
+
unmount(): void;
|
|
47
|
+
/** Return the currently applied props snapshot. */
|
|
48
|
+
getProps(): EdelSwapWidgetProps;
|
|
49
|
+
}
|
|
50
|
+
export interface WidgetRootLike {
|
|
51
|
+
render(element: ReturnType<typeof createElement>): void;
|
|
52
|
+
unmount(): void;
|
|
53
|
+
}
|
|
54
|
+
export interface MountEdelSwapWidgetOptions {
|
|
55
|
+
/** Attach and render inside an open Shadow DOM root for full CSS isolation. Defaults to true. */
|
|
56
|
+
shadowDom?: boolean;
|
|
57
|
+
/** Dispatch standard CustomEvents on the target element for widget lifecycle events. */
|
|
58
|
+
dispatchEvents?: boolean;
|
|
59
|
+
/** Optional custom React root factory (primarily for headless unit tests). */
|
|
60
|
+
createRootImpl?: (container: Element) => WidgetRootLike;
|
|
61
|
+
}
|
|
62
|
+
export interface EdelSwapWidgetElement extends HTMLElement {
|
|
63
|
+
widgetProps: Partial<EdelSwapWidgetProps>;
|
|
64
|
+
setProps(nextProps: Partial<EdelSwapWidgetProps>): void;
|
|
65
|
+
}
|
|
20
66
|
/**
|
|
21
67
|
* Builds the canonical iframe URL for embedding the Edel Swap Widget in
|
|
22
68
|
* partner dApps (SuperNova, Loop, institutional portals) or standalone pages.
|
|
@@ -28,9 +74,16 @@ export declare function buildSwapWidgetUrl(options?: SwapWidgetEmbedOptions): st
|
|
|
28
74
|
export declare function buildSwapWidgetEmbedSnippet(options?: SwapWidgetEmbedOptions & {
|
|
29
75
|
height?: number | string;
|
|
30
76
|
}): string;
|
|
77
|
+
/**
|
|
78
|
+
* Imperatively mounts the interactive `<EdelSwapWidget />` into any DOM element,
|
|
79
|
+
* with Shadow DOM CSS isolation enabled by default so host styles in Vue, Svelte,
|
|
80
|
+
* Angular, SolidJS, or plain HTML never collide with the widget.
|
|
81
|
+
*/
|
|
82
|
+
export declare function mountEdelSwapWidget(target: HTMLElement, props?: EdelSwapWidgetProps, options?: MountEdelSwapWidgetOptions): MountedEdelSwapWidget;
|
|
31
83
|
/**
|
|
32
84
|
* Registers the `<edel-swap-widget>` Custom Element (Web Component) with Shadow DOM
|
|
33
|
-
* CSS isolation
|
|
34
|
-
*
|
|
85
|
+
* CSS isolation. Mounts the full interactive React `<EdelSwapWidget />` inside an open
|
|
86
|
+
* Shadow Root and dispatches standard `CustomEvent`s (`edel-swap-quote`,
|
|
87
|
+
* `edel-swap-complete`, `edel-swap-error`, `edel-swap-slippage-change`).
|
|
35
88
|
*/
|
|
36
|
-
export declare function registerEdelSwapWidgetElement(tagName?: string): void;
|
|
89
|
+
export declare function registerEdelSwapWidgetElement(tagName?: string, options?: Pick<MountEdelSwapWidgetOptions, "createRootImpl">): void;
|
package/dist/embed.js
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
+
import { createElement } from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import { getIsolatedWidgetCss } from "./theme.js";
|
|
4
|
+
import { EdelSwapWidget } from "./widget.js";
|
|
1
5
|
export const SWAP_WIDGET_ORIGINS = {
|
|
2
6
|
mainnet: "https://app.edel.finance/embed/swap",
|
|
3
7
|
testnet: "https://testnet.edel.finance/embed/swap",
|
|
4
8
|
};
|
|
9
|
+
export const EDEL_SWAP_WIDGET_TAG = "edel-swap-widget";
|
|
10
|
+
export const EDEL_SWAP_WIDGET_EVENTS = {
|
|
11
|
+
quote: "edel-swap-quote",
|
|
12
|
+
complete: "edel-swap-complete",
|
|
13
|
+
error: "edel-swap-error",
|
|
14
|
+
slippageChange: "edel-swap-slippage-change",
|
|
15
|
+
};
|
|
5
16
|
/**
|
|
6
17
|
* Builds the canonical iframe URL for embedding the Edel Swap Widget in
|
|
7
18
|
* partner dApps (SuperNova, Loop, institutional portals) or standalone pages.
|
|
@@ -34,51 +45,220 @@ export function buildSwapWidgetEmbedSnippet(options = {}) {
|
|
|
34
45
|
` style="border:0;border-radius:18px;overflow:hidden;max-width:440px;"></iframe>`,
|
|
35
46
|
].join("\n");
|
|
36
47
|
}
|
|
48
|
+
function buildShadowStyles(theme) {
|
|
49
|
+
return `:host{display:block;max-width:440px;width:100%;}\n${getIsolatedWidgetCss(theme)}`;
|
|
50
|
+
}
|
|
51
|
+
function dispatchWidgetEvent(target, type, detail) {
|
|
52
|
+
if (typeof CustomEvent === "undefined" || typeof target.dispatchEvent !== "function") {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
target.dispatchEvent(new CustomEvent(type, {
|
|
56
|
+
detail,
|
|
57
|
+
bubbles: true,
|
|
58
|
+
composed: true,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
function decoratePropsWithEvents(target, props, dispatchEvents) {
|
|
62
|
+
if (!dispatchEvents) {
|
|
63
|
+
return props;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
...props,
|
|
67
|
+
onQuote: (quote) => {
|
|
68
|
+
props.onQuote?.(quote);
|
|
69
|
+
dispatchWidgetEvent(target, EDEL_SWAP_WIDGET_EVENTS.quote, quote);
|
|
70
|
+
},
|
|
71
|
+
onSwapComplete: (order) => {
|
|
72
|
+
props.onSwapComplete?.(order);
|
|
73
|
+
dispatchWidgetEvent(target, EDEL_SWAP_WIDGET_EVENTS.complete, order);
|
|
74
|
+
},
|
|
75
|
+
onError: (error) => {
|
|
76
|
+
props.onError?.(error);
|
|
77
|
+
dispatchWidgetEvent(target, EDEL_SWAP_WIDGET_EVENTS.error, {
|
|
78
|
+
message: error.message,
|
|
79
|
+
error,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
onSlippageChange: (maxSlippageBps) => {
|
|
83
|
+
props.onSlippageChange?.(maxSlippageBps);
|
|
84
|
+
dispatchWidgetEvent(target, EDEL_SWAP_WIDGET_EVENTS.slippageChange, {
|
|
85
|
+
maxSlippageBps,
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Imperatively mounts the interactive `<EdelSwapWidget />` into any DOM element,
|
|
92
|
+
* with Shadow DOM CSS isolation enabled by default so host styles in Vue, Svelte,
|
|
93
|
+
* Angular, SolidJS, or plain HTML never collide with the widget.
|
|
94
|
+
*/
|
|
95
|
+
export function mountEdelSwapWidget(target, props = {}, options = {}) {
|
|
96
|
+
const useShadowDom = options.shadowDom !== false;
|
|
97
|
+
const dispatchEvents = options.dispatchEvents === true;
|
|
98
|
+
const createRootFn = options.createRootImpl ?? ((container) => createRoot(container));
|
|
99
|
+
let currentProps = { ...props };
|
|
100
|
+
let unmounted = false;
|
|
101
|
+
const ownerDoc = target.ownerDocument ?? (typeof document !== "undefined" ? document : undefined);
|
|
102
|
+
if (!ownerDoc) {
|
|
103
|
+
throw new Error("mountEdelSwapWidget requires a browser DOM environment.");
|
|
104
|
+
}
|
|
105
|
+
let styleEl = null;
|
|
106
|
+
let hostRoot = target;
|
|
107
|
+
if (useShadowDom && (target.shadowRoot || typeof target.attachShadow === "function")) {
|
|
108
|
+
const shadow = target.shadowRoot ?? target.attachShadow({ mode: "open" });
|
|
109
|
+
hostRoot = shadow;
|
|
110
|
+
const existingStyle = typeof shadow.querySelector === "function"
|
|
111
|
+
? shadow.querySelector('style[data-edel-swap-widget-styles="true"]')
|
|
112
|
+
: null;
|
|
113
|
+
if (existingStyle) {
|
|
114
|
+
styleEl = existingStyle;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
styleEl = ownerDoc.createElement("style");
|
|
118
|
+
styleEl.setAttribute("data-edel-swap-widget-styles", "true");
|
|
119
|
+
shadow.appendChild(styleEl);
|
|
120
|
+
}
|
|
121
|
+
styleEl.textContent = buildShadowStyles(currentProps.theme ?? "dark");
|
|
122
|
+
}
|
|
123
|
+
const mountNode = ownerDoc.createElement("div");
|
|
124
|
+
mountNode.setAttribute("data-edel-swap-widget-mount", "true");
|
|
125
|
+
hostRoot.appendChild(mountNode);
|
|
126
|
+
const root = createRootFn(mountNode);
|
|
127
|
+
const renderCurrent = () => {
|
|
128
|
+
if (unmounted)
|
|
129
|
+
return;
|
|
130
|
+
if (styleEl) {
|
|
131
|
+
styleEl.textContent = buildShadowStyles(currentProps.theme ?? "dark");
|
|
132
|
+
}
|
|
133
|
+
const effectiveProps = decoratePropsWithEvents(target, currentProps, dispatchEvents);
|
|
134
|
+
root.render(createElement(EdelSwapWidget, effectiveProps));
|
|
135
|
+
};
|
|
136
|
+
renderCurrent();
|
|
137
|
+
return {
|
|
138
|
+
update(nextProps) {
|
|
139
|
+
if (unmounted)
|
|
140
|
+
return;
|
|
141
|
+
currentProps = { ...currentProps, ...nextProps };
|
|
142
|
+
renderCurrent();
|
|
143
|
+
},
|
|
144
|
+
unmount() {
|
|
145
|
+
if (unmounted)
|
|
146
|
+
return;
|
|
147
|
+
unmounted = true;
|
|
148
|
+
root.unmount();
|
|
149
|
+
if (mountNode.parentNode) {
|
|
150
|
+
mountNode.parentNode.removeChild(mountNode);
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
getProps() {
|
|
154
|
+
return { ...currentProps };
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function parseOptionalInt(raw) {
|
|
159
|
+
if (raw === null || raw.trim() === "")
|
|
160
|
+
return undefined;
|
|
161
|
+
const n = Number(raw);
|
|
162
|
+
return Number.isFinite(n) ? Math.round(n) : undefined;
|
|
163
|
+
}
|
|
164
|
+
function readElementAttributeProps(el) {
|
|
165
|
+
const networkAttr = el.getAttribute("network");
|
|
166
|
+
const themeAttr = el.getAttribute("theme");
|
|
167
|
+
const props = {
|
|
168
|
+
network: networkAttr === "testnet" ? "testnet" : "mainnet",
|
|
169
|
+
theme: themeAttr === "light" ? "light" : "dark",
|
|
170
|
+
};
|
|
171
|
+
const integratorId = el.getAttribute("integrator-id") ?? el.getAttribute("partner-id");
|
|
172
|
+
if (integratorId)
|
|
173
|
+
props.integratorId = integratorId;
|
|
174
|
+
const defaultFrom = el.getAttribute("from");
|
|
175
|
+
if (defaultFrom)
|
|
176
|
+
props.defaultFrom = defaultFrom;
|
|
177
|
+
const defaultTo = el.getAttribute("to");
|
|
178
|
+
if (defaultTo)
|
|
179
|
+
props.defaultTo = defaultTo;
|
|
180
|
+
const maxSlippageBps = parseOptionalInt(el.getAttribute("max-slippage-bps"));
|
|
181
|
+
if (maxSlippageBps !== undefined)
|
|
182
|
+
props.defaultMaxSlippageBps = maxSlippageBps;
|
|
183
|
+
const integratorFeeBps = parseOptionalInt(el.getAttribute("integrator-fee-bps"));
|
|
184
|
+
if (integratorFeeBps !== undefined)
|
|
185
|
+
props.integratorFeeBps = integratorFeeBps;
|
|
186
|
+
const recipientAddress = el.getAttribute("recipient-address");
|
|
187
|
+
if (recipientAddress)
|
|
188
|
+
props.recipientAddress = recipientAddress;
|
|
189
|
+
return props;
|
|
190
|
+
}
|
|
37
191
|
/**
|
|
38
192
|
* Registers the `<edel-swap-widget>` Custom Element (Web Component) with Shadow DOM
|
|
39
|
-
* CSS isolation
|
|
40
|
-
*
|
|
193
|
+
* CSS isolation. Mounts the full interactive React `<EdelSwapWidget />` inside an open
|
|
194
|
+
* Shadow Root and dispatches standard `CustomEvent`s (`edel-swap-quote`,
|
|
195
|
+
* `edel-swap-complete`, `edel-swap-error`, `edel-swap-slippage-change`).
|
|
41
196
|
*/
|
|
42
|
-
export function registerEdelSwapWidgetElement(tagName =
|
|
43
|
-
if (typeof
|
|
197
|
+
export function registerEdelSwapWidgetElement(tagName = EDEL_SWAP_WIDGET_TAG, options = {}) {
|
|
198
|
+
if (typeof customElements === "undefined" || typeof HTMLElement === "undefined") {
|
|
44
199
|
return;
|
|
45
200
|
}
|
|
46
201
|
if (customElements.get(tagName)) {
|
|
47
202
|
return;
|
|
48
203
|
}
|
|
49
|
-
class
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
super();
|
|
53
|
-
this.shadow = this.attachShadow({ mode: "open" });
|
|
54
|
-
}
|
|
204
|
+
class EdelSwapWidgetCustomElement extends HTMLElement {
|
|
205
|
+
mounted = null;
|
|
206
|
+
customProps = {};
|
|
55
207
|
static get observedAttributes() {
|
|
56
|
-
return [
|
|
208
|
+
return [
|
|
209
|
+
"network",
|
|
210
|
+
"integrator-id",
|
|
211
|
+
"partner-id",
|
|
212
|
+
"from",
|
|
213
|
+
"to",
|
|
214
|
+
"theme",
|
|
215
|
+
"max-slippage-bps",
|
|
216
|
+
"integrator-fee-bps",
|
|
217
|
+
"recipient-address",
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
get widgetProps() {
|
|
221
|
+
return { ...this.customProps };
|
|
222
|
+
}
|
|
223
|
+
set widgetProps(nextProps) {
|
|
224
|
+
this.customProps = { ...nextProps };
|
|
225
|
+
this.syncMount();
|
|
226
|
+
}
|
|
227
|
+
setProps(nextProps) {
|
|
228
|
+
this.customProps = { ...this.customProps, ...nextProps };
|
|
229
|
+
this.syncMount();
|
|
57
230
|
}
|
|
58
231
|
connectedCallback() {
|
|
59
|
-
this.
|
|
232
|
+
this.syncMount();
|
|
60
233
|
}
|
|
61
234
|
attributeChangedCallback() {
|
|
62
|
-
this.
|
|
235
|
+
if (this.mounted) {
|
|
236
|
+
this.syncMount();
|
|
237
|
+
}
|
|
63
238
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
239
|
+
disconnectedCallback() {
|
|
240
|
+
this.mounted?.unmount();
|
|
241
|
+
this.mounted = null;
|
|
242
|
+
}
|
|
243
|
+
resolveMergedProps() {
|
|
244
|
+
return {
|
|
245
|
+
...readElementAttributeProps(this),
|
|
246
|
+
...this.customProps,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
syncMount() {
|
|
250
|
+
const merged = this.resolveMergedProps();
|
|
251
|
+
if (!this.mounted) {
|
|
252
|
+
this.mounted = mountEdelSwapWidget(this, merged, {
|
|
253
|
+
shadowDom: true,
|
|
254
|
+
dispatchEvents: true,
|
|
255
|
+
createRootImpl: options.createRootImpl,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
this.mounted.update(merged);
|
|
260
|
+
}
|
|
81
261
|
}
|
|
82
262
|
}
|
|
83
|
-
customElements.define(tagName,
|
|
263
|
+
customElements.define(tagName, EdelSwapWidgetCustomElement);
|
|
84
264
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { buildSwapWidgetEmbedSnippet, buildSwapWidgetUrl, registerEdelSwapWidgetElement, SWAP_WIDGET_ORIGINS, type SwapWidgetEmbedOptions, } from "./embed.js";
|
|
1
|
+
export { buildSwapWidgetEmbedSnippet, buildSwapWidgetUrl, EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, type EdelSwapWidgetElement, type EdelSwapWidgetErrorDetail, type EdelSwapWidgetEventMap, type EdelSwapWidgetSlippageDetail, type MountEdelSwapWidgetOptions, type MountedEdelSwapWidget, mountEdelSwapWidget, registerEdelSwapWidgetElement, SWAP_WIDGET_ORIGINS, type SwapWidgetEmbedOptions, type WidgetRootLike, } from "./embed.js";
|
|
2
2
|
export { ALPINE_FOREST_THEMES, type AlpineThemeTokens, getIsolatedWidgetCss, type SwapWidgetTheme, } from "./theme.js";
|
|
3
3
|
export { DEFAULT_SWAP_ASSETS, DEFAULT_WIDGET_QUOTE_TTL_SECONDS, DEFAULT_WIDGET_SLIPPAGE_BPS, EdelSwapWidget, type EdelSwapWidgetProps, formatBps, formatPresetPercent, getStepState, parseCustomSlippagePercent, resolveInitialSlippageBps, SLIPPAGE_PRESETS_BPS, SWAP_STATUS_STEPS, } from "./widget.js";
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { buildSwapWidgetEmbedSnippet, buildSwapWidgetUrl, registerEdelSwapWidgetElement, SWAP_WIDGET_ORIGINS, } from "./embed.js";
|
|
1
|
+
export { buildSwapWidgetEmbedSnippet, buildSwapWidgetUrl, EDEL_SWAP_WIDGET_EVENTS, EDEL_SWAP_WIDGET_TAG, mountEdelSwapWidget, registerEdelSwapWidgetElement, SWAP_WIDGET_ORIGINS, } from "./embed.js";
|
|
2
2
|
export { ALPINE_FOREST_THEMES, getIsolatedWidgetCss, } from "./theme.js";
|
|
3
3
|
export { DEFAULT_SWAP_ASSETS, DEFAULT_WIDGET_QUOTE_TTL_SECONDS, DEFAULT_WIDGET_SLIPPAGE_BPS, EdelSwapWidget, formatBps, formatPresetPercent, getStepState, parseCustomSlippagePercent, resolveInitialSlippageBps, SLIPPAGE_PRESETS_BPS, SWAP_STATUS_STEPS, } from "./widget.js";
|
package/dist/widget.d.ts
CHANGED
|
@@ -50,6 +50,10 @@ export interface EdelSwapWidgetProps {
|
|
|
50
50
|
initialQuote?: SwapQuote | null;
|
|
51
51
|
/** Optional initial active order (useful for SSR preview or testing). */
|
|
52
52
|
initialOrder?: SwapOrder | null;
|
|
53
|
+
/** Callback fired whenever a live quote is fetched. */
|
|
54
|
+
onQuote?: (quote: SwapQuote) => void;
|
|
55
|
+
/** Callback fired when the user updates max slippage (in basis points). */
|
|
56
|
+
onSlippageChange?: (maxSlippageBps: number) => void;
|
|
53
57
|
/** Callback fired when an active swap reaches the "settled" state. */
|
|
54
58
|
onSwapComplete?: (order: SwapOrder) => void;
|
|
55
59
|
/** Callback fired when an order is submitted via POST /v2/swaps/orders/:swapId/submit. */
|
package/dist/widget.js
CHANGED
|
@@ -64,7 +64,7 @@ export function formatPresetPercent(bps) {
|
|
|
64
64
|
return `${(bps / 100).toFixed(1)}%`;
|
|
65
65
|
}
|
|
66
66
|
export function EdelSwapWidget(props) {
|
|
67
|
-
const { integratorId, partnerId, theme = "dark", network = "mainnet", defaultFrom, defaultTo, defaultFromAsset, defaultToAsset, defaultMaxSlippageBps, recipientAddress = "", integratorFeeBps, client: providedClient, initialQuote = null, initialOrder = null, onSwapComplete, onOrderSubmitted, onError, style, } = props;
|
|
67
|
+
const { integratorId, partnerId, theme = "dark", network = "mainnet", defaultFrom, defaultTo, defaultFromAsset, defaultToAsset, defaultMaxSlippageBps, recipientAddress = "", integratorFeeBps, client: providedClient, initialQuote = null, initialOrder = null, onQuote, onSlippageChange, onSwapComplete, onOrderSubmitted, onError, style, } = props;
|
|
68
68
|
const effectiveIntegratorId = integratorId ?? partnerId;
|
|
69
69
|
const client = useMemo(() => providedClient ?? new EdelClient({ network }), [providedClient, network]);
|
|
70
70
|
const initialSlippage = resolveInitialSlippageBps(defaultMaxSlippageBps);
|
|
@@ -141,6 +141,7 @@ export function EdelSwapWidget(props) {
|
|
|
141
141
|
const fallbackTtl = q.ttlSeconds || DEFAULT_WIDGET_QUOTE_TTL_SECONDS;
|
|
142
142
|
const secs = Math.max(0, Math.ceil((q.expiresAtEpochMs - Date.now()) / 1000));
|
|
143
143
|
setRemainingSec(secs || fallbackTtl);
|
|
144
|
+
onQuote?.(q);
|
|
144
145
|
})
|
|
145
146
|
.catch((err) => {
|
|
146
147
|
if (cancelled)
|
|
@@ -165,6 +166,7 @@ export function EdelSwapWidget(props) {
|
|
|
165
166
|
effectiveIntegratorId,
|
|
166
167
|
integratorFeeBps,
|
|
167
168
|
recipient,
|
|
169
|
+
onQuote,
|
|
168
170
|
onError,
|
|
169
171
|
]);
|
|
170
172
|
// Countdown quote TTL (20s default window)
|
|
@@ -222,6 +224,7 @@ export function EdelSwapWidget(props) {
|
|
|
222
224
|
setCustomMode(false);
|
|
223
225
|
setCustomSlippageInput("");
|
|
224
226
|
setMaxSlippageBps(bps);
|
|
227
|
+
onSlippageChange?.(bps);
|
|
225
228
|
};
|
|
226
229
|
const handleCustomSlippageChange = (raw) => {
|
|
227
230
|
setCustomMode(true);
|
|
@@ -229,6 +232,7 @@ export function EdelSwapWidget(props) {
|
|
|
229
232
|
const parsed = parseCustomSlippagePercent(raw);
|
|
230
233
|
if (parsed !== null) {
|
|
231
234
|
setMaxSlippageBps(parsed);
|
|
235
|
+
onSlippageChange?.(parsed);
|
|
232
236
|
}
|
|
233
237
|
};
|
|
234
238
|
const handleSwap = async () => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edel-finance/swap-widget",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Embeddable React
|
|
3
|
+
"version": "0.1.4-alpha",
|
|
4
|
+
"description": "Embeddable React, Vue 3, Svelte 5, Angular, SolidJS, Shadow DOM Web Component, and iframe Swap Widget for Edel Markets.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -9,6 +9,22 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./vue": {
|
|
14
|
+
"types": "./dist/adapters/vue.d.ts",
|
|
15
|
+
"import": "./dist/adapters/vue.js"
|
|
16
|
+
},
|
|
17
|
+
"./svelte": {
|
|
18
|
+
"types": "./dist/adapters/svelte.d.ts",
|
|
19
|
+
"import": "./dist/adapters/svelte.js"
|
|
20
|
+
},
|
|
21
|
+
"./angular": {
|
|
22
|
+
"types": "./dist/adapters/angular.d.ts",
|
|
23
|
+
"import": "./dist/adapters/angular.js"
|
|
24
|
+
},
|
|
25
|
+
"./solid": {
|
|
26
|
+
"types": "./dist/adapters/solid.d.ts",
|
|
27
|
+
"import": "./dist/adapters/solid.js"
|
|
12
28
|
}
|
|
13
29
|
},
|
|
14
30
|
"files": [
|
|
@@ -16,21 +32,28 @@
|
|
|
16
32
|
"README.md"
|
|
17
33
|
],
|
|
18
34
|
"dependencies": {
|
|
19
|
-
"@edel-finance/sdk": "0.
|
|
35
|
+
"@edel-finance/sdk": "0.1.4-alpha"
|
|
20
36
|
},
|
|
21
37
|
"peerDependencies": {
|
|
22
|
-
"react": ">=18.2.0"
|
|
38
|
+
"react": ">=18.2.0",
|
|
39
|
+
"react-dom": ">=18.2.0"
|
|
23
40
|
},
|
|
24
41
|
"devDependencies": {
|
|
42
|
+
"@storybook/react-vite": "^11.0.2",
|
|
25
43
|
"@types/react": "^19.0.0",
|
|
26
44
|
"@types/react-dom": "^19.0.0",
|
|
27
45
|
"@typescript/native-preview": "^7.0.0-dev",
|
|
46
|
+
"@vitejs/plugin-react": "^6.3.1",
|
|
28
47
|
"react": "^19.0.0",
|
|
29
|
-
"react-dom": "^19.0.0"
|
|
48
|
+
"react-dom": "^19.0.0",
|
|
49
|
+
"storybook": "^11.0.2",
|
|
50
|
+
"vite": "^8.4.1"
|
|
30
51
|
},
|
|
31
52
|
"license": "MIT",
|
|
32
53
|
"scripts": {
|
|
33
54
|
"build": "tsgo -p tsconfig.json",
|
|
55
|
+
"build-storybook": "storybook build -o storybook-static",
|
|
56
|
+
"storybook": "storybook dev -p 6006",
|
|
34
57
|
"test": "bun test",
|
|
35
58
|
"typecheck": "tsgo --noEmit -p tsconfig.json"
|
|
36
59
|
}
|