@cowprotocol/widget-react 0.5.0 → 0.6.0
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 +1 -1
- package/index.js +1 -1
- package/index.mjs +180 -157
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ Prepare the config for the widget:
|
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
26
|
const cowSwapWidgetParams: CowSwapWidgetParams = {
|
|
27
|
-
appCode: '
|
|
27
|
+
appCode: 'NAME-OF-YOU-APP', // Add here the name of your app. e.g. "Pig Swap"
|
|
28
28
|
width: '600px',
|
|
29
29
|
height: '700px',
|
|
30
30
|
tradeType: 'swap',
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var U=Object.defineProperty;var V=(e,t,n)=>t in e?U(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var p=(e,t,n)=>(V(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),u=require("react"),D="cowSwapWidget";function g(e,t,n){const r={key:D,method:t,...typeof n=="object"?n:{}};e.postMessage(r,"*")}function R(e,t,n){const s=r=>{!b(r.data)||r.data.key!==D||r.data.method!==t||n(r.data)};return e.addEventListener("message",s),s}function b(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function L(e,t,n){e.removeEventListener("message",n)}function _(e,t){e.removeEventListener("message",t)}var E=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(E||{}),m=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(m||{}),v=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(v||{});const N=["connect","disconnect","close","chainChanged","accountsChanged"];class F{constructor(t){p(this,"ethereumProvider",null);p(this,"requestWaitingForConnection",{});p(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,L(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():R(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),N.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:r}=t;if(!this.ethereumProvider||!n)return;(r==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:o}}))}onProviderEvent(t,n){g(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}const S="_";function H(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=y(e),s=O(e);return t+"/#"+n+"?"+s}function y(e){const{chainId:t=1,sell:n,buy:s,tradeType:r=v.SWAP}=e,a=[(n==null?void 0:n.asset)||S,(s==null?void 0:s.asset)||S].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function O(e){const{sell:t,buy:n,theme:s}=e,r=new URLSearchParams;return t!=null&&t.amount&&r.append("sellAmount",t.amount),n!=null&&n.amount&&r.append("buyAmount",n.amount),s&&r.append("theme",typeof s=="string"?s:s.baseTheme),r}class q{constructor(){p(this,"subscriptions",{})}on(t){const{event:n,handler:s}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s)}off(t){const{event:n,handler:s}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(r=>r!==s))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}class k{constructor(t,n=[]){p(this,"eventEmitter",new q);p(this,"listeners",[]);p(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=R(this.contentWindow,E.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){_(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class x{constructor(t,n){p(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{G(s.data)&&($(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Q(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function G(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function $(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function Q(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const M="640px",j="450px",B=20;function T(e,t){const{params:n,provider:s,listeners:r}=t;let a=s;const o=J(n);e.innerHTML="",e.appendChild(o);const{contentWindow:c}=o;if(!c)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const l=[];l.push(K(c,n.appCode)),l.push(Y(o,n.height));const f=new k(window,r);let d=C(c,null,a);o.addEventListener("load",()=>I(c,n,a));const w=new x(window,c);return{updateParams:i=>I(c,i,a),updateListeners:i=>f.updateListeners(i),updateProvider:i=>{a=i,d=C(c,d,i)},destroy:()=>{d.disconnect(),f.stopListeningIframe(),l.forEach(i=>window.removeEventListener("message",i)),w.stopListening(),e.removeChild(o)}}}function C(e,t,n){t&&t.disconnect();const s=t||new F(e);return n&&s.onConnect(n),s}function J(e){const{width:t=j,height:n=M}=e,s=document.createElement("iframe");return s.src=H(e),s.width=t,s.height=n,s.style.border="0",s}function I(e,t,n){const s=!!n,r=y(t),a=O(t).toString();g(e,m.UPDATE_PARAMS,{urlParams:{pathname:r,search:a},appParams:{...t},hasProvider:s})}function K(e,t){const n=R(window,E.ACTIVATE,()=>{_(window,n),g(e,m.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})});return n}function Y(e,t=M){return R(window,E.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+B}px`:t})}function z(e){const{params:t,provider:n,listeners:s}=e,[r,a]=u.useState(null),o=u.useRef(null),c=u.useRef(n),l=u.useRef(s),f=u.useRef(null),d=u.useRef(null),w=u.useCallback((i,h)=>{try{console.log(`[WIDGET] ${i}`),h()}catch(A){const W=`Error ${i.toLowerCase()}`;console.error(`[WIDGET] ${W}`,A),a({message:W,error:A})}},[a]);return u.useEffect(()=>{if(!f.current||JSON.stringify(o.current)===JSON.stringify(t))return;const i=f.current,h=d.current;o.current=t,h===null?w("Creating a new widget",()=>{d.current=T(i,{params:t,provider:c.current,listeners:s}),l.current=s}):w("Updating the widget",()=>h.updateParams(t))},[t]),u.useEffect(()=>{if(!d.current||c.current===n)return;c.current=n;const i=f.current;i&&w("Updating the provider",()=>{var h;(h=d.current)==null||h.destroy(),d.current=T(i,{params:t,provider:c.current,listeners:s})})},[n]),u.useEffect(()=>{if(!d.current||l.current===s)return;const i=d.current;w("Updating the listeners",()=>i.updateListeners(s))},[s]),r?P.jsxs("div",{style:{color:"#ff3a3a"},children:[r.message,r.error.message&&P.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:r.error.message})]}):P.jsx("div",{ref:f})}exports.CowSwapWidget=z;exports.TradeType=v;exports.WidgetMethodsEmit=E;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.listenToMessageFromWindow=R;exports.postMessageToWindow=g;exports.stopListeningToMessageFromWindow=L;exports.stopListeningWindowListener=_;
|
package/index.mjs
CHANGED
|
@@ -1,270 +1,293 @@
|
|
|
1
1
|
var V = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { jsxs as
|
|
5
|
-
import { useState as H, useRef as
|
|
2
|
+
var M = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var u = (e, t, n) => (M(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
import { jsxs as N, jsx as v } from "react/jsx-runtime";
|
|
5
|
+
import { useState as H, useRef as m, useCallback as b, useEffect as g } from "react";
|
|
6
6
|
const W = "cowSwapWidget";
|
|
7
|
-
function
|
|
7
|
+
function R(e, t, n) {
|
|
8
8
|
const s = {
|
|
9
9
|
key: W,
|
|
10
|
-
method:
|
|
11
|
-
...typeof
|
|
10
|
+
method: t,
|
|
11
|
+
...typeof n == "object" ? n : {}
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
e.postMessage(
|
|
14
14
|
s,
|
|
15
15
|
"*"
|
|
16
16
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function P(e, t, n) {
|
|
20
20
|
const r = (s) => {
|
|
21
|
-
!
|
|
21
|
+
!F(s.data) || s.data.key !== W || s.data.method !== t || n(s.data);
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return e.addEventListener("message", r), r;
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
return typeof
|
|
25
|
+
function F(e) {
|
|
26
|
+
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
28
|
+
function k(e, t, n) {
|
|
29
|
+
e.removeEventListener("message", n);
|
|
30
30
|
}
|
|
31
|
-
function
|
|
32
|
-
|
|
31
|
+
function D(e, t) {
|
|
32
|
+
e.removeEventListener("message", t);
|
|
33
33
|
}
|
|
34
|
-
var
|
|
35
|
-
const
|
|
36
|
-
class
|
|
34
|
+
var l = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(l || {}), w = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e))(w || {}), O = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(O || {});
|
|
35
|
+
const q = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
36
|
+
class G {
|
|
37
37
|
/**
|
|
38
38
|
* Creates an instance of IframeRpcProviderBridge.
|
|
39
39
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
40
40
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
41
41
|
*/
|
|
42
|
-
constructor(
|
|
42
|
+
constructor(t) {
|
|
43
43
|
/**
|
|
44
44
|
* The Ethereum provider instance.
|
|
45
45
|
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
46
46
|
* */
|
|
47
|
-
|
|
47
|
+
u(this, "ethereumProvider", null);
|
|
48
48
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
u(this, "requestWaitingForConnection", {});
|
|
50
|
+
u(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
51
51
|
if (!this.ethereumProvider) {
|
|
52
|
-
|
|
52
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
this.processRpcRequest(
|
|
55
|
+
this.processRpcRequest(t);
|
|
56
56
|
});
|
|
57
|
-
this.iframeWidow =
|
|
57
|
+
this.iframeWidow = t;
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
61
61
|
*/
|
|
62
62
|
disconnect() {
|
|
63
|
-
this.ethereumProvider = null,
|
|
63
|
+
this.ethereumProvider = null, k(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
67
67
|
* @param newProvider - The Ethereum provider to connect.
|
|
68
68
|
*/
|
|
69
|
-
onConnect(
|
|
70
|
-
this.ethereumProvider ? this.disconnect() :
|
|
71
|
-
|
|
69
|
+
onConnect(t) {
|
|
70
|
+
this.ethereumProvider ? this.disconnect() : P(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), q.forEach((n) => {
|
|
71
|
+
t.on(n, (r) => this.onProviderEvent(n, r));
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
processPendingRequests() {
|
|
75
|
-
Object.keys(this.requestWaitingForConnection).forEach((
|
|
76
|
-
this.processRpcRequest(this.requestWaitingForConnection[
|
|
75
|
+
Object.keys(this.requestWaitingForConnection).forEach((t) => {
|
|
76
|
+
this.processRpcRequest(this.requestWaitingForConnection[t]);
|
|
77
77
|
}), this.requestWaitingForConnection = {};
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
81
81
|
* @param request - The JSON-RPC request to be processed.
|
|
82
82
|
*/
|
|
83
|
-
processRpcRequest(
|
|
84
|
-
const { id:
|
|
85
|
-
if (!this.ethereumProvider || !
|
|
83
|
+
processRpcRequest(t) {
|
|
84
|
+
const { id: n, jsonrpc: r, method: s } = t;
|
|
85
|
+
if (!this.ethereumProvider || !n)
|
|
86
86
|
return;
|
|
87
|
-
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...
|
|
88
|
-
(
|
|
89
|
-
rpcResponse: { jsonrpc: r, id:
|
|
87
|
+
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...t, id: n })).then(
|
|
88
|
+
(o) => this.forwardRpcResponseToIframe({
|
|
89
|
+
rpcResponse: { jsonrpc: r, id: n, result: o }
|
|
90
90
|
})
|
|
91
91
|
).catch(
|
|
92
|
-
(
|
|
93
|
-
rpcResponse: { jsonrpc: r, id:
|
|
92
|
+
(o) => this.forwardRpcResponseToIframe({
|
|
93
|
+
rpcResponse: { jsonrpc: r, id: n, error: o }
|
|
94
94
|
})
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
-
onProviderEvent(
|
|
98
|
-
|
|
99
|
-
event:
|
|
100
|
-
params:
|
|
97
|
+
onProviderEvent(t, n) {
|
|
98
|
+
R(this.iframeWidow, w.PROVIDER_ON_EVENT, {
|
|
99
|
+
event: t,
|
|
100
|
+
params: n
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
104
104
|
* Forward a JSON-RPC message to the content window.
|
|
105
105
|
*/
|
|
106
|
-
forwardRpcResponseToIframe(
|
|
107
|
-
|
|
106
|
+
forwardRpcResponseToIframe(t) {
|
|
107
|
+
R(this.iframeWidow, w.PROVIDER_RPC_RESPONSE, t);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
const
|
|
111
|
-
function
|
|
112
|
-
const
|
|
113
|
-
return
|
|
110
|
+
const A = "_";
|
|
111
|
+
function x(e) {
|
|
112
|
+
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = y(e), r = L(e);
|
|
113
|
+
return t + "/#" + n + "?" + r;
|
|
114
114
|
}
|
|
115
|
-
function y(
|
|
116
|
-
const { chainId:
|
|
117
|
-
return `/${
|
|
115
|
+
function y(e) {
|
|
116
|
+
const { chainId: t = 1, sell: n, buy: r, tradeType: s = O.SWAP } = e, a = [(n == null ? void 0 : n.asset) || A, (r == null ? void 0 : r.asset) || A].map(encodeURIComponent).join("/");
|
|
117
|
+
return `/${t}/widget/${s}/${a}`;
|
|
118
118
|
}
|
|
119
|
-
function L(
|
|
120
|
-
const { sell:
|
|
121
|
-
return
|
|
119
|
+
function L(e) {
|
|
120
|
+
const { sell: t, buy: n, theme: r } = e, s = new URLSearchParams();
|
|
121
|
+
return t != null && t.amount && s.append("sellAmount", t.amount), n != null && n.amount && s.append("buyAmount", n.amount), r && s.append("theme", typeof r == "string" ? r : r.baseTheme), s;
|
|
122
122
|
}
|
|
123
|
-
class
|
|
123
|
+
class $ {
|
|
124
124
|
constructor() {
|
|
125
|
-
|
|
125
|
+
u(this, "subscriptions", {});
|
|
126
126
|
}
|
|
127
|
-
on(
|
|
128
|
-
const { event:
|
|
129
|
-
this.subscriptions[
|
|
127
|
+
on(t) {
|
|
128
|
+
const { event: n, handler: r } = t;
|
|
129
|
+
this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(r);
|
|
130
130
|
}
|
|
131
|
-
off(
|
|
132
|
-
const { event:
|
|
133
|
-
this.subscriptions[
|
|
131
|
+
off(t) {
|
|
132
|
+
const { event: n, handler: r } = t;
|
|
133
|
+
this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((s) => s !== r));
|
|
134
134
|
}
|
|
135
|
-
emit(
|
|
136
|
-
this.subscriptions[
|
|
135
|
+
emit(t, n) {
|
|
136
|
+
this.subscriptions[t] && this.subscriptions[t].forEach((r) => r(n));
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
h(this, "instanceNumber", Q++);
|
|
146
|
-
this.contentWindow = e, this.updateListeners(t), this.widgetListener = R(
|
|
139
|
+
class Q {
|
|
140
|
+
constructor(t, n = []) {
|
|
141
|
+
u(this, "eventEmitter", new $());
|
|
142
|
+
u(this, "listeners", []);
|
|
143
|
+
u(this, "widgetListener");
|
|
144
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = P(
|
|
147
145
|
this.contentWindow,
|
|
148
|
-
|
|
146
|
+
l.EMIT_COW_EVENT,
|
|
149
147
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
150
148
|
);
|
|
151
149
|
}
|
|
152
150
|
stopListeningIframe() {
|
|
153
|
-
|
|
151
|
+
D(this.contentWindow, this.widgetListener);
|
|
154
152
|
}
|
|
155
|
-
updateListeners(
|
|
156
|
-
for (const
|
|
157
|
-
this.eventEmitter.off(
|
|
158
|
-
this.listeners =
|
|
159
|
-
for (const
|
|
160
|
-
this.eventEmitter.on(
|
|
153
|
+
updateListeners(t) {
|
|
154
|
+
for (const n of this.listeners)
|
|
155
|
+
this.eventEmitter.off(n);
|
|
156
|
+
this.listeners = t || [];
|
|
157
|
+
for (const n of this.listeners)
|
|
158
|
+
this.eventEmitter.on(n);
|
|
161
159
|
}
|
|
162
160
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
161
|
+
class B {
|
|
162
|
+
constructor(t, n) {
|
|
163
|
+
u(this, "forwardSdkMessage");
|
|
164
|
+
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (r) => {
|
|
165
|
+
J(r.data) && (K(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : Y(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
166
|
+
}, this.startListening();
|
|
167
|
+
}
|
|
168
|
+
startListening() {
|
|
169
|
+
this.appWindow.addEventListener("message", this.forwardSdkMessage);
|
|
170
|
+
}
|
|
171
|
+
stopListening() {
|
|
172
|
+
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function J(e) {
|
|
176
|
+
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
177
|
+
}
|
|
178
|
+
function K(e) {
|
|
179
|
+
return "method" in e && typeof e.method == "string" && "params" in e && "env" in e && typeof e.env == "object" && e.env !== null && "sdkVersion" in e.env;
|
|
180
|
+
}
|
|
181
|
+
function Y(e) {
|
|
182
|
+
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
183
|
+
}
|
|
184
|
+
const U = "640px", j = "450px", z = 20;
|
|
185
|
+
function I(e, t) {
|
|
186
|
+
const { params: n, provider: r, listeners: s } = t;
|
|
187
|
+
let a = r;
|
|
188
|
+
const o = X(n);
|
|
189
|
+
e.innerHTML = "", e.appendChild(o);
|
|
190
|
+
const { contentWindow: c } = o;
|
|
191
|
+
if (!c)
|
|
192
|
+
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
172
193
|
const f = [];
|
|
173
|
-
f.push(
|
|
174
|
-
const p = new
|
|
175
|
-
let
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
194
|
+
f.push(Z(c, n.appCode)), f.push(ee(o, n.height));
|
|
195
|
+
const p = new Q(window, s);
|
|
196
|
+
let d = C(c, null, a);
|
|
197
|
+
o.addEventListener("load", () => S(c, n, a));
|
|
198
|
+
const E = new B(window, c);
|
|
199
|
+
return {
|
|
200
|
+
updateParams: (i) => S(c, i, a),
|
|
201
|
+
updateListeners: (i) => p.updateListeners(i),
|
|
202
|
+
updateProvider: (i) => {
|
|
203
|
+
a = i, d = C(c, d, i);
|
|
181
204
|
},
|
|
182
205
|
destroy: () => {
|
|
183
|
-
p.stopListeningIframe(), f.forEach((
|
|
206
|
+
d.disconnect(), p.stopListeningIframe(), f.forEach((i) => window.removeEventListener("message", i)), E.stopListening(), e.removeChild(o);
|
|
184
207
|
}
|
|
185
208
|
};
|
|
186
209
|
}
|
|
187
|
-
function
|
|
188
|
-
|
|
189
|
-
const r =
|
|
190
|
-
return
|
|
210
|
+
function C(e, t, n) {
|
|
211
|
+
t && t.disconnect();
|
|
212
|
+
const r = t || new G(e);
|
|
213
|
+
return n && r.onConnect(n), r;
|
|
191
214
|
}
|
|
192
|
-
function
|
|
193
|
-
const { width:
|
|
194
|
-
return r.src =
|
|
215
|
+
function X(e) {
|
|
216
|
+
const { width: t = j, height: n = U } = e, r = document.createElement("iframe");
|
|
217
|
+
return r.src = x(e), r.width = t, r.height = n, r.style.border = "0", r;
|
|
195
218
|
}
|
|
196
|
-
function
|
|
197
|
-
const r = !!
|
|
198
|
-
|
|
219
|
+
function S(e, t, n) {
|
|
220
|
+
const r = !!n, s = y(t), a = L(t).toString();
|
|
221
|
+
R(e, w.UPDATE_PARAMS, {
|
|
199
222
|
urlParams: {
|
|
200
223
|
pathname: s,
|
|
201
|
-
search:
|
|
224
|
+
search: a
|
|
202
225
|
},
|
|
203
226
|
appParams: {
|
|
204
|
-
...
|
|
227
|
+
...t
|
|
205
228
|
},
|
|
206
229
|
hasProvider: r
|
|
207
230
|
});
|
|
208
231
|
}
|
|
209
|
-
function
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
metaData:
|
|
232
|
+
function Z(e, t) {
|
|
233
|
+
const n = P(window, l.ACTIVATE, () => {
|
|
234
|
+
D(window, n), R(e, w.UPDATE_APP_DATA, {
|
|
235
|
+
metaData: t ? { appCode: t } : void 0
|
|
213
236
|
});
|
|
214
237
|
});
|
|
215
|
-
return
|
|
238
|
+
return n;
|
|
216
239
|
}
|
|
217
|
-
function
|
|
218
|
-
return
|
|
219
|
-
|
|
240
|
+
function ee(e, t = U) {
|
|
241
|
+
return P(window, l.UPDATE_HEIGHT, (n) => {
|
|
242
|
+
e.style.height = n.height ? `${n.height + z}px` : t;
|
|
220
243
|
});
|
|
221
244
|
}
|
|
222
|
-
function
|
|
223
|
-
const { params:
|
|
224
|
-
(
|
|
245
|
+
function se(e) {
|
|
246
|
+
const { params: t, provider: n, listeners: r } = e, [s, a] = H(null), o = m(null), c = m(n), f = m(r), p = m(null), d = m(null), E = b(
|
|
247
|
+
(i, h) => {
|
|
225
248
|
try {
|
|
226
|
-
console.log(`[WIDGET] ${
|
|
227
|
-
} catch (
|
|
228
|
-
const _ = `Error ${
|
|
229
|
-
console.error(`[WIDGET] ${_}`,
|
|
249
|
+
console.log(`[WIDGET] ${i}`), h();
|
|
250
|
+
} catch (T) {
|
|
251
|
+
const _ = `Error ${i.toLowerCase()}`;
|
|
252
|
+
console.error(`[WIDGET] ${_}`, T), a({ message: _, error: T });
|
|
230
253
|
}
|
|
231
254
|
},
|
|
232
|
-
[
|
|
255
|
+
[a]
|
|
233
256
|
);
|
|
234
|
-
return
|
|
235
|
-
if (!p.current || JSON.stringify(
|
|
257
|
+
return g(() => {
|
|
258
|
+
if (!p.current || JSON.stringify(o.current) === JSON.stringify(t))
|
|
236
259
|
return;
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}) :
|
|
241
|
-
}, [
|
|
242
|
-
if (!
|
|
260
|
+
const i = p.current, h = d.current;
|
|
261
|
+
o.current = t, h === null ? E("Creating a new widget", () => {
|
|
262
|
+
d.current = I(i, { params: t, provider: c.current, listeners: r }), f.current = r;
|
|
263
|
+
}) : E("Updating the widget", () => h.updateParams(t));
|
|
264
|
+
}, [t]), g(() => {
|
|
265
|
+
if (!d.current || c.current === n)
|
|
243
266
|
return;
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
var
|
|
248
|
-
(
|
|
267
|
+
c.current = n;
|
|
268
|
+
const i = p.current;
|
|
269
|
+
i && E("Updating the provider", () => {
|
|
270
|
+
var h;
|
|
271
|
+
(h = d.current) == null || h.destroy(), d.current = I(i, { params: t, provider: c.current, listeners: r });
|
|
249
272
|
});
|
|
250
|
-
}, [
|
|
251
|
-
if (!
|
|
273
|
+
}, [n]), g(() => {
|
|
274
|
+
if (!d.current || f.current === r)
|
|
252
275
|
return;
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
}, [r]), s ? /* @__PURE__ */
|
|
276
|
+
const i = d.current;
|
|
277
|
+
E("Updating the listeners", () => i.updateListeners(r));
|
|
278
|
+
}, [r]), s ? /* @__PURE__ */ N("div", { style: { color: "#ff3a3a" }, children: [
|
|
256
279
|
s.message,
|
|
257
|
-
s.error.message && /* @__PURE__ */
|
|
258
|
-
] }) : /* @__PURE__ */
|
|
280
|
+
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
281
|
+
] }) : /* @__PURE__ */ v("div", { ref: p });
|
|
259
282
|
}
|
|
260
283
|
export {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
284
|
+
se as CowSwapWidget,
|
|
285
|
+
O as TradeType,
|
|
286
|
+
l as WidgetMethodsEmit,
|
|
264
287
|
w as WidgetMethodsListen,
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
288
|
+
I as createCowSwapWidget,
|
|
289
|
+
P as listenToMessageFromWindow,
|
|
290
|
+
R as postMessageToWindow,
|
|
291
|
+
k as stopListeningToMessageFromWindow,
|
|
292
|
+
D as stopListeningWindowListener
|
|
270
293
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"react"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cowprotocol/widget-lib": "^0.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.8.0"
|
|
26
26
|
}
|
|
27
27
|
}
|