UNPKG

@midcontract/protocol

Version:

Crypto payment protocol with escrow

2 lines (1 loc) 2.73 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-isZQLfFm.js");class w extends e.BaseError{constructor({callbackSelector:o,cause:t,data:n,extraData:i,sender:u,urls:a}){var f;super(t.shortMessage||"An error occurred while fetching for an offchain result.",{cause:t,metaMessages:[...t.metaMessages||[],(f=t.metaMessages)!=null&&f.length?"":[],"Offchain Gateway Call:",a&&[" Gateway URL(s):",...a.map(d=>` ${e.getUrl(d)}`)],` Sender: ${u}`,` Data: ${n}`,` Callback selector: ${o}`,` Extra data: ${i}`].flat(),name:"OffchainLookupError"})}}class k extends e.BaseError{constructor({result:o,url:t}){super("Offchain gateway response is malformed. Response data must be a hex value.",{metaMessages:[`Gateway URL: ${e.getUrl(t)}`,`Response: ${e.stringify(o)}`],name:"OffchainLookupResponseMalformedError"})}}class b extends e.BaseError{constructor({sender:o,to:t}){super("Reverted sender address does not match target contract address (`to`).",{metaMessages:[`Contract address: ${t}`,`OffchainLookup sender address: ${o}`],name:"OffchainLookupSenderMismatchError"})}}const E="0x556f1830",h={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 L(c,{blockNumber:o,blockTag:t,data:n,to:i}){const{args:u}=e.decodeErrorResult({data:n,abi:[h]}),[a,f,d,r,s]=u,{ccipRead:l}=c,m=l&&typeof(l==null?void 0:l.request)=="function"?l.request:y;try{if(!e.isAddressEqual(i,a))throw new b({sender:a,to:i});const p=await m({data:d,sender:a,urls:f}),{data:g}=await e.call(c,{blockNumber:o,blockTag:t,data:e.concat([r,e.encodeAbiParameters([{type:"bytes"},{type:"bytes"}],[p,s])]),to:i});return g}catch(p){throw new w({callbackSelector:r,cause:p,data:n,extraData:s,sender:a,urls:f})}}async function y({data:c,sender:o,urls:t}){var i;let n=new Error("An unknown error occurred.");for(let u=0;u<t.length;u++){const a=t[u],f=a.includes("{data}")?"GET":"POST",d=f==="POST"?{data:c,sender:o}:void 0;try{const r=await fetch(a.replace("{sender}",o).replace("{data}",c),{body:JSON.stringify(d),method:f});let s;if((i=r.headers.get("Content-Type"))!=null&&i.startsWith("application/json")?s=(await r.json()).data:s=await r.text(),!r.ok){n=new e.HttpRequestError({body:d,details:s!=null&&s.error?e.stringify(s.error):r.statusText,headers:r.headers,status:r.status,url:a});continue}if(!e.isHex(s)){n=new k({result:s,url:a});continue}return s}catch(r){n=new e.HttpRequestError({body:d,details:r.message,url:a})}}throw n}exports.ccipRequest=y;exports.offchainLookup=L;exports.offchainLookupAbiItem=h;exports.offchainLookupSignature=E;