multicoyn-sdk 0.1.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 ADDED
@@ -0,0 +1,95 @@
1
+ # Multicoyn SDK
2
+
3
+ A Web3 payment SDK for multichain crypto payments with support for multiple tokens and settlement options.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install multicoyn-sdk
9
+ # or
10
+ yarn add multicoyn-sdk
11
+ # or
12
+ pnpm add multicoyn-sdk
13
+ ```
14
+
15
+ ## Peer Dependencies
16
+
17
+ Make sure you have the following peer dependencies installed:
18
+
19
+ ```bash
20
+ npm install react react-dom viem wagmi @tanstack/react-query @rainbow-me/rainbowkit
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### 1. Setup Providers
26
+
27
+ Wrap your app with the required providers:
28
+
29
+ ```tsx
30
+ import { Web3Provider, PaymentProvider } from 'multicoyn-sdk';
31
+ import 'multicoyn-sdk/styles';
32
+
33
+ function App() {
34
+ return (
35
+ <Web3Provider>
36
+ <PaymentProvider>
37
+ <YourApp />
38
+ </PaymentProvider>
39
+ </Web3Provider>
40
+ );
41
+ }
42
+ ```
43
+
44
+ ### 2. Use the Payment Button
45
+
46
+ ```tsx
47
+ import { MulticoynButton } from 'multicoyn-sdk';
48
+
49
+ function YourComponent() {
50
+ return (
51
+ <MulticoynButton
52
+ amount="100"
53
+ currency="USD"
54
+ recipientAddress="0x..."
55
+ onSuccess={(txHash) => console.log('Payment success:', txHash)}
56
+ onError={(error) => console.error('Payment error:', error)}
57
+ />
58
+ );
59
+ }
60
+ ```
61
+
62
+ ### 3. Access Payment Configuration
63
+
64
+ ```tsx
65
+ import { usePaymentConfig } from 'multicoyn-sdk';
66
+
67
+ function YourComponent() {
68
+ const config = usePaymentConfig();
69
+ // Access payment configuration and state
70
+ }
71
+ ```
72
+
73
+ ## Features
74
+
75
+ - Multi-chain payment support
76
+ - Multiple token options (USDC, USDT, DAI, WBTC, Native tokens)
77
+ - Settlement token support (IDRX)
78
+ - Built-in wallet connection via RainbowKit
79
+ - TypeScript support
80
+ - Tailwind CSS styling
81
+
82
+ ## Configuration
83
+
84
+ The SDK comes with pre-configured contracts and chains. You can also access these configurations:
85
+
86
+ ```tsx
87
+ import { CONTRACTS, TOKENS, liskSepolia } from 'multicoyn-sdk';
88
+
89
+ console.log(CONTRACTS.PAYMENT_ROUTER);
90
+ console.log(TOKENS.USDC);
91
+ ```
92
+
93
+ ## License
94
+
95
+ MIT
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-C71hhSSX.cjs");function k(r){const{abi:n,data:a}=r,o=e.slice(a,0,4),t=n.find(s=>s.type==="function"&&o===e.toFunctionSelector(e.formatAbiItem(s)));if(!t)throw new e.AbiFunctionSignatureNotFoundError(o,{docsPath:"/docs/contract/decodeFunctionData"});return{functionName:t.name,args:"inputs"in t&&t.inputs&&t.inputs.length>0?e.decodeAbiParameters(t.inputs,e.slice(a,4)):void 0}}const h="/docs/contract/encodeErrorResult";function y(r){const{abi:n,errorName:a,args:o}=r;let t=n[0];if(a){const f=e.getAbiItem({abi:n,args:o,name:a});if(!f)throw new e.AbiErrorNotFoundError(a,{docsPath:h});t=f}if(t.type!=="error")throw new e.AbiErrorNotFoundError(void 0,{docsPath:h});const s=e.formatAbiItem(t),c=e.toFunctionSelector(s);let i="0x";if(o&&o.length>0){if(!t.inputs)throw new e.AbiErrorInputsNotFoundError(t.name,{docsPath:h});i=e.encodeAbiParameters(t.inputs,o)}return e.concatHex([c,i])}const m="/docs/contract/encodeFunctionResult";function L(r){const{abi:n,functionName:a,result:o}=r;let t=n[0];if(a){const c=e.getAbiItem({abi:n,name:a});if(!c)throw new e.AbiFunctionNotFoundError(a,{docsPath:m});t=c}if(t.type!=="function")throw new e.AbiFunctionNotFoundError(void 0,{docsPath:m});if(!t.outputs)throw new e.AbiFunctionOutputsNotFoundError(t.name,{docsPath:m});const s=(()=>{if(t.outputs.length===0)return[];if(t.outputs.length===1)return[o];if(Array.isArray(o))return o;throw new e.InvalidArrayError(o)})();return e.encodeAbiParameters(t.outputs,s)}const b="x-batch-gateway:true";async function g(r){const{data:n,ccipRequest:a}=r,{args:[o]}=k({abi:e.batchGatewayAbi,data:n}),t=[],s=[];return await Promise.all(o.map(async(c,i)=>{try{s[i]=c.urls.includes(b)?await g({data:c.data,ccipRequest:a}):await a(c),t[i]=!1}catch(f){t[i]=!0,s[i]=F(f)}})),L({abi:e.batchGatewayAbi,functionName:"query",result:[t,s]})}function F(r){return r.name==="HttpRequestError"&&r.status?y({abi:e.batchGatewayAbi,errorName:"HttpError",args:[r.status,r.shortMessage]}):y({abi:[e.solidityError],errorName:"Error",args:["shortMessage"in r?r.shortMessage:r.message]})}function O(r,n){if(!e.isAddress(r,{strict:!1}))throw new e.InvalidAddressError({address:r});if(!e.isAddress(n,{strict:!1}))throw new e.InvalidAddressError({address:n});return r.toLowerCase()===n.toLowerCase()}class P extends e.BaseError{constructor({callbackSelector:n,cause:a,data:o,extraData:t,sender:s,urls:c}){super(a.shortMessage||"An error occurred while fetching for an offchain result.",{cause:a,metaMessages:[...a.metaMessages||[],a.metaMessages?.length?"":[],"Offchain Gateway Call:",c&&[" Gateway URL(s):",...c.map(i=>` ${e.getUrl(i)}`)],` Sender: ${s}`,` Data: ${o}`,` Callback selector: ${n}`,` Extra data: ${t}`].flat(),name:"OffchainLookupError"})}}class S extends e.BaseError{constructor({result:n,url:a}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${e.getUrl(a)}`,`Response: ${e.stringify(n)}`],name:"OffchainLookupResponseMalformedError"})}}class x extends e.BaseError{constructor({sender:n,to:a}){super("Reverted sender address does not match target contract address (`to`).",{metaMessages:[`Contract address: ${a}`,`OffchainLookup sender address: ${n}`],name:"OffchainLookupSenderMismatchError"})}}const M="0x556f1830",E={name:"OffchainLookup",type:"error",inputs:[{name:"sender",type:"address"},{name:"urls",type:"string[]"},{name:"callData",type:"bytes"},{name:"callbackFunction",type:"bytes4"},{name:"extraData",type:"bytes"}]};async function N(r,{blockNumber:n,blockTag:a,data:o,to:t}){const{args:s}=e.decodeErrorResult({data:o,abi:[E]}),[c,i,f,u,d]=s,{ccipRead:l}=r,w=l&&typeof l?.request=="function"?l.request:A;try{if(!O(t,c))throw new x({sender:c,to:t});const p=i.includes(b)?await g({data:f,ccipRequest:w}):await w({data:f,sender:c,urls:i}),{data:R}=await e.call(r,{blockNumber:n,blockTag:a,data:e.concat([u,e.encodeAbiParameters([{type:"bytes"},{type:"bytes"}],[p,d])]),to:t});return R}catch(p){throw new P({callbackSelector:u,cause:p,data:o,extraData:d,sender:c,urls:i})}}async function A({data:r,sender:n,urls:a}){let o=new Error("An unknown error occurred.");for(let t=0;t<a.length;t++){const s=a[t],c=s.includes("{data}")?"GET":"POST",i=c==="POST"?{data:r,sender:n}:void 0,f=c==="POST"?{"Content-Type":"application/json"}:{};try{const u=await fetch(s.replace("{sender}",n.toLowerCase()).replace("{data}",r),{body:JSON.stringify(i),headers:f,method:c});let d;if(u.headers.get("Content-Type")?.startsWith("application/json")?d=(await u.json()).data:d=await u.text(),!u.ok){o=new e.HttpRequestError({body:i,details:d?.error?e.stringify(d.error):u.statusText,headers:u.headers,status:u.status,url:s});continue}if(!e.isHex(d)){o=new S({result:d,url:s});continue}return d}catch(u){o=new e.HttpRequestError({body:i,details:u.message,url:s})}}throw o}exports.ccipRequest=A;exports.offchainLookup=N;exports.offchainLookupAbiItem=E;exports.offchainLookupSignature=M;
@@ -0,0 +1,237 @@
1
+ import { s as b, t as x, f as F, A as I, d as C, g as M, a as E, b as D, e as w, c as G, h as A, i as T, I as H, j as m, k as v, l as R, m as k, B as y, n as N, o as P, p as j, q as B, r as U, H as L, u as _ } from "./index-QyDcHjLV.js";
2
+ function J(e) {
3
+ const { abi: s, data: a } = e, r = b(a, 0, 4), t = s.find((n) => n.type === "function" && r === x(F(n)));
4
+ if (!t)
5
+ throw new I(r, {
6
+ docsPath: "/docs/contract/decodeFunctionData"
7
+ });
8
+ return {
9
+ functionName: t.name,
10
+ args: "inputs" in t && t.inputs && t.inputs.length > 0 ? C(t.inputs, b(a, 4)) : void 0
11
+ };
12
+ }
13
+ const p = "/docs/contract/encodeErrorResult";
14
+ function O(e) {
15
+ const { abi: s, errorName: a, args: r } = e;
16
+ let t = s[0];
17
+ if (a) {
18
+ const d = M({ abi: s, args: r, name: a });
19
+ if (!d)
20
+ throw new E(a, { docsPath: p });
21
+ t = d;
22
+ }
23
+ if (t.type !== "error")
24
+ throw new E(void 0, { docsPath: p });
25
+ const n = F(t), o = x(n);
26
+ let c = "0x";
27
+ if (r && r.length > 0) {
28
+ if (!t.inputs)
29
+ throw new D(t.name, { docsPath: p });
30
+ c = w(t.inputs, r);
31
+ }
32
+ return G([o, c]);
33
+ }
34
+ const h = "/docs/contract/encodeFunctionResult";
35
+ function W(e) {
36
+ const { abi: s, functionName: a, result: r } = e;
37
+ let t = s[0];
38
+ if (a) {
39
+ const o = M({ abi: s, name: a });
40
+ if (!o)
41
+ throw new A(a, { docsPath: h });
42
+ t = o;
43
+ }
44
+ if (t.type !== "function")
45
+ throw new A(void 0, { docsPath: h });
46
+ if (!t.outputs)
47
+ throw new T(t.name, { docsPath: h });
48
+ const n = (() => {
49
+ if (t.outputs.length === 0)
50
+ return [];
51
+ if (t.outputs.length === 1)
52
+ return [r];
53
+ if (Array.isArray(r))
54
+ return r;
55
+ throw new H(r);
56
+ })();
57
+ return w(t.outputs, n);
58
+ }
59
+ const q = "x-batch-gateway:true";
60
+ async function S(e) {
61
+ const { data: s, ccipRequest: a } = e, { args: [r] } = J({ abi: m, data: s }), t = [], n = [];
62
+ return await Promise.all(r.map(async (o, c) => {
63
+ try {
64
+ n[c] = o.urls.includes(q) ? await S({ data: o.data, ccipRequest: a }) : await a(o), t[c] = !1;
65
+ } catch (d) {
66
+ t[c] = !0, n[c] = z(d);
67
+ }
68
+ })), W({
69
+ abi: m,
70
+ functionName: "query",
71
+ result: [t, n]
72
+ });
73
+ }
74
+ function z(e) {
75
+ return e.name === "HttpRequestError" && e.status ? O({
76
+ abi: m,
77
+ errorName: "HttpError",
78
+ args: [e.status, e.shortMessage]
79
+ }) : O({
80
+ abi: [v],
81
+ errorName: "Error",
82
+ args: ["shortMessage" in e ? e.shortMessage : e.message]
83
+ });
84
+ }
85
+ function K(e, s) {
86
+ if (!R(e, { strict: !1 }))
87
+ throw new k({ address: e });
88
+ if (!R(s, { strict: !1 }))
89
+ throw new k({ address: s });
90
+ return e.toLowerCase() === s.toLowerCase();
91
+ }
92
+ class Q extends y {
93
+ constructor({ callbackSelector: s, cause: a, data: r, extraData: t, sender: n, urls: o }) {
94
+ super(a.shortMessage || "An error occurred while fetching for an offchain result.", {
95
+ cause: a,
96
+ metaMessages: [
97
+ ...a.metaMessages || [],
98
+ a.metaMessages?.length ? "" : [],
99
+ "Offchain Gateway Call:",
100
+ o && [
101
+ " Gateway URL(s):",
102
+ ...o.map((c) => ` ${N(c)}`)
103
+ ],
104
+ ` Sender: ${n}`,
105
+ ` Data: ${r}`,
106
+ ` Callback selector: ${s}`,
107
+ ` Extra data: ${t}`
108
+ ].flat(),
109
+ name: "OffchainLookupError"
110
+ });
111
+ }
112
+ }
113
+ class V extends y {
114
+ constructor({ result: s, url: a }) {
115
+ super("Offchain gateway response is malformed. Response data must be a hex value.", {
116
+ metaMessages: [
117
+ `Gateway URL: ${N(a)}`,
118
+ `Response: ${P(s)}`
119
+ ],
120
+ name: "OffchainLookupResponseMalformedError"
121
+ });
122
+ }
123
+ }
124
+ class X extends y {
125
+ constructor({ sender: s, to: a }) {
126
+ super("Reverted sender address does not match target contract address (`to`).", {
127
+ metaMessages: [
128
+ `Contract address: ${a}`,
129
+ `OffchainLookup sender address: ${s}`
130
+ ],
131
+ name: "OffchainLookupSenderMismatchError"
132
+ });
133
+ }
134
+ }
135
+ const et = "0x556f1830", Y = {
136
+ name: "OffchainLookup",
137
+ type: "error",
138
+ inputs: [
139
+ {
140
+ name: "sender",
141
+ type: "address"
142
+ },
143
+ {
144
+ name: "urls",
145
+ type: "string[]"
146
+ },
147
+ {
148
+ name: "callData",
149
+ type: "bytes"
150
+ },
151
+ {
152
+ name: "callbackFunction",
153
+ type: "bytes4"
154
+ },
155
+ {
156
+ name: "extraData",
157
+ type: "bytes"
158
+ }
159
+ ]
160
+ };
161
+ async function at(e, { blockNumber: s, blockTag: a, data: r, to: t }) {
162
+ const { args: n } = j({
163
+ data: r,
164
+ abi: [Y]
165
+ }), [o, c, d, i, u] = n, { ccipRead: f } = e, g = f && typeof f?.request == "function" ? f.request : Z;
166
+ try {
167
+ if (!K(t, o))
168
+ throw new X({ sender: o, to: t });
169
+ const l = c.includes(q) ? await S({
170
+ data: d,
171
+ ccipRequest: g
172
+ }) : await g({ data: d, sender: o, urls: c }), { data: $ } = await B(e, {
173
+ blockNumber: s,
174
+ blockTag: a,
175
+ data: U([
176
+ i,
177
+ w([{ type: "bytes" }, { type: "bytes" }], [l, u])
178
+ ]),
179
+ to: t
180
+ });
181
+ return $;
182
+ } catch (l) {
183
+ throw new Q({
184
+ callbackSelector: i,
185
+ cause: l,
186
+ data: r,
187
+ extraData: u,
188
+ sender: o,
189
+ urls: c
190
+ });
191
+ }
192
+ }
193
+ async function Z({ data: e, sender: s, urls: a }) {
194
+ let r = new Error("An unknown error occurred.");
195
+ for (let t = 0; t < a.length; t++) {
196
+ const n = a[t], o = n.includes("{data}") ? "GET" : "POST", c = o === "POST" ? { data: e, sender: s } : void 0, d = o === "POST" ? { "Content-Type": "application/json" } : {};
197
+ try {
198
+ const i = await fetch(n.replace("{sender}", s.toLowerCase()).replace("{data}", e), {
199
+ body: JSON.stringify(c),
200
+ headers: d,
201
+ method: o
202
+ });
203
+ let u;
204
+ if (i.headers.get("Content-Type")?.startsWith("application/json") ? u = (await i.json()).data : u = await i.text(), !i.ok) {
205
+ r = new L({
206
+ body: c,
207
+ details: u?.error ? P(u.error) : i.statusText,
208
+ headers: i.headers,
209
+ status: i.status,
210
+ url: n
211
+ });
212
+ continue;
213
+ }
214
+ if (!_(u)) {
215
+ r = new V({
216
+ result: u,
217
+ url: n
218
+ });
219
+ continue;
220
+ }
221
+ return u;
222
+ } catch (i) {
223
+ r = new L({
224
+ body: c,
225
+ details: i.message,
226
+ url: n
227
+ });
228
+ }
229
+ }
230
+ throw r;
231
+ }
232
+ export {
233
+ Z as ccipRequest,
234
+ at as offchainLookup,
235
+ Y as offchainLookupAbiItem,
236
+ et as offchainLookupSignature
237
+ };