UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 2.03 kB
import{useEffect as t}from"react";import{a as e,b as a}from"./internal-context-Z-fyxadS.mjs";const r=792703809,s="11111111111111111111111111111111",n="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";let i="0x0000000000000000000000000000000000000000";const o=({appId:t,originCurrency:e,destinationCurrency:a,...r})=>({tradeType:"EXPECTED_OUTPUT",originCurrency:e??i,destinationCurrency:a??i,referrer:`privy|${t}`,...r});let c="https://api.relay.link",l="https://api.testnets.relay.link";const u=async({input:t,isTestnet:e})=>{let a=await fetch((e?l:c)+"/quote",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}),r=await a.json();if(!(a.ok||"string"==typeof r.message&&r.message.startsWith("Invalid address")))throw console.error("Relay error:",r),Error(r.message??"Error fetching quote from relay");return r},d=t=>{let e=t.steps[0]?.items?.[0];if(e)return{from:e.data.from,to:e.data.to,value:Number(e.data.value),chainId:Number(e.data.chainId),data:e.data.data}};async function y({transactionHash:t,isTestnet:e}){let a=await fetch((e?l:c)+"/requests/v2?hash="+t),r=await a.json();if(!a.ok){if("message"in r&&"string"==typeof r.message)throw Error(r.message);throw Error("Error fetching request from relay")}return r.requests.at(0)?.status??"pending"}function f({transactionHash:e,isTestnet:a,bridgingStatus:r,setBridgingStatus:s,onSuccess:n,onFailure:i}){t((()=>{if(e&&r){if(["delayed","waiting","pending"].includes(r)){let t=setInterval((async()=>{try{let t=await y({transactionHash:e,isTestnet:a});s(t)}catch(t){console.error(t)}}),1e3);return()=>clearInterval(t)}"success"===r?n({transactionHash:e}):["refund","failure"].includes(r)&&i({error:new h(e,a)})}}),[r,e,a])}class h extends e{constructor(t,e){super("We were unable to complete the bridging transaction. Funds will be refunded on your wallet.",void 0,a.TRANSACTION_FAILURE),this.relayLink=e?`https://testnets.relay.link/transaction/${t}`:`https://relay.link/transaction/${t}`}}export{h as R,s as a,r as b,d as c,n as d,u as g,o as t,f as u};