zksync-easy-onramp
Version:
SDK package for ZKsync easy on ramp
158 lines (157 loc) • 4.11 kB
JavaScript
import { i as w, I as y, B as p, g, s as k, d as E, l as O, a as b, c as x, b as R, e as M, H as m, f as S } from "./index-MjMIrW38.js";
function $(r, a) {
if (!w(r, { strict: !1 }))
throw new y({ address: r });
if (!w(a, { strict: !1 }))
throw new y({ address: a });
return r.toLowerCase() === a.toLowerCase();
}
class q extends p {
constructor({ callbackSelector: a, cause: t, data: c, extraData: i, sender: f, urls: s }) {
var n;
super(t.shortMessage || "An error occurred while fetching for an offchain result.", {
cause: t,
metaMessages: [
...t.metaMessages || [],
(n = t.metaMessages) != null && n.length ? "" : [],
"Offchain Gateway Call:",
s && [
" Gateway URL(s):",
...s.map((d) => ` ${g(d)}`)
],
` Sender: ${f}`,
` Data: ${c}`,
` Callback selector: ${a}`,
` Extra data: ${i}`
].flat(),
name: "OffchainLookupError"
});
}
}
class C extends p {
constructor({ result: a, url: t }) {
super("Offchain gateway response is malformed. Response data must be a hex value.", {
metaMessages: [
`Gateway URL: ${g(t)}`,
`Response: ${k(a)}`
],
name: "OffchainLookupResponseMalformedError"
});
}
}
class A extends p {
constructor({ sender: a, to: t }) {
super("Reverted sender address does not match target contract address (`to`).", {
metaMessages: [
`Contract address: ${t}`,
`OffchainLookup sender address: ${a}`
],
name: "OffchainLookupSenderMismatchError"
});
}
}
const B = "0x556f1830", T = {
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 P(r, { blockNumber: a, blockTag: t, data: c, to: i }) {
const { args: f } = E({
data: c,
abi: [T]
}), [s, n, d, l, o] = f, { ccipRead: e } = r, h = e && typeof (e == null ? void 0 : e.request) == "function" ? e.request : G;
try {
if (!$(i, s))
throw new A({ sender: s, to: i });
const u = n.includes(O) ? await b({
data: d,
ccipRequest: h
}) : await h({ data: d, sender: s, urls: n }), { data: L } = await x(r, {
blockNumber: a,
blockTag: t,
data: R([
l,
M([{ type: "bytes" }, { type: "bytes" }], [u, o])
]),
to: i
});
return L;
} catch (u) {
throw new q({
callbackSelector: l,
cause: u,
data: c,
extraData: o,
sender: s,
urls: n
});
}
}
async function G({ data: r, sender: a, urls: t }) {
var i;
let c = new Error("An unknown error occurred.");
for (let f = 0; f < t.length; f++) {
const s = t[f], n = s.includes("{data}") ? "GET" : "POST", d = n === "POST" ? { data: r, sender: a } : void 0, l = n === "POST" ? { "Content-Type": "application/json" } : {};
try {
const o = await fetch(s.replace("{sender}", a.toLowerCase()).replace("{data}", r), {
body: JSON.stringify(d),
headers: l,
method: n
});
let e;
if ((i = o.headers.get("Content-Type")) != null && i.startsWith("application/json") ? e = (await o.json()).data : e = await o.text(), !o.ok) {
c = new m({
body: d,
details: e != null && e.error ? k(e.error) : o.statusText,
headers: o.headers,
status: o.status,
url: s
});
continue;
}
if (!S(e)) {
c = new C({
result: e,
url: s
});
continue;
}
return e;
} catch (o) {
c = new m({
body: d,
details: o.message,
url: s
});
}
}
throw c;
}
export {
G as ccipRequest,
P as offchainLookup,
T as offchainLookupAbiItem,
B as offchainLookupSignature
};
//# sourceMappingURL=ccip-7k_P9n65.js.map