@swapper-finance/deposit-sdk 0.2.0 → 0.2.1
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/dist/SwapperIframe.js +233 -1
- package/dist/SwapperModal.js +175 -1
- package/dist/SwapperWalletProvider.d.ts.map +1 -1
- package/dist/SwapperWalletProvider.js +139 -1
- package/dist/SwapperWalletProvider.js.map +1 -1
- package/dist/adapters.js +136 -1
- package/dist/helpers.js +74 -1
- package/dist/index.js +7 -1
- package/dist/types/config.js +8 -1
- package/dist/types/events.js +2 -1
- package/dist/types/index.js +5 -1
- package/dist/types/styles.js +2 -1
- package/dist/types/wallet.js +11 -1
- package/package.json +2 -3
- package/dist/types.d.ts +0 -53
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
- package/dist/types.js.map +0 -1
package/dist/SwapperIframe.js
CHANGED
|
@@ -1 +1,233 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SwapperWalletProvider } from "./SwapperWalletProvider";
|
|
2
|
+
import { createSwapperSigner } from "./adapters";
|
|
3
|
+
const DEFAULT_IFRAME_URL = "https://deposit.swapper.finance/";
|
|
4
|
+
export class SwapperIframe {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.eventHandlers = new Map();
|
|
7
|
+
this.validateConfig(options);
|
|
8
|
+
this.config = {
|
|
9
|
+
integratorId: options.integratorId,
|
|
10
|
+
dstChainId: options.dstChainId,
|
|
11
|
+
dstTokenAddr: options.dstTokenAddr,
|
|
12
|
+
depositWalletAddress: options.depositWalletAddress,
|
|
13
|
+
styles: options.styles,
|
|
14
|
+
customContractCalls: options.customContractCalls,
|
|
15
|
+
supportedDepositOptions: options.supportedDepositOptions,
|
|
16
|
+
webhookUrl: options.webhookUrl,
|
|
17
|
+
};
|
|
18
|
+
this.iframeUrl = options.iframeUrl || DEFAULT_IFRAME_URL;
|
|
19
|
+
this.iframe = this.createIframe(options);
|
|
20
|
+
this.setupMessageListener();
|
|
21
|
+
if (options.container) {
|
|
22
|
+
this.mount(options.container);
|
|
23
|
+
}
|
|
24
|
+
if (!!options.wallet) {
|
|
25
|
+
const iframeOrigin = new URL(this.iframeUrl).origin;
|
|
26
|
+
if ("signer" in options.wallet) {
|
|
27
|
+
const signer = createSwapperSigner(options.wallet.signer);
|
|
28
|
+
const onTransactionRequest = (tx) => signer.sendTransaction(tx);
|
|
29
|
+
const onChainSwitchRequest = (chainId) => signer.switchChain(chainId);
|
|
30
|
+
Promise.all([signer.getAddress(), signer.getChainId()])
|
|
31
|
+
.then(([address, chainId]) => {
|
|
32
|
+
this.walletProvider = new SwapperWalletProvider({
|
|
33
|
+
iframe: this.iframe,
|
|
34
|
+
iframeOrigin,
|
|
35
|
+
onTransactionRequest,
|
|
36
|
+
onChainSwitchRequest,
|
|
37
|
+
autoConnect: { address, chainId },
|
|
38
|
+
});
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
console.error("[SwapperIframe] Failed to initialize wallet provider:", err);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
this.walletProvider = new SwapperWalletProvider({
|
|
46
|
+
iframe: this.iframe,
|
|
47
|
+
iframeOrigin,
|
|
48
|
+
onTransactionRequest: options.wallet.onTransactionRequest,
|
|
49
|
+
onChainSwitchRequest: options.wallet.onChainSwitchRequest,
|
|
50
|
+
autoConnect: options.wallet.autoConnect,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
validateConfig(config) {
|
|
56
|
+
const required = [
|
|
57
|
+
"integratorId",
|
|
58
|
+
"dstChainId",
|
|
59
|
+
"dstTokenAddr",
|
|
60
|
+
"depositWalletAddress",
|
|
61
|
+
];
|
|
62
|
+
for (const field of required) {
|
|
63
|
+
if (!config[field]) {
|
|
64
|
+
throw new Error(`Missing required parameter: ${field}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
createIframe(options) {
|
|
69
|
+
const iframe = document.createElement("iframe");
|
|
70
|
+
const url = new URL(this.iframeUrl);
|
|
71
|
+
url.searchParams.set("integratorId", this.config.integratorId);
|
|
72
|
+
url.searchParams.set("dstChainId", this.config.dstChainId);
|
|
73
|
+
url.searchParams.set("dstTokenAddr", this.config.dstTokenAddr);
|
|
74
|
+
url.searchParams.set("depositWalletAddress", this.config.depositWalletAddress);
|
|
75
|
+
if (this.config.styles) {
|
|
76
|
+
url.searchParams.set("styles", JSON.stringify(this.config.styles));
|
|
77
|
+
}
|
|
78
|
+
if (this.config.customContractCalls) {
|
|
79
|
+
url.searchParams.set("customContractCalls", JSON.stringify(this.config.customContractCalls));
|
|
80
|
+
}
|
|
81
|
+
if (this.config.supportedDepositOptions) {
|
|
82
|
+
url.searchParams.set("supportedDepositOptions", JSON.stringify(this.config.supportedDepositOptions));
|
|
83
|
+
}
|
|
84
|
+
if (this.config.webhookUrl) {
|
|
85
|
+
url.searchParams.set("webhookUrl", this.config.webhookUrl);
|
|
86
|
+
}
|
|
87
|
+
url.searchParams.set("t", Date.now().toString());
|
|
88
|
+
iframe.src = url.toString();
|
|
89
|
+
iframe.style.border = "none";
|
|
90
|
+
iframe.style.width = options.iframeAttributes?.width || "100%";
|
|
91
|
+
iframe.style.height = options.iframeAttributes?.height || "560px";
|
|
92
|
+
iframe.title = options.iframeAttributes?.title || "Swapper Deposit Widget";
|
|
93
|
+
const iframeOrigin = new URL(this.iframeUrl).origin;
|
|
94
|
+
const defaultAllow = `camera; clipboard-write self ${iframeOrigin}`;
|
|
95
|
+
const additionalAllow = options.iframeAttributes?.allow || "";
|
|
96
|
+
const allowPermissions = additionalAllow
|
|
97
|
+
? `${defaultAllow} ${additionalAllow}`
|
|
98
|
+
: defaultAllow;
|
|
99
|
+
iframe.setAttribute("allow", allowPermissions);
|
|
100
|
+
const defaultSandbox = "allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts";
|
|
101
|
+
const additionalSandbox = options.iframeAttributes?.sandbox || "";
|
|
102
|
+
const sandboxPermissions = additionalSandbox
|
|
103
|
+
? `${defaultSandbox} ${additionalSandbox}`
|
|
104
|
+
: defaultSandbox;
|
|
105
|
+
iframe.setAttribute("sandbox", sandboxPermissions);
|
|
106
|
+
if (options.iframeAttributes) {
|
|
107
|
+
Object.entries(options.iframeAttributes).forEach(([key, value]) => {
|
|
108
|
+
if (value &&
|
|
109
|
+
key !== "width" &&
|
|
110
|
+
key !== "height" &&
|
|
111
|
+
key !== "title" &&
|
|
112
|
+
key !== "allow" &&
|
|
113
|
+
key !== "sandbox") {
|
|
114
|
+
iframe.setAttribute(key, value);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return iframe;
|
|
119
|
+
}
|
|
120
|
+
setupMessageListener() {
|
|
121
|
+
this.messageListener = (event) => {
|
|
122
|
+
const iframeOrigin = new URL(this.iframeUrl).origin;
|
|
123
|
+
if (event.origin !== iframeOrigin) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (event.data && typeof event.data === "object") {
|
|
127
|
+
const swapperEvent = {
|
|
128
|
+
type: event.data.type,
|
|
129
|
+
data: event.data.data,
|
|
130
|
+
};
|
|
131
|
+
const handlers = this.eventHandlers.get(swapperEvent.type);
|
|
132
|
+
if (handlers) {
|
|
133
|
+
handlers.forEach((handler) => handler(swapperEvent));
|
|
134
|
+
}
|
|
135
|
+
const wildcardHandlers = this.eventHandlers.get("*");
|
|
136
|
+
if (wildcardHandlers) {
|
|
137
|
+
wildcardHandlers.forEach((handler) => handler(swapperEvent));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
window.addEventListener("message", this.messageListener);
|
|
142
|
+
}
|
|
143
|
+
mount(container) {
|
|
144
|
+
const containerElement = typeof container === "string"
|
|
145
|
+
? document.querySelector(container)
|
|
146
|
+
: container;
|
|
147
|
+
if (!containerElement) {
|
|
148
|
+
throw new Error(`Container not found: ${typeof container === "string" ? container : "provided element"}`);
|
|
149
|
+
}
|
|
150
|
+
containerElement.appendChild(this.iframe);
|
|
151
|
+
}
|
|
152
|
+
updateConfig(config) {
|
|
153
|
+
this.config = { ...this.config, ...config };
|
|
154
|
+
this.sendMessage({
|
|
155
|
+
type: "SWAPPER_CONFIG",
|
|
156
|
+
config: config,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
updateStyles(styles) {
|
|
160
|
+
this.updateConfig({ styles });
|
|
161
|
+
}
|
|
162
|
+
updateCustomContractCalls(customContractCalls) {
|
|
163
|
+
this.updateConfig({ customContractCalls });
|
|
164
|
+
}
|
|
165
|
+
updateSupportedDepositOptions(supportedDepositOptions) {
|
|
166
|
+
this.updateConfig({ supportedDepositOptions });
|
|
167
|
+
}
|
|
168
|
+
updateWebhookUrl(webhookUrl) {
|
|
169
|
+
this.updateConfig({ webhookUrl });
|
|
170
|
+
}
|
|
171
|
+
sendMessage(message) {
|
|
172
|
+
const iframeOrigin = new URL(this.iframeUrl).origin;
|
|
173
|
+
this.iframe.contentWindow?.postMessage(message, iframeOrigin);
|
|
174
|
+
}
|
|
175
|
+
on(eventType, handler) {
|
|
176
|
+
if (!this.eventHandlers.has(eventType)) {
|
|
177
|
+
this.eventHandlers.set(eventType, new Set());
|
|
178
|
+
}
|
|
179
|
+
this.eventHandlers.get(eventType).add(handler);
|
|
180
|
+
}
|
|
181
|
+
off(eventType, handler) {
|
|
182
|
+
const handlers = this.eventHandlers.get(eventType);
|
|
183
|
+
if (handlers) {
|
|
184
|
+
handlers.delete(handler);
|
|
185
|
+
if (handlers.size === 0) {
|
|
186
|
+
this.eventHandlers.delete(eventType);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
getIframe() {
|
|
191
|
+
return this.iframe;
|
|
192
|
+
}
|
|
193
|
+
getConfig() {
|
|
194
|
+
return { ...this.config };
|
|
195
|
+
}
|
|
196
|
+
updateSigner(rawSigner) {
|
|
197
|
+
const signer = createSwapperSigner(rawSigner);
|
|
198
|
+
if (this.walletProvider) {
|
|
199
|
+
this.walletProvider.destroy();
|
|
200
|
+
this.walletProvider = undefined;
|
|
201
|
+
}
|
|
202
|
+
const iframeOrigin = new URL(this.iframeUrl).origin;
|
|
203
|
+
const onTransactionRequest = (tx) => signer.sendTransaction(tx);
|
|
204
|
+
const onChainSwitchRequest = (chainId) => signer.switchChain(chainId);
|
|
205
|
+
Promise.all([signer.getAddress(), signer.getChainId()])
|
|
206
|
+
.then(([address, chainId]) => {
|
|
207
|
+
this.walletProvider = new SwapperWalletProvider({
|
|
208
|
+
iframe: this.iframe,
|
|
209
|
+
iframeOrigin,
|
|
210
|
+
onTransactionRequest,
|
|
211
|
+
onChainSwitchRequest,
|
|
212
|
+
autoConnect: { address, chainId },
|
|
213
|
+
});
|
|
214
|
+
})
|
|
215
|
+
.catch((err) => {
|
|
216
|
+
console.error("[SwapperIframe] Failed to update wallet provider:", err);
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
destroy() {
|
|
220
|
+
if (this.walletProvider) {
|
|
221
|
+
this.walletProvider.destroy();
|
|
222
|
+
this.walletProvider = undefined;
|
|
223
|
+
}
|
|
224
|
+
if (this.messageListener) {
|
|
225
|
+
window.removeEventListener("message", this.messageListener);
|
|
226
|
+
}
|
|
227
|
+
this.eventHandlers.clear();
|
|
228
|
+
if (this.iframe.parentNode) {
|
|
229
|
+
this.iframe.parentNode.removeChild(this.iframe);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=SwapperIframe.js.map
|
package/dist/SwapperModal.js
CHANGED
|
@@ -1 +1,175 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SwapperIframe } from "./SwapperIframe";
|
|
2
|
+
export class SwapperModal {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.iframe = null;
|
|
5
|
+
this.overlay = null;
|
|
6
|
+
this.modalContainer = null;
|
|
7
|
+
this.iframeContainer = null;
|
|
8
|
+
this.isOpen = false;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
open() {
|
|
12
|
+
if (this.isOpen) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
this.createModal();
|
|
16
|
+
this.isOpen = true;
|
|
17
|
+
}
|
|
18
|
+
close() {
|
|
19
|
+
if (!this.isOpen) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this.destroyModal();
|
|
23
|
+
this.isOpen = false;
|
|
24
|
+
if (this.options.onClose) {
|
|
25
|
+
this.options.onClose();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
isModalOpen() {
|
|
29
|
+
return this.isOpen;
|
|
30
|
+
}
|
|
31
|
+
getIframe() {
|
|
32
|
+
return this.iframe;
|
|
33
|
+
}
|
|
34
|
+
createModal() {
|
|
35
|
+
const style = this.options.modalStyle || {};
|
|
36
|
+
const iframeStyles = this.options.styles || {};
|
|
37
|
+
this.overlay = document.createElement("div");
|
|
38
|
+
this.overlay.style.cssText = `
|
|
39
|
+
position: fixed;
|
|
40
|
+
top: 0;
|
|
41
|
+
left: 0;
|
|
42
|
+
width: 100vw;
|
|
43
|
+
height: 100vh;
|
|
44
|
+
background: ${style.overlayColor || "rgba(0, 0, 0, 0.7)"};
|
|
45
|
+
z-index: ${style.zIndex || 10000};
|
|
46
|
+
display: flex;
|
|
47
|
+
align-items: center;
|
|
48
|
+
justify-content: center;
|
|
49
|
+
animation: swapper-fade-in 0.2s ease-out;
|
|
50
|
+
`;
|
|
51
|
+
if (!document.getElementById("swapper-modal-styles")) {
|
|
52
|
+
const styleSheet = document.createElement("style");
|
|
53
|
+
styleSheet.id = "swapper-modal-styles";
|
|
54
|
+
styleSheet.textContent = `
|
|
55
|
+
@keyframes swapper-fade-in {
|
|
56
|
+
from { opacity: 0; }
|
|
57
|
+
to { opacity: 1; }
|
|
58
|
+
}
|
|
59
|
+
@keyframes swapper-scale-in {
|
|
60
|
+
from {
|
|
61
|
+
transform: scale(0.95);
|
|
62
|
+
opacity: 0;
|
|
63
|
+
}
|
|
64
|
+
to {
|
|
65
|
+
transform: scale(1);
|
|
66
|
+
opacity: 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
document.head.appendChild(styleSheet);
|
|
71
|
+
}
|
|
72
|
+
this.overlay.addEventListener("click", (e) => {
|
|
73
|
+
if (e.target === this.overlay) {
|
|
74
|
+
this.close();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const escapeHandler = (e) => {
|
|
78
|
+
if (e.key === "Escape") {
|
|
79
|
+
this.close();
|
|
80
|
+
document.removeEventListener("keydown", escapeHandler);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
document.addEventListener("keydown", escapeHandler);
|
|
84
|
+
this.modalContainer = document.createElement("div");
|
|
85
|
+
this.modalContainer.style.cssText = `
|
|
86
|
+
position: relative;
|
|
87
|
+
width: ${style.width || "450px"};
|
|
88
|
+
height: ${style.height || "560px"};
|
|
89
|
+
background: ${iframeStyles.themeMode === "light" ? "#ffffff" : "#111111"};
|
|
90
|
+
border-radius: 30px;
|
|
91
|
+
overflow: hidden;
|
|
92
|
+
animation: swapper-scale-in 0.3s ease-out;
|
|
93
|
+
`;
|
|
94
|
+
if (style.showCloseButton === true) {
|
|
95
|
+
const closeButton = document.createElement("button");
|
|
96
|
+
closeButton.innerHTML = "×";
|
|
97
|
+
closeButton.style.cssText = `
|
|
98
|
+
position: absolute;
|
|
99
|
+
top: 12px;
|
|
100
|
+
right: 12px;
|
|
101
|
+
width: 32px;
|
|
102
|
+
height: 32px;
|
|
103
|
+
border: none;
|
|
104
|
+
background: rgba(0, 0, 0, 0.5);
|
|
105
|
+
color: white;
|
|
106
|
+
font-size: 24px;
|
|
107
|
+
line-height: 1;
|
|
108
|
+
border-radius: 50%;
|
|
109
|
+
cursor: pointer;
|
|
110
|
+
z-index: ${(style.zIndex || 10000) + 1};
|
|
111
|
+
display: flex;
|
|
112
|
+
align-items: center;
|
|
113
|
+
justify-content: center;
|
|
114
|
+
transition: background 0.2s ease;
|
|
115
|
+
`;
|
|
116
|
+
closeButton.onmouseover = () => {
|
|
117
|
+
closeButton.style.background = "rgba(0, 0, 0, 0.7)";
|
|
118
|
+
};
|
|
119
|
+
closeButton.onmouseout = () => {
|
|
120
|
+
closeButton.style.background = "rgba(0, 0, 0, 0.5)";
|
|
121
|
+
};
|
|
122
|
+
closeButton.onclick = () => this.close();
|
|
123
|
+
this.modalContainer.appendChild(closeButton);
|
|
124
|
+
}
|
|
125
|
+
this.iframeContainer = document.createElement("div");
|
|
126
|
+
this.iframeContainer.style.cssText = `
|
|
127
|
+
width: 100%;
|
|
128
|
+
height: 100%;
|
|
129
|
+
`;
|
|
130
|
+
this.modalContainer.appendChild(this.iframeContainer);
|
|
131
|
+
this.overlay.appendChild(this.modalContainer);
|
|
132
|
+
document.body.appendChild(this.overlay);
|
|
133
|
+
document.body.style.overflow = "hidden";
|
|
134
|
+
this.iframe = new SwapperIframe({
|
|
135
|
+
container: this.iframeContainer,
|
|
136
|
+
integratorId: this.options.integratorId,
|
|
137
|
+
dstChainId: this.options.dstChainId,
|
|
138
|
+
dstTokenAddr: this.options.dstTokenAddr,
|
|
139
|
+
depositWalletAddress: this.options.depositWalletAddress,
|
|
140
|
+
styles: this.options.styles,
|
|
141
|
+
customContractCalls: this.options.customContractCalls,
|
|
142
|
+
supportedDepositOptions: this.options.supportedDepositOptions,
|
|
143
|
+
webhookUrl: this.options.webhookUrl,
|
|
144
|
+
wallet: this.options.wallet,
|
|
145
|
+
iframeUrl: this.options.iframeUrl,
|
|
146
|
+
iframeAttributes: {
|
|
147
|
+
...this.options.iframeAttributes,
|
|
148
|
+
width: "100%",
|
|
149
|
+
height: "100%",
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
destroyModal() {
|
|
154
|
+
if (this.iframe) {
|
|
155
|
+
this.iframe.destroy();
|
|
156
|
+
this.iframe = null;
|
|
157
|
+
}
|
|
158
|
+
if (this.overlay && this.overlay.parentNode) {
|
|
159
|
+
this.overlay.parentNode.removeChild(this.overlay);
|
|
160
|
+
}
|
|
161
|
+
document.body.style.overflow = "";
|
|
162
|
+
this.overlay = null;
|
|
163
|
+
this.modalContainer = null;
|
|
164
|
+
this.iframeContainer = null;
|
|
165
|
+
}
|
|
166
|
+
destroy() {
|
|
167
|
+
this.close();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function openSwapperModal(options) {
|
|
171
|
+
const modal = new SwapperModal(options);
|
|
172
|
+
modal.open();
|
|
173
|
+
return modal;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=SwapperModal.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwapperWalletProvider.d.ts","sourceRoot":"","sources":["../src/SwapperWalletProvider.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"SwapperWalletProvider.d.ts","sourceRoot":"","sources":["../src/SwapperWalletProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAM7B,MAAM,SAAS,CAAC;AAajB,qBAAa,qBAAqB;IAChC,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,oBAAoB,CAA4B;IACxD,OAAO,CAAC,oBAAoB,CAA4B;IACxD,OAAO,CAAC,iBAAiB,CAAC,CAAuC;IACjE,OAAO,CAAC,eAAe,CAAgC;IAEvD,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAqB;IACpC,OAAO,CAAC,OAAO,CAAqB;gBAExB,OAAO,EAAE,4BAA4B;IAWjD,OAAO,CAAC,aAAa;YA0BP,eAAe;YAiBf,wBAAwB;IAuBtC,OAAO,CAAC,WAAW;IAMZ,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAWrD,gBAAgB,IAAI,IAAI;IASxB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQzC,cAAc,CACnB,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,GACvD,IAAI;IAOA,UAAU,IAAI,OAAO;IAIrB,cAAc,IAAI,OAAO;IAIzB,UAAU,IAAI,MAAM,GAAG,SAAS;IAIhC,UAAU,IAAI,MAAM,GAAG,SAAS;IAIhC,OAAO,IAAI,IAAI;CAQvB"}
|
|
@@ -1 +1,139 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SWAPPER_WALLET_MSGS, } from "./types";
|
|
2
|
+
const DEFAULT_ORIGIN = "https://deposit.swapper.finance";
|
|
3
|
+
function resolveOrigin(iframe, explicit) {
|
|
4
|
+
if (explicit)
|
|
5
|
+
return explicit;
|
|
6
|
+
try {
|
|
7
|
+
return new URL(iframe.src).origin;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return DEFAULT_ORIGIN;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class SwapperWalletProvider {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.isReady = false;
|
|
16
|
+
this.isConnected = false;
|
|
17
|
+
this.iframe = options.iframe;
|
|
18
|
+
this.iframeOrigin = resolveOrigin(options.iframe, options.iframeOrigin);
|
|
19
|
+
this.onTransactionRequest = options.onTransactionRequest;
|
|
20
|
+
this.onChainSwitchRequest = options.onChainSwitchRequest;
|
|
21
|
+
this.autoConnectConfig = options.autoConnect;
|
|
22
|
+
this.messageListener = this.handleMessage.bind(this);
|
|
23
|
+
window.addEventListener("message", this.messageListener);
|
|
24
|
+
}
|
|
25
|
+
handleMessage(event) {
|
|
26
|
+
if (event.origin !== this.iframeOrigin)
|
|
27
|
+
return;
|
|
28
|
+
if (!event.data || typeof event.data !== "object")
|
|
29
|
+
return;
|
|
30
|
+
const { type } = event.data;
|
|
31
|
+
switch (type) {
|
|
32
|
+
case SWAPPER_WALLET_MSGS.WALLET_READY:
|
|
33
|
+
this.isReady = true;
|
|
34
|
+
if (this.autoConnectConfig) {
|
|
35
|
+
this.connectWallet(this.autoConnectConfig.address, this.autoConnectConfig.chainId);
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
case SWAPPER_WALLET_MSGS.TX_REQUEST:
|
|
39
|
+
this.handleTxRequest(event.data);
|
|
40
|
+
break;
|
|
41
|
+
case SWAPPER_WALLET_MSGS.CHAIN_SWITCH_REQUEST:
|
|
42
|
+
this.handleChainSwitchRequest(event.data);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async handleTxRequest(msg) {
|
|
47
|
+
try {
|
|
48
|
+
const result = await this.onTransactionRequest(msg.transaction);
|
|
49
|
+
this.postMessage({
|
|
50
|
+
type: SWAPPER_WALLET_MSGS.TX_RESPONSE,
|
|
51
|
+
requestId: msg.requestId,
|
|
52
|
+
hash: result.hash,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
this.postMessage({
|
|
57
|
+
type: SWAPPER_WALLET_MSGS.TX_RESPONSE,
|
|
58
|
+
requestId: msg.requestId,
|
|
59
|
+
error: err instanceof Error ? err.message : String(err),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async handleChainSwitchRequest(msg) {
|
|
64
|
+
try {
|
|
65
|
+
await this.onChainSwitchRequest(Number(msg.chainId));
|
|
66
|
+
this.chainId = Number(msg.chainId);
|
|
67
|
+
this.postMessage({
|
|
68
|
+
type: SWAPPER_WALLET_MSGS.CHAIN_SWITCH_RESPONSE,
|
|
69
|
+
requestId: msg.requestId,
|
|
70
|
+
});
|
|
71
|
+
this.postMessage({
|
|
72
|
+
type: SWAPPER_WALLET_MSGS.WALLET_CHAIN_CHANGED,
|
|
73
|
+
chainId: msg.chainId,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
this.postMessage({
|
|
78
|
+
type: SWAPPER_WALLET_MSGS.CHAIN_SWITCH_RESPONSE,
|
|
79
|
+
requestId: msg.requestId,
|
|
80
|
+
error: err instanceof Error ? err.message : String(err),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
postMessage(message) {
|
|
85
|
+
this.iframe.contentWindow?.postMessage(message, this.iframeOrigin);
|
|
86
|
+
}
|
|
87
|
+
connectWallet(address, chainId) {
|
|
88
|
+
this.isConnected = true;
|
|
89
|
+
this.address = address;
|
|
90
|
+
this.chainId = chainId;
|
|
91
|
+
this.postMessage({
|
|
92
|
+
type: SWAPPER_WALLET_MSGS.WALLET_CONNECT,
|
|
93
|
+
address,
|
|
94
|
+
chainId: String(chainId),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
disconnectWallet() {
|
|
98
|
+
this.isConnected = false;
|
|
99
|
+
this.address = undefined;
|
|
100
|
+
this.chainId = undefined;
|
|
101
|
+
this.postMessage({
|
|
102
|
+
type: SWAPPER_WALLET_MSGS.WALLET_DISCONNECT,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
notifyChainChanged(chainId) {
|
|
106
|
+
this.chainId = chainId;
|
|
107
|
+
this.postMessage({
|
|
108
|
+
type: SWAPPER_WALLET_MSGS.WALLET_CHAIN_CHANGED,
|
|
109
|
+
chainId: String(chainId),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
setAutoConnect(config) {
|
|
113
|
+
this.autoConnectConfig = config;
|
|
114
|
+
if (config && this.isReady) {
|
|
115
|
+
this.connectWallet(config.address, config.chainId);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
getIsReady() {
|
|
119
|
+
return this.isReady;
|
|
120
|
+
}
|
|
121
|
+
getIsConnected() {
|
|
122
|
+
return this.isConnected;
|
|
123
|
+
}
|
|
124
|
+
getAddress() {
|
|
125
|
+
return this.address;
|
|
126
|
+
}
|
|
127
|
+
getChainId() {
|
|
128
|
+
return this.chainId;
|
|
129
|
+
}
|
|
130
|
+
destroy() {
|
|
131
|
+
window.removeEventListener("message", this.messageListener);
|
|
132
|
+
this.isReady = false;
|
|
133
|
+
this.isConnected = false;
|
|
134
|
+
this.address = undefined;
|
|
135
|
+
this.chainId = undefined;
|
|
136
|
+
this.autoConnectConfig = undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=SwapperWalletProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwapperWalletProvider.js","sourceRoot":"","sources":["../src/SwapperWalletProvider.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"SwapperWalletProvider.js","sourceRoot":"","sources":["../src/SwapperWalletProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,mBAAmB,GACpB,MAAM,SAAS,CAAC;AAEjB,MAAM,cAAc,GAAG,iCAAiC,CAAC;AAEzD,SAAS,aAAa,CAAC,MAAyB,EAAE,QAAiB;IACjE,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC9B,IAAI,CAAC;QACH,OAAO,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,cAAc,CAAC;IACxB,CAAC;AACH,CAAC;AAED,MAAM,OAAO,qBAAqB;IAahC,YAAY,OAAqC;QALzC,YAAO,GAAG,KAAK,CAAC;QAChB,gBAAW,GAAG,KAAK,CAAC;QAK1B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;QACxE,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;QACzD,IA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M,UAAU;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAEM,OAAO;QACZ,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QAC5D,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;QACzB,IAAI,CAAC,iBAAiB,GAAG,SAAS,CAAC;IACrC,CAAC;CACF"}
|
package/dist/adapters.js
CHANGED
|
@@ -1 +1,136 @@
|
|
|
1
|
-
export class EthersV5SignerAdapter
|
|
1
|
+
export class EthersV5SignerAdapter {
|
|
2
|
+
constructor(signer) {
|
|
3
|
+
this.signer = signer;
|
|
4
|
+
}
|
|
5
|
+
async getAddress() {
|
|
6
|
+
return this.signer.getAddress();
|
|
7
|
+
}
|
|
8
|
+
async getChainId() {
|
|
9
|
+
return this.signer.getChainId();
|
|
10
|
+
}
|
|
11
|
+
async sendTransaction(transaction) {
|
|
12
|
+
const txResponse = await this.signer.sendTransaction({
|
|
13
|
+
to: transaction.to,
|
|
14
|
+
data: transaction.data,
|
|
15
|
+
value: transaction.value,
|
|
16
|
+
gasLimit: transaction.gasLimit,
|
|
17
|
+
});
|
|
18
|
+
return { hash: txResponse.hash };
|
|
19
|
+
}
|
|
20
|
+
async switchChain(chainId) {
|
|
21
|
+
const hexChainId = "0x" + Number(chainId).toString(16);
|
|
22
|
+
if (this.signer.provider && "send" in this.signer.provider) {
|
|
23
|
+
await this.signer.provider.send("wallet_switchEthereumChain", [
|
|
24
|
+
{ chainId: hexChainId },
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error("Provider does not support wallet_switchEthereumChain");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class EthersV6SignerAdapter {
|
|
33
|
+
constructor(signer) {
|
|
34
|
+
this.signer = signer;
|
|
35
|
+
}
|
|
36
|
+
async getAddress() {
|
|
37
|
+
return this.signer.getAddress();
|
|
38
|
+
}
|
|
39
|
+
async getChainId() {
|
|
40
|
+
const network = await this.signer.provider.getNetwork();
|
|
41
|
+
return Number(network.chainId);
|
|
42
|
+
}
|
|
43
|
+
async sendTransaction(transaction) {
|
|
44
|
+
const txResponse = await this.signer.sendTransaction({
|
|
45
|
+
to: transaction.to,
|
|
46
|
+
data: transaction.data,
|
|
47
|
+
value: transaction.value,
|
|
48
|
+
gasLimit: transaction.gasLimit,
|
|
49
|
+
});
|
|
50
|
+
return { hash: txResponse.hash };
|
|
51
|
+
}
|
|
52
|
+
async switchChain(chainId) {
|
|
53
|
+
const hexChainId = "0x" + Number(chainId).toString(16);
|
|
54
|
+
await this.signer.provider.send("wallet_switchEthereumChain", [
|
|
55
|
+
{ chainId: hexChainId },
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class ViemSignerAdapter {
|
|
60
|
+
constructor(client) {
|
|
61
|
+
this.client = client;
|
|
62
|
+
}
|
|
63
|
+
async getAddress() {
|
|
64
|
+
const [address] = await this.client.getAddresses();
|
|
65
|
+
if (!address)
|
|
66
|
+
throw new Error("No account found in WalletClient");
|
|
67
|
+
return address;
|
|
68
|
+
}
|
|
69
|
+
async getChainId() {
|
|
70
|
+
return this.client.getChainId();
|
|
71
|
+
}
|
|
72
|
+
async sendTransaction(transaction) {
|
|
73
|
+
const [account] = await this.client.getAddresses();
|
|
74
|
+
if (!account)
|
|
75
|
+
throw new Error("No account found");
|
|
76
|
+
const hash = await this.client.sendTransaction({
|
|
77
|
+
account,
|
|
78
|
+
to: transaction.to,
|
|
79
|
+
data: transaction.data,
|
|
80
|
+
value: transaction.value ? BigInt(transaction.value) : undefined,
|
|
81
|
+
gas: transaction.gasLimit ? BigInt(transaction.gasLimit) : undefined,
|
|
82
|
+
chain: undefined,
|
|
83
|
+
});
|
|
84
|
+
return { hash };
|
|
85
|
+
}
|
|
86
|
+
async switchChain(chainId) {
|
|
87
|
+
await this.client.switchChain({ id: chainId });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function isViemWalletClient(signer) {
|
|
91
|
+
return (typeof signer === "object" &&
|
|
92
|
+
signer !== null &&
|
|
93
|
+
typeof signer.getAddresses === "function");
|
|
94
|
+
}
|
|
95
|
+
function isEthersV6Signer(signer) {
|
|
96
|
+
return (typeof signer === "object" &&
|
|
97
|
+
signer !== null &&
|
|
98
|
+
typeof signer.getAddress === "function" &&
|
|
99
|
+
typeof signer.sendTransaction === "function" &&
|
|
100
|
+
signer.provider != null &&
|
|
101
|
+
typeof signer.provider.getNetwork === "function" &&
|
|
102
|
+
!("getChainId" in signer));
|
|
103
|
+
}
|
|
104
|
+
function isEthersV5Signer(signer) {
|
|
105
|
+
return (typeof signer === "object" &&
|
|
106
|
+
signer !== null &&
|
|
107
|
+
typeof signer.getAddress === "function" &&
|
|
108
|
+
typeof signer.getChainId === "function" &&
|
|
109
|
+
typeof signer.sendTransaction === "function" &&
|
|
110
|
+
!("getAddresses" in signer));
|
|
111
|
+
}
|
|
112
|
+
function isSwapperSigner(signer) {
|
|
113
|
+
return (typeof signer === "object" &&
|
|
114
|
+
signer !== null &&
|
|
115
|
+
typeof signer.getAddress === "function" &&
|
|
116
|
+
typeof signer.getChainId === "function" &&
|
|
117
|
+
typeof signer.sendTransaction === "function" &&
|
|
118
|
+
typeof signer.switchChain === "function");
|
|
119
|
+
}
|
|
120
|
+
export function createSwapperSigner(signer) {
|
|
121
|
+
if (isViemWalletClient(signer)) {
|
|
122
|
+
return new ViemSignerAdapter(signer);
|
|
123
|
+
}
|
|
124
|
+
if (isEthersV6Signer(signer)) {
|
|
125
|
+
return new EthersV6SignerAdapter(signer);
|
|
126
|
+
}
|
|
127
|
+
if (isEthersV5Signer(signer)) {
|
|
128
|
+
return new EthersV5SignerAdapter(signer);
|
|
129
|
+
}
|
|
130
|
+
if (isSwapperSigner(signer)) {
|
|
131
|
+
return signer;
|
|
132
|
+
}
|
|
133
|
+
throw new Error("Unsupported signer type. Expected an ethers v5 Signer, ethers v6 Signer, " +
|
|
134
|
+
"viem WalletClient, or an object implementing the SwapperSigner interface.");
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=adapters.js.map
|
package/dist/helpers.js
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ContractCallType } from "./types";
|
|
2
|
+
class ABICoder {
|
|
3
|
+
encodeApprove(spender, amount) {
|
|
4
|
+
const selector = "0x095ea7b3";
|
|
5
|
+
const paddedSpender = spender.slice(2).padStart(64, "0");
|
|
6
|
+
const paddedAmount = BigInt(amount).toString(16).padStart(64, "0");
|
|
7
|
+
return selector + paddedSpender + paddedAmount;
|
|
8
|
+
}
|
|
9
|
+
encodeTransfer(recipient, amount) {
|
|
10
|
+
const selector = "0xa9059cbb";
|
|
11
|
+
const paddedRecipient = recipient.slice(2).padStart(64, "0");
|
|
12
|
+
const paddedAmount = BigInt(amount).toString(16).padStart(64, "0");
|
|
13
|
+
return selector + paddedRecipient + paddedAmount;
|
|
14
|
+
}
|
|
15
|
+
encodeTransferFrom(from, to, amount) {
|
|
16
|
+
const selector = "0x23b872dd";
|
|
17
|
+
const paddedFrom = from.slice(2).padStart(64, "0");
|
|
18
|
+
const paddedTo = to.slice(2).padStart(64, "0");
|
|
19
|
+
const paddedAmount = BigInt(amount).toString(16).padStart(64, "0");
|
|
20
|
+
return selector + paddedFrom + paddedTo + paddedAmount;
|
|
21
|
+
}
|
|
22
|
+
encodeBalancePayload(tokenAddress, balanceSlot) {
|
|
23
|
+
const paddedAddress = tokenAddress.slice(2).padStart(64, "0");
|
|
24
|
+
const paddedSlot = BigInt(balanceSlot).toString(16).padStart(64, "0");
|
|
25
|
+
return "0x" + paddedAddress + paddedSlot;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const abiCoder = new ABICoder();
|
|
29
|
+
export function approve(tokenAddress, spender, amount) {
|
|
30
|
+
return {
|
|
31
|
+
callType: ContractCallType.DEFAULT,
|
|
32
|
+
target: tokenAddress,
|
|
33
|
+
value: "0",
|
|
34
|
+
callData: abiCoder.encodeApprove(spender, amount),
|
|
35
|
+
payload: "0x",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function transfer(tokenAddress, recipient, amount) {
|
|
39
|
+
return {
|
|
40
|
+
callType: ContractCallType.DEFAULT,
|
|
41
|
+
target: tokenAddress,
|
|
42
|
+
value: "0",
|
|
43
|
+
callData: abiCoder.encodeTransfer(recipient, amount),
|
|
44
|
+
payload: "0x",
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function transferFrom(tokenAddress, from, to, amount) {
|
|
48
|
+
return {
|
|
49
|
+
callType: ContractCallType.DEFAULT,
|
|
50
|
+
target: tokenAddress,
|
|
51
|
+
value: "0",
|
|
52
|
+
callData: abiCoder.encodeTransferFrom(from, to, amount),
|
|
53
|
+
payload: "0x",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function approveBalance(tokenAddress, spender) {
|
|
57
|
+
return {
|
|
58
|
+
callType: ContractCallType.FULL_TOKEN_BALANCE,
|
|
59
|
+
target: tokenAddress,
|
|
60
|
+
value: "0",
|
|
61
|
+
callData: abiCoder.encodeApprove(spender, "0"),
|
|
62
|
+
payload: abiCoder.encodeBalancePayload(tokenAddress, "1"),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function transferBalance(tokenAddress, recipient) {
|
|
66
|
+
return {
|
|
67
|
+
callType: ContractCallType.FULL_TOKEN_BALANCE,
|
|
68
|
+
target: tokenAddress,
|
|
69
|
+
value: "0",
|
|
70
|
+
callData: abiCoder.encodeTransfer(recipient, "0"),
|
|
71
|
+
payload: abiCoder.encodeBalancePayload(tokenAddress, "1"),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=helpers.js.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
export{SwapperIframe}from"./SwapperIframe";
|
|
1
|
+
export { SwapperIframe } from "./SwapperIframe";
|
|
2
|
+
export { SwapperModal, openSwapperModal } from "./SwapperModal";
|
|
3
|
+
export { SwapperWalletProvider } from "./SwapperWalletProvider";
|
|
4
|
+
export { ContractCallType, SWAPPER_WALLET_MSGS, } from "./types";
|
|
5
|
+
export { approve, transfer, transferFrom, approveBalance, transferBalance, } from "./helpers";
|
|
6
|
+
export { createSwapperSigner, EthersV5SignerAdapter, EthersV6SignerAdapter, ViemSignerAdapter, } from "./adapters";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
package/dist/types/config.js
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
-
export var ContractCallType
|
|
1
|
+
export var ContractCallType;
|
|
2
|
+
(function (ContractCallType) {
|
|
3
|
+
ContractCallType[ContractCallType["DEFAULT"] = 0] = "DEFAULT";
|
|
4
|
+
ContractCallType[ContractCallType["FULL_TOKEN_BALANCE"] = 1] = "FULL_TOKEN_BALANCE";
|
|
5
|
+
ContractCallType[ContractCallType["FULL_NATIVE_BALANCE"] = 2] = "FULL_NATIVE_BALANCE";
|
|
6
|
+
ContractCallType[ContractCallType["COLLECT_TOKEN_BALANCE"] = 3] = "COLLECT_TOKEN_BALANCE";
|
|
7
|
+
})(ContractCallType || (ContractCallType = {}));
|
|
8
|
+
//# sourceMappingURL=config.js.map
|
package/dist/types/events.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export{};
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=events.js.map
|
package/dist/types/index.js
CHANGED
package/dist/types/styles.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export{};
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=styles.js.map
|
package/dist/types/wallet.js
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
export const SWAPPER_WALLET_MSGS={
|
|
1
|
+
export const SWAPPER_WALLET_MSGS = {
|
|
2
|
+
WALLET_READY: "SWAPPER_WALLET_READY",
|
|
3
|
+
WALLET_CONNECT: "SWAPPER_WALLET_CONNECT",
|
|
4
|
+
WALLET_DISCONNECT: "SWAPPER_WALLET_DISCONNECT",
|
|
5
|
+
WALLET_CHAIN_CHANGED: "SWAPPER_WALLET_CHAIN_CHANGED",
|
|
6
|
+
TX_REQUEST: "SWAPPER_TX_REQUEST",
|
|
7
|
+
TX_RESPONSE: "SWAPPER_TX_RESPONSE",
|
|
8
|
+
CHAIN_SWITCH_REQUEST: "SWAPPER_CHAIN_SWITCH_REQUEST",
|
|
9
|
+
CHAIN_SWITCH_RESPONSE: "SWAPPER_CHAIN_SWITCH_RESPONSE",
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=wallet.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swapper-finance/deposit-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Easy iframe embedding for Swapper deposit widget with full TypeScript support",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,8 +12,7 @@
|
|
|
12
12
|
"LICENSE"
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
|
-
"build": "tsc
|
|
16
|
-
"minify": "find dist -name '*.js' -exec terser {} -o {} --compress --mangle \\;",
|
|
15
|
+
"build": "tsc",
|
|
17
16
|
"prepare": "npm run build",
|
|
18
17
|
"clean": "rm -rf dist",
|
|
19
18
|
"watch": "tsc --watch",
|
package/dist/types.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
export type ThemeMode = "light" | "dark";
|
|
2
|
-
export interface ComponentStyles {
|
|
3
|
-
primaryColor?: string;
|
|
4
|
-
primaryBorderColor?: string;
|
|
5
|
-
accentColor?: string;
|
|
6
|
-
primaryTextColor?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface SwapperStyles {
|
|
9
|
-
themeMode?: ThemeMode;
|
|
10
|
-
componentStyles?: ComponentStyles;
|
|
11
|
-
}
|
|
12
|
-
export type SupportedDepositOption = "transferCrypto" | "depositWithCash" | "walletDeposit";
|
|
13
|
-
export declare enum ContractCallType {
|
|
14
|
-
DEFAULT = 0,
|
|
15
|
-
FULL_TOKEN_BALANCE = 1,
|
|
16
|
-
FULL_NATIVE_BALANCE = 2,
|
|
17
|
-
COLLECT_TOKEN_BALANCE = 3
|
|
18
|
-
}
|
|
19
|
-
export interface ContractCall {
|
|
20
|
-
callType: ContractCallType;
|
|
21
|
-
target: string;
|
|
22
|
-
value: string;
|
|
23
|
-
callData: string;
|
|
24
|
-
payload: string;
|
|
25
|
-
}
|
|
26
|
-
export interface SwapperConfig {
|
|
27
|
-
integratorId: string;
|
|
28
|
-
dstChainId: string;
|
|
29
|
-
dstTokenAddr: string;
|
|
30
|
-
depositWalletAddress: string;
|
|
31
|
-
styles?: SwapperStyles;
|
|
32
|
-
customContractCalls?: ContractCall[];
|
|
33
|
-
supportedDepositOptions?: SupportedDepositOption[];
|
|
34
|
-
webhookUrl?: string;
|
|
35
|
-
}
|
|
36
|
-
export interface SwapperIframeOptions extends SwapperConfig {
|
|
37
|
-
container?: HTMLElement | string;
|
|
38
|
-
iframeUrl?: string;
|
|
39
|
-
iframeAttributes?: {
|
|
40
|
-
width?: string;
|
|
41
|
-
height?: string;
|
|
42
|
-
title?: string;
|
|
43
|
-
allow?: string;
|
|
44
|
-
sandbox?: string;
|
|
45
|
-
[key: string]: string | undefined;
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
export interface SwapperEvent {
|
|
49
|
-
type: string;
|
|
50
|
-
data?: unknown;
|
|
51
|
-
}
|
|
52
|
-
export type SwapperEventHandler = (event: SwapperEvent) => void;
|
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a;IAKzD,SAAS,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC;IAKjC,SAAS,CAAC,EAAE,MAAM,CAAC;IAKnB,gBAAgB,CAAC,EAAE;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;KACnC,CAAC;CACH;AAKD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAKD,MAAM,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC"}
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export var ContractCallType;!function(L){L[L.DEFAULT=0]="DEFAULT",L[L.FULL_TOKEN_BALANCE=1]="FULL_TOKEN_BALANCE",L[L.FULL_NATIVE_BALANCE=2]="FULL_NATIVE_BALANCE",L[L.COLLECT_TOKEN_BALANCE=3]="COLLECT_TOKEN_BALANCE"}(ContractCallType||(ContractCallType={}));
|
package/dist/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAyCA,MAAM,CAAN,IAAY,gBAKX;AALD,WAAY,gBAAgB;IAC1B,6DAAW,CAAA;IACX,mFAAsB,CAAA;IACtB,qFAAuB,CAAA;IACvB,yFAAyB,CAAA;AAC3B,CAAC,EALW,gBAAgB,KAAhB,gBAAgB,QAK3B"}
|