@cowprotocol/widget-lib 0.7.0 → 0.8.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/IframeCowEventEmitter.d.ts +0 -1
- package/IframeSafeSdkBridge.d.ts +8 -0
- package/README.md +9 -8
- package/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +120 -97
- package/package.json +2 -2
- package/types.d.ts +58 -30
|
@@ -4,7 +4,6 @@ export declare class IframeCowEventEmitter {
|
|
|
4
4
|
private eventEmitter;
|
|
5
5
|
private listeners;
|
|
6
6
|
private widgetListener;
|
|
7
|
-
private instanceNumber;
|
|
8
7
|
constructor(contentWindow: Window, listeners?: CowEventListeners);
|
|
9
8
|
stopListeningIframe(): void;
|
|
10
9
|
updateListeners(listeners?: CowEventListeners): void;
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ See the widget in action in the [widget configurator](https://widget.cow.fi)
|
|
|
10
10
|
|
|
11
11
|
## Docs
|
|
12
12
|
|
|
13
|
-
You can find a detailed description of all widget parameters in the [documentation](
|
|
13
|
+
You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.fi/cow-protocol/tutorials/widget)
|
|
14
14
|
|
|
15
15
|
## Quick start
|
|
16
16
|
|
|
@@ -31,27 +31,28 @@ Create a container somewhere in your website, the widget will be rendered inside
|
|
|
31
31
|
Import the widget and initialise it:
|
|
32
32
|
|
|
33
33
|
```js
|
|
34
|
-
import {
|
|
34
|
+
import { createCowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-lib'
|
|
35
35
|
|
|
36
36
|
// Initialise the widget
|
|
37
37
|
const widgetContainer = document.getElementById('cowswap-widget')
|
|
38
38
|
|
|
39
|
+
// instantiate your own web3 provider
|
|
40
|
+
const provider = window.ethereum
|
|
41
|
+
|
|
39
42
|
const params: CowSwapWidgetParams = {
|
|
40
|
-
appCode: '
|
|
43
|
+
appCode: 'NAME-OF-YOU-APP', // Add here the name of your app. e.g. "Pig Swap"
|
|
41
44
|
sell: { asset: 'DAI' },
|
|
42
45
|
buy: { asset: 'USDC', amount: '0.1' },
|
|
43
|
-
// instantiate your own web3 provider
|
|
44
|
-
provider: window.ethereum,
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
const
|
|
48
|
+
const {updateParams} = createCowSwapWidget(
|
|
48
49
|
widgetContainer,
|
|
49
50
|
// Optionally, you can provide some additional params to customise your widget
|
|
50
|
-
params
|
|
51
|
+
{params, provider}
|
|
51
52
|
)
|
|
52
53
|
|
|
53
54
|
// You also can change widget configuration on the fly
|
|
54
|
-
|
|
55
|
+
updateParams({ ...params, tradeType: 'limit' })
|
|
55
56
|
```
|
|
56
57
|
|
|
57
58
|
## Developers
|
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(o){"use strict";const
|
|
1
|
+
var cowSwapWidget=function(o){"use strict";const l="cowSwapWidget";function p(e,n,t){const s={key:l,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function f(e,n,t){const i=s=>{!C(s.data)||s.data.key!==l||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function C(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function R(e,n,t){e.removeEventListener("message",t)}function E(e,n){e.removeEventListener("message",n)}var d=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(d||{}),u=(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))(u||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class D{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,R(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():f(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:r}}))}onProviderEvent(n,t){p(this.iframeWidow,u.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){p(this.iframeWidow,u.PROVIDER_RPC_RESPONSE,n)}}const T="_";function L(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=g(e),i=_(e);return n+"/#"+t+"?"+i}function g(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=w.SWAP}=e,a=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${a}`}function _(e){const{sell:n,buy:t,theme:i}=e,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}class O{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}class y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new O,this.listeners=[],this.updateListeners(t),this.widgetListener=f(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){E(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}class V{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&(U(i.data)?this.appWindow.parent.postMessage(i.data,"*"):b(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function U(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 b(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const A="640px",F="450px",N=20;function H(e,n){const{params:t,provider:i,listeners:s}=n;let a=i;const r=q(t);e.innerHTML="",e.appendChild(r);const{contentWindow:h}=r;if(!h)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const m=[];m.push(k(h,t.appCode)),m.push(G(r,t.height));const I=new y(window,s);let P=v(h,null,a);r.addEventListener("load",()=>W(h,t,a));const Q=new V(window,h);return{updateParams:c=>W(h,c,a),updateListeners:c=>I.updateListeners(c),updateProvider:c=>{a=c,P=v(h,P,c)},destroy:()=>{P.disconnect(),I.stopListeningIframe(),m.forEach(c=>window.removeEventListener("message",c)),Q.stopListening(),e.removeChild(r)}}}function v(e,n,t){n&&n.disconnect();const i=n||new D(e);return t&&i.onConnect(t),i}function q(e){const{width:n=F,height:t=A}=e,i=document.createElement("iframe");return i.src=L(e),i.width=n,i.height=t,i.style.border="0",i}function W(e,n,t){const i=!!t,s=g(n),a=_(n).toString();p(e,u.UPDATE_PARAMS,{urlParams:{pathname:s,search:a},appParams:{...n},hasProvider:i})}function k(e,n){const t=f(window,d.ACTIVATE,()=>{E(window,t),p(e,u.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function G(e,n=A){return f(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+N}px`:n})}return o.TradeType=w,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=u,o.createCowSwapWidget=H,o.listenToMessageFromWindow=f,o.postMessageToWindow=p,o.stopListeningToMessageFromWindow=R,o.stopListeningWindowListener=E,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g="cowSwapWidget";function u(e,n,t){const i={key:g,method:n,...typeof t=="object"?t:{}};e.postMessage(i,"*")}function p(e,n,t){const s=i=>{!C(i.data)||i.data.key!==g||i.data.method!==n||t(i.data)};return e.addEventListener("message",s),s}function C(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function _(e,n,t){e.removeEventListener("message",t)}function m(e,n){e.removeEventListener("message",n)}var d=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(d||{}),h=(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))(h||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class D{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,_(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():p(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.forEach(t=>{n.on(t,s=>this.onProviderEvent(t,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!t)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,error:o}}))}onProviderEvent(n,t){u(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}const l="_";function L(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e),s=v(e);return n+"/#"+t+"?"+s}function A(e){const{chainId:n=1,sell:t,buy:s,tradeType:i=w.SWAP}=e,r=[(t==null?void 0:t.asset)||l,(s==null?void 0:s.asset)||l].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function v(e){const{sell:n,buy:t,theme:s}=e,i=new URLSearchParams;return n!=null&&n.amount&&i.append("sellAmount",n.amount),t!=null&&t.amount&&i.append("buyAmount",t.amount),s&&i.append("theme",typeof s=="string"?s:s.baseTheme),i}class O{constructor(){this.subscriptions={}}on(n){const{event:t,handler:s}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(s)}off(n){const{event:t,handler:s}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(i=>i!==s))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(t))}}class y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new O,this.listeners=[],this.updateListeners(t),this.widgetListener=p(this.contentWindow,d.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}class V{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=s=>{M(s.data)&&(U(s.data)?this.appWindow.parent.postMessage(s.data,"*"):b(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function U(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 b(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const W="640px",F="450px",N=20;function H(e,n){const{params:t,provider:s,listeners:i}=n;let r=s;const o=q(t);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 f=[];f.push(k(c,t.appCode)),f.push(G(o,t.height));const P=new y(window,i);let E=R(c,null,r);o.addEventListener("load",()=>T(c,t,r));const I=new V(window,c);return{updateParams:a=>T(c,a,r),updateListeners:a=>P.updateListeners(a),updateProvider:a=>{r=a,E=R(c,E,a)},destroy:()=>{E.disconnect(),P.stopListeningIframe(),f.forEach(a=>window.removeEventListener("message",a)),I.stopListening(),e.removeChild(o)}}}function R(e,n,t){n&&n.disconnect();const s=n||new D(e);return t&&s.onConnect(t),s}function q(e){const{width:n=F,height:t=W}=e,s=document.createElement("iframe");return s.src=L(e),s.width=n,s.height=t,s.style.border="0",s}function T(e,n,t){const s=!!t,i=A(n),r=v(n).toString();u(e,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function k(e,n){const t=p(window,d.ACTIVATE,()=>{m(window,t),u(e,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function G(e,n=W){return p(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+N}px`:n})}exports.TradeType=w;exports.WidgetMethodsEmit=d;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=H;exports.listenToMessageFromWindow=p;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=_;exports.stopListeningWindowListener=m;
|
package/index.mjs
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
const
|
|
2
|
-
function u(
|
|
1
|
+
const l = "cowSwapWidget";
|
|
2
|
+
function u(e, n, t) {
|
|
3
3
|
const i = {
|
|
4
|
-
key:
|
|
4
|
+
key: l,
|
|
5
5
|
method: n,
|
|
6
|
-
...typeof
|
|
6
|
+
...typeof t == "object" ? t : {}
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
e.postMessage(
|
|
9
9
|
i,
|
|
10
10
|
"*"
|
|
11
11
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function p(e, n, t) {
|
|
15
15
|
const s = (i) => {
|
|
16
|
-
!
|
|
16
|
+
!C(i.data) || i.data.key !== l || i.data.method !== n || t(i.data);
|
|
17
17
|
};
|
|
18
|
-
return
|
|
18
|
+
return e.addEventListener("message", s), s;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
return typeof
|
|
20
|
+
function C(e) {
|
|
21
|
+
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
22
22
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
23
|
+
function W(e, n, t) {
|
|
24
|
+
e.removeEventListener("message", t);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
function T(e, n) {
|
|
27
|
+
e.removeEventListener("message", n);
|
|
28
28
|
}
|
|
29
|
-
var
|
|
30
|
-
const
|
|
31
|
-
class
|
|
29
|
+
var d = /* @__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))(d || {}), h = /* @__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))(h || {}), _ = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(_ || {});
|
|
30
|
+
const S = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
31
|
+
class D {
|
|
32
32
|
/**
|
|
33
33
|
* Creates an instance of IframeRpcProviderBridge.
|
|
34
34
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
35
35
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
36
36
|
*/
|
|
37
37
|
constructor(n) {
|
|
38
|
-
this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest:
|
|
38
|
+
this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: t }) => {
|
|
39
39
|
if (!this.ethereumProvider) {
|
|
40
|
-
|
|
40
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
|
-
this.processRpcRequest(
|
|
43
|
+
this.processRpcRequest(t);
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
47
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
48
48
|
*/
|
|
49
49
|
disconnect() {
|
|
50
|
-
this.ethereumProvider = null,
|
|
50
|
+
this.ethereumProvider = null, W(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
54
54
|
* @param newProvider - The Ethereum provider to connect.
|
|
55
55
|
*/
|
|
56
56
|
onConnect(n) {
|
|
57
|
-
this.ethereumProvider ? this.disconnect() :
|
|
58
|
-
n.on(
|
|
57
|
+
this.ethereumProvider ? this.disconnect() : p(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), S.forEach((t) => {
|
|
58
|
+
n.on(t, (s) => this.onProviderEvent(t, s));
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
processPendingRequests() {
|
|
@@ -68,117 +68,140 @@ class W {
|
|
|
68
68
|
* @param request - The JSON-RPC request to be processed.
|
|
69
69
|
*/
|
|
70
70
|
processRpcRequest(n) {
|
|
71
|
-
const { id:
|
|
72
|
-
if (!this.ethereumProvider || !
|
|
71
|
+
const { id: t, jsonrpc: s, method: i } = n;
|
|
72
|
+
if (!this.ethereumProvider || !t)
|
|
73
73
|
return;
|
|
74
|
-
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id:
|
|
74
|
+
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
|
|
75
75
|
(o) => this.forwardRpcResponseToIframe({
|
|
76
|
-
rpcResponse: { jsonrpc: s, id:
|
|
76
|
+
rpcResponse: { jsonrpc: s, id: t, result: o }
|
|
77
77
|
})
|
|
78
78
|
).catch(
|
|
79
79
|
(o) => this.forwardRpcResponseToIframe({
|
|
80
|
-
rpcResponse: { jsonrpc: s, id:
|
|
80
|
+
rpcResponse: { jsonrpc: s, id: t, error: o }
|
|
81
81
|
})
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
|
-
onProviderEvent(n,
|
|
85
|
-
u(this.iframeWidow,
|
|
84
|
+
onProviderEvent(n, t) {
|
|
85
|
+
u(this.iframeWidow, h.PROVIDER_ON_EVENT, {
|
|
86
86
|
event: n,
|
|
87
|
-
params:
|
|
87
|
+
params: t
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* Forward a JSON-RPC message to the content window.
|
|
92
92
|
*/
|
|
93
93
|
forwardRpcResponseToIframe(n) {
|
|
94
|
-
u(this.iframeWidow,
|
|
94
|
+
u(this.iframeWidow, h.PROVIDER_RPC_RESPONSE, n);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
const
|
|
98
|
-
function O(
|
|
99
|
-
const n = typeof
|
|
100
|
-
return n + "/#" +
|
|
97
|
+
const w = "_";
|
|
98
|
+
function O(e) {
|
|
99
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = g(e), s = A(e);
|
|
100
|
+
return n + "/#" + t + "?" + s;
|
|
101
101
|
}
|
|
102
|
-
function
|
|
103
|
-
const { chainId: n = 1, sell:
|
|
102
|
+
function g(e) {
|
|
103
|
+
const { chainId: n = 1, sell: t, buy: s, tradeType: i = _.SWAP } = e, r = [(t == null ? void 0 : t.asset) || w, (s == null ? void 0 : s.asset) || w].map(encodeURIComponent).join("/");
|
|
104
104
|
return `/${n}/widget/${i}/${r}`;
|
|
105
105
|
}
|
|
106
|
-
function
|
|
107
|
-
const { sell: n, buy:
|
|
108
|
-
return n != null && n.amount && i.append("sellAmount", n.amount),
|
|
106
|
+
function A(e) {
|
|
107
|
+
const { sell: n, buy: t, theme: s } = e, i = new URLSearchParams();
|
|
108
|
+
return n != null && n.amount && i.append("sellAmount", n.amount), t != null && t.amount && i.append("buyAmount", t.amount), s && i.append("theme", typeof s == "string" ? s : s.baseTheme), i;
|
|
109
109
|
}
|
|
110
|
-
class
|
|
110
|
+
class L {
|
|
111
111
|
constructor() {
|
|
112
112
|
this.subscriptions = {};
|
|
113
113
|
}
|
|
114
114
|
on(n) {
|
|
115
|
-
const { event:
|
|
116
|
-
this.subscriptions[
|
|
115
|
+
const { event: t, handler: s } = n;
|
|
116
|
+
this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(s);
|
|
117
117
|
}
|
|
118
118
|
off(n) {
|
|
119
|
-
const { event:
|
|
120
|
-
this.subscriptions[
|
|
119
|
+
const { event: t, handler: s } = n;
|
|
120
|
+
this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((i) => i !== s));
|
|
121
121
|
}
|
|
122
|
-
emit(n,
|
|
123
|
-
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(
|
|
122
|
+
emit(n, t) {
|
|
123
|
+
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.contentWindow = n, this.eventEmitter = new S(), this.listeners = [], this.instanceNumber = V++, this.updateListeners(e), this.widgetListener = E(
|
|
126
|
+
class V {
|
|
127
|
+
constructor(n, t = []) {
|
|
128
|
+
this.contentWindow = n, this.eventEmitter = new L(), this.listeners = [], this.updateListeners(t), this.widgetListener = p(
|
|
130
129
|
this.contentWindow,
|
|
131
|
-
|
|
130
|
+
d.EMIT_COW_EVENT,
|
|
132
131
|
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
133
132
|
);
|
|
134
133
|
}
|
|
135
134
|
stopListeningIframe() {
|
|
136
|
-
|
|
135
|
+
T(this.contentWindow, this.widgetListener);
|
|
137
136
|
}
|
|
138
137
|
updateListeners(n) {
|
|
139
|
-
for (const
|
|
140
|
-
this.eventEmitter.off(
|
|
138
|
+
for (const t of this.listeners)
|
|
139
|
+
this.eventEmitter.off(t);
|
|
141
140
|
this.listeners = n || [];
|
|
142
|
-
for (const
|
|
143
|
-
this.eventEmitter.on(
|
|
141
|
+
for (const t of this.listeners)
|
|
142
|
+
this.eventEmitter.on(t);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
class y {
|
|
146
|
+
constructor(n, t) {
|
|
147
|
+
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (s) => {
|
|
148
|
+
U(s.data) && (M(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : N(s.data) && this.iframeWidow.postMessage(s.data, "*"));
|
|
149
|
+
}, this.startListening();
|
|
150
|
+
}
|
|
151
|
+
startListening() {
|
|
152
|
+
this.appWindow.addEventListener("message", this.forwardSdkMessage);
|
|
144
153
|
}
|
|
154
|
+
stopListening() {
|
|
155
|
+
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function U(e) {
|
|
159
|
+
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
160
|
+
}
|
|
161
|
+
function M(e) {
|
|
162
|
+
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;
|
|
163
|
+
}
|
|
164
|
+
function N(e) {
|
|
165
|
+
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
145
166
|
}
|
|
146
|
-
const
|
|
147
|
-
function
|
|
148
|
-
const { params:
|
|
167
|
+
const v = "640px", b = "450px", F = 20;
|
|
168
|
+
function G(e, n) {
|
|
169
|
+
const { params: t, provider: s, listeners: i } = n;
|
|
149
170
|
let r = s;
|
|
150
|
-
const o =
|
|
151
|
-
|
|
152
|
-
const { contentWindow:
|
|
153
|
-
if (!
|
|
171
|
+
const o = H(t);
|
|
172
|
+
e.innerHTML = "", e.appendChild(o);
|
|
173
|
+
const { contentWindow: c } = o;
|
|
174
|
+
if (!c)
|
|
154
175
|
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
const m = new
|
|
158
|
-
let
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
176
|
+
const f = [];
|
|
177
|
+
f.push(q(c, t.appCode)), f.push(k(o, t.height));
|
|
178
|
+
const m = new V(window, i);
|
|
179
|
+
let E = P(c, null, r);
|
|
180
|
+
o.addEventListener("load", () => R(c, t, r));
|
|
181
|
+
const I = new y(window, c);
|
|
182
|
+
return {
|
|
183
|
+
updateParams: (a) => R(c, a, r),
|
|
184
|
+
updateListeners: (a) => m.updateListeners(a),
|
|
185
|
+
updateProvider: (a) => {
|
|
186
|
+
r = a, E = P(c, E, a);
|
|
164
187
|
},
|
|
165
188
|
destroy: () => {
|
|
166
|
-
m.stopListeningIframe(),
|
|
189
|
+
E.disconnect(), m.stopListeningIframe(), f.forEach((a) => window.removeEventListener("message", a)), I.stopListening(), e.removeChild(o);
|
|
167
190
|
}
|
|
168
191
|
};
|
|
169
192
|
}
|
|
170
|
-
function
|
|
193
|
+
function P(e, n, t) {
|
|
171
194
|
n && n.disconnect();
|
|
172
|
-
const s = n || new
|
|
173
|
-
return
|
|
195
|
+
const s = n || new D(e);
|
|
196
|
+
return t && s.onConnect(t), s;
|
|
174
197
|
}
|
|
175
|
-
function
|
|
176
|
-
const { width: n =
|
|
177
|
-
return s.src = O(
|
|
198
|
+
function H(e) {
|
|
199
|
+
const { width: n = b, height: t = v } = e, s = document.createElement("iframe");
|
|
200
|
+
return s.src = O(e), s.width = n, s.height = t, s.style.border = "0", s;
|
|
178
201
|
}
|
|
179
|
-
function
|
|
180
|
-
const s = !!
|
|
181
|
-
u(
|
|
202
|
+
function R(e, n, t) {
|
|
203
|
+
const s = !!t, i = g(n), r = A(n).toString();
|
|
204
|
+
u(e, h.UPDATE_PARAMS, {
|
|
182
205
|
urlParams: {
|
|
183
206
|
pathname: i,
|
|
184
207
|
search: r
|
|
@@ -189,26 +212,26 @@ function w(t, n, e) {
|
|
|
189
212
|
hasProvider: s
|
|
190
213
|
});
|
|
191
214
|
}
|
|
192
|
-
function
|
|
193
|
-
const
|
|
194
|
-
|
|
215
|
+
function q(e, n) {
|
|
216
|
+
const t = p(window, d.ACTIVATE, () => {
|
|
217
|
+
T(window, t), u(e, h.UPDATE_APP_DATA, {
|
|
195
218
|
metaData: n ? { appCode: n } : void 0
|
|
196
219
|
});
|
|
197
220
|
});
|
|
198
|
-
return
|
|
221
|
+
return t;
|
|
199
222
|
}
|
|
200
|
-
function
|
|
201
|
-
return
|
|
202
|
-
|
|
223
|
+
function k(e, n = v) {
|
|
224
|
+
return p(window, d.UPDATE_HEIGHT, (t) => {
|
|
225
|
+
e.style.height = t.height ? `${t.height + F}px` : n;
|
|
203
226
|
});
|
|
204
227
|
}
|
|
205
228
|
export {
|
|
206
229
|
_ as TradeType,
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
230
|
+
d as WidgetMethodsEmit,
|
|
231
|
+
h as WidgetMethodsListen,
|
|
232
|
+
G as createCowSwapWidget,
|
|
233
|
+
p as listenToMessageFromWindow,
|
|
211
234
|
u as postMessageToWindow,
|
|
212
|
-
|
|
213
|
-
|
|
235
|
+
W as stopListeningToMessageFromWindow,
|
|
236
|
+
T as stopListeningWindowListener
|
|
214
237
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"widget-lib"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cowprotocol/events": "^
|
|
24
|
+
"@cowprotocol/events": "^1.1.0"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/types.d.ts
CHANGED
|
@@ -82,6 +82,17 @@ export interface PartnerFee {
|
|
|
82
82
|
*/
|
|
83
83
|
recipient: string;
|
|
84
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* ERC-20 token information
|
|
87
|
+
*/
|
|
88
|
+
export type TokenInfo = {
|
|
89
|
+
chainId: number;
|
|
90
|
+
address: string;
|
|
91
|
+
name: string;
|
|
92
|
+
decimals: number;
|
|
93
|
+
symbol: string;
|
|
94
|
+
logoURI?: string;
|
|
95
|
+
};
|
|
85
96
|
export interface CowSwapWidgetPalette {
|
|
86
97
|
baseTheme: CowSwapTheme;
|
|
87
98
|
primary: string;
|
|
@@ -94,6 +105,43 @@ export interface CowSwapWidgetPalette {
|
|
|
94
105
|
info: string;
|
|
95
106
|
success: string;
|
|
96
107
|
}
|
|
108
|
+
export interface CowSwapWidgetSounds {
|
|
109
|
+
/**
|
|
110
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
|
|
111
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
112
|
+
*/
|
|
113
|
+
postOrder?: string | null;
|
|
114
|
+
/**
|
|
115
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
|
|
116
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
117
|
+
*/
|
|
118
|
+
orderExecuted?: string | null;
|
|
119
|
+
/**
|
|
120
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
|
|
121
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
122
|
+
*/
|
|
123
|
+
orderError?: string | null;
|
|
124
|
+
}
|
|
125
|
+
export interface CowSwapWidgetImages {
|
|
126
|
+
/**
|
|
127
|
+
* The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
|
|
128
|
+
* Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
|
|
129
|
+
*/
|
|
130
|
+
emptyOrders?: string | null;
|
|
131
|
+
}
|
|
132
|
+
export interface CowSwapWidgetBanners {
|
|
133
|
+
/**
|
|
134
|
+
* Banner text: "Use Safe web app..."
|
|
135
|
+
*
|
|
136
|
+
* Conditions for displaying the banner:
|
|
137
|
+
* - Safe-like app is connected to CoW Swap via WalletConnect
|
|
138
|
+
* - Selling native token via Swap
|
|
139
|
+
* - Sell token needs approval
|
|
140
|
+
*
|
|
141
|
+
* If the flag is set to true, the banner will not be displayed
|
|
142
|
+
*/
|
|
143
|
+
hideSafeWebAppBanner?: boolean;
|
|
144
|
+
}
|
|
97
145
|
export interface CowSwapWidgetParams {
|
|
98
146
|
/**
|
|
99
147
|
* The unique identifier of the widget consumer.
|
|
@@ -122,9 +170,7 @@ export interface CowSwapWidgetParams {
|
|
|
122
170
|
tradeType?: TradeType;
|
|
123
171
|
/**
|
|
124
172
|
* The base url of the widget implementation
|
|
125
|
-
*
|
|
126
173
|
* The parameter can have the URL directly, or an object with the environment property,
|
|
127
|
-
*
|
|
128
174
|
* The base URL will default to the production environment if not specified, so it will use https://swap.cow.fi by default.
|
|
129
175
|
*/
|
|
130
176
|
baseUrl?: string;
|
|
@@ -177,40 +223,22 @@ export interface CowSwapWidgetParams {
|
|
|
177
223
|
* The theme of the widget UI.
|
|
178
224
|
*/
|
|
179
225
|
theme?: CowSwapTheme | CowSwapWidgetPalette;
|
|
180
|
-
/**
|
|
181
|
-
* Allows to set a custom logo for the widget.
|
|
182
|
-
*/
|
|
183
|
-
logoUrl?: string;
|
|
184
226
|
/**
|
|
185
227
|
* Customizable images for the widget.
|
|
186
228
|
*/
|
|
187
|
-
images?:
|
|
188
|
-
/**
|
|
189
|
-
* The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
|
|
190
|
-
* Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
|
|
191
|
-
*/
|
|
192
|
-
emptyOrders?: string | null;
|
|
193
|
-
};
|
|
229
|
+
images?: CowSwapWidgetImages;
|
|
194
230
|
/**
|
|
195
231
|
* Sounds configuration for the app.
|
|
196
232
|
*/
|
|
197
|
-
sounds?:
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
*/
|
|
207
|
-
orderExecuted?: string | null;
|
|
208
|
-
/**
|
|
209
|
-
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
|
|
210
|
-
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
211
|
-
*/
|
|
212
|
-
orderError?: string | null;
|
|
213
|
-
};
|
|
233
|
+
sounds?: CowSwapWidgetSounds;
|
|
234
|
+
/**
|
|
235
|
+
* Flags to control the display of banners in the widget.
|
|
236
|
+
*/
|
|
237
|
+
banners?: CowSwapWidgetBanners;
|
|
238
|
+
/**
|
|
239
|
+
* In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
|
|
240
|
+
*/
|
|
241
|
+
customTokens?: TokenInfo[];
|
|
214
242
|
}
|
|
215
243
|
export interface WidgetMethodsEmitPayloadMap {
|
|
216
244
|
[WidgetMethodsEmit.ACTIVATE]: void;
|