UNPKG

@goat-sdk/plugin-betswirl

Version:

<div align="center"> <a href="https://github.com/goat-sdk/goat">

2 lines (1 loc) 3.77 kB
import{a as c}from"./chunk-YSXGDEY5.mjs";import{FORMAT_TYPE as b,casinoChainById as l,chainNativeCurrencyToToken as f,fetchBetByHash as d,formatTxnUrl as h,getBetRequirementsFunctionData as m,getCasinoTokensFunctionData as g,getPlaceBetFunctionData as T,parseRawBetRequirements as p,rawTokenToToken as k,slugById as y}from"@betswirl/sdk-core";import{parseUnits as B}from"viem";async function R(r,n){let o=r.getChain().id,a=l[o],t;if(n&&n!==a.viemChain.nativeCurrency.symbol){let s=await A(r);if(t=s.find(i=>i.symbol===n),!t)throw new Error(`The token must be one of ${s.map(i=>i.symbol).join(", ")}`)}else t=f(a.viemChain.nativeCurrency);return t}c(R,"getBetToken");function D(r,n){let o=B(r,n.decimals);if(o<=0n)throw new Error("The bet amount must be greater than 0");return o}c(D,"getBetAmountInWei");async function C(r,n,o,a){let t=r.getChain().id;try{let s=m(o.address,a,t),{value:i}=await r.read({address:s.data.to,functionName:s.data.functionName,abi:s.data.abi,args:s.data.args});return p(i,o,a,n,t)}catch(s){throw new Error(`An error occured while getting the bet requirements: ${s}`)}}c(C,"getBetRequirements");async function x(r,n,o,a,t){let s=r.getChain().id;try{let i=new URLSearchParams({game:n.toString(),tokenAddress:o,betCount:a.toString(),chainId:s.toString()}),e=await fetch(`https://api.betswirl.com/api/vrfFees?${i}`,{});if(!e.ok)throw new Error(`An error occured while fetching the chainlink vrf cost from API: ${e.statusText}`);return BigInt(await e.json())}catch(i){throw new Error(`An error occured while getting the chainlink vrf cost: ${i}`)}}c(x,"getChainlinkVrfCost");async function N(r,n,o,a,t){let s=r.getChain().id,i=await C(r,n,t.betToken,a);if(!i.isAllowed)throw new Error("The token isn't allowed for betting");if(t.betAmount>i.maxBetAmount)throw new Error(`Bet amount should be less than ${i.maxBetAmount}`);if(t.betCount>i.maxBetCount)throw new Error(`Bet count should be less than ${i.maxBetCount}`);let e=T({betAmount:t.betAmount,game:n,gameEncodedInput:o,receiver:t.receiver,betCount:t.betCount,tokenAddress:t.betToken.address,stopGain:t.stopGain,stopLoss:t.stopLoss},s);try{let u=await x(r,n,t.betToken.address,t.betCount,0n)*120n/100n,{hash:w}=await r.sendTransaction({to:e.data.to,functionName:e.data.functionName,args:e.data.args,value:e.extraData.getValue(u),abi:e.data.abi});return w}catch(u){throw new Error(`An error occured while placing the bet: ${u}`)}}c(N,"placeBet");async function q(r,n,o){let a=r.getChain().id;try{let t=await d(n,{chainId:a,theGraphKey:o,formatType:b.PRECISE}),s=Date.now(),i=6e4;for(;(!t.bet||!t.bet.isResolved)&&!t.error;){if(Date.now()-s>=i)throw new Error("Timeout: Bet data retrieval exceeded 1 minute.");if(await new Promise(u=>setTimeout(u,1e3)),t=await d(n,{chainId:a,theGraphKey:o}),t.error)break}if(t.error)throw new Error(`[${t.error.code}] Error fetching bet: ${t.error.message}`);if(!t.bet)throw new Error(`The bet hasn't been indexed in time, please retry later: ${n}`);let e=t.bet;return{id:String(e.id),input:e.decodedInput,betTxnHash:e.betTxnHash,betTxnLink:h(e.betTxnHash,a),betAmount:e.formattedBetAmount,token:e.token.symbol,isWin:e.isWin,payoutMultiplier:e.payoutMultiplier,rolled:e.decodedRolled,payout:e.formattedPayout,rollTxnHash:e.rollTxnHash,rollTxnLink:e.rollTxnHash?h(e.rollTxnHash,a):null,linkOnBetSwirl:`https://www.betswirl.com/${y[a]}/casino/${e.game}/${e.id}`}}catch(t){throw new Error(`An error occured while getting the bet: ${t}`)}}c(q,"getBet");async function A(r){let n=r.getChain().id,o=g(n),{value:a}=await r.read({address:o.data.to,functionName:o.data.functionName,abi:o.data.abi});return a.filter(t=>t.token.allowed&&!t.token.paused).map(t=>({...k(t,n),decimals:Number(t.decimals)}))}c(A,"getCasinoTokens");export{R as a,D as b,N as c,q as d,A as e};