UNPKG

@depay/web3-payments

Version:

JavaScript library to scan crypto wallets for liquefiable assets and perform cost-effective, auto-converted payments on-chain.

1,200 lines (1,043 loc) 173 kB
import Exchanges from '@depay/web3-exchanges'; import Token from '@depay/web3-tokens'; import Blockchains from '@depay/web3-blockchains'; import { BN, struct, u64, i64, bool, u8, Connection, ACCOUNT_LAYOUT, PublicKey, ComputeBudgetProgram, SystemProgram, Buffer, TransactionInstruction, u128 } from '@depay/solana-web3.js'; import { ethers } from 'ethers'; const config = { endpoints: { routesBest: 'https://public.depay.com/routes/best' } }; const API = [{"inputs":[{"internalType":"address","name":"_PERMIT2","type":"address"},{"internalType":"address","name":"_FORWARDER","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ExchangeCallFailed","type":"error"},{"inputs":[],"name":"ExchangeCallMissing","type":"error"},{"inputs":[],"name":"ExchangeNotApproved","type":"error"},{"inputs":[],"name":"ForwardingPaymentFailed","type":"error"},{"inputs":[],"name":"InsufficientBalanceInAfterPayment","type":"error"},{"inputs":[],"name":"InsufficientBalanceOutAfterPayment","type":"error"},{"inputs":[],"name":"InsufficientProtocolAmount","type":"error"},{"inputs":[],"name":"NativeFeePaymentFailed","type":"error"},{"inputs":[],"name":"NativePaymentFailed","type":"error"},{"inputs":[],"name":"PaymentDeadlineReached","type":"error"},{"inputs":[],"name":"PaymentToZeroAddressNotAllowed","type":"error"},{"inputs":[],"name":"WrongAmountPaidIn","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"exchange","type":"address"}],"name":"Disabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"exchange","type":"address"}],"name":"Enabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"deadline","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slippageInAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slippageOutAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"tokenInAddress","type":"address"},{"indexed":false,"internalType":"address","name":"tokenOutAddress","type":"address"},{"indexed":false,"internalType":"address","name":"feeReceiverAddress","type":"address"},{"indexed":false,"internalType":"address","name":"feeReceiverAddress2","type":"address"}],"name":"Payment","type":"event"},{"inputs":[],"name":"FORWARDER","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT2","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"exchange","type":"address"},{"internalType":"bool","name":"enabled","type":"bool"}],"name":"enable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"exchanges","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"},{"components":[{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"internalType":"struct IPermit2.TokenPermissions","name":"permitted","type":"tuple"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"internalType":"struct IPermit2.PermitTransferFrom","name":"permitTransferFrom","type":"tuple"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct IDePayRouterV3.PermitTransferFromAndSignature","name":"permitTransferFromAndSignature","type":"tuple"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"},{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint160","name":"amount","type":"uint160"},{"internalType":"uint48","name":"expiration","type":"uint48"},{"internalType":"uint48","name":"nonce","type":"uint48"}],"internalType":"struct IPermit2.PermitDetails","name":"details","type":"tuple"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"sigDeadline","type":"uint256"}],"internalType":"struct IPermit2.PermitSingle","name":"permitSingle","type":"tuple"},{"internalType":"bytes","name":"signature","type":"bytes"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]; var routers$2 = { ethereum: { address: '0x365f7B56D2fB16C8Af89D7d33b420E4e013461e8', api: API }, bsc: { address: '0x5F565EDfB9C446976a9F9910631cfeDb6A87220c', api: API }, polygon: { address: '0xe04b08Dfc6CaA0F4Ec523a3Ae283Ece7efE00019', api: API }, avalanche: { address: '0x39E7C98BF4ac3E4C394dD600397f5f7Ee3779BE8', api: API }, gnosis: { address: '0x328FE8bbd30487BB7b5A8eEb909f892E9E229271', api: API }, arbitrum: { address: '0x328FE8bbd30487BB7b5A8eEb909f892E9E229271', api: API }, optimism: { address: '0x558302715e3011Be6695605c11A65526D2ba2245', api: API }, base: { address: '0x48825133EF08327535D0b24d73779E82BE6Ea4d9', api: API }, worldchain: { address: '0x886eb82a7e5E7310F66A0E83748662A17E391eb0', api: API }, }; var routers$1 = { solana: { address: 'DePayR1gQfDmViCPKctnZXNtUgqRwnEqMax8LX9ho1Zg', exchanges: { orca: 'whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc', raydiumCP: 'CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C', raydiumCL: 'CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK', }, alt: '8bYq3tcwX1NM2K2JYMjrEqAPtCXFPCjzPazFothc618e', api: { createEscrowSolAccount: { anchorDiscriminator: new BN("2482112285991870004"), layout: struct([ u64("anchorDiscriminator"), ]) }, createEscrowTokenAccount: { anchorDiscriminator: new BN("16156440424245087"), layout: struct([ u64("anchorDiscriminator"), ]) }, routeSol: { anchorDiscriminator: new BN("6497164560834983274"), layout: struct([ u64("anchorDiscriminator"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeToken: { anchorDiscriminator: new BN("13483873682232752277"), layout: struct([ u64("anchorDiscriminator"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwap: { anchorDiscriminator: new BN("9797248061404332986"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwapSolOut: { anchorDiscriminator: new BN("13662217913752830165"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwapSolIn: { anchorDiscriminator: new BN("16115018480206947614"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwap: { anchorDiscriminator: new BN("15695720599845325801"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwapSolOut: { anchorDiscriminator: new BN("15074061855608091530"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwapSolIn: { anchorDiscriminator: new BN("2678451299937372540"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwap: { anchorDiscriminator: new BN("2954182973248174268"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwapSolOut: { anchorDiscriminator: new BN("18389700643710627390"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwapSolIn: { anchorDiscriminator: new BN("564150378912976829"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClTwoHopSwap: { anchorDiscriminator: new BN("3828760301615328551"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumClTwoHopSwapSolOut: { anchorDiscriminator: new BN("11373220799455718953"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumClTwoHopSwapSolIn: { anchorDiscriminator: new BN("1635173573630140652"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumCpSwap: { anchorDiscriminator: new BN("7437765211943645137"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpSwapSolOut: { anchorDiscriminator: new BN("9045257739866411286"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpSwapSolIn: { anchorDiscriminator: new BN("432305509198797158"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwap: { anchorDiscriminator: new BN("3384279312781294015"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwapSolOut: { anchorDiscriminator: new BN("18428464202744806632"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwapSolIn: { anchorDiscriminator: new BN("16266677464406446072"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) } } }, }; var routers = {... routers$2, ...routers$1}; let _window; let getWindow = () => { if(_window) { return _window } if (typeof global == 'object') { _window = global; } else { _window = window; } return _window }; const getConfiguration = () =>{ if(getWindow()._Web3ClientConfiguration === undefined) { getWindow()._Web3ClientConfiguration = {}; } return getWindow()._Web3ClientConfiguration }; function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } const BATCH_INTERVAL$1 = 10; const CHUNK_SIZE$1 = 50; const MAX_RETRY$1 = 5; class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider { constructor(url, network, endpoints, failover) { super(url); this._network = network; this._endpoint = url; this._endpoints = endpoints; this._failover = failover; this._pendingBatch = []; } handleError(error, attempt, chunk) { if(attempt < MAX_RETRY$1 && error) { const index = this._endpoints.indexOf(this._endpoint)+1; this._failover(); this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; this.requestChunk(chunk, this._endpoint, attempt+1); } else { chunk.forEach((inflightRequest) => { inflightRequest.reject(error); }); } } detectNetwork() { return Promise.resolve(Blockchains.findByName(this._network).id) } batchRequest(batch, attempt) { return new Promise((resolve, reject) => { if (batch.length === 0) resolve([]); // Do nothing if requests is empty fetch( this._endpoint, { method: 'POST', body: JSON.stringify(batch), headers: { 'Content-Type': 'application/json' }, signal: _optionalChain$6([AbortSignal, 'optionalAccess', _ => _.timeout]) ? AbortSignal.timeout(10000) : undefined // 10-second timeout } ).then((response)=>{ if(response.ok) { response.json().then((parsedJson)=>{ if(!(parsedJson instanceof Array)) { parsedJson = [parsedJson]; } if(parsedJson.find((entry)=>{ return _optionalChain$6([entry, 'optionalAccess', _2 => _2.error]) && [-32062,-32016].includes(_optionalChain$6([entry, 'optionalAccess', _3 => _3.error, 'optionalAccess', _4 => _4.code])) })) { if(attempt < MAX_RETRY$1) { reject('Error in batch found!'); } else { resolve(parsedJson); } } else { resolve(parsedJson); } }).catch(reject); } else { reject(`${response.status} ${response.text}`); } }).catch(reject); }) } requestChunk(chunk, endpoint, attempt) { const batch = chunk.map((inflight) => inflight.request); try { return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error chunk.forEach((inflightRequest, index) => { const payload = result[index]; if (_optionalChain$6([payload, 'optionalAccess', _5 => _5.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); } else if(_optionalChain$6([payload, 'optionalAccess', _6 => _6.result])) { inflightRequest.resolve(payload.result); } else { inflightRequest.reject(); } }); }).catch((error) => this.handleError(error, attempt, chunk)) } catch (error){ this.handleError(error, attempt, chunk); } } send(method, params) { const request = { method: method, params: params, id: (this._nextId++), jsonrpc: "2.0" }; if (this._pendingBatch == null) { this._pendingBatch = []; } const inflightRequest = { request, resolve: null, reject: null }; const promise = new Promise((resolve, reject) => { inflightRequest.resolve = resolve; inflightRequest.reject = reject; }); this._pendingBatch.push(inflightRequest); if (!this._pendingBatchAggregator) { // Schedule batch for next event loop + short duration this._pendingBatchAggregator = setTimeout(() => { // Get the current batch and clear it, so new requests // go into the next batch const batch = this._pendingBatch; this._pendingBatch = []; this._pendingBatchAggregator = null; // Prepare Chunks of CHUNK_SIZE const chunks = []; for (let i = 0; i < Math.ceil(batch.length / CHUNK_SIZE$1); i++) { chunks[i] = batch.slice(i*CHUNK_SIZE$1, (i+1)*CHUNK_SIZE$1); } chunks.forEach((chunk)=>{ // Get the request as an array of requests chunk.map((inflight) => inflight.request); return this.requestChunk(chunk, this._endpoint, 1) }); }, getConfiguration().batchInterval || BATCH_INTERVAL$1); } return promise } } function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } const getAllProviders$1 = ()=> { if(getWindow()._Web3ClientProviders == undefined) { getWindow()._Web3ClientProviders = {}; } return getWindow()._Web3ClientProviders }; const setProvider$2 = (blockchain, provider)=> { if(provider == undefined) { return } if(getAllProviders$1()[blockchain] === undefined) { getAllProviders$1()[blockchain] = []; } const index = getAllProviders$1()[blockchain].indexOf(provider); if(index > -1) { getAllProviders$1()[blockchain].splice(index, 1); } getAllProviders$1()[blockchain].unshift(provider); }; const setProviderEndpoints$2 = async (blockchain, endpoints, detectFastest = true)=> { getAllProviders$1()[blockchain] = endpoints.map((endpoint, index)=> new StaticJsonRpcBatchProvider(endpoint, blockchain, endpoints, ()=>{ if(getAllProviders$1()[blockchain].length === 1) { setProviderEndpoints$2(blockchain, endpoints, detectFastest); } else { getAllProviders$1()[blockchain].splice(index, 1); } }) ); let provider; let window = getWindow(); if( window.fetch == undefined || (typeof process != 'undefined' && process['env'] && process['env']['NODE_ENV'] == 'test') || (typeof window.cy != 'undefined') || detectFastest === false ) { provider = getAllProviders$1()[blockchain][0]; } else { let responseTimes = await Promise.all(endpoints.map((endpoint)=>{ return new Promise(async (resolve)=>{ let timeout = 900; let before = new Date().getTime(); setTimeout(()=>resolve(timeout), timeout); let response; try { response = await fetch(endpoint, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, referrer: "", referrerPolicy: "no-referrer", body: JSON.stringify({ method: 'net_version', id: 1, jsonrpc: '2.0' }), signal: _optionalChain$5([AbortSignal, 'optionalAccess', _ => _.timeout]) ? AbortSignal.timeout(10000) : undefined // 10-second timeout }); } catch (e) {} if(!_optionalChain$5([response, 'optionalAccess', _2 => _2.ok])) { return resolve(999) } let after = new Date().getTime(); resolve(after-before); }) })); const fastestResponse = Math.min(...responseTimes); const fastestIndex = responseTimes.indexOf(fastestResponse); provider = getAllProviders$1()[blockchain][fastestIndex]; } setProvider$2(blockchain, provider); }; const getProvider$2 = async (blockchain)=> { let providers = getAllProviders$1(); if(providers && providers[blockchain]){ return providers[blockchain][0] } let window = getWindow(); if(window._Web3ClientGetProviderPromise && window._Web3ClientGetProviderPromise[blockchain]) { return await window._Web3ClientGetProviderPromise[blockchain] } if(!window._Web3ClientGetProviderPromise){ window._Web3ClientGetProviderPromise = {}; } window._Web3ClientGetProviderPromise[blockchain] = new Promise(async(resolve)=> { await setProviderEndpoints$2(blockchain, Blockchains[blockchain].endpoints); resolve(getWindow()._Web3ClientProviders[blockchain][0]); }); return await window._Web3ClientGetProviderPromise[blockchain] }; const getProviders$2 = async(blockchain)=>{ let providers = getAllProviders$1(); if(providers && providers[blockchain]){ return providers[blockchain] } let window = getWindow(); if(window._Web3ClientGetProvidersPromise && window._Web3ClientGetProvidersPromise[blockchain]) { return await window._Web3ClientGetProvidersPromise[blockchain] } if(!window._Web3ClientGetProvidersPromise){ window._Web3ClientGetProvidersPromise = {}; } window._Web3ClientGetProvidersPromise[blockchain] = new Promise(async(resolve)=> { await setProviderEndpoints$2(blockchain, Blockchains[blockchain].endpoints); resolve(getWindow()._Web3ClientProviders[blockchain]); }); return await window._Web3ClientGetProvidersPromise[blockchain] }; var EVM = { getProvider: getProvider$2, getProviders: getProviders$2, setProviderEndpoints: setProviderEndpoints$2, setProvider: setProvider$2, }; function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } const BATCH_INTERVAL = 10; const CHUNK_SIZE = 25; const MAX_RETRY = 10; class StaticJsonRpcSequentialProvider extends Connection { constructor(url, network, endpoints, failover) { super(url); this._provider = new Connection(url); this._network = network; this._endpoint = url; this._endpoints = endpoints; this._failover = failover; this._pendingBatch = []; this._rpcRequest = this._rpcRequestReplacement.bind(this); } handleError(error, attempt, chunk) { if(attempt < MAX_RETRY) { const index = this._endpoints.indexOf(this._endpoint)+1; this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; this._provider = new Connection(this._endpoint); this.requestChunk(chunk, attempt+1); } else { chunk.forEach((inflightRequest) => { inflightRequest.reject(error); }); } } batchRequest(requests, attempt) { return new Promise((resolve, reject) => { if (requests.length === 0) resolve([]); // Do nothing if requests is empty const batch = requests.map(params => { return this._rpcClient.request(params.methodName, params.args) }); fetch( this._endpoint, { method: 'POST', body: JSON.stringify(batch), headers: { 'Content-Type': 'application/json' }, signal: _optionalChain$4([AbortSignal, 'optionalAccess', _ => _.timeout]) ? AbortSignal.timeout(60000) : undefined // 60-second timeout } ).then((response)=>{ if(response.ok) { response.json().then((parsedJson)=>{ if(!(parsedJson instanceof Array)) { parsedJson = [parsedJson]; } if(parsedJson.find((entry)=>_optionalChain$4([entry, 'optionalAccess', _2 => _2.error]))) { if(attempt < MAX_RETRY) { reject('Error in batch found!'); } else { resolve(parsedJson); } } else { resolve(parsedJson); } }).catch(reject); } else { reject(`${response.status} ${response.text}`); } }).catch(reject); }) } requestChunk(chunk, attempt) { const batch = chunk.map((inflight) => inflight.request); try { return this.batchRequest(batch, attempt) .then((result) => { chunk.forEach((inflightRequest, index) => { const payload = result[index]; if (_optionalChain$4([payload, 'optionalAccess', _3 => _3.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); } else if(payload) { inflightRequest.resolve(payload); } else { inflightRequest.reject(); } }); }).catch((error)=>this.handleError(error, attempt, chunk)) } catch (error){ return this.handleError(error, attempt, chunk) } } _rpcRequestReplacement(methodName, args) { const request = { methodName, args }; if (this._pendingBatch == null) { this._pendingBatch = []; } const inflightRequest = { request, resolve: null, reject: null }; const promise = new Promise((resolve, reject) => { inflightRequest.resolve = resolve; inflightRequest.reject = reject; }); this._pendingBatch.push(inflightRequest); if (!this._pendingBatchAggregator) { // Schedule batch for next event loop + short duration this._pendingBatchAggregator = setTimeout(() => { // Get the current batch and clear it, so new requests // go into the next batch const batch = this._pendingBatch; this._pendingBatch = []; this._pendingBatchAggregator = null; // Prepare Chunks of CHUNK_SIZE const chunks = []; for (let i = 0; i < Math.ceil(batch.length / CHUNK_SIZE); i++) { chunks[i] = batch.slice(i*CHUNK_SIZE, (i+1)*CHUNK_SIZE); } chunks.forEach((chunk)=>{ // Get the request as an array of requests chunk.map((inflight) => inflight.request); return this.requestChunk(chunk, 1) }); }, getConfiguration().batchInterval || BATCH_INTERVAL); } return promise } } function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } const getAllProviders = ()=> { if(getWindow()._Web3ClientProviders == undefined) { getWindow()._Web3ClientProviders = {}; } return getWindow()._Web3ClientProviders }; const setProvider$1 = (blockchain, provider)=> { if(provider == undefined) { return } if(getAllProviders()[blockchain] === undefined) { getAllProviders()[blockchain] = []; } const index = getAllProviders()[blockchain].indexOf(provider); if(index > -1) { getAllProviders()[blockchain].splice(index, 1); } getAllProviders()[blockchain].unshift(provider); }; const setProviderEndpoints$1 = async (blockchain, endpoints, detectFastest = true)=> { getAllProviders()[blockchain] = endpoints.map((endpoint, index)=> new StaticJsonRpcSequentialProvider(endpoint, blockchain, endpoints, ()=>{ if(getAllProviders()[blockchain].length === 1) { setProviderEndpoints$1(blockchain, endpoints, detectFastest); } else { getAllProviders()[blockchain].splice(index, 1); } }) ); let provider; let window = getWindow(); if( window.fetch == undefined || (typeof process != 'undefined' && process['env'] && process['env']['NODE_ENV'] == 'test') || (typeof window.cy != 'undefined') || detectFastest === false ) { provider = getAllProviders()[blockchain][0]; } else { let responseTimes = await Promise.all(endpoints.map((endpoint)=>{ return new Promise(async (resolve)=>{ let timeout = 900; let before = new Date().getTime(); setTimeout(()=>resolve(timeout), timeout); let response; try { response = await fetch(endpoint, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, referrer: "", referrerPolicy: "no-referrer", body: JSON.stringify({ method: 'getIdentity', id: 1, jsonrpc: '2.0' }), signal: _optionalChain$3([AbortSignal, 'optionalAccess', _ => _.timeout]) ? AbortSignal.timeout(60000) : undefined // 60-second timeout }); } catch (e) {} if(!_optionalChain$3([response, 'optionalAccess', _2 => _2.ok])) { return resolve(999) } let after = new Date().getTime(); resolve(after-before); }) })); const fastestResponse = Math.min(...responseTimes); const fastestIndex = responseTimes.indexOf(fastestResponse); provider = getAllProviders()[blockchain][fastestIndex]; } setProvider$1(blockchain, provider); }; const getProvider$1 = async (blockchain)=> { let providers = getAllProviders(); if(providers && providers[blockchain]){ return providers[blockchain][0] } let window = getWindow(); if(window._Web3ClientGetProviderPromise && window._Web3ClientGetProviderPromise[blockchain]) { return await window._Web3ClientGetProviderPromise[blockchain] } if(!window._Web3ClientGetProviderPromise){ window._Web3ClientGetProviderPromise = {}; } window._Web3ClientGetProviderPromise[blockchain] = new Promise(async(resolve)=> { await setProviderEndpoints$1(blockchain, Blockchains[blockchain].endpoints); resolve(getWindow()._Web3ClientProviders[blockchain][0]); }); return await window._Web3ClientGetProviderPromise[blockchain] }; const getProviders$1 = async(blockchain)=>{ let providers = getAllProviders(); if(providers && providers[blockchain]){ return providers[blockchain] } let window = getWindow(); if(window._Web3ClientGetProvidersPromise && window._Web3ClientGetProvidersPromise[blockchain]) { return await window._Web3ClientGetProvidersPromise[blockchain] } if(!window._Web3ClientGetProvidersPromise){ window._Web3ClientGetProvidersPromise = {}; } window._Web3ClientGetProvidersPromise[blockchain] = new Promise(async(resolve)=> { await setProviderEndpoints$1(blockchain, Blockchains[blockchain].endpoints); resolve(getWindow()._Web3ClientProviders[blockchain]); }); return await window._Web3ClientGetProvidersPromise[blockchain] }; var Solana = { getProvider: getProvider$1, getProviders: getProviders$1, setProviderEndpoints: setProviderEndpoints$1, setProvider: setProvider$1, }; let supported$1 = ['ethereum', 'bsc', 'polygon', 'solana', 'fantom', 'arbitrum', 'avalanche', 'gnosis', 'optimism', 'base', 'worldchain']; supported$1.evm = ['ethereum', 'bsc', 'polygon', 'fantom', 'arbitrum', 'avalanche', 'gnosis', 'optimism', 'base', 'worldchain']; supported$1.svm = ['solana']; function _optionalChain$2$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } let getCacheStore = () => { if (getWindow()._Web3ClientCacheStore == undefined) { getWindow()._Web3ClientCacheStore = {}; } return getWindow()._Web3ClientCacheStore }; let getPromiseStore = () => { if (getWindow()._Web3ClientPromiseStore == undefined) { getWindow()._Web3ClientPromiseStore = {}; } return getWindow()._Web3ClientPromiseStore }; let set = function ({ key, value, expires }) { getCacheStore()[key] = { expiresAt: Date.now() + expires, value, }; }; let get = function ({ key, expires }) { let cachedEntry = getCacheStore()[key]; if (_optionalChain$2$1([cachedEntry, 'optionalAccess', _ => _.expiresAt]) > Date.now()) { return cachedEntry.value } }; let getPromise = function({ key }) { return getPromiseStore()[key] }; let setPromise = function({ key, promise }) { getPromiseStore()[key] = promise; return promise }; let deletePromise = function({ key }) { getPromiseStore()[key] = undefined; }; let cache = function ({ call, key, expires = 0 }) { return new Promise((resolve, reject)=>{ let value; key = JSON.stringify(key); // get existing promise (of a previous pending request asking for the exact same thing) let existingPromise = getPromise({ key }); if(existingPromise) { return existingPromise .then(resolve) .catch(reject) } setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ if (expires === 0) { return call() .then((value)=>{ resolve(value); resolveQueue(value); }) .catch((error)=>{ reject(error); rejectQueue(error); }) } // get cached value value = get({ key, expires }); if (value) { resolve(value); resolveQueue(value); return value } // set new cache value call() .then((value)=>{ if (value) { set({ key, value, expires }); } resolve(value); resolveQueue(value); }) .catch((error)=>{ reject(error); rejectQueue(error); }); }) }).then(()=>{ deletePromise({ key }); }).catch(()=>{ deletePromise({ key }); }); }) }; // Periodically clean up expired cache entries (every 5 minutes), to prevent memory leaks if (typeof process == 'undefined' || process.env && "production" === 'test') { setInterval(() => { const store = getCacheStore(); const now = Date.now(); for (const key in store) { if (store[key].expiresAt < now) { delete store[key]; } } }, 10 * 60 * 1000); // 10 minutes in milliseconds } const getProvider = async (blockchain)=>{ if(supported$1.evm.includes(blockchain)) { return await EVM.getProvider(blockchain) } else if(supported$1.svm.includes(blockchain)) { return await Solana.getProvider(blockchain) } else { throw 'Unknown blockchain: ' + blockchain } }; let paramsToContractArgs = ({ contract, method, params }) => { let fragment = contract.interface.fragments.find((fragment) => { return fragment.name == method }); return fragment.inputs.map((input, index) => { if (Array.isArray(params)) { return params[index] } else { return params[input.name] } }) }; const contractCall = ({ address, api, method, params, provider, block }) => { const contract = new ethers.Contract(address, api, provider); const args = paramsToContractArgs({ contract, method, params }); const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method); if(contract[method] === undefined) { method = `${method}(${fragment.inputs.map((input)=>input.type).join(',')})`; } if(fragment && fragment.stateMutability === 'nonpayable') { return contract.callStatic[method](...args, { blockTag: block }) } else { return contract[method](...args, { blockTag: block }) } }; const balance$1 = ({ address, provider, block }) => { return provider.getBalance(address, block) }; const transactionCount = ({ address, provider }) => { return provider.getTransactionCount(address) }; const singleRequest$3 = ({ blockchain, address, api, method, params, block, provider }) =>{ if (api) { return contractCall({ address, api, method, params, provider, block }) } else if (method === 'latestBlockNumber') { return provider.getBlockNumber() } else if (method === 'balance') { return balance$1({ address, provider, block }) } else if (method === 'transactionCount') { return transactionCount({ address, provider }) } }; var requestEVM = async ({ blockchain, address, api, method, params, block, timeout, strategy }) => { strategy = strategy ? strategy : (getConfiguration().strategy || 'failover'); timeout = timeout ? timeout : (getConfiguration().timeout || undefined); if(strategy === 'fastest') { const providers = await EVM.getProviders(blockchain); let allRequestsFailed = []; const allRequestsInParallel = providers.map((provider)=>{ return new Promise((resolve)=>{ allRequestsFailed.push( singleRequest$3({ blockchain, address, api, method, params, block, provider }).then(resolve) ); }) }); const timeoutPromise = new Promise((_, reject)=>setTimeout(()=>{ reject(new Error("Web3ClientTimeout")); }, timeout || 10000)); allRequestsFailed = Promise.all(allRequestsFailed.map((request)=>{ return new Promise((resolve)=>{ request.catch(resolve); }) })).then(()=>{ return }); return Promise.race([...allRequestsInParallel, timeoutPromise, allRequestsFailed]) } else { // failover const provider = await EVM.getProvider(blockchain); const request = singleRequest$3({ blockchain, address, api, method, params, block, provider }); if(timeout) { timeout = new Promise((_, reject)=>setTimeout(()=>{ reject(new Error("Web3ClientTimeout")); }, timeout)); return Promise.race([request, timeout]) } else { return request } } }; const accountInfo = async ({ address, api, method, params, provider, block }) => { const info = await provider.getAccountInfo(new PublicKey(address)); if(!info || !info.data) { return } return api.decode(info.data) }; const balance = ({ address, provider }) => { return provider.getBalance(new PublicKey(address)) }; const singleRequest$2 = async({ blockchain, address, api, method, params, block, provider, providers })=> { try { if(method == undefined || method === 'getAccountInfo') { if(api == undefined) { api = ACCOUNT_LAYOUT; } return await accountInfo({ address, api, method, params, provider, block }) } else if(method === 'getProgramAccounts') { return await provider.getProgramAccounts(new PublicKey(address), params).then((accounts)=>{ if(api){ return accounts.map((account)=>{ account.data = api.decode(account.account.data); return account }) } else { return accounts } }) } else if(method === 'getTokenAccountBalance') { return await provider.getTokenAccountBalance(new PublicKey(address)) } else if (method === 'latestBlockNumber') { return await provider.getSlot(params ? params : undefined) } else if (method === 'balance') { return await balance({ address, provider }) } } catch (error){ if(providers && error && [ 'Failed to fetch', 'limit reached', '504', '503', '502', '500', '429', '426', '422', '413', '409', '408', '406', '405', '404', '403', '402', '401', '400' ].some((errorType)=>error.toString().match(errorType))) { let nextProvider = providers[providers.indexOf(provider)+1] || providers[0]; return singleRequest$2({ blockchain, address, api, method, params, block, provider: nextProvider, providers }) } else { throw error } } }; var requestSolana = async ({ blockchain, address, api, method, params, block, timeout, strategy }) => { strategy = strategy ? strategy : (getConfiguration().strategy || 'failover'); timeout = timeout ? timeout : (getConfiguration().timeout || undefined); const providers = await Solana.getProviders(blockchain); if(strategy === 'fastest') { let allRequestsFailed = []; const allRequestsInParallel = providers.map((provider)=>{ return new Promise((resolve)=>{ allRequestsFailed.push( singleRequest$2({ blockchain, address, api, method, params, block, provider }).then(resolve) ); }) }); const timeoutPromise = new Promise((_, reject)=>setTimeout(()=>{ reject(new Error("Web3ClientTimeout")); }, timeout || 60000)); // 60s default timeout allRequestsFailed = Promise.all(allRequestsFailed.map((request)=>{ return new Promise((resolve)=>{ request.catch(resolve); }) })).then(()=>{ return }); return Promise.race([...allRequestsInParallel, timeoutPromise, allRequestsFailed]) } else { // failover const provider = await Solana.getProvider(blockchain); const request = singleRequest$2({ blockchain, address, api, method, params, block, provider, providers }); if(timeout) { timeout = new Promise((_, reject)=>setTimeout(()=>{ reject(new Error("Web3ClientTimeout")); }, timeout)); return Promise.race([request, timeout]) } else { return request } } }; var parseUrl = (url) => { if (typeof url == 'object') { return url }