@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 2.23 kB
JavaScript
;var e=require("react"),t=require("./internal-context-BJv4f_bO.js");let r="0x0000000000000000000000000000000000000000";let s="https://api.relay.link",a="https://api.testnets.relay.link";async function n({transactionHash:e,isTestnet:t}){let r=await fetch((t?a:s)+"/requests/v2?hash="+e),n=await r.json();if(!r.ok){if("message"in n&&"string"==typeof n.message)throw Error(n.message);throw Error("Error fetching request from relay")}return n.requests.at(0)?.status??"pending"}class i extends t.PrivyClientError{constructor(e,r){super("We were unable to complete the bridging transaction. Funds will be refunded on your wallet.",void 0,t.PrivyErrorCode.TRANSACTION_FAILURE),this.relayLink=r?`https://testnets.relay.link/transaction/${e}`:`https://relay.link/transaction/${e}`}}exports.RELAY_SOLANA_MAINNET_CHAIN_ID=792703809,exports.RELAY_SOLANA_NATIVE_CURRENCY_ID="11111111111111111111111111111111",exports.RELAY_SOLANA_USDC_TOKEN_ID="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",exports.RelayRequestError=i,exports.getQuote=async({input:e,isTestnet:t})=>{let r=await fetch((t?a:s)+"/quote",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),n=await r.json();if(!(r.ok||"string"==typeof n.message&&n.message.startsWith("Invalid address")))throw console.error("Relay error:",n),Error(n.message??"Error fetching quote from relay");return n},exports.toEvmTransactionRequestInfoFromQuote=e=>{let t=e.steps[0]?.items?.[0];if(t)return{from:t.data.from,to:t.data.to,value:Number(t.data.value),chainId:Number(t.data.chainId),data:t.data.data}},exports.toGetQuoteInput=({appId:e,originCurrency:t,destinationCurrency:s,...a})=>({tradeType:"EXPECTED_OUTPUT",originCurrency:t??r,destinationCurrency:s??r,referrer:`privy|${e}`,...a}),exports.useBridgingStatusPolling=function({transactionHash:t,isTestnet:r,bridgingStatus:s,setBridgingStatus:a,onSuccess:o,onFailure:u}){e.useEffect((()=>{if(t&&s){if(["delayed","waiting","pending"].includes(s)){let e=setInterval((async()=>{try{let e=await n({transactionHash:t,isTestnet:r});a(e)}catch(e){console.error(e)}}),1e3);return()=>clearInterval(e)}"success"===s?o({transactionHash:t}):["refund","failure"].includes(s)&&u({error:new i(t,r)})}}),[s,t,r])};