@oceanprotocol/lib 1.0.0-next.8 → 1.0.0-next.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- import t from"web3";import e from"bignumber.js";import a from"cross-fetch";import s from"@oceanprotocol/contracts/addresses/address.json";import i from"crypto-js/sha256";import o from"decimal.js";import n from"@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json";import r from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json";import c from"@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json";import h from"@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json";import l from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json";import d from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json";import w from"@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json";var m;!function(t){t[t.None=-1]="None",t[t.Error=0]="Error",t[t.Warn=1]="Warn",t[t.Log=2]="Log",t[t.Verbose=3]="Verbose"}(m||(m={}));class u{constructor(t=m.Error){this.logLevel=void 0,this.logLevel=t}setLevel(t){this.logLevel=t}bypass(...t){this.dispatch("log",-Infinity,...t)}debug(...t){this.dispatch("debug",m.Verbose,...t)}log(...t){this.dispatch("log",m.Log,...t)}warn(...t){this.dispatch("warn",m.Warn,...t)}error(...t){this.dispatch("error",m.Error,...t)}dispatch(t,e,...a){this.logLevel>=e&&console[t](...a)}}const g=new u;var p={nouns:["Crab","Fish","Seal","Octopus","Shark","Seahorse","Walrus","Starfish","Whale","Orca","Penguin","Jellyfish","Squid","Lobster","Pelican","Shrimp","Oyster","Clam","Seagull","Dolphin","Shell","Cormorant","Otter","Anemone","Turtle","Coral","Ray","Barracuda","Krill","Anchovy","Angelfish","Barnacle","Clownfish","Cod","Cuttlefish","Eel","Fugu","Herring","Haddock","Ling","Mackerel","Manatee","Narwhal","Nautilus","Plankton","Porpoise","Prawn","Pufferfish","Swordfish","Tuna"],adjectives:["adamant","adroit","amatory","ambitious","amused","animistic","antic","arcadian","artistic","astonishing","astounding","baleful","bellicose","bilious","blissful","boorish","brave","breathtaking","brilliant","calamitous","caustic","cerulean","clever","charming","comely","competent","concomitant","confident","contumacious","corpulent","crapulous","creative","dazzling","dedicated","defamatory","delighted","delightful","determined","didactic","dilatory","dowdy","efficacious","effulgent","egregious","empowered","endemic","enthusiastic","equanimous","exceptional","execrable","fabulous","fantastic","fastidious","feckless","fecund","friable","fulsome","garrulous","generous","gentle","guileless","gustatory","heuristic","histrionic","hubristic","incendiary","incredible","insidious","insolent","inspired","intransigent","inveterate","invidious","invigorated","irksome","jejune","juicy","jocular","joyful","judicious","kind","lachrymose","limpid","loquacious","lovely","luminous","mannered","marvelous","mendacious","meretricious","minatory","mordant","motivated","munificent","nefarious","noxious","obtuse","optimistic","parsimonious","pendulous","pernicious","pervasive","petulant","passionate","phenomenal","platitudinous","pleasant","powerful","precipitate","propitious","puckish","querulous","quiescent","rebarbative","recalcitant","redolent","rhadamanthine","risible","ruminative","sagacious","salubrious","sartorial","sclerotic","serpentine","smart","spasmodic","strident","stunning","stupendous","taciturn","tactful","tasty","tenacious","tremendous","tremulous","trenchant","turbulent","turgid","ubiquitous","uxorious","verdant","vibrant","voluble","voracious","wheedling","withering","wonderful","zealous"]};function f(t){const e=t||p,a=Math.floor(Math.random()*e.adjectives.length),s=Math.floor(Math.random()*e.nouns.length),i=Math.floor(100*Math.random()),o=e.adjectives[a].replace(/^\w/,t=>t.toUpperCase()),n=e.nouns[s].replace(/^\w/,t=>t.toUpperCase());return{name:`${o} ${n} Token`,symbol:`${(o.substring(0,3)+n.substring(0,3)).toUpperCase()}-${i}`}}const y=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];function b(t,e){return e&&(e.transactionBlockTimeout&&(t.transactionBlockTimeout=e.transactionBlockTimeout),e.transactionConfirmationBlocks&&(t.transactionConfirmationBlocks=e.transactionConfirmationBlocks),e.transactionPollingTimeout&&(t.transactionPollingTimeout=e.transactionPollingTimeout)),t}async function A(t,a){const s=new e(await t.eth.getGasPrice());return a&&a.gasFeeMultiplier?s.multipliedBy(a.gasFeeMultiplier).integerValue(e.ROUND_DOWN).toString(10):s.toString(10)}function T(e){let a,s;return e.name&&e.symbol||({name:a,symbol:s}=f()),{templateIndex:e.templateIndex,strings:[e.name||a,e.symbol||s],addresses:[e.minter,e.feeManager,e.mpFeeAddress,e.feeToken],uints:[t.utils.toWei(e.cap),t.utils.toWei(e.feeAmount)],bytess:[]}}function E(t){return t.allowedConsumer||(t.allowedConsumer="0x0000000000000000000000000000000000000000"),{fixedPriceAddress:t.fixedRateAddress,addresses:[t.baseTokenAddress,t.owner,t.marketFeeCollector,t.allowedConsumer],uints:[t.baseTokenDecimals,t.datatokenDecimals,t.fixedRate,t.marketFee,t.withMint?1:0]}}function C(e){return{addresses:[e.ssContract,e.baseTokenAddress,e.baseTokenSender,e.publisherAddress,e.marketFeeCollector,e.poolTemplateAddress],ssParams:[t.utils.toWei(e.rate),e.baseTokenDecimals,t.utils.toWei(e.vestingAmount),e.vestedBlocks,t.utils.toWei(e.initialBaseTokenLiquidity)],swapFees:[e.swapFeeLiquidityProvider,e.swapFeeMarketRunner]}}async function F(t,a,s){try{const i=new t.eth.Contract(y,a);let o=await i.methods.decimals().call();"0"===o&&(o=18);const n=new e(parseInt(s)/10**o);return e.config({EXPONENTIAL_AT:50}),n.toString()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}}async function R(t,a,s){try{const i=new t.eth.Contract(y,a);let o=await i.methods.decimals().call();"0"===o&&(o=18);const n=new e(parseInt(s)*10**o);return e.config({EXPONENTIAL_AT:50}),n.toString()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}}async function k(t,e){const s=await a(t,e);if(!s.ok)throw g.error(`Error requesting [${e.method}] ${t}`),g.error(`Response message: \n${await s.text()}`),s;return s}async function I(t){const e=document.createElement("a");e.download="",e.href=t,e.click()}async function v(t,e){const s=await a(t);if(!s.ok)throw new Error("Response error.");let i;try{i=s.headers.get("content-disposition").match(/attachment;filename=(.+)/)[1]}catch(a){try{i=t.split("/").pop()}catch(t){i=`file${e}`}}return{data:await s.arrayBuffer(),filename:i}}async function S(t){return a(t,{method:"GET",headers:{"Content-type":"application/json"}})}async function G(t,e){return async function(t,e,s){return a(t,null!=e?{method:"POST",body:e,headers:{"Content-type":"application/json"}}:{method:"POST"})}(t,e)}function x(){return x=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var s in a)Object.prototype.hasOwnProperty.call(a,s)&&(t[s]=a[s])}return t},x.apply(this,arguments)}const M={chainId:null,network:"unknown",metadataCacheUri:"https://aquarius.oceanprotocol.com",nodeUri:"http://localhost:8545",providerUri:"http://127.0.0.1:8030",subgraphUri:null,explorerUri:null,oceanTokenAddress:null,oceanTokenSymbol:"OCEAN",poolTemplateAddress:null,fixedRateExchangeAddress:null,dispenserAddress:null,startBlock:0,transactionBlockTimeout:50,transactionConfirmationBlocks:1,transactionPollingTimeout:750,gasFeeMultiplier:1},O=[x({},M),x({},M,{chainId:8996,network:"development",metadataCacheUri:"http://127.0.0.1:5000"}),x({},M,{chainId:3,network:"ropsten",nodeUri:"https://ropsten.infura.io/v3",providerUri:"https://provider.ropsten.oceanprotocol.com",subgraphUri:"https://subgraph.ropsten.oceanprotocol.com",explorerUri:"https://ropsten.etherscan.io",startBlock:9227563}),x({},M,{chainId:4,network:"rinkeby",nodeUri:"https://rinkeby.infura.io/v3",providerUri:"https://providerv4.rinkeby.oceanprotocol.com",subgraphUri:"https://subgraphv4.rinkeby.oceanprotocol.com",explorerUri:"https://rinkeby.etherscan.io",startBlock:7294090}),x({},M,{chainId:1,network:"mainnet",nodeUri:"https://mainnet.infura.io/v3",providerUri:"https://provider.mainnet.oceanprotocol.com",subgraphUri:"https://subgraph.mainnet.oceanprotocol.com",explorerUri:"https://etherscan.io",startBlock:11105459,transactionBlockTimeout:150,transactionConfirmationBlocks:5,transactionPollingTimeout:1750,gasFeeMultiplier:1.05}),x({},M,{chainId:137,network:"polygon",nodeUri:"https://polygon-mainnet.infura.io/v3",providerUri:"https://provider.polygon.oceanprotocol.com",subgraphUri:"https://subgraph.polygon.oceanprotocol.com",explorerUri:"https://polygonscan.com",oceanTokenSymbol:"mOCEAN",startBlock:11005222}),x({},M,{chainId:1287,network:"moonbeamalpha",nodeUri:"https://rpc.testnet.moonbeam.network",providerUri:"https://provider.moonbeamalpha.oceanprotocol.com",subgraphUri:"https://subgraph.moonbeamalpha.oceanprotocol.com",explorerUri:"https://moonbase-blockscout.testnet.moonbeam.network/",startBlock:90707}),x({},M,{chainId:2021e3,network:"gaiaxtestnet",nodeUri:"https://rpc.gaiaxtestnet.oceanprotocol.com",providerUri:"https://provider.gaiaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.gaiaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.gaiaxtestnet.oceanprotocol.com"}),x({},M,{chainId:2021001,network:"catenaxtestnet",nodeUri:"https://rpc.catenaxtestnet.oceanprotocol.com",providerUri:"https://provider.catenaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.catenaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.catenaxtestnet.oceanprotocol.com",metadataCacheUri:"https://aquarius.catenaxtestnet.oceanprotocol.com"}),x({},M,{chainId:80001,network:"mumbai",nodeUri:"https://polygon-mumbai.infura.io/v3",providerUri:"https://provider.mumbai.oceanprotocol.com",subgraphUri:"https://subgraph.mumbai.oceanprotocol.com",explorerUri:"https://mumbai.polygonscan.com"}),x({},M,{chainId:56,network:"bsc",nodeUri:"https://bsc-dataseed.binance.org",providerUri:"https://provider.bsc.oceanprotocol.com",subgraphUri:"https://subgraph.bsc.oceanprotocol.com",explorerUri:"https://bscscan.com/",gasFeeMultiplier:1.05}),x({},M,{chainId:44787,network:"celoalfajores",nodeUri:"https://alfajores-forno.celo-testnet.org",providerUri:"https://provider.celoalfajores.oceanprotocol.com",subgraphUri:"https://subgraph.celoalfajores.oceanprotocol.com",explorerUri:"https://alfajores-blockscout.celo-testnet.org"}),x({},M,{chainId:246,network:"energyweb",nodeUri:"https://rpc.energyweb.org",providerUri:"https://provider.energyweb.oceanprotocol.com",subgraphUri:"https://subgraph.energyweb.oceanprotocol.com",explorerUri:"https://explorer.energyweb.org",gasFeeMultiplier:1.05}),x({},M,{chainId:1285,network:"moonriver",nodeUri:"https://moonriver.api.onfinality.io/public",providerUri:"https://provider.moonriver.oceanprotocol.com",subgraphUri:"https://subgraph.moonriver.oceanprotocol.com",explorerUri:"https://blockscout.moonriver.moonbeam.network",gasFeeMultiplier:1.05})];class U{getAddressesFromEnv(t,e){let a;if(e)try{const{FixedPrice:s,Dispenser:i,Staking:o,poolTemplate:n,ERC721Factory:r,OPFCommunityFeeCollector:c,Ocean:h,chainId:l,startBlock:d}=e[t];a=x({erc721FactoryAddress:r,sideStakingAddress:o,opfCommunityFeeCollector:c,poolTemplateAddress:n,fixedRateExchangeAddress:s,dispenserAddress:i,oceanTokenAddress:h,chainId:l,startBlock:d},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}catch(t){}else if(s[t]){const{FixedPrice:e,Dispenser:i,Staking:o,poolTemplate:n,OPFCommunityFeeCollector:r,ERC721Factory:c,Ocean:h,chainId:l,startBlock:d}=s[t];a=x({erc721FactoryAddress:c,sideStakingAddress:o,opfCommunityFeeCollector:r,poolTemplateAddress:n,fixedRateExchangeAddress:e,dispenserAddress:i,oceanTokenAddress:h,chainId:l,startBlock:d},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}return a}getConfig(t,e){const a="string"==typeof t?"network":"chainId";let s=O.find(e=>e[a]===t);return s?(s=x({},s,this.getAddressesFromEnv(s.network)),x({},s,{nodeUri:e?`${s.nodeUri}/${e}`:s.nodeUri})):(g.error(`No config found for given network '${t}'`),null)}}function L(e,a){return e=t.utils.toChecksumAddress(e),`did:op:${i(e+a.toString(10)).toString()}`}function P(t){return i(t).toString()}const D="0x0000000000000000000000000000000000000000",N=1e6;async function W(t,e,a,s){const i=t&&t.currentProvider&&t.currentProvider.isMetaMask;try{return await t.eth.personal.sign(e,a,s)}catch(s){if(i)throw s;g.warn("Error on personal sign."),g.warn(s);try{return await t.eth.sign(e,a)}catch(t){throw g.error("Error on sign."),g.error(t),new Error("Error executing personal sign")}}}async function _(t,e,a){let s=await t.eth.sign(e,a);s=s.substr(2);const i="0x"+s.slice(0,64),o="0x"+s.slice(64,128);let n="0x"+s.slice(128,130);return"0x00"===n&&(n="0x1b"),"0x01"===n&&(n="0x1c"),{v:n,r:i,s:o}}async function $(t,e,a,s){const i=t.utils.utf8ToHex(e),o=t&&t.currentProvider&&t.currentProvider.isMetaMask;try{return await t.eth.personal.sign(i,a,s)}catch(e){if(o)throw e;g.warn("Error on personal sign."),g.warn(e);try{return await t.eth.sign(i,a)}catch(t){throw g.error("Error on sign."),g.error(t),new Error("Error executing personal sign")}}}async function B(t,e,a,s,i,o){const n=o||new t.eth.Contract(y,a),r=1e6;let c;try{c=await n.methods.approve(s,i).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){c=r,g.error("estimate gas failed for approve!",t)}return c}async function j(t,e,a,s,i,n=!1){const r=new t.eth.Contract(y,a);if(!n){const n=await z(t,a,e,s);if(new o(n).greaterThanOrEqualTo(new o(i)))return n}let c=null;const h=await R(t,a,i),l=await B(t,e,a,s,h,r);try{c=await r.methods.approve(s,h).send({from:e,gas:l+1,gasPrice:await A(t,null)})}catch(t){g.error(`ERRPR: Failed to approve spender to spend tokens : ${t.message}`)}return c}async function z(t,e,a,s){const i=new t.eth.Contract(y,e),o=await i.methods.allowance(a,s).call();return await F(t,e,o)}async function J(t,e,a){const s=new t.eth.Contract(y,e),i=await s.methods.balanceOf(a).call();return await F(t,e,i)}const q="115792089237316195423570985008687907853269984665640564039457584007913129639934";class H{constructor(t,e,a=null,s){this.poolAbi=void 0,this.web3=void 0,this.GASLIMIT_DEFAULT=1e6,this.logger=void 0,this.config=void 0,this.poolAbi=a||n.abi,this.web3=t,this.logger=e,this.config=s||O[0]}async sharesBalance(t,e){let a=null;try{const s=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=await s.methods.balanceOf(t).call();a=this.web3.utils.fromWei(i)}catch(t){this.logger.error(`ERROR: Failed to get shares of pool : ${t.message}`)}return a}async estSetSwapFee(t,e,a,s){const i=s||b(new this.web3.eth.Contract(r.abi,e),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setSwapFee(a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setSwapFee(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,e,{from:t}),this.config);let i=null;const o=await this.estSetSwapFee(t,e,a);try{i=await s.methods.setSwapFee(this.web3.utils.toWei(a)).send({from:t,gas:o,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to set pool swap fee: ${t.message}`)}return i}async getNumTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getNumTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get number of tokens: ${t.message}`)}return a}async getPoolSharesTotalSupply(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.totalSupply().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get total supply of pool shares: ${t.message}`)}return a}async getCurrentTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getCurrentTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get tokens composing this pool: ${t.message}`)}return a}async getFinalTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getFinalTokens().call()}catch(t){this.logger.error("ERROR: Failed to get the final tokens composing this pool")}return a}async getController(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getController().call()}catch(t){this.logger.error(`ERROR: Failed to get pool controller address: ${t.message}`)}return a}async getBaseToken(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getBaseTokenAddress().call()}catch(t){this.logger.error(`ERROR: Failed to get baseToken address: ${t.message}`)}return a}async getDatatoken(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getDatatokenAddress().call()}catch(t){this.logger.error(`ERROR: Failed to get datatoken address: ${t.message}`)}return a}async getMarketFeeCollector(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods._publishMarketCollector().call()}catch(t){this.logger.error(`ERROR: Failed to get marketFeeCollector address: ${t.message}`)}return a}async getOPFCollector(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods._opfCollector().call()}catch(t){this.logger.error(`ERROR: Failed to get OPF Collector address: ${t.message}`)}return a}async isBound(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{s=await a.methods.isBound(e).call()}catch(t){this.logger.error(`ERROR: Failed to check whether a token bounded to a pool. ${t.message}`)}return s}async getReserve(t,e){let a=null;try{const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),i=await s.methods.getBalance(e).call();a=await F(this.web3,e,i)}catch(t){this.logger.error(`ERROR: Failed to get how many tokens are in the pool: ${t.message}`)}return a.toString()}async isFinalized(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.isFinalized().call()}catch(t){this.logger.error(`ERROR: Failed to check whether pool is finalized: ${t.message}`)}return a}async getSwapFee(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.getSwapFee().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get pool fee: ${t.message}`)}return a}async getNormalizedWeight(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.getNormalizedWeight(e).call();s=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get normalized weight of a token: ${t.message}`)}return s}async getDenormalizedWeight(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.getDenormalizedWeight(e).call();s=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get denormalized weight of a token in pool")}return s}async getTotalDenormalizedWeight(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.getTotalDenormalizedWeight().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get total denormalized weight in pool")}return a}async getMarketFees(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.publishMarketFees(e).call();s=await F(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to get market fees for a token: ${t.message}`)}return s}async getCurrentMarketFees(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);try{return await e.methods.getCurrentOPFFees().call()}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}}async getCurrentOPFFees(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);try{return await e.methods.getCurrentOPFFees().call()}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}}async getCommunityFees(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.communityFees(e).call();s=await F(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}return s}async estCollectOPF(t,e,a){const s=a||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectOPF().estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectOPF(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let s=null;const i=await this.estCollectOPF(t,e);try{s=await a.methods.collectOPF().send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return s}async estCollectMarketFee(t,e,a){const s=a||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee().estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectMarketFee(t,e){if(await this.getMarketFeeCollector(e)!==t)throw new Error("Caller is not MarketFeeCollector");const a=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let s=null;const i=await this.estCollectMarketFee(t,e);try{s=await a.methods.collectMarketFee().send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return s}async estUpdateMarketFeeCollector(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFeeCollector(a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFeeCollector(t,e,a){if(await this.getMarketFeeCollector(e)!==t)throw new Error("Caller is not MarketFeeCollector");const s=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let i=null;const o=await this.estUpdateMarketFeeCollector(t,e,a);try{i=await s.methods.updateMarketFeeCollector(a).send({from:t,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return i}async estSwapExactAmountIn(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q,r=this.GASLIMIT_DEFAULT;let c;try{c=await o.methods.swapExactAmountIn([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.tokenAmountIn,s.minAmountOut,n,this.web3.utils.toWei(s.swapMarketFee)]).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async swapExactAmountIn(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);s.tokenAmountIn=await R(this.web3,a.tokenIn,s.tokenAmountIn),s.minAmountOut=await R(this.web3,a.tokenOut,s.minAmountOut);let o=null;const n=await this.estSwapExactAmountIn(t,e,a,s),r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;try{o=await i.methods.swapExactAmountIn([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.tokenAmountIn,s.minAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return o}async estSwapExactAmountOut(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT,r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;let c;try{c=await o.methods.swapExactAmountOut([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.maxAmountIn,s.tokenAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){c=n}return c}async swapExactAmountOut(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let o=null;s.maxAmountIn=await R(this.web3,a.tokenIn,s.maxAmountIn),s.tokenAmountOut=await R(this.web3,a.tokenOut,s.tokenAmountOut);const n=await this.estSwapExactAmountOut(t,e,a,s),r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;try{o=await i.methods.swapExactAmountOut([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.maxAmountIn,s.tokenAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount out: ${t.message}`)}return o}async estJoinPool(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.joinPool(a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async joinPool(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=[],n=await this.getFinalTokens(e);for(let t=0;t<2;t++){const e=await R(this.web3,n[t],s[t]);o.push(e)}let r=null;const c=await this.estJoinPool(t,e,this.web3.utils.toWei(a),o);try{r=await i.methods.joinPool(this.web3.utils.toWei(a),o).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to join pool: ${t.message}`)}return r}async estExitPool(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.exitPool(a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async exitPool(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=[],n=await this.getFinalTokens(e);for(let t=0;t<2;t++){const e=await R(this.web3,n[t],s[t]);o.push(e)}let r=null;const c=await this.estExitPool(t,e,this.web3.utils.toWei(a),o);try{r=await i.methods.exitPool(this.web3.utils.toWei(a),o).send({from:t,gas:c,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to exit pool: ${t.message}`)}return r}async estJoinswapExternAmountIn(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.joinswapExternAmountIn(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async joinswapExternAmountIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await R(this.web3,a,s),c=await this.estJoinswapExternAmountIn(t,e,a,r,this.web3.utils.toWei(i));try{n=await o.methods.joinswapExternAmountIn(a,r,this.web3.utils.toWei(i)).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay tokens in order to join the pool: ${t.message}`)}return n}async estJoinswapPoolAmountOut(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.joinswapPoolAmountOut(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async joinswapPoolAmountOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await R(this.web3,a,i),c=await this.estJoinswapPoolAmountOut(t,e,a,this.web3.utils.toWei(s),r);try{n=await o.methods.joinswapPoolAmountOut(a,this.web3.utils.toWei(s),r).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to join swap pool amount out")}return n}async estExitswapPoolAmountIn(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.exitswapPoolAmountIn(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async exitswapPoolAmountIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await R(this.web3,a,i),c=await this.estExitswapPoolAmountIn(t,e,a,this.web3.utils.toWei(s),r);try{n=await o.methods.exitswapPoolAmountIn(a,this.web3.utils.toWei(s),r).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${t.message}`)}return n}async estExitswapExternAmountOut(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.exitswapExternAmountOut(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async exitswapExternAmountOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await this.estExitswapExternAmountOut(t,e,a,this.web3.utils.toWei(s),this.web3.utils.toWei(i));try{n=await o.methods.exitswapExternAmountOut(a,this.web3.utils.toWei(s),this.web3.utils.toWei(i)).send({from:t,gas:r+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to exitswapExternAmountOut")}return n}async getSpotPrice(t,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let n=18,c=18;const h=b(new this.web3.eth.Contract(r.abi,a),this.config),l=b(new this.web3.eth.Contract(r.abi,s),this.config);try{n=await h.methods.decimals().call()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}try{c=await l.methods.decimals().call()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}let d,w=null;try{w=await o.methods.getSpotPrice(a,s,this.web3.utils.toWei(i)).call(),w=new e(w.toString())}catch(t){this.logger.error("ERROR: Failed to get spot price of swapping tokenIn to tokenOut")}return n>c?(d=n-c,w=new e(w/10**d),w/=10**c):(d=c-n,w=new e(w*10**(2*d)),w/=10**c),w.toString()}async getAmountInExactOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),n=await R(this.web3,a,s);let r=null;try{const t=await o.methods.getAmountInExactOut(e,a,n,this.web3.utils.toWei(i)).call();r=await F(this.web3,e,t)}catch(t){this.logger.error("ERROR: Failed to calcInGivenOut")}return r}async getAmountOutExactIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),n=await R(this.web3,e,s);let r=null;try{const t=await o.methods.getAmountOutExactIn(e,a,n,this.web3.utils.toWei(i)).call();r=await F(this.web3,a,t)}catch(t){this.logger.error("ERROR: Failed to calcOutGivenIn")}return r}async calcPoolOutGivenSingleIn(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcPoolOutSingleIn(e,await R(this.web3,e,a)).call();i=await F(this.web3,t,o)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${t.message}`)}return i}async calcSingleInGivenPoolOut(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;const o=await R(this.web3,t,a);try{const t=await s.methods.calcSingleInPoolOut(e,o).call();i=await F(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${t.message}`)}return i}async calcSingleOutGivenPoolIn(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcSingleOutPoolIn(e,await R(this.web3,t,a)).call();i=await F(this.web3,e,o)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${t}`)}return i}async calcPoolInGivenSingleOut(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcPoolInSingleOut(e,await R(this.web3,e,a)).call();i=await F(this.web3,t,o)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${t.message}`)}return i}getSwapEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_SWAP"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getJoinEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_JOIN"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getExitEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_EXIT"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}}class Z{constructor(t,e=null,a=null,s){this.GASLIMIT_DEFAULT=1e6,this.web3=null,this.dispenserAddress=void 0,this.config=void 0,this.dispenserAbi=void 0,this.dispenserContract=void 0,this.web3=t,this.dispenserAddress=e,this.dispenserAbi=a||c.abi,this.config=s||O[0],t&&(this.dispenserContract=b(new this.web3.eth.Contract(this.dispenserAbi,this.dispenserAddress),this.config))}async status(t){try{const e=await this.dispenserContract.methods.status(t).call();return e.maxTokens=this.web3.utils.fromWei(e.maxTokens),e.maxBalance=this.web3.utils.fromWei(e.maxBalance),e.balance=this.web3.utils.fromWei(e.balance),e}catch(e){g.warn(`No dispenser available for datatoken: ${t}`)}return null}async estGasCreate(t,e,a,s,i){const o=this.GASLIMIT_DEFAULT;let n;try{n=await this.dispenserContract.methods.create(t,this.web3.utils.toWei(a),this.web3.utils.toWei(s),e,i).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async create(t,e,a,s,i){const o=await this.estGasCreate(t,e,a,s,i);return await this.dispenserContract.methods.create(t,this.web3.utils.toWei(a),this.web3.utils.toWei(s),e,i).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasActivate(t,e,a,s){let i;const o=this.GASLIMIT_DEFAULT;try{i=await this.dispenserContract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(a)).estimateGas({from:s},(t,e)=>t?o:e)}catch(t){i=o}return i}async activate(t,e,a,s){try{const i=await this.estGasActivate(t,e,a,s);return await this.dispenserContract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(a)).send({from:s,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasDeactivate(t,e){let a;const s=this.GASLIMIT_DEFAULT;try{a=await this.dispenserContract.methods.deactivate(t).estimateGas({from:e},(t,e)=>t?s:e)}catch(t){a=s}return a}async deactivate(t,e){try{const a=await this.estGasDeactivate(t,e);return await this.dispenserContract.methods.deactivate(t).send({from:e,gas:a+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasSetAllowedSwapper(t,e,a){let s;const i=this.GASLIMIT_DEFAULT;try{s=await this.dispenserContract.methods.setAllowedSwapper(t,a).estimateGas({from:e},(t,e)=>t?i:e)}catch(t){s=i}return s}async setAllowedSwapper(t,e,a){try{const s=await this.estGasSetAllowedSwapper(t,e,a);return await this.dispenserContract.methods.setAllowedSwapper(t,a).send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasDispense(t,e,a="1",s){let i;const o=this.GASLIMIT_DEFAULT;try{i=await this.dispenserContract.methods.dispense(t,this.web3.utils.toWei(a),s).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){i=o}return i}async dispense(t,e,a="1",s){const i=await this.estGasDispense(t,e,a,s);try{return await this.dispenserContract.methods.dispense(t,this.web3.utils.toWei(a),s).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to dispense tokens: ${t.message}`)}return null}async estGasOwnerWithdraw(t,e){let a;const s=this.GASLIMIT_DEFAULT;try{a=await this.dispenserContract.methods.ownerWithdraw(t).estimateGas({from:e},(t,e)=>t?s:e)}catch(t){a=s}return a}async ownerWithdraw(t,e){const a=await this.estGasOwnerWithdraw(t,e);try{return await this.dispenserContract.methods.ownerWithdraw(t).send({from:e,gas:a+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to withdraw tokens: ${t.message}`)}return null}async isDispensable(t,e,a,s="1"){const i=await this.status(t);return!(!i||!1===i.active||new o(await e.balance(t,a)).greaterThanOrEqualTo(i.maxBalance)||new o(String(s)).greaterThan(i.maxTokens)||!new o(i.balance).greaterThanOrEqualTo(s)&&!0!==i.isMinter)}}var V;!function(t){t[t.CreatingExchange=0]="CreatingExchange",t[t.ApprovingDatatoken=1]="ApprovingDatatoken"}(V||(V={}));class X{constructor(t,e,a=null,s=null,i){this.GASLIMIT_DEFAULT=1e6,this.oceanAddress=null,this.fixedRateAddress=void 0,this.fixedRateExchangeAbi=void 0,this.fixedRateContract=void 0,this.web3=void 0,this.contract=null,this.config=void 0,this.ssAbi=void 0,this.web3=t,this.config=i||O[0],this.fixedRateExchangeAbi=a||h.abi,this.oceanAddress=s,this.fixedRateAddress=e,this.contract=b(new this.web3.eth.Contract(this.fixedRateExchangeAbi,this.fixedRateAddress),this.config)}async amountToUnits(t,a){let s=18;const i=b(new this.web3.eth.Contract(r.abi,t),this.config);try{s=await i.methods.decimals().call()}catch(t){g.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}return new e(parseInt(a)*10**s).toString()}async unitsToAmount(t,a){let s=18;const i=b(new this.web3.eth.Contract(r.abi,t),this.config);try{s=await i.methods.decimals().call()}catch(t){g.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}return new e(parseInt(a)/10**s).toString()}async generateExchangeId(t,e,a){return await this.contract.methods.generateExchangeId(t,e,a).call()}async estBuyDT(t,e,a,s,i){const o=i||this.fixedRateContract,n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyDT(e,a.toString(),s.toString()).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyDT(t,e,a,s){const i=await this.amountToUnits((await this.getExchange(e)).datatoken,a),o=await this.amountToUnits((await this.getExchange(e)).baseToken,s),n=await this.estBuyDT(t,e,i,o);try{return await this.contract.methods.buyDT(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){return g.error(`ERROR: Failed to buy datatokens: ${t.message}`),null}}async estSellDT(t,e,a,s,i){const o=i||this.fixedRateContract,n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.sellDT(e,a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async sellDT(t,e,a,s){const i=await this.amountToUnits((await this.getExchange(e)).datatoken,a),o=await this.amountToUnits((await this.getExchange(e)).baseToken,s),n=await this.estBuyDT(t,e,i,o);try{return await this.contract.methods.sellDT(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){return g.error(`ERROR: Failed to sell datatokens: ${t.message}`),null}}async getNumberOfExchanges(){return await this.contract.methods.getNumberOfExchanges().call()}async estSetRate(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setRate(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setRate(t,e,a){const s=await this.estSetRate(t,e,this.web3.utils.toWei(String(a)));return await this.contract.methods.setRate(e,this.web3.utils.toWei(a)).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estSetAllowedSwapper(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setRate(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setAllowedSwapper(t,e,a){const s=await this.estSetAllowedSwapper(t,e,a);return await this.contract.methods.setAllowedSwapper(e,a).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estActivate(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleExchangeState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async activate(t,e){const a=await this.getExchange(e);if(!a)return null;if(!0===a.active)return null;const s=await this.estActivate(t,e);return await this.contract.methods.toggleExchangeState(e).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estDeactivate(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleExchangeState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async deactivate(t,e){const a=await this.getExchange(e);if(!a)return null;if(!1===a.active)return null;const s=await this.estDeactivate(t,e);return await this.contract.methods.toggleExchangeState(e).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async getRate(t){const e=await this.contract.methods.getRate(t).call();return this.web3.utils.fromWei(e)}async getDTSupply(t){const e=await this.contract.methods.getDTSupply(t).call();return await this.unitsToAmount((await this.getExchange(t)).datatoken,e)}async getBTSupply(t){const e=await this.contract.methods.getBTSupply(t).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,e)}async getAllowedSwapper(t){return await this.contract.methods.getAllowedSwapper(t).call()}async getAmountBTIn(t,e){const a=await this.contract.methods.calcBaseInGivenOutDT(t,await this.amountToUnits((await this.getExchange(t)).datatoken,e)).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,a.baseTokenAmount)}async getAmountBTOut(t,e){const a=await this.contract.methods.calcBaseOutGivenInDT(t,await this.amountToUnits((await this.getExchange(t)).datatoken,e)).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,a.baseTokenAmount)}async getExchange(t){const e=await this.contract.methods.getExchange(t).call();return e.dtDecimals=e.dtDecimals.toString(),e.btDecimals=e.btDecimals.toString(),e.dtBalance=await this.unitsToAmount(e.datatoken,e.dtBalance),e.btBalance=await this.unitsToAmount(e.baseToken,e.btBalance),e.dtSupply=await this.unitsToAmount(e.datatoken,e.dtSupply),e.btSupply=await this.unitsToAmount(e.baseToken,e.btSupply),e.fixedRate=this.web3.utils.fromWei(e.fixedRate),e.exchangeId=t,e}async getFeesInfo(t){const e=await this.contract.methods.getFeesInfo(t).call();return e.opfFee=this.web3.utils.fromWei(e.opfFee.toString()),e.marketFee=this.web3.utils.fromWei(e.marketFee.toString()),e.marketFeeAvailable=await this.unitsToAmount((await this.getExchange(t)).baseToken,e.marketFeeAvailable),e.oceanFeeAvailable=await this.unitsToAmount((await this.getExchange(t)).baseToken,e.oceanFeeAvailable),e.exchangeId=t,e}async getExchanges(){return await this.contract.methods.getExchanges().call()}async isActive(t){return await this.contract.methods.isActive(t).call()}async estActivateMint(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleMintState(e,!0).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async activateMint(t,e){const a=await this.getExchange(e);if(!a)return null;if(!0===a.withMint)return null;const s=await this.estActivateMint(t,e);return await this.contract.methods.toggleMintState(e,!0).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estDeactivateMint(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleMintState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async deactivateMint(t,e){const a=await this.getExchange(e);if(!a)return null;if(!1===a.withMint)return null;const s=await this.estDeactivate(t,e);return await this.contract.methods.toggleMintState(e,!1).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estCollectBT(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectBT(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectBT(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectBT(t,e);return await this.contract.methods.collectBT(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectDT(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectDT(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectDT(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectDT(t,e);return await this.contract.methods.collectDT(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectMarketFee(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectMarketFee(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectMarketFee(t,e);return await this.contract.methods.collectMarketFee(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectOceanFee(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectOceanFee(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectOceanFee(t,e);return await this.contract.methods.collectOceanFee(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async getOPFCollector(){let t=null;try{t=await this.contract.methods.opfCollector().call()}catch(t){g.error(`ERROR: Failed to get OPF Collector address: ${t.message}`)}return t}async getRouter(){let t=null;try{t=await this.contract.methods.router().call()}catch(t){g.error(`ERROR: Failed to get Router address: ${t.message}`)}return t}async getExchangeOwner(t){let e=null;try{e=await(await this.getExchange(t)).exchangeOwner}catch(t){g.error(`ERROR: Failed to get OPF Collector address: ${t.message}`)}return e}async estUpdateMarketFee(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFee(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFee(t,e,a){const s=await this.estSetRate(t,e,this.web3.utils.toWei(a));return await this.contract.methods.updateMarketFee(e,this.web3.utils.toWei(a)).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estUpdateMarketFeeCollector(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFeeCollector(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFeeCollector(t,e,a){const s=await this.estUpdateMarketFeeCollector(t,e,a);return await this.contract.methods.updateMarketFeeCollector(e,a).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}}class Q{constructor(t,e,a){this.GASLIMIT_DEFAULT=1e6,this.factory721Address=void 0,this.factory721Abi=void 0,this.nftAbi=void 0,this.web3=void 0,this.startBlock=void 0,this.config=void 0,this.nftAbi=e||d.abi,this.web3=t,this.config=a||O[0]}async estGasCreateErc20(t,e,a,s,i,o,n,r,c,h,l,d){const w=d||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),m=this.GASLIMIT_DEFAULT;let u;try{u=await w.methods.createERC20(l,[c,h],[a,s,i,o],[this.web3.utils.toWei(r),this.web3.utils.toWei(n)],[]).estimateGas({from:e},(t,e)=>t?m:e)}catch(t){u=m}return u}async createErc20(t,e,a,s,i,o,n,r,c,h,l){if(!0!==(await this.getNftPermissions(t,e)).deployERC20)throw new Error("Caller is not ERC20Deployer");l||(l=1),c&&h||({name:c,symbol:h}=f());const d=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),w=await this.estGasCreateErc20(t,e,a,s,i,o,n,r,c,h,l,d),m=await d.methods.createERC20(l,[c,h],[a,s,i,o],[this.web3.utils.toWei(r),this.web3.utils.toWei(n)],[]).send({from:e,gas:w+1,gasPrice:await A(this.web3,this.config)});let u=null;try{u=m.events.TokenCreated.returnValues[0]}catch(t){g.error(`ERROR: Failed to create datatoken : ${t.message}`)}return u}async estGasAddManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addManager(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const i=await this.estGasAddManager(t,e,a,s);return await s.methods.addManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeManager(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const i=await this.estGasRemoveManager(t,e,a,s);return await s.methods.removeManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddErc20Deployer(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addToCreateERC20List(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addErc20Deployer(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddErc20Deployer(t,e,a,s);return await s.methods.addToCreateERC20List(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveErc20Deployer(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFromCreateErc20List(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeErc20Deployer(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e===a&&!0!==(await this.getNftPermissions(t,e)).deployERC20)throw new Error("Caller is not Manager nor ERC20Deployer");const i=await this.estGasRemoveErc20Deployer(t,e,a,s);return await s.methods.removeFromCreateERC20List(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddMetadataUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addToMetadataList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addMetadataUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddMetadataUpdater(t,e,a,s);return await s.methods.addToMetadataList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async esGasRemoveMetadataUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFromMetadataList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeMetadataUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e!==a&&!0!==(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Manager nor Metadata Updater");const i=await this.esGasRemoveMetadataUpdater(t,e,a,s);return await s.methods.removeFromMetadataList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddStoreUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addTo725StoreList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addStoreUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddStoreUpdater(t,e,a,s);return await s.methods.addTo725StoreList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveStoreUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFrom725StoreList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeStoreUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e!==a&&!0!==(await this.getNftPermissions(t,e)).store)throw new Error("Caller is not Manager nor storeUpdater");const i=await this.estGasRemoveStoreUpdater(t,e,a,s);return await s.methods.removeFrom725StoreList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCleanPermissions(t,e,a){const s=a||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.cleanPermissions().estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async cleanPermissions(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const s=await this.estGasCleanPermissions(t,e,a);return await a.methods.cleanPermissions().send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estGasTransferNft(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.transferFrom(e,a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async transferNft(t,e,a,s){const i=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const o=s||1,n=await this.estGasTransferNft(t,e,a,o,i);return await i.methods.transferFrom(e,a,o).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasSafeTransferNft(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.safeTransferFrom(e,a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async safeTransferNft(t,e,a,s){const i=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const o=s||1,n=await this.estGasSafeTransferNft(t,e,a,o,i);return await i.methods.safeTransferFrom(e,a,o).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadata(t,e,a,s,i,o,n,r,c,h){const l=h||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),d=this.GASLIMIT_DEFAULT;let w;try{w=await l.methods.setMetaData(a,s,i,o,n,r,c).estimateGas({from:e},(t,e)=>t?d:e)}catch(t){w=d}return w}async setMetadata(t,e,a,s,i,o,n,r,c){const h=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(c||(c=[]),!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const l=await this.estGasSetMetadata(t,e,a,s,i,o,n,r,c,h);return await h.methods.setMetaData(a,s,i,o,n,r,c).send({from:e,gas:l+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadataAndTokenURI(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setMetaDataAndTokenURI(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setMetadataAndTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const i=await this.estGasSetMetadataAndTokenURI(t,e,a,s);return await s.methods.setMetaDataAndTokenURI(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadataState(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setMetaDataState(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setMetadataState(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const i=await this.estGasSetMetadataState(t,e,a);return await s.methods.setMetaDataState(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estSetTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.setTokenURI("1",a).estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async setTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=await this.estSetTokenURI(t,e,a);return await s.methods.setTokenURI("1",a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async getNftOwner(t){const e=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await e.methods.ownerOf(1).call()}async getNftPermissions(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.getPermissions(e).call()}async getMetadata(t){const e=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await e.methods.getMetaData().call()}async isErc20Deployer(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.isERC20Deployer(e).call()}async getData(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.getData(e).call()}async getTokenURI(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.tokenURI(e).call()}}class K{constructor(t,e,a,s){this.GASLIMIT_DEFAULT=1e6,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensAbi=void 0,this.datatokensEnterpriseAbi=void 0,this.web3=void 0,this.config=void 0,this.nft=void 0,this.web3=t,this.datatokensAbi=e||r.abi,this.datatokensEnterpriseAbi=a||l.abi,this.config=s||O[0],this.nft=new Q(this.web3)}async estGasApprove(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.approve(e,this.web3.utils.toWei(a)).estimateGas({from:s},(t,e)=>t?n:e)}catch(t){r=n}return r}async approve(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=await this.estGasApprove(t,e,a,s,i);return await i.methods.approve(e,this.web3.utils.toWei(a)).send({from:s,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasMint(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.mint(s||e,this.web3.utils.toWei(a)).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async estGasCreateFixedRate(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;a.allowedConsumer||(a.allowedConsumer="0x0000000000000000000000000000000000000000");const n=a.withMint?1:0;let r;try{r=await i.methods.createFixedRate(a.fixedRateAddress,[a.baseTokenAddress,e,a.marketFeeCollector,a.allowedConsumer],[a.baseTokenDecimals,a.datatokenDecimals,a.fixedRate,a.marketFee,n]).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){r=o}return r}async createFixedRate(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");a.allowedConsumer||(a.allowedConsumer="0x0000000000000000000000000000000000000000");const i=a.withMint?1:0,o=await this.estGasCreateFixedRate(t,e,a,s);return await s.methods.createFixedRate(a.fixedRateAddress,[a.baseTokenAddress,a.owner,a.marketFeeCollector,a.allowedConsumer],[a.baseTokenDecimals,a.datatokenDecimals,a.fixedRate,a.marketFee,i]).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateDispenser(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);s.allowedSwapper||(s.allowedSwapper="0x0000000000000000000000000000000000000000"),s.withMint||(s.withMint=!1);const n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.createDispenser(a,s.maxTokens,s.maxBalance,s.withMint,s.allowedSwapper).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async createDispenser(t,e,a,s){if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);s.allowedSwapper||(s.allowedSwapper="0x0000000000000000000000000000000000000000"),s.withMint||(s.withMint=!1);const o=await this.estGasCreateDispenser(t,e,a,s,i);return await i.methods.createDispenser(a,s.maxTokens,s.maxBalance,s.withMint,s.allowedSwapper).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async mint(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==(await this.getDTPermissions(t,e)).minter)throw new Error("Caller is not Minter");const n=await this.getCap(t);if(new o(n).gte(a)){const o=await this.estGasMint(t,e,a,s,i);return await i.methods.mint(s||e,this.web3.utils.toWei(a)).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}throw new Error("Mint amount exceeds cap available")}async estGasAddMinter(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addMinter(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addMinter(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasAddMinter(t,e,a,s);return await s.methods.addMinter(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveMinter(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeMinter(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeMinter(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasRemoveMinter(t,e,a,s);return await s.methods.removeMinter(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddPaymentManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addPaymentManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addPaymentManager(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasAddPaymentManager(t,e,a,s);return await s.methods.addPaymentManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemovePaymentManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removePaymentManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removePaymentManager(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasRemovePaymentManager(t,e,a,s);return await s.methods.removePaymentManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasSetPaymentCollector(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setPaymentCollector(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setPaymentCollector(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==(await this.getDTPermissions(t,e)).paymentManager)throw new Error("Caller is not Fee Manager");const i=await this.estGasSetPaymentCollector(t,e,a,s);return await s.methods.setPaymentCollector(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async getPaymentCollector(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.getPaymentCollector().call()}async transfer(t,e,a,s){const i=this.web3.utils.toWei(a);return this.transferWei(t,e,i,s)}async estGasTransfer(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.transfer(e,a).estimateGas({from:s},(t,e)=>t?n:e)}catch(t){r=n}return r}async transferWei(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);try{const o=await this.estGasTransfer(t,e,a,s,i);return await i.methods.transfer(e,a).send({from:s,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to transfer tokens: ${t.message}`),new Error(`Failed Failed to transfer tokens: ${t.message}`)}}async estGasStartOrder(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.startOrder(a,s,i).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){c=r}return c}async startOrder(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);try{const n=await this.estGasStartOrder(t,e,a,s,i,o);return await o.methods.startOrder(a,s,i).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to start order : ${t.message}`),new Error(`Failed to start order: ${t.message}`)}}async estGasBuyFromFreAndOrder(t,e,a,s,i){const o=i||new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyFromFreAndOrder(a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyFromFreAndOrder(t,e,a,s){const i=new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t);try{const o=await this.estGasBuyFromFreAndOrder(t,e,a,s,i);return await i.methods.buyFromFreAndOrder(a,s).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to buy DT From Fre And Order : ${t.message}`),new Error(`Failed to buy DT From Fre And Order: ${t.message}`)}}async estGasBuyFromDispenserAndOrder(t,e,a,s,i){const o=i||new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyFromDispenserAndOrder(a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyFromDispenserAndOrder(t,e,a,s){const i=new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t);try{const o=await this.estGasBuyFromDispenserAndOrder(t,e,a,s,i);return await i.methods.buyFromDispenserAndOrder(a,s).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to buy DT From Fre And Order : ${t.message}`),new Error(`Failed to buy DT From Fre And Order: ${t.message}`)}}async estGasSetData(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setData(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setData(t,e,a){if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),i=await this.estGasSetData(t,e,a,s);return await s.methods.setData(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCleanPermissions(t,e,a){const s=a||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.cleanPermissions().estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async cleanPermissions(t,e){if(await this.nft.getNftOwner(await this.getNFTAddress(t))!==e)throw new Error("Caller is NOT Nft Owner");const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),s=await this.estGasCleanPermissions(t,e,a);return await a.methods.cleanPermissions().send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}async getDTPermissions(t,e){const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await a.methods.permissions(e).call()}async getCap(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),a=await e.methods.cap().call();return this.web3.utils.fromWei(a)}async getDecimals(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.decimals().call()}async getNFTAddress(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.getERC721Address().call()}async isERC20Deployer(t,e){const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await a.methods.isERC20Deployer(e).call()}async balance(t,e){const a=new this.web3.eth.Contract(this.datatokensAbi,t,{from:e}),s=await a.methods.balanceOf(e).call();return this.web3.utils.fromWei(s)}}const Y="0x0000000000000000000000000000000000000000";class tt{constructor(t,e,a,s){this.GASLIMIT_DEFAULT=1e6,this.factory721Address=void 0,this.factory721Abi=void 0,this.web3=void 0,this.config=void 0,this.factory721=void 0,this.factory721Address=t,this.factory721Abi=a||w.abi,this.web3=e,this.config=s||O[0],this.factory721=b(new this.web3.eth.Contract(this.factory721Abi,this.factory721Address),this.config)}async estGasCreateNFT(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.deployERC721Contract(e.name,e.symbol,e.templateIndex,Y,e.tokenURI).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async createNFT(t,e){if(e.templateIndex||(e.templateIndex=1),!e.name||!e.symbol){const{name:t,symbol:a}=f();e.name=t,e.symbol=a}if(e.templateIndex>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e.templateIndex)throw new Error("Template index cannot be ZERO");if(!1===(await this.getNFTTemplate(e.templateIndex)).isActive)throw new Error("Template is not active");const a=await this.estGasCreateNFT(t,e),s=await this.factory721.methods.deployERC721Contract(e.name,e.symbol,e.templateIndex,Y,e.tokenURI).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)});let i=null;try{i=s.events.NFTCreated.returnValues[0]}catch(t){g.error(`ERROR: Failed to create datatoken : ${t.message}`)}return i}async getCurrentNFTCount(){return await this.factory721.methods.getCurrentNFTCount().call()}async getCurrentTokenCount(){return await this.factory721.methods.getCurrentTokenCount().call()}async getOwner(){return await this.factory721.methods.owner().call()}async getCurrentNFTTemplateCount(){return await this.factory721.methods.getCurrentNFTTemplateCount().call()}async getCurrentTokenTemplateCount(){return await this.factory721.methods.getCurrentTemplateCount().call()}async getNFTTemplate(t){if(t>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===t)throw new Error("Template index cannot be ZERO");return await this.factory721.methods.getNFTTemplate(t).call()}async getTokenTemplate(t){return await this.factory721.methods.getTokenTemplate(t).call()}async checkDatatoken(t){return await this.factory721.methods.erc20List(t).call()}async checkNFT(t){return await this.factory721.methods.erc721List(t).call()}async estGasAddNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.add721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async addNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e===Y)throw new Error("Template cannot be ZERO address");const a=await this.estGasAddNFTTemplate(t,e);return await this.factory721.methods.add721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasDisableNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.disable721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async disableNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");const a=await this.estGasDisableNFTTemplate(t,e);return await this.factory721.methods.disable721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasReactivateNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.reactivate721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async reactivateNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");const a=await this.estGasReactivateNFTTemplate(t,e);return await this.factory721.methods.reactivate721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasAddTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.addTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async addTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e===Y)throw new Error("Template cannot be address ZERO");const a=await this.estGasAddTokenTemplate(t,e);return await this.factory721.methods.addTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasDisableTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.disableTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async disableTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");if(!1===(await this.getNFTTemplate(e)).isActive)throw new Error("Template is already disabled");const a=await this.estGasDisableTokenTemplate(t,e);return await this.factory721.methods.disableTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasReactivateTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.reactivateTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async reactivateTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");if(!0===(await this.getTokenTemplate(e)).isActive)throw new Error("Template is already active");const a=await this.estGasReactivateTokenTemplate(t,e);return await this.factory721.methods.reactivateTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasStartMultipleTokenOrder(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.startMultipleTokenOrder(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async startMultipleTokenOrder(t,e){if(e.length>50)throw new Error("Too many orders");const a=await this.estGasStartMultipleTokenOrder(t,e);return await this.factory721.methods.startMultipleTokenOrder(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftWithErc(t,e,a){const s=this.GASLIMIT_DEFAULT;let i;try{const o=T(a);i=await this.factory721.methods.createNftWithErc(e,o).estimateGas({from:t},(t,e)=>t?s:e)}catch(t){i=s}return i}async createNftWithErc(t,e,a){const s=T(a),i=await this.estGasCreateNftWithErc(t,e,a);return await this.factory721.methods.createNftWithErc(e,s).send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithPool(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;try{const n=T(a),r=C(s);o=await this.factory721.methods.createNftErcWithPool(e,n,r).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async createNftErcWithPool(t,e,a,s){const i=await this.estGasCreateNftErcWithPool(t,e,a,s),o=T(a),n=C(s);return await this.factory721.methods.createNftErcWithPool(e,o,n).send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithFixedRate(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;const n=T(a),r=E(s);try{o=await this.factory721.methods.createNftErcWithFixedRate(e,n,r).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async createNftErcWithFixedRate(t,e,a,s){const i=T(a),o=E(s),n=await this.estGasCreateNftErcWithFixedRate(t,e,a,s);return await this.factory721.methods.createNftErcWithFixedRate(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithDispenser(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;const n=T(a);try{o=await this.factory721.methods.createNftErcWithDispenser(e,n,s).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i,g.error("Failed to estimate gas for createNftErcWithDispenser",t)}return o}async createNftErcWithDispenser(t,e,a,s){const i=T(a),o=await this.estGasCreateNftErcWithDispenser(t,e,a,s);return await this.factory721.methods.createNftErcWithDispenser(e,i,s).send({from:t,gas:o+1,gasPrice:await A(this.web3,this.config)})}}class et{constructor(){this.nodeUri=void 0,this.providerAddress=void 0,this.metadataCacheUri=void 0,this.providerUri=void 0,this.web3Provider=void 0,this.oceanTokenAddress=void 0,this.erc721FactoryAddress=void 0,this.erc721FFactoryABI=void 0,this.datatokensABI=void 0,this.poolTemplateAddress=void 0,this.poolFactoryABI=void 0,this.poolABI=void 0,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeAddressABI=void 0,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.opfCommunityFeeCollector=void 0,this.sideStakingAddress=void 0,this.startBlock=void 0,this.verbose=void 0,this.authMessage=void 0,this.authTokenExpiration=void 0,this.parityUri=void 0,this.threshold=void 0,this.chainId=void 0,this.network=void 0,this.subgraphUri=void 0,this.explorerUri=void 0,this.oceanTokenSymbol=void 0,this.transactionBlockTimeout=void 0,this.transactionConfirmationBlocks=void 0,this.transactionPollingTimeout=void 0,this.gasFeeMultiplier=void 0}}const at=t=>function(t="",e){const{valid:a,output:s}=function(t,e,a){if("string"!=typeof t)throw g.debug("Not input string:"),g.debug(t),new Error("[zeroXTransformer] Expected string, input type: "+typeof t);const s=t.match(/^(?:0x)*([a-f0-9]+)$/i);return s?{valid:!0,output:s[1]}:(g.warn("[zeroXTransformer] Input transformation failed."),{valid:!1,output:t})}(t);return(e&&a?"0x":"")+s}(t,!1);class st{async getEndpoints(t){try{const e=await S(t);return await e.json()}catch(t){return g.error("Finding the service endpoints failed:",t),null}}getEndpointURL(t,e){return t?t.find(t=>t.serviceName===e):null}async getServiceEndpoints(t,e){const a=[];for(const s in e.serviceEndpoints)a.push({serviceName:s,method:e.serviceEndpoints[s][0],urlPath:t+e.serviceEndpoints[s][1]});return a}async getNonce(t,e,s,i,o){i||(i=await this.getEndpoints(t)),o||(o=await this.getServiceEndpoints(t,i));const n=this.getEndpointURL(o,"nonce")?this.getEndpointURL(o,"nonce").urlPath:null;if(!n)return null;try{const t=await a(n+`?userAddress=${e}`,{method:"GET",headers:{"Content-Type":"application/json"},signal:s});return String((await t.json()).nonce)}catch(t){throw g.error(t),new Error("HTTP request failed")}}async createSignature(t,e,a){return await W(t,at(a),e)}async createHashSignature(t,e,a){return await $(t,a,e)}async encrypt(t,e,s){const i=await this.getEndpoints(e),o=await this.getServiceEndpoints(e,i),n=this.getEndpointURL(o,"encrypt")?this.getEndpointURL(o,"encrypt").urlPath:null;if(!n)return null;try{const e=await a(n,{method:"POST",body:decodeURI(JSON.stringify(t)),headers:{"Content-Type":"application/octet-stream"},signal:s});return await e.text()}catch(t){throw g.error(t),new Error("HTTP request failed")}}async checkDidFiles(t,e,s,i){const o=await this.getEndpoints(s),n=await this.getServiceEndpoints(s,o),r={did:t,serviceId:e},c=[],h=this.getEndpointURL(n,"fileinfo")?this.getEndpointURL(n,"fileinfo").urlPath:null;if(!h)return null;try{const t=await a(h,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"},signal:i}),e=await t.json();for(const t of e)c.push(t);return c}catch(t){return null}}async checkFileUrl(t,e,s){const i=await this.getEndpoints(e),o=await this.getServiceEndpoints(e,i),n={url:t,type:"url"},r=[],c=this.getEndpointURL(o,"fileinfo")?this.getEndpointURL(o,"fileinfo").urlPath:null;if(!c)return null;try{const t=await a(c,{method:"POST",body:JSON.stringify(n),headers:{"Content-Type":"application/json"},signal:s}),e=await t.json();for(const t of e)r.push(t);return r}catch(t){return null}}async initialize(t,e,s,i,o,n,r,c,h){const l=await this.getEndpoints(o),d=await this.getServiceEndpoints(o,l);let w=this.getEndpointURL(d,"initialize")?this.getEndpointURL(d,"initialize").urlPath:null;if(!w)return null;w+=`?documentId=${t}`,w+=`&serviceId=${e}`,w+=`&fileIndex=${s}`,w+=`&consumerAddress=${i}`,r&&(w+="&userdata="+encodeURI(JSON.stringify(r))),c&&(w+="&computeEnv="+encodeURI(c)),h&&(w+="&validUntil="+h);try{const t=await a(w,{method:"GET",headers:{"Content-Type":"application/json"},signal:n});return await t.json()}catch(t){throw g.error(t),new Error("Asset URL not found or not available.")}}async getDownloadUrl(t,e,a,s,i,o,n,r){const c=await this.getEndpoints(o),h=await this.getServiceEndpoints(o,c),l=this.getEndpointURL(h,"download")?this.getEndpointURL(h,"download").urlPath:null;if(!l)return null;const d=Date.now();let w=l;return w+=`?fileIndex=${s}`,w+=`&documentId=${t}`,w+=`&transferTxId=${i}`,w+=`&serviceId=${a}`,w+=`&consumerAddress=${e}`,w+=`&nonce=${d}`,w+=`&signature=${await this.createSignature(n,e,t+d)}`,r&&(w+="&userdata="+encodeURI(JSON.stringify(r))),w}async computeStart(t,e,s,i,o,n,r,c,h){const l=await this.getEndpoints(t),d=await this.getServiceEndpoints(t,l),w=this.getEndpointURL(d,"computeStart")?this.getEndpointURL(d,"computeStart").urlPath:null,m=Date.now();let u=s;u+=o.documentId,u+=m;const p=await this.createHashSignature(e,s,u),f=Object();if(f.consumerAddress=s,f.signature=p,f.nonce=m,f.computeEnv=i,f.dataset=o,f.algorithm=n,f.additionalDatasets&&(f.additionalDatasets=c),h&&(f.output=h),!w)return null;try{const t=await a(w,{method:"POST",body:JSON.stringify(f),headers:{"Content-Type":"application/json"},signal:r});return null!=t&&t.ok?await t.json():(console.error("Compute start failed:",t.status,t.statusText),g.error("Payload was:",f),null)}catch(t){return g.error("Compute start failed:"),g.error(t),g.error("Payload was:",f),null}}async computeStop(t,e,s,i,o,n){const r=await this.getEndpoints(i),c=await this.getServiceEndpoints(i,r),h=this.getEndpointURL(c,"computeStop")?this.getEndpointURL(c,"computeStop").urlPath:null,l=await this.getNonce(i,e,n,r,c);let d=e;d+=s||"",d+=t&&`${at(t)}`||"",d+=l;const w=await this.createHashSignature(o,e,d),m=Object();if(m.signature=w,m.documentId=at(t),m.consumerAddress=e,s&&(m.jobId=s),!h)return null;try{const t=await a(h,{method:"PUT",body:JSON.stringify(m),headers:{"Content-Type":"application/json"},signal:n});return null!=t&&t.ok?await t.json():(g.error("Compute stop failed:",t.status,t.statusText),g.error("Payload was:",m),null)}catch(t){return g.error("Compute stop failed:"),g.error(t),g.error("Payload was:",m),null}}async computeStatus(t,e,s,i,o){if(!s&&!i&&!o)throw new Error("You need at least one of jobId, did, consumerAddress");const n=await this.getEndpoints(t),r=await this.getServiceEndpoints(t,n),c=this.getEndpointURL(r,"computeStatus")?this.getEndpointURL(r,"computeStatus").urlPath:null;let h="?documentId="+at(i);if(h+=`&consumerAddress=${o}`,h+=s&&`&jobId=${s}`||"",!c)return null;try{const t=await a(c+h,{method:"GET",headers:{"Content-Type":"application/json"},signal:e});return null!=t&&t.ok?await t.json():(g.error("Get compute status failed:",t.status,t.statusText),null)}catch(t){return g.error("Get compute status failed"),g.error(t),null}}async computeResult(t,e,a,s,i,o){const n=await this.getEndpoints(s),r=await this.getServiceEndpoints(s,n),c=this.getEndpointURL(r,"computeResult")?this.getEndpointURL(r,"computeResult").urlPath:null,h=await this.getNonce(s,a,o,n,r);let l=a;l+=t,l+=String(e),l+=h;const d=await this.createHashSignature(i,a,l);let w=c;if(w+=`?consumerAddress=${a}`,w+=`&jobId=${t}`,w+=`&index=${String(e)}`,w+=d&&`&signature=${d}`||"",!c)return null;try{if(!document)return await v(w,e);await I(w)}catch(t){throw g.error("Error getting job result"),g.error(t),t}}async computeDelete(t,e,s,i,o,n){const r=await this.getEndpoints(i),c=await this.getServiceEndpoints(i,r),h=this.getEndpointURL(c,"computeDelete")?this.getEndpointURL(c,"computeDelete").urlPath:null,l=await this.getNonce(i,e,n,r,c);let d=e;d+=s||"",d+=t&&`${at(t)}`||"",d+=l;const w=await this.createHashSignature(o,e,d),m=Object();if(m.documentId=at(t),m.consumerAddress=e,m.jobId=s,w&&(m.signature=w),!h)return null;try{const t=await a(h,{method:"DELETE",body:JSON.stringify(m),headers:{"Content-Type":"application/json"},signal:n});return null!=t&&t.ok?await t.json():(g.error("Delete compute job failed:",t.status,t.statusText),g.error("Payload was:",m),null)}catch(t){return g.error("Delete compute job failed:"),g.error(t),g.error("Payload was:",m),null}}async isValidProvider(t,e){try{const s=await a(t,{method:"GET",headers:{"Content-Type":"application/json"},signal:e});if(null!=s&&s.ok){const t=await s.json();if(t&&t.providerAddress)return!0}return!1}catch(t){return g.error(`Error validating provider: ${t.message}`),!1}}}const it=new st;export{et as Config,U as ConfigHelper,K as Datatoken,Z as Dispenser,V as FixedRateCreateProgressStep,X as FixedRateExchange,N as GASLIMIT_DEFAULT,m as LogLevel,u as Logger,g as LoggerInstance,Q as Nft,tt as NftFactory,H as Pool,st as Provider,it as ProviderInstance,D as ZERO_ADDRESS,z as allowance,R as amountToUnits,j as approve,J as balance,O as configHelperNetworks,v as downloadFile,I as downloadFileBrowser,B as estApprove,k as fetchData,L as generateDid,f as generateDtName,S as getData,T as getErcCreationParams,A as getFairGasPrice,E as getFreCreationParams,P as getHash,C as getPoolCreationParams,G as postData,b as setContractDefaults,_ as signHash,W as signText,$ as signWithHash,F as unitsToAmount};
1
+ import t from"web3";import e from"bignumber.js";import a from"cross-fetch";import s from"@oceanprotocol/contracts/addresses/address.json";import i from"crypto-js/sha256";import o from"decimal.js";import n from"@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json";import r from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json";import c from"@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json";import h from"@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json";import l from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json";import d from"@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json";import w from"@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json";var m;!function(t){t[t.None=-1]="None",t[t.Error=0]="Error",t[t.Warn=1]="Warn",t[t.Log=2]="Log",t[t.Verbose=3]="Verbose"}(m||(m={}));class u{constructor(t=m.Error){this.logLevel=void 0,this.logLevel=t}setLevel(t){this.logLevel=t}bypass(...t){this.dispatch("log",-Infinity,...t)}debug(...t){this.dispatch("debug",m.Verbose,...t)}log(...t){this.dispatch("log",m.Log,...t)}warn(...t){this.dispatch("warn",m.Warn,...t)}error(...t){this.dispatch("error",m.Error,...t)}dispatch(t,e,...a){this.logLevel>=e&&console[t](...a)}}const g=new u;var p={nouns:["Crab","Fish","Seal","Octopus","Shark","Seahorse","Walrus","Starfish","Whale","Orca","Penguin","Jellyfish","Squid","Lobster","Pelican","Shrimp","Oyster","Clam","Seagull","Dolphin","Shell","Cormorant","Otter","Anemone","Turtle","Coral","Ray","Barracuda","Krill","Anchovy","Angelfish","Barnacle","Clownfish","Cod","Cuttlefish","Eel","Fugu","Herring","Haddock","Ling","Mackerel","Manatee","Narwhal","Nautilus","Plankton","Porpoise","Prawn","Pufferfish","Swordfish","Tuna"],adjectives:["adamant","adroit","amatory","ambitious","amused","animistic","antic","arcadian","artistic","astonishing","astounding","baleful","bellicose","bilious","blissful","boorish","brave","breathtaking","brilliant","calamitous","caustic","cerulean","clever","charming","comely","competent","concomitant","confident","contumacious","corpulent","crapulous","creative","dazzling","dedicated","defamatory","delighted","delightful","determined","didactic","dilatory","dowdy","efficacious","effulgent","egregious","empowered","endemic","enthusiastic","equanimous","exceptional","execrable","fabulous","fantastic","fastidious","feckless","fecund","friable","fulsome","garrulous","generous","gentle","guileless","gustatory","heuristic","histrionic","hubristic","incendiary","incredible","insidious","insolent","inspired","intransigent","inveterate","invidious","invigorated","irksome","jejune","juicy","jocular","joyful","judicious","kind","lachrymose","limpid","loquacious","lovely","luminous","mannered","marvelous","mendacious","meretricious","minatory","mordant","motivated","munificent","nefarious","noxious","obtuse","optimistic","parsimonious","pendulous","pernicious","pervasive","petulant","passionate","phenomenal","platitudinous","pleasant","powerful","precipitate","propitious","puckish","querulous","quiescent","rebarbative","recalcitant","redolent","rhadamanthine","risible","ruminative","sagacious","salubrious","sartorial","sclerotic","serpentine","smart","spasmodic","strident","stunning","stupendous","taciturn","tactful","tasty","tenacious","tremendous","tremulous","trenchant","turbulent","turgid","ubiquitous","uxorious","verdant","vibrant","voluble","voracious","wheedling","withering","wonderful","zealous"]};function f(t){const e=t||p,a=Math.floor(Math.random()*e.adjectives.length),s=Math.floor(Math.random()*e.nouns.length),i=Math.floor(100*Math.random()),o=e.adjectives[a].replace(/^\w/,t=>t.toUpperCase()),n=e.nouns[s].replace(/^\w/,t=>t.toUpperCase());return{name:`${o} ${n} Token`,symbol:`${(o.substring(0,3)+n.substring(0,3)).toUpperCase()}-${i}`}}const y=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];function b(t,e){return e&&(e.transactionBlockTimeout&&(t.transactionBlockTimeout=e.transactionBlockTimeout),e.transactionConfirmationBlocks&&(t.transactionConfirmationBlocks=e.transactionConfirmationBlocks),e.transactionPollingTimeout&&(t.transactionPollingTimeout=e.transactionPollingTimeout)),t}async function A(t,a){const s=new e(await t.eth.getGasPrice());return a&&a.gasFeeMultiplier?s.multipliedBy(a.gasFeeMultiplier).integerValue(e.ROUND_DOWN).toString(10):s.toString(10)}function T(e){let a,s;return e.name&&e.symbol||({name:a,symbol:s}=f()),{templateIndex:e.templateIndex,strings:[e.name||a,e.symbol||s],addresses:[e.minter,e.feeManager,e.mpFeeAddress,e.feeToken],uints:[t.utils.toWei(e.cap),t.utils.toWei(e.feeAmount)],bytess:[]}}function E(t){return t.allowedConsumer||(t.allowedConsumer="0x0000000000000000000000000000000000000000"),{fixedPriceAddress:t.fixedRateAddress,addresses:[t.baseTokenAddress,t.owner,t.marketFeeCollector,t.allowedConsumer],uints:[t.baseTokenDecimals,t.datatokenDecimals,t.fixedRate,t.marketFee,t.withMint?1:0]}}function C(e){return{addresses:[e.ssContract,e.baseTokenAddress,e.baseTokenSender,e.publisherAddress,e.marketFeeCollector,e.poolTemplateAddress],ssParams:[t.utils.toWei(e.rate),e.baseTokenDecimals,t.utils.toWei(e.vestingAmount),e.vestedBlocks,t.utils.toWei(e.initialBaseTokenLiquidity)],swapFees:[e.swapFeeLiquidityProvider,e.swapFeeMarketRunner]}}async function R(t,a,s){try{const i=new t.eth.Contract(y,a);let o=await i.methods.decimals().call();"0"===o&&(o=18);const n=new e(parseInt(s)/10**o);return e.config({EXPONENTIAL_AT:50}),n.toString()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}}async function F(t,a,s){try{const i=new t.eth.Contract(y,a);let o=await i.methods.decimals().call();"0"===o&&(o=18);const n=new e(parseInt(s)*10**o);return e.config({EXPONENTIAL_AT:50}),n.toString()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}}async function k(t,e){const s=await a(t,e);if(!s.ok)throw g.error(`Error requesting [${e.method}] ${t}`),g.error(`Response message: \n${await s.text()}`),s;return s}async function I(t){const e=document.createElement("a");e.download="",e.href=t,e.click()}async function v(t,e){const s=await a(t);if(!s.ok)throw new Error("Response error.");let i;try{i=s.headers.get("content-disposition").match(/attachment;filename=(.+)/)[1]}catch(a){try{i=t.split("/").pop()}catch(t){i=`file${e}`}}return{data:await s.arrayBuffer(),filename:i}}async function S(t){return a(t,{method:"GET",headers:{"Content-type":"application/json"}})}async function G(t,e){return async function(t,e,s){return a(t,null!=e?{method:"POST",body:e,headers:{"Content-type":"application/json"}}:{method:"POST"})}(t,e)}function x(){return x=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var s in a)Object.prototype.hasOwnProperty.call(a,s)&&(t[s]=a[s])}return t},x.apply(this,arguments)}const M={chainId:null,network:"unknown",metadataCacheUri:"https://aquarius.oceanprotocol.com",nodeUri:"http://localhost:8545",providerUri:"http://127.0.0.1:8030",subgraphUri:null,explorerUri:null,oceanTokenAddress:null,oceanTokenSymbol:"OCEAN",poolTemplateAddress:null,fixedRateExchangeAddress:null,dispenserAddress:null,startBlock:0,transactionBlockTimeout:50,transactionConfirmationBlocks:1,transactionPollingTimeout:750,gasFeeMultiplier:1},O=[x({},M),x({},M,{chainId:8996,network:"development",metadataCacheUri:"http://127.0.0.1:5000"}),x({},M,{chainId:3,network:"ropsten",nodeUri:"https://ropsten.infura.io/v3",providerUri:"https://provider.ropsten.oceanprotocol.com",subgraphUri:"https://subgraph.ropsten.oceanprotocol.com",explorerUri:"https://ropsten.etherscan.io",startBlock:9227563}),x({},M,{chainId:4,network:"rinkeby",nodeUri:"https://rinkeby.infura.io/v3",providerUri:"https://providerv4.rinkeby.oceanprotocol.com",subgraphUri:"https://subgraphv4.rinkeby.oceanprotocol.com",explorerUri:"https://rinkeby.etherscan.io",startBlock:7294090}),x({},M,{chainId:1,network:"mainnet",nodeUri:"https://mainnet.infura.io/v3",providerUri:"https://provider.mainnet.oceanprotocol.com",subgraphUri:"https://subgraph.mainnet.oceanprotocol.com",explorerUri:"https://etherscan.io",startBlock:11105459,transactionBlockTimeout:150,transactionConfirmationBlocks:5,transactionPollingTimeout:1750,gasFeeMultiplier:1.05}),x({},M,{chainId:137,network:"polygon",nodeUri:"https://polygon-mainnet.infura.io/v3",providerUri:"https://provider.polygon.oceanprotocol.com",subgraphUri:"https://subgraph.polygon.oceanprotocol.com",explorerUri:"https://polygonscan.com",oceanTokenSymbol:"mOCEAN",startBlock:11005222}),x({},M,{chainId:1287,network:"moonbeamalpha",nodeUri:"https://rpc.testnet.moonbeam.network",providerUri:"https://provider.moonbeamalpha.oceanprotocol.com",subgraphUri:"https://subgraph.moonbeamalpha.oceanprotocol.com",explorerUri:"https://moonbase-blockscout.testnet.moonbeam.network/",startBlock:90707}),x({},M,{chainId:2021e3,network:"gaiaxtestnet",nodeUri:"https://rpc.gaiaxtestnet.oceanprotocol.com",providerUri:"https://provider.gaiaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.gaiaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.gaiaxtestnet.oceanprotocol.com"}),x({},M,{chainId:2021001,network:"catenaxtestnet",nodeUri:"https://rpc.catenaxtestnet.oceanprotocol.com",providerUri:"https://provider.catenaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.catenaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.catenaxtestnet.oceanprotocol.com",metadataCacheUri:"https://aquarius.catenaxtestnet.oceanprotocol.com"}),x({},M,{chainId:80001,network:"mumbai",nodeUri:"https://polygon-mumbai.infura.io/v3",providerUri:"https://provider.mumbai.oceanprotocol.com",subgraphUri:"https://subgraph.mumbai.oceanprotocol.com",explorerUri:"https://mumbai.polygonscan.com"}),x({},M,{chainId:56,network:"bsc",nodeUri:"https://bsc-dataseed.binance.org",providerUri:"https://provider.bsc.oceanprotocol.com",subgraphUri:"https://subgraph.bsc.oceanprotocol.com",explorerUri:"https://bscscan.com/",gasFeeMultiplier:1.05}),x({},M,{chainId:44787,network:"celoalfajores",nodeUri:"https://alfajores-forno.celo-testnet.org",providerUri:"https://provider.celoalfajores.oceanprotocol.com",subgraphUri:"https://subgraph.celoalfajores.oceanprotocol.com",explorerUri:"https://alfajores-blockscout.celo-testnet.org"}),x({},M,{chainId:246,network:"energyweb",nodeUri:"https://rpc.energyweb.org",providerUri:"https://provider.energyweb.oceanprotocol.com",subgraphUri:"https://subgraph.energyweb.oceanprotocol.com",explorerUri:"https://explorer.energyweb.org",gasFeeMultiplier:1.05}),x({},M,{chainId:1285,network:"moonriver",nodeUri:"https://moonriver.api.onfinality.io/public",providerUri:"https://provider.moonriver.oceanprotocol.com",subgraphUri:"https://subgraph.moonriver.oceanprotocol.com",explorerUri:"https://blockscout.moonriver.moonbeam.network",gasFeeMultiplier:1.05})];class U{getAddressesFromEnv(t,e){let a;if(e)try{const{FixedPrice:s,Dispenser:i,Staking:o,poolTemplate:n,ERC721Factory:r,OPFCommunityFeeCollector:c,Ocean:h,chainId:l,startBlock:d}=e[t];a=x({erc721FactoryAddress:r,sideStakingAddress:o,opfCommunityFeeCollector:c,poolTemplateAddress:n,fixedRateExchangeAddress:s,dispenserAddress:i,oceanTokenAddress:h,chainId:l,startBlock:d},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}catch(t){}else if(s[t]){const{FixedPrice:e,Dispenser:i,Staking:o,poolTemplate:n,OPFCommunityFeeCollector:r,ERC721Factory:c,Ocean:h,chainId:l,startBlock:d}=s[t];a=x({erc721FactoryAddress:c,sideStakingAddress:o,opfCommunityFeeCollector:r,poolTemplateAddress:n,fixedRateExchangeAddress:e,dispenserAddress:i,oceanTokenAddress:h,chainId:l,startBlock:d},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}return a}getConfig(t,e){const a="string"==typeof t?"network":"chainId";let s=O.find(e=>e[a]===t);return s?(s=x({},s,this.getAddressesFromEnv(s.network)),x({},s,{nodeUri:e?`${s.nodeUri}/${e}`:s.nodeUri})):(g.error(`No config found for given network '${t}'`),null)}}function L(e,a){return e=t.utils.toChecksumAddress(e),`did:op:${i(e+a.toString(10)).toString()}`}function P(t){return i(t).toString()}const D="0x0000000000000000000000000000000000000000",N=1e6;async function W(t,e,a,s){const i=t&&t.currentProvider&&t.currentProvider.isMetaMask;try{return await t.eth.personal.sign(e,a,s)}catch(s){if(i)throw s;g.warn("Error on personal sign."),g.warn(s);try{return await t.eth.sign(e,a)}catch(t){throw g.error("Error on sign."),g.error(t),new Error("Error executing personal sign")}}}async function _(t,e,a){let s=await t.eth.sign(e,a);s=s.substr(2);const i="0x"+s.slice(0,64),o="0x"+s.slice(64,128);let n="0x"+s.slice(128,130);return"0x00"===n&&(n="0x1b"),"0x01"===n&&(n="0x1c"),{v:n,r:i,s:o}}async function $(t,e,a,s){const i=t.utils.utf8ToHex(e),o=t&&t.currentProvider&&t.currentProvider.isMetaMask;try{return await t.eth.personal.sign(i,a,s)}catch(e){if(o)throw e;g.warn("Error on personal sign."),g.warn(e);try{return await t.eth.sign(i,a)}catch(t){throw g.error("Error on sign."),g.error(t),new Error("Error executing personal sign")}}}async function B(t,e,a,s,i,o){const n=o||new t.eth.Contract(y,a),r=1e6;let c;try{c=await n.methods.approve(s,i).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){c=r,g.error("estimate gas failed for approve!",t)}return c}async function j(t,e,a,s,i,n=!1){const r=new t.eth.Contract(y,a);if(!n){const n=await z(t,a,e,s);if(new o(n).greaterThanOrEqualTo(new o(i)))return n}let c=null;const h=await F(t,a,i),l=await B(t,e,a,s,h,r);try{c=await r.methods.approve(s,h).send({from:e,gas:l+1,gasPrice:await A(t,null)})}catch(t){g.error(`ERRPR: Failed to approve spender to spend tokens : ${t.message}`)}return c}async function z(t,e,a,s){const i=new t.eth.Contract(y,e),o=await i.methods.allowance(a,s).call();return await R(t,e,o)}async function J(t,e,a){const s=new t.eth.Contract(y,e),i=await s.methods.balanceOf(a).call();return await R(t,e,i)}const q="115792089237316195423570985008687907853269984665640564039457584007913129639934";class H{constructor(t,e,a=null,s){this.poolAbi=void 0,this.web3=void 0,this.GASLIMIT_DEFAULT=1e6,this.logger=void 0,this.config=void 0,this.poolAbi=a||n.abi,this.web3=t,this.logger=e,this.config=s||O[0]}async sharesBalance(t,e){let a=null;try{const s=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=await s.methods.balanceOf(t).call();a=this.web3.utils.fromWei(i)}catch(t){this.logger.error(`ERROR: Failed to get shares of pool : ${t.message}`)}return a}async estSetSwapFee(t,e,a,s){const i=s||b(new this.web3.eth.Contract(r.abi,e),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setSwapFee(a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setSwapFee(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,e,{from:t}),this.config);let i=null;const o=await this.estSetSwapFee(t,e,a);try{i=await s.methods.setSwapFee(this.web3.utils.toWei(a)).send({from:t,gas:o,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to set pool swap fee: ${t.message}`)}return i}async getNumTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getNumTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get number of tokens: ${t.message}`)}return a}async getPoolSharesTotalSupply(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.totalSupply().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get total supply of pool shares: ${t.message}`)}return a}async getCurrentTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getCurrentTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get tokens composing this pool: ${t.message}`)}return a}async getFinalTokens(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getFinalTokens().call()}catch(t){this.logger.error("ERROR: Failed to get the final tokens composing this pool")}return a}async getController(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getController().call()}catch(t){this.logger.error(`ERROR: Failed to get pool controller address: ${t.message}`)}return a}async getBaseToken(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getBaseTokenAddress().call()}catch(t){this.logger.error(`ERROR: Failed to get baseToken address: ${t.message}`)}return a}async getDatatoken(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.getDatatokenAddress().call()}catch(t){this.logger.error(`ERROR: Failed to get datatoken address: ${t.message}`)}return a}async getMarketFeeCollector(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods._publishMarketCollector().call()}catch(t){this.logger.error(`ERROR: Failed to get marketFeeCollector address: ${t.message}`)}return a}async getOPCCollector(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods._opcCollector().call()}catch(t){this.logger.error(`ERROR: Failed to get OPF Collector address: ${t.message}`)}return a}async isBound(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{s=await a.methods.isBound(e).call()}catch(t){this.logger.error(`ERROR: Failed to check whether a token bounded to a pool. ${t.message}`)}return s}async getReserve(t,e){let a=null;try{const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),i=await s.methods.getBalance(e).call();a=await R(this.web3,e,i)}catch(t){this.logger.error(`ERROR: Failed to get how many tokens are in the pool: ${t.message}`)}return a.toString()}async isFinalized(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{a=await e.methods.isFinalized().call()}catch(t){this.logger.error(`ERROR: Failed to check whether pool is finalized: ${t.message}`)}return a}async getSwapFee(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.getSwapFee().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get pool fee: ${t.message}`)}return a}async getNormalizedWeight(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.getNormalizedWeight(e).call();s=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get normalized weight of a token: ${t.message}`)}return s}async getDenormalizedWeight(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.getDenormalizedWeight(e).call();s=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get denormalized weight of a token in pool")}return s}async getTotalDenormalizedWeight(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let a=null;try{const t=await e.methods.getTotalDenormalizedWeight().call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get total denormalized weight in pool")}return a}async getMarketFees(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.publishMarketFees(e).call();s=await R(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to get market fees for a token: ${t.message}`)}return s}async getCurrentMarketFees(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);try{return await e.methods.getCurrentOPCFees().call()}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}}async getCurrentOPCFees(t){const e=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);try{return await e.methods.getCurrentOPCFees().call()}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}}async getCommunityFees(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let s=null;try{const t=await a.methods.communityFees(e).call();s=await R(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to get community fees for a token: ${t.message}`)}return s}async estCollectOPC(t,e,a){const s=a||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectOPC().estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectOPC(t,e){const a=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let s=null;const i=await this.estCollectOPC(t,e);try{s=await a.methods.collectOPC().send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return s}async estCollectMarketFee(t,e,a){const s=a||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee().estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectMarketFee(t,e){if(await this.getMarketFeeCollector(e)!==t)throw new Error("Caller is not MarketFeeCollector");const a=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let s=null;const i=await this.estCollectMarketFee(t,e);try{s=await a.methods.collectMarketFee().send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return s}async estUpdateMarketFeeCollector(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFeeCollector(a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFeeCollector(t,e,a){if(await this.getMarketFeeCollector(e)!==t)throw new Error("Caller is not MarketFeeCollector");const s=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let i=null;const o=await this.estUpdateMarketFeeCollector(t,e,a);try{i=await s.methods.updateMarketFeeCollector(a).send({from:t,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return i}async estSwapExactAmountIn(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q,r=this.GASLIMIT_DEFAULT;let c;try{c=await o.methods.swapExactAmountIn([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.tokenAmountIn,s.minAmountOut,n,this.web3.utils.toWei(s.swapMarketFee)]).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async swapExactAmountIn(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);s.tokenAmountIn=await F(this.web3,a.tokenIn,s.tokenAmountIn),s.minAmountOut=await F(this.web3,a.tokenOut,s.minAmountOut);let o=null;const n=await this.estSwapExactAmountIn(t,e,a,s),r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;try{o=await i.methods.swapExactAmountIn([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.tokenAmountIn,s.minAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return o}async estSwapExactAmountOut(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT,r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;let c;try{c=await o.methods.swapExactAmountOut([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.maxAmountIn,s.tokenAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){c=n}return c}async swapExactAmountOut(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let o=null;s.maxAmountIn=await F(this.web3,a.tokenIn,s.maxAmountIn),s.tokenAmountOut=await F(this.web3,a.tokenOut,s.tokenAmountOut);const n=await this.estSwapExactAmountOut(t,e,a,s),r=s.maxPrice?this.web3.utils.toWei(s.maxPrice):q;try{o=await i.methods.swapExactAmountOut([a.tokenIn,a.tokenOut,a.marketFeeAddress],[s.maxAmountIn,s.tokenAmountOut,r,this.web3.utils.toWei(s.swapMarketFee)]).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount out: ${t.message}`)}return o}async estJoinPool(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.joinPool(a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async joinPool(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=[],n=await this.getFinalTokens(e);for(let t=0;t<2;t++){const e=await F(this.web3,n[t],s[t]);o.push(e)}let r=null;const c=await this.estJoinPool(t,e,this.web3.utils.toWei(a),o);try{r=await i.methods.joinPool(this.web3.utils.toWei(a),o).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to join pool: ${t.message}`)}return r}async estExitPool(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.exitPool(a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async exitPool(t,e,a,s){const i=b(new this.web3.eth.Contract(this.poolAbi,e),this.config),o=[],n=await this.getFinalTokens(e);for(let t=0;t<2;t++){const e=await F(this.web3,n[t],s[t]);o.push(e)}let r=null;const c=await this.estExitPool(t,e,this.web3.utils.toWei(a),o);try{r=await i.methods.exitPool(this.web3.utils.toWei(a),o).send({from:t,gas:c,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to exit pool: ${t.message}`)}return r}async estJoinswapExternAmountIn(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.joinswapExternAmountIn(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async joinswapExternAmountIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await F(this.web3,a,s),c=await this.estJoinswapExternAmountIn(t,e,a,r,this.web3.utils.toWei(i));try{n=await o.methods.joinswapExternAmountIn(a,r,this.web3.utils.toWei(i)).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay tokens in order to join the pool: ${t.message}`)}return n}async estJoinswapPoolAmountOut(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.joinswapPoolAmountOut(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async joinswapPoolAmountOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await F(this.web3,a,i),c=await this.estJoinswapPoolAmountOut(t,e,a,this.web3.utils.toWei(s),r);try{n=await o.methods.joinswapPoolAmountOut(a,this.web3.utils.toWei(s),r).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to join swap pool amount out")}return n}async estExitswapPoolAmountIn(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.exitswapPoolAmountIn(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async exitswapPoolAmountIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await F(this.web3,a,i),c=await this.estExitswapPoolAmountIn(t,e,a,this.web3.utils.toWei(s),r);try{n=await o.methods.exitswapPoolAmountIn(a,this.web3.utils.toWei(s),r).send({from:t,gas:c+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${t.message}`)}return n}async estExitswapExternAmountOut(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.poolAbi,e),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.exitswapExternAmountOut(a,s,i).estimateGas({from:t},(t,e)=>t?r:e)}catch(t){c=r}return c}async exitswapExternAmountOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,e),this.config);let n=null;const r=await this.estExitswapExternAmountOut(t,e,a,this.web3.utils.toWei(s),this.web3.utils.toWei(i));try{n=await o.methods.exitswapExternAmountOut(a,this.web3.utils.toWei(s),this.web3.utils.toWei(i)).send({from:t,gas:r+1,gasPrice:await A(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to exitswapExternAmountOut")}return n}async getSpotPrice(t,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let n=18,c=18;const h=b(new this.web3.eth.Contract(r.abi,a),this.config),l=b(new this.web3.eth.Contract(r.abi,s),this.config);try{n=await h.methods.decimals().call()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}try{c=await l.methods.decimals().call()}catch(t){this.logger.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}let d,w=null;try{w=await o.methods.getSpotPrice(a,s,this.web3.utils.toWei(i)).call(),w=new e(w.toString())}catch(t){this.logger.error("ERROR: Failed to get spot price of swapping tokenIn to tokenOut")}return n>c?(d=n-c,w=new e(w/10**d),w/=10**c):(d=c-n,w=new e(w*10**(2*d)),w/=10**c),w.toString()}async getAmountInExactOut(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),n=await F(this.web3,a,s);let r=null;try{const t=await o.methods.getAmountInExactOut(e,a,n,this.web3.utils.toWei(i)).call();r=await R(this.web3,e,t)}catch(t){this.logger.error("ERROR: Failed to calcInGivenOut")}return r}async getAmountOutExactIn(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.poolAbi,t),this.config),n=await F(this.web3,e,s);let r=null;try{const t=await o.methods.getAmountOutExactIn(e,a,n,this.web3.utils.toWei(i)).call();r=await R(this.web3,a,t)}catch(t){this.logger.error("ERROR: Failed to calcOutGivenIn")}return r}async calcPoolOutGivenSingleIn(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcPoolOutSingleIn(e,await F(this.web3,e,a)).call();i=await R(this.web3,t,o)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${t.message}`)}return i}async calcSingleInGivenPoolOut(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;const o=await F(this.web3,t,a);try{const t=await s.methods.calcSingleInPoolOut(e,o).call();i=await R(this.web3,e,t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${t.message}`)}return i}async calcSingleOutGivenPoolIn(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcSingleOutPoolIn(e,await F(this.web3,t,a)).call();i=await R(this.web3,e,o)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${t}`)}return i}async calcPoolInGivenSingleOut(t,e,a){const s=b(new this.web3.eth.Contract(this.poolAbi,t),this.config);let i=null;try{const o=await s.methods.calcPoolInSingleOut(e,await F(this.web3,e,a)).call();i=await R(this.web3,t,o)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${t.message}`)}return i}getSwapEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_SWAP"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getJoinEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_JOIN"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getExitEventSignature(){const t=this.poolAbi.find(function(t){if("LOG_EXIT"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}}class Z{constructor(t,e=null,a=null,s){this.GASLIMIT_DEFAULT=1e6,this.web3=null,this.dispenserAddress=void 0,this.config=void 0,this.dispenserAbi=void 0,this.dispenserContract=void 0,this.web3=t,this.dispenserAddress=e,this.dispenserAbi=a||c.abi,this.config=s||O[0],t&&(this.dispenserContract=b(new this.web3.eth.Contract(this.dispenserAbi,this.dispenserAddress),this.config))}async status(t){try{const e=await this.dispenserContract.methods.status(t).call();return e.maxTokens=this.web3.utils.fromWei(e.maxTokens),e.maxBalance=this.web3.utils.fromWei(e.maxBalance),e.balance=this.web3.utils.fromWei(e.balance),e}catch(e){g.warn(`No dispenser available for datatoken: ${t}`)}return null}async estGasCreate(t,e,a,s,i){const o=this.GASLIMIT_DEFAULT;let n;try{n=await this.dispenserContract.methods.create(t,this.web3.utils.toWei(a),this.web3.utils.toWei(s),e,i).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async create(t,e,a,s,i){const o=await this.estGasCreate(t,e,a,s,i);return await this.dispenserContract.methods.create(t,this.web3.utils.toWei(a),this.web3.utils.toWei(s),e,i).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasActivate(t,e,a,s){let i;const o=this.GASLIMIT_DEFAULT;try{i=await this.dispenserContract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(a)).estimateGas({from:s},(t,e)=>t?o:e)}catch(t){i=o}return i}async activate(t,e,a,s){try{const i=await this.estGasActivate(t,e,a,s);return await this.dispenserContract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(a)).send({from:s,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasDeactivate(t,e){let a;const s=this.GASLIMIT_DEFAULT;try{a=await this.dispenserContract.methods.deactivate(t).estimateGas({from:e},(t,e)=>t?s:e)}catch(t){a=s}return a}async deactivate(t,e){try{const a=await this.estGasDeactivate(t,e);return await this.dispenserContract.methods.deactivate(t).send({from:e,gas:a+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasSetAllowedSwapper(t,e,a){let s;const i=this.GASLIMIT_DEFAULT;try{s=await this.dispenserContract.methods.setAllowedSwapper(t,a).estimateGas({from:e},(t,e)=>t?i:e)}catch(t){s=i}return s}async setAllowedSwapper(t,e,a){try{const s=await this.estGasSetAllowedSwapper(t,e,a);return await this.dispenserContract.methods.setAllowedSwapper(t,a).send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return null}async estGasDispense(t,e,a="1",s){let i;const o=this.GASLIMIT_DEFAULT;try{i=await this.dispenserContract.methods.dispense(t,this.web3.utils.toWei(a),s).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){i=o}return i}async dispense(t,e,a="1",s){const i=await this.estGasDispense(t,e,a,s);try{return await this.dispenserContract.methods.dispense(t,this.web3.utils.toWei(a),s).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to dispense tokens: ${t.message}`)}return null}async estGasOwnerWithdraw(t,e){let a;const s=this.GASLIMIT_DEFAULT;try{a=await this.dispenserContract.methods.ownerWithdraw(t).estimateGas({from:e},(t,e)=>t?s:e)}catch(t){a=s}return a}async ownerWithdraw(t,e){const a=await this.estGasOwnerWithdraw(t,e);try{return await this.dispenserContract.methods.ownerWithdraw(t).send({from:e,gas:a+1,gasPrice:await A(this.web3,this.config)})}catch(t){g.error(`ERROR: Failed to withdraw tokens: ${t.message}`)}return null}async isDispensable(t,e,a,s="1"){const i=await this.status(t);return!(!i||!1===i.active||new o(await e.balance(t,a)).greaterThanOrEqualTo(i.maxBalance)||new o(String(s)).greaterThan(i.maxTokens)||!new o(i.balance).greaterThanOrEqualTo(s)&&!0!==i.isMinter)}}var V;!function(t){t[t.CreatingExchange=0]="CreatingExchange",t[t.ApprovingDatatoken=1]="ApprovingDatatoken"}(V||(V={}));class X{constructor(t,e,a=null,s=null,i){this.GASLIMIT_DEFAULT=1e6,this.oceanAddress=null,this.fixedRateAddress=void 0,this.fixedRateExchangeAbi=void 0,this.fixedRateContract=void 0,this.web3=void 0,this.contract=null,this.config=void 0,this.ssAbi=void 0,this.web3=t,this.config=i||O[0],this.fixedRateExchangeAbi=a||h.abi,this.oceanAddress=s,this.fixedRateAddress=e,this.contract=b(new this.web3.eth.Contract(this.fixedRateExchangeAbi,this.fixedRateAddress),this.config)}async amountToUnits(t,a){let s=18;const i=b(new this.web3.eth.Contract(r.abi,t),this.config);try{s=await i.methods.decimals().call()}catch(t){g.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}return new e(parseInt(a)*10**s).toString()}async unitsToAmount(t,a){let s=18;const i=b(new this.web3.eth.Contract(r.abi,t),this.config);try{s=await i.methods.decimals().call()}catch(t){g.error("ERROR: FAILED TO CALL DECIMALS(), USING 18")}return new e(parseInt(a)/10**s).toString()}async generateExchangeId(t,e,a){return await this.contract.methods.generateExchangeId(t,e,a).call()}async estBuyDT(t,e,a,s,i){const o=i||this.fixedRateContract,n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyDT(e,a.toString(),s.toString()).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyDT(t,e,a,s){const i=await this.amountToUnits((await this.getExchange(e)).datatoken,a),o=await this.amountToUnits((await this.getExchange(e)).baseToken,s),n=await this.estBuyDT(t,e,i,o);try{return await this.contract.methods.buyDT(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){return g.error(`ERROR: Failed to buy datatokens: ${t.message}`),null}}async estSellDT(t,e,a,s,i){const o=i||this.fixedRateContract,n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.sellDT(e,a,s).estimateGas({from:t},(t,e)=>t?n:e)}catch(t){r=n}return r}async sellDT(t,e,a,s){const i=await this.amountToUnits((await this.getExchange(e)).datatoken,a),o=await this.amountToUnits((await this.getExchange(e)).baseToken,s),n=await this.estBuyDT(t,e,i,o);try{return await this.contract.methods.sellDT(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){return g.error(`ERROR: Failed to sell datatokens: ${t.message}`),null}}async getNumberOfExchanges(){return await this.contract.methods.getNumberOfExchanges().call()}async estSetRate(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setRate(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setRate(t,e,a){const s=await this.estSetRate(t,e,this.web3.utils.toWei(String(a)));return await this.contract.methods.setRate(e,this.web3.utils.toWei(a)).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estSetAllowedSwapper(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setRate(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async setAllowedSwapper(t,e,a){const s=await this.estSetAllowedSwapper(t,e,a);return await this.contract.methods.setAllowedSwapper(e,a).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estActivate(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleExchangeState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async activate(t,e){const a=await this.getExchange(e);if(!a)return null;if(!0===a.active)return null;const s=await this.estActivate(t,e);return await this.contract.methods.toggleExchangeState(e).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estDeactivate(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleExchangeState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async deactivate(t,e){const a=await this.getExchange(e);if(!a)return null;if(!1===a.active)return null;const s=await this.estDeactivate(t,e);return await this.contract.methods.toggleExchangeState(e).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async getRate(t){const e=await this.contract.methods.getRate(t).call();return this.web3.utils.fromWei(e)}async getDTSupply(t){const e=await this.contract.methods.getDTSupply(t).call();return await this.unitsToAmount((await this.getExchange(t)).datatoken,e)}async getBTSupply(t){const e=await this.contract.methods.getBTSupply(t).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,e)}async getAllowedSwapper(t){return await this.contract.methods.getAllowedSwapper(t).call()}async getAmountBTIn(t,e){const a=await this.contract.methods.calcBaseInGivenOutDT(t,await this.amountToUnits((await this.getExchange(t)).datatoken,e)).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,a.baseTokenAmount)}async getAmountBTOut(t,e){const a=await this.contract.methods.calcBaseOutGivenInDT(t,await this.amountToUnits((await this.getExchange(t)).datatoken,e)).call();return await this.unitsToAmount((await this.getExchange(t)).baseToken,a.baseTokenAmount)}async getExchange(t){const e=await this.contract.methods.getExchange(t).call();return e.dtDecimals=e.dtDecimals.toString(),e.btDecimals=e.btDecimals.toString(),e.dtBalance=await this.unitsToAmount(e.datatoken,e.dtBalance),e.btBalance=await this.unitsToAmount(e.baseToken,e.btBalance),e.dtSupply=await this.unitsToAmount(e.datatoken,e.dtSupply),e.btSupply=await this.unitsToAmount(e.baseToken,e.btSupply),e.fixedRate=this.web3.utils.fromWei(e.fixedRate),e.exchangeId=t,e}async getFeesInfo(t){const e=await this.contract.methods.getFeesInfo(t).call();return e.opcFee=this.web3.utils.fromWei(e.opcFee.toString()),e.marketFee=this.web3.utils.fromWei(e.marketFee.toString()),e.marketFeeAvailable=await this.unitsToAmount((await this.getExchange(t)).baseToken,e.marketFeeAvailable),e.oceanFeeAvailable=await this.unitsToAmount((await this.getExchange(t)).baseToken,e.oceanFeeAvailable),e.exchangeId=t,e}async getExchanges(){return await this.contract.methods.getExchanges().call()}async isActive(t){return await this.contract.methods.isActive(t).call()}async estActivateMint(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleMintState(e,!0).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async activateMint(t,e){const a=await this.getExchange(e);if(!a)return null;if(!0===a.withMint)return null;const s=await this.estActivateMint(t,e);return await this.contract.methods.toggleMintState(e,!0).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estDeactivateMint(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.toggleMintState(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async deactivateMint(t,e){const a=await this.getExchange(e);if(!a)return null;if(!1===a.withMint)return null;const s=await this.estDeactivate(t,e);return await this.contract.methods.toggleMintState(e,!1).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estCollectBT(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectBT(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectBT(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectBT(t,e);return await this.contract.methods.collectBT(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectDT(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectDT(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectDT(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectDT(t,e);return await this.contract.methods.collectDT(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectMarketFee(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectMarketFee(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectMarketFee(t,e);return await this.contract.methods.collectMarketFee(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estCollectOceanFee(t,e,a){const s=a||this.fixedRateContract,i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.collectMarketFee(e).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async collectOceanFee(t,e){if(!await this.getExchange(e))return null;const a=await this.estCollectOceanFee(t,e);return await this.contract.methods.collectOceanFee(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async getOPCCollector(){let t=null;try{t=await this.contract.methods.opcCollector().call()}catch(t){g.error(`ERROR: Failed to get OPC Collector address: ${t.message}`)}return t}async getRouter(){let t=null;try{t=await this.contract.methods.router().call()}catch(t){g.error(`ERROR: Failed to get Router address: ${t.message}`)}return t}async getExchangeOwner(t){let e=null;try{e=await(await this.getExchange(t)).exchangeOwner}catch(t){g.error(`ERROR: Failed to get OPF Collector address: ${t.message}`)}return e}async estUpdateMarketFee(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFee(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFee(t,e,a){const s=await this.estSetRate(t,e,this.web3.utils.toWei(a));return await this.contract.methods.updateMarketFee(e,this.web3.utils.toWei(a)).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estUpdateMarketFeeCollector(t,e,a,s){const i=s||this.fixedRateContract,o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.updateMarketFeeCollector(e,a).estimateGas({from:t},(t,e)=>t?o:e)}catch(t){n=o}return n}async updateMarketFeeCollector(t,e,a){const s=await this.estUpdateMarketFeeCollector(t,e,a);return await this.contract.methods.updateMarketFeeCollector(e,a).send({from:t,gas:s+1,gasPrice:await A(this.web3,this.config)})}}class Q{constructor(t,e,a){this.GASLIMIT_DEFAULT=1e6,this.factory721Address=void 0,this.factory721Abi=void 0,this.nftAbi=void 0,this.web3=void 0,this.startBlock=void 0,this.config=void 0,this.nftAbi=e||d.abi,this.web3=t,this.config=a||O[0]}async estGasCreateErc20(t,e,a,s,i,o,n,r,c,h,l,d){const w=d||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),m=this.GASLIMIT_DEFAULT;let u;try{u=await w.methods.createERC20(l,[c,h],[a,s,i,o],[this.web3.utils.toWei(r),this.web3.utils.toWei(n)],[]).estimateGas({from:e},(t,e)=>t?m:e)}catch(t){u=m}return u}async createErc20(t,e,a,s,i,o,n,r,c,h,l){if(!0!==(await this.getNftPermissions(t,e)).deployERC20)throw new Error("Caller is not ERC20Deployer");l||(l=1),c&&h||({name:c,symbol:h}=f());const d=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),w=await this.estGasCreateErc20(t,e,a,s,i,o,n,r,c,h,l,d),m=await d.methods.createERC20(l,[c,h],[a,s,i,o],[this.web3.utils.toWei(r),this.web3.utils.toWei(n)],[]).send({from:e,gas:w+1,gasPrice:await A(this.web3,this.config)});let u=null;try{u=m.events.TokenCreated.returnValues[0]}catch(t){g.error(`ERROR: Failed to create datatoken : ${t.message}`)}return u}async estGasAddManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addManager(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const i=await this.estGasAddManager(t,e,a,s);return await s.methods.addManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeManager(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const i=await this.estGasRemoveManager(t,e,a,s);return await s.methods.removeManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddErc20Deployer(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addToCreateERC20List(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addErc20Deployer(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddErc20Deployer(t,e,a,s);return await s.methods.addToCreateERC20List(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveErc20Deployer(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFromCreateErc20List(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeErc20Deployer(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e===a&&!0!==(await this.getNftPermissions(t,e)).deployERC20)throw new Error("Caller is not Manager nor ERC20Deployer");const i=await this.estGasRemoveErc20Deployer(t,e,a,s);return await s.methods.removeFromCreateERC20List(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddMetadataUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addToMetadataList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addMetadataUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddMetadataUpdater(t,e,a,s);return await s.methods.addToMetadataList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async esGasRemoveMetadataUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFromMetadataList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeMetadataUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e!==a&&!0!==(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Manager nor Metadata Updater");const i=await this.esGasRemoveMetadataUpdater(t,e,a,s);return await s.methods.removeFromMetadataList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddStoreUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addTo725StoreList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addStoreUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager)throw new Error("Caller is not Manager");const i=await this.estGasAddStoreUpdater(t,e,a,s);return await s.methods.addTo725StoreList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveStoreUpdater(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeFrom725StoreList(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeStoreUpdater(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!0!==(await this.getNftPermissions(t,e)).manager||e!==a&&!0!==(await this.getNftPermissions(t,e)).store)throw new Error("Caller is not Manager nor storeUpdater");const i=await this.estGasRemoveStoreUpdater(t,e,a,s);return await s.methods.removeFrom725StoreList(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCleanPermissions(t,e,a){const s=a||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.cleanPermissions().estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async cleanPermissions(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const s=await this.estGasCleanPermissions(t,e,a);return await a.methods.cleanPermissions().send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}async estGasTransferNft(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.transferFrom(e,a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async transferNft(t,e,a,s){const i=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const o=s||1,n=await this.estGasTransferNft(t,e,a,o,i);return await i.methods.transferFrom(e,a,o).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasSafeTransferNft(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.safeTransferFrom(e,a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async safeTransferNft(t,e,a,s){const i=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(await this.getNftOwner(t)!==e)throw new Error("Caller is not NFT Owner");const o=s||1,n=await this.estGasSafeTransferNft(t,e,a,o,i);return await i.methods.safeTransferFrom(e,a,o).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadata(t,e,a,s,i,o,n,r,c,h){const l=h||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),d=this.GASLIMIT_DEFAULT;let w;try{w=await l.methods.setMetaData(a,s,i,o,n,r,c).estimateGas({from:e},(t,e)=>t?d:e)}catch(t){w=d}return w}async setMetadata(t,e,a,s,i,o,n,r,c){const h=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(c||(c=[]),!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const l=await this.estGasSetMetadata(t,e,a,s,i,o,n,r,c,h);return await h.methods.setMetaData(a,s,i,o,n,r,c).send({from:e,gas:l+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadataAndTokenURI(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setMetaDataAndTokenURI(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setMetadataAndTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const i=await this.estGasSetMetadataAndTokenURI(t,e,a,s);return await s.methods.setMetaDataAndTokenURI(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasSetMetadataState(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.nftAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setMetaDataState(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setMetadataState(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);if(!(await this.getNftPermissions(t,e)).updateMetadata)throw new Error("Caller is not Metadata updater");const i=await this.estGasSetMetadataState(t,e,a);return await s.methods.setMetaDataState(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estSetTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.setTokenURI("1",a).estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async setTokenURI(t,e,a){const s=b(new this.web3.eth.Contract(this.nftAbi,t),this.config),i=await this.estSetTokenURI(t,e,a);return await s.methods.setTokenURI("1",a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async getNftOwner(t){const e=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await e.methods.ownerOf(1).call()}async getNftPermissions(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.getPermissions(e).call()}async getMetadata(t){const e=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await e.methods.getMetaData().call()}async isErc20Deployer(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.isERC20Deployer(e).call()}async getData(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.getData(e).call()}async getTokenURI(t,e){const a=b(new this.web3.eth.Contract(this.nftAbi,t),this.config);return await a.methods.tokenURI(e).call()}}class K{constructor(t,e,a,s){this.GASLIMIT_DEFAULT=1e6,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensAbi=void 0,this.datatokensEnterpriseAbi=void 0,this.web3=void 0,this.config=void 0,this.nft=void 0,this.web3=t,this.datatokensAbi=e||r.abi,this.datatokensEnterpriseAbi=a||l.abi,this.config=s||O[0],this.nft=new Q(this.web3)}async estGasApprove(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.approve(e,this.web3.utils.toWei(a)).estimateGas({from:s},(t,e)=>t?n:e)}catch(t){r=n}return r}async approve(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=await this.estGasApprove(t,e,a,s,i);return await i.methods.approve(e,this.web3.utils.toWei(a)).send({from:s,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasMint(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.mint(s||e,this.web3.utils.toWei(a)).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async estGasCreateFixedRate(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;a.allowedConsumer||(a.allowedConsumer="0x0000000000000000000000000000000000000000");const n=a.withMint?1:0;let r;try{r=await i.methods.createFixedRate(a.fixedRateAddress,[a.baseTokenAddress,e,a.marketFeeCollector,a.allowedConsumer],[a.baseTokenDecimals,a.datatokenDecimals,a.fixedRate,a.marketFee,n]).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){r=o}return r}async createFixedRate(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");a.allowedConsumer||(a.allowedConsumer="0x0000000000000000000000000000000000000000");const i=a.withMint?1:0,o=await this.estGasCreateFixedRate(t,e,a,s);return await s.methods.createFixedRate(a.fixedRateAddress,[a.baseTokenAddress,a.owner,a.marketFeeCollector,a.allowedConsumer],[a.baseTokenDecimals,a.datatokenDecimals,a.fixedRate,a.marketFee,i]).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateDispenser(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);s.allowedSwapper||(s.allowedSwapper="0x0000000000000000000000000000000000000000"),s.withMint||(s.withMint=!1);const n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.createDispenser(a,s.maxTokens,s.maxBalance,s.withMint,s.allowedSwapper).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async createDispenser(t,e,a,s){if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);s.allowedSwapper||(s.allowedSwapper="0x0000000000000000000000000000000000000000"),s.withMint||(s.withMint=!1);const o=await this.estGasCreateDispenser(t,e,a,s,i);return await i.methods.createDispenser(a,s.maxTokens,s.maxBalance,s.withMint,s.allowedSwapper).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}async mint(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==(await this.getDTPermissions(t,e)).minter)throw new Error("Caller is not Minter");const n=await this.getCap(t);if(new o(n).gte(a)){const o=await this.estGasMint(t,e,a,s,i);return await i.methods.mint(s||e,this.web3.utils.toWei(a)).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}throw new Error("Mint amount exceeds cap available")}async estGasAddMinter(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addMinter(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addMinter(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasAddMinter(t,e,a,s);return await s.methods.addMinter(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemoveMinter(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removeMinter(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removeMinter(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasRemoveMinter(t,e,a,s);return await s.methods.removeMinter(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasAddPaymentManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.addPaymentManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async addPaymentManager(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasAddPaymentManager(t,e,a,s);return await s.methods.addPaymentManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasRemovePaymentManager(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.removePaymentManager(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async removePaymentManager(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==await this.isERC20Deployer(t,e))throw new Error("Caller is not ERC20Deployer");const i=await this.estGasRemovePaymentManager(t,e,a,s);return await s.methods.removePaymentManager(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasSetPaymentCollector(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setPaymentCollector(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setPaymentCollector(t,e,a){const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);if(!0!==(await this.getDTPermissions(t,e)).paymentManager)throw new Error("Caller is not Fee Manager");const i=await this.estGasSetPaymentCollector(t,e,a,s);return await s.methods.setPaymentCollector(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async getPaymentCollector(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.getPaymentCollector().call()}async transfer(t,e,a,s){const i=this.web3.utils.toWei(a);return this.transferWei(t,e,i,s)}async estGasTransfer(t,e,a,s,i){const o=i||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.transfer(e,a).estimateGas({from:s},(t,e)=>t?n:e)}catch(t){r=n}return r}async transferWei(t,e,a,s){const i=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);try{const o=await this.estGasTransfer(t,e,a,s,i);return await i.methods.transfer(e,a).send({from:s,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to transfer tokens: ${t.message}`),new Error(`Failed Failed to transfer tokens: ${t.message}`)}}async estGasStartOrder(t,e,a,s,i,o){const n=o||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),r=this.GASLIMIT_DEFAULT;let c;try{c=await n.methods.startOrder(a,s,i).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){c=r}return c}async startOrder(t,e,a,s,i){const o=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);try{const n=await this.estGasStartOrder(t,e,a,s,i,o);return await o.methods.startOrder(a,s,i).send({from:e,gas:n+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to start order : ${t.message}`),new Error(`Failed to start order: ${t.message}`)}}async estGasBuyFromFreAndOrder(t,e,a,s,i){const o=i||new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyFromFreAndOrder(a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyFromFreAndOrder(t,e,a,s){const i=new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t);try{const o=await this.estGasBuyFromFreAndOrder(t,e,a,s,i);return await i.methods.buyFromFreAndOrder(a,s).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to buy DT From Fre And Order : ${t.message}`),new Error(`Failed to buy DT From Fre And Order: ${t.message}`)}}async estGasBuyFromDispenserAndOrder(t,e,a,s,i){const o=i||new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t),n=this.GASLIMIT_DEFAULT;let r;try{r=await o.methods.buyFromDispenserAndOrder(a,s).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}return r}async buyFromDispenserAndOrder(t,e,a,s){const i=new this.web3.eth.Contract(this.datatokensEnterpriseAbi,t);try{const o=await this.estGasBuyFromDispenserAndOrder(t,e,a,s,i);return await i.methods.buyFromDispenserAndOrder(a,s).send({from:e,gas:o+1,gasPrice:await A(this.web3,this.config)})}catch(t){throw g.error(`ERROR: Failed to buy DT From Fre And Order : ${t.message}`),new Error(`Failed to buy DT From Fre And Order: ${t.message}`)}}async estGasSetData(t,e,a,s){const i=s||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),o=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.setData(a).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){n=o}return n}async setData(t,e,a){if(!await this.isERC20Deployer(t,e))throw new Error("User is not ERC20 Deployer");const s=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),i=await this.estGasSetData(t,e,a,s);return await s.methods.setData(a).send({from:e,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCleanPermissions(t,e,a){const s=a||b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),i=this.GASLIMIT_DEFAULT;let o;try{o=await s.methods.cleanPermissions().estimateGas({from:e},(t,e)=>t?i:e)}catch(t){o=i}return o}async cleanPermissions(t,e){if(await this.nft.getNftOwner(await this.getNFTAddress(t))!==e)throw new Error("Caller is NOT Nft Owner");const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),s=await this.estGasCleanPermissions(t,e,a);return await a.methods.cleanPermissions().send({from:e,gas:s+1,gasPrice:await A(this.web3,this.config)})}async getDTPermissions(t,e){const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await a.methods.permissions(e).call()}async getCap(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config),a=await e.methods.cap().call();return this.web3.utils.fromWei(a)}async getDecimals(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.decimals().call()}async getNFTAddress(t){const e=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await e.methods.getERC721Address().call()}async isERC20Deployer(t,e){const a=b(new this.web3.eth.Contract(this.datatokensAbi,t),this.config);return await a.methods.isERC20Deployer(e).call()}async balance(t,e){const a=new this.web3.eth.Contract(this.datatokensAbi,t,{from:e}),s=await a.methods.balanceOf(e).call();return this.web3.utils.fromWei(s)}}const Y="0x0000000000000000000000000000000000000000";class tt{constructor(t,e,a,s){this.GASLIMIT_DEFAULT=1e6,this.factory721Address=void 0,this.factory721Abi=void 0,this.web3=void 0,this.config=void 0,this.factory721=void 0,this.factory721Address=t,this.factory721Abi=a||w.abi,this.web3=e,this.config=s||O[0],this.factory721=b(new this.web3.eth.Contract(this.factory721Abi,this.factory721Address),this.config)}async estGasCreateNFT(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.deployERC721Contract(e.name,e.symbol,e.templateIndex,Y,e.tokenURI).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async createNFT(t,e){if(e.templateIndex||(e.templateIndex=1),!e.name||!e.symbol){const{name:t,symbol:a}=f();e.name=t,e.symbol=a}if(e.templateIndex>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e.templateIndex)throw new Error("Template index cannot be ZERO");if(!1===(await this.getNFTTemplate(e.templateIndex)).isActive)throw new Error("Template is not active");const a=await this.estGasCreateNFT(t,e),s=await this.factory721.methods.deployERC721Contract(e.name,e.symbol,e.templateIndex,Y,e.tokenURI).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)});let i=null;try{i=s.events.NFTCreated.returnValues[0]}catch(t){g.error(`ERROR: Failed to create datatoken : ${t.message}`)}return i}async getCurrentNFTCount(){return await this.factory721.methods.getCurrentNFTCount().call()}async getCurrentTokenCount(){return await this.factory721.methods.getCurrentTokenCount().call()}async getOwner(){return await this.factory721.methods.owner().call()}async getCurrentNFTTemplateCount(){return await this.factory721.methods.getCurrentNFTTemplateCount().call()}async getCurrentTokenTemplateCount(){return await this.factory721.methods.getCurrentTemplateCount().call()}async getNFTTemplate(t){if(t>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===t)throw new Error("Template index cannot be ZERO");return await this.factory721.methods.getNFTTemplate(t).call()}async getTokenTemplate(t){return await this.factory721.methods.getTokenTemplate(t).call()}async checkDatatoken(t){return await this.factory721.methods.erc20List(t).call()}async checkNFT(t){return await this.factory721.methods.erc721List(t).call()}async estGasAddNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.add721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async addNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e===Y)throw new Error("Template cannot be ZERO address");const a=await this.estGasAddNFTTemplate(t,e);return await this.factory721.methods.add721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasDisableNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.disable721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async disableNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");const a=await this.estGasDisableNFTTemplate(t,e);return await this.factory721.methods.disable721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasReactivateNFTTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.reactivate721TokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async reactivateNFTTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");const a=await this.estGasReactivateNFTTemplate(t,e);return await this.factory721.methods.reactivate721TokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasAddTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.addTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async addTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e===Y)throw new Error("Template cannot be address ZERO");const a=await this.estGasAddTokenTemplate(t,e);return await this.factory721.methods.addTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasDisableTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.disableTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async disableTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");if(!1===(await this.getNFTTemplate(e)).isActive)throw new Error("Template is already disabled");const a=await this.estGasDisableTokenTemplate(t,e);return await this.factory721.methods.disableTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasReactivateTokenTemplate(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.reactivateTokenTemplate(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async reactivateTokenTemplate(t,e){if(await this.getOwner()!==t)throw new Error("Caller is not Factory Owner");if(e>await this.getCurrentNFTTemplateCount())throw new Error("Template index doesnt exist");if(0===e)throw new Error("Template index cannot be ZERO");if(!0===(await this.getTokenTemplate(e)).isActive)throw new Error("Template is already active");const a=await this.estGasReactivateTokenTemplate(t,e);return await this.factory721.methods.reactivateTokenTemplate(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasStartMultipleTokenOrder(t,e){const a=this.GASLIMIT_DEFAULT;let s;try{s=await this.factory721.methods.startMultipleTokenOrder(e).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){s=a}return s}async startMultipleTokenOrder(t,e){if(e.length>50)throw new Error("Too many orders");const a=await this.estGasStartMultipleTokenOrder(t,e);return await this.factory721.methods.startMultipleTokenOrder(e).send({from:t,gas:a+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftWithErc(t,e,a){const s=this.GASLIMIT_DEFAULT;let i;try{const o=T(a);i=await this.factory721.methods.createNftWithErc(e,o).estimateGas({from:t},(t,e)=>t?s:e)}catch(t){i=s}return i}async createNftWithErc(t,e,a){const s=T(a),i=await this.estGasCreateNftWithErc(t,e,a);return await this.factory721.methods.createNftWithErc(e,s).send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithPool(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;try{const n=T(a),r=C(s);o=await this.factory721.methods.createNftErcWithPool(e,n,r).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async createNftErcWithPool(t,e,a,s){const i=await this.estGasCreateNftErcWithPool(t,e,a,s),o=T(a),n=C(s);return await this.factory721.methods.createNftErcWithPool(e,o,n).send({from:t,gas:i+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithFixedRate(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;const n=T(a),r=E(s);try{o=await this.factory721.methods.createNftErcWithFixedRate(e,n,r).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i}return o}async createNftErcWithFixedRate(t,e,a,s){const i=T(a),o=E(s),n=await this.estGasCreateNftErcWithFixedRate(t,e,a,s);return await this.factory721.methods.createNftErcWithFixedRate(e,i,o).send({from:t,gas:n+1,gasPrice:await A(this.web3,this.config)})}async estGasCreateNftErcWithDispenser(t,e,a,s){const i=this.GASLIMIT_DEFAULT;let o;const n=T(a);try{o=await this.factory721.methods.createNftErcWithDispenser(e,n,s).estimateGas({from:t},(t,e)=>t?i:e)}catch(t){o=i,g.error("Failed to estimate gas for createNftErcWithDispenser",t)}return o}async createNftErcWithDispenser(t,e,a,s){const i=T(a),o=await this.estGasCreateNftErcWithDispenser(t,e,a,s);return await this.factory721.methods.createNftErcWithDispenser(e,i,s).send({from:t,gas:o+1,gasPrice:await A(this.web3,this.config)})}}class et{constructor(){this.nodeUri=void 0,this.providerAddress=void 0,this.metadataCacheUri=void 0,this.providerUri=void 0,this.web3Provider=void 0,this.oceanTokenAddress=void 0,this.erc721FactoryAddress=void 0,this.erc721FFactoryABI=void 0,this.datatokensABI=void 0,this.poolTemplateAddress=void 0,this.poolFactoryABI=void 0,this.poolABI=void 0,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeAddressABI=void 0,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.opfCommunityFeeCollector=void 0,this.sideStakingAddress=void 0,this.startBlock=void 0,this.verbose=void 0,this.authMessage=void 0,this.authTokenExpiration=void 0,this.parityUri=void 0,this.threshold=void 0,this.chainId=void 0,this.network=void 0,this.subgraphUri=void 0,this.explorerUri=void 0,this.oceanTokenSymbol=void 0,this.transactionBlockTimeout=void 0,this.transactionConfirmationBlocks=void 0,this.transactionPollingTimeout=void 0,this.gasFeeMultiplier=void 0}}const at=t=>function(t="",e){const{valid:a,output:s}=function(t,e,a){if("string"!=typeof t)throw g.debug("Not input string:"),g.debug(t),new Error("[zeroXTransformer] Expected string, input type: "+typeof t);const s=t.match(/^(?:0x)*([a-f0-9]+)$/i);return s?{valid:!0,output:s[1]}:(g.warn("[zeroXTransformer] Input transformation failed."),{valid:!1,output:t})}(t);return(e&&a?"0x":"")+s}(t,!1);class st{async getEndpoints(t){try{const e=await S(t);return await e.json()}catch(t){return g.error("Finding the service endpoints failed:",t),null}}getEndpointURL(t,e){return t?t.find(t=>t.serviceName===e):null}async getServiceEndpoints(t,e){const a=[];for(const s in e.serviceEndpoints)a.push({serviceName:s,method:e.serviceEndpoints[s][0],urlPath:t+e.serviceEndpoints[s][1]});return a}async getNonce(t,e,s,i,o){i||(i=await this.getEndpoints(t)),o||(o=await this.getServiceEndpoints(t,i));const n=this.getEndpointURL(o,"nonce")?this.getEndpointURL(o,"nonce").urlPath:null;if(!n)return null;try{const t=await a(n+`?userAddress=${e}`,{method:"GET",headers:{"Content-Type":"application/json"},signal:s});return String((await t.json()).nonce)}catch(t){throw g.error(t),new Error("HTTP request failed")}}async createSignature(t,e,a){return await W(t,at(a),e)}async createHashSignature(t,e,a){return await $(t,a,e)}async encrypt(t,e,s){const i=await this.getEndpoints(e),o=await this.getServiceEndpoints(e,i),n=this.getEndpointURL(o,"encrypt")?this.getEndpointURL(o,"encrypt").urlPath:null;if(!n)return null;try{const e=await a(n,{method:"POST",body:decodeURI(JSON.stringify(t)),headers:{"Content-Type":"application/octet-stream"},signal:s});return await e.text()}catch(t){throw g.error(t),new Error("HTTP request failed")}}async checkDidFiles(t,e,s,i){const o=await this.getEndpoints(s),n=await this.getServiceEndpoints(s,o),r={did:t,serviceId:e},c=[],h=this.getEndpointURL(n,"fileinfo")?this.getEndpointURL(n,"fileinfo").urlPath:null;if(!h)return null;try{const t=await a(h,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json"},signal:i}),e=await t.json();for(const t of e)c.push(t);return c}catch(t){return null}}async checkFileUrl(t,e,s){const i=await this.getEndpoints(e),o=await this.getServiceEndpoints(e,i),n={url:t,type:"url"},r=[],c=this.getEndpointURL(o,"fileinfo")?this.getEndpointURL(o,"fileinfo").urlPath:null;if(!c)return null;try{const t=await a(c,{method:"POST",body:JSON.stringify(n),headers:{"Content-Type":"application/json"},signal:s}),e=await t.json();for(const t of e)r.push(t);return r}catch(t){return null}}async initialize(t,e,s,i,o,n,r,c,h){const l=await this.getEndpoints(o),d=await this.getServiceEndpoints(o,l);let w=this.getEndpointURL(d,"initialize")?this.getEndpointURL(d,"initialize").urlPath:null;if(!w)return null;w+=`?documentId=${t}`,w+=`&serviceId=${e}`,w+=`&fileIndex=${s}`,w+=`&consumerAddress=${i}`,r&&(w+="&userdata="+encodeURI(JSON.stringify(r))),c&&(w+="&computeEnv="+encodeURI(c)),h&&(w+="&validUntil="+h);try{const t=await a(w,{method:"GET",headers:{"Content-Type":"application/json"},signal:n});return await t.json()}catch(t){throw g.error(t),new Error("Asset URL not found or not available.")}}async getDownloadUrl(t,e,a,s,i,o,n,r){const c=await this.getEndpoints(o),h=await this.getServiceEndpoints(o,c),l=this.getEndpointURL(h,"download")?this.getEndpointURL(h,"download").urlPath:null;if(!l)return null;const d=Date.now();let w=l;return w+=`?fileIndex=${s}`,w+=`&documentId=${t}`,w+=`&transferTxId=${i}`,w+=`&serviceId=${a}`,w+=`&consumerAddress=${e}`,w+=`&nonce=${d}`,w+=`&signature=${await this.createSignature(n,e,t+d)}`,r&&(w+="&userdata="+encodeURI(JSON.stringify(r))),w}async computeStart(t,e,s,i,o,n,r,c,h){const l=await this.getEndpoints(t),d=await this.getServiceEndpoints(t,l),w=this.getEndpointURL(d,"computeStart")?this.getEndpointURL(d,"computeStart").urlPath:null,m=Date.now();let u=s;u+=o.documentId,u+=m;const p=await this.createHashSignature(e,s,u),f=Object();if(f.consumerAddress=s,f.signature=p,f.nonce=m,f.computeEnv=i,f.dataset=o,f.algorithm=n,f.additionalDatasets&&(f.additionalDatasets=c),h&&(f.output=h),!w)return null;try{const t=await a(w,{method:"POST",body:JSON.stringify(f),headers:{"Content-Type":"application/json"},signal:r});return null!=t&&t.ok?await t.json():(console.error("Compute start failed:",t.status,t.statusText),g.error("Payload was:",f),null)}catch(t){return g.error("Compute start failed:"),g.error(t),g.error("Payload was:",f),null}}async computeStop(t,e,s,i,o,n){const r=await this.getEndpoints(i),c=await this.getServiceEndpoints(i,r),h=this.getEndpointURL(c,"computeStop")?this.getEndpointURL(c,"computeStop").urlPath:null,l=await this.getNonce(i,e,n,r,c);let d=e;d+=s||"",d+=t&&`${at(t)}`||"",d+=l;const w=await this.createHashSignature(o,e,d),m=Object();if(m.signature=w,m.documentId=at(t),m.consumerAddress=e,s&&(m.jobId=s),!h)return null;try{const t=await a(h,{method:"PUT",body:JSON.stringify(m),headers:{"Content-Type":"application/json"},signal:n});return null!=t&&t.ok?await t.json():(g.error("Compute stop failed:",t.status,t.statusText),g.error("Payload was:",m),null)}catch(t){return g.error("Compute stop failed:"),g.error(t),g.error("Payload was:",m),null}}async computeStatus(t,e,s,i,o){if(!s&&!i&&!o)throw new Error("You need at least one of jobId, did, consumerAddress");const n=await this.getEndpoints(t),r=await this.getServiceEndpoints(t,n),c=this.getEndpointURL(r,"computeStatus")?this.getEndpointURL(r,"computeStatus").urlPath:null;let h="?documentId="+at(i);if(h+=`&consumerAddress=${o}`,h+=s&&`&jobId=${s}`||"",!c)return null;try{const t=await a(c+h,{method:"GET",headers:{"Content-Type":"application/json"},signal:e});return null!=t&&t.ok?await t.json():(g.error("Get compute status failed:",t.status,t.statusText),null)}catch(t){return g.error("Get compute status failed"),g.error(t),null}}async computeResult(t,e,a,s,i,o){const n=await this.getEndpoints(s),r=await this.getServiceEndpoints(s,n),c=this.getEndpointURL(r,"computeResult")?this.getEndpointURL(r,"computeResult").urlPath:null,h=await this.getNonce(s,a,o,n,r);let l=a;l+=t,l+=String(e),l+=h;const d=await this.createHashSignature(i,a,l);let w=c;if(w+=`?consumerAddress=${a}`,w+=`&jobId=${t}`,w+=`&index=${String(e)}`,w+=d&&`&signature=${d}`||"",!c)return null;try{if(!document)return await v(w,e);await I(w)}catch(t){throw g.error("Error getting job result"),g.error(t),t}}async computeDelete(t,e,s,i,o,n){const r=await this.getEndpoints(i),c=await this.getServiceEndpoints(i,r),h=this.getEndpointURL(c,"computeDelete")?this.getEndpointURL(c,"computeDelete").urlPath:null,l=await this.getNonce(i,e,n,r,c);let d=e;d+=s||"",d+=t&&`${at(t)}`||"",d+=l;const w=await this.createHashSignature(o,e,d),m=Object();if(m.documentId=at(t),m.consumerAddress=e,m.jobId=s,w&&(m.signature=w),!h)return null;try{const t=await a(h,{method:"DELETE",body:JSON.stringify(m),headers:{"Content-Type":"application/json"},signal:n});return null!=t&&t.ok?await t.json():(g.error("Delete compute job failed:",t.status,t.statusText),g.error("Payload was:",m),null)}catch(t){return g.error("Delete compute job failed:"),g.error(t),g.error("Payload was:",m),null}}async isValidProvider(t,e){try{const s=await a(t,{method:"GET",headers:{"Content-Type":"application/json"},signal:e});if(null!=s&&s.ok){const t=await s.json();if(t&&t.providerAddress)return!0}return!1}catch(t){return g.error(`Error validating provider: ${t.message}`),!1}}}const it=new st;export{et as Config,U as ConfigHelper,K as Datatoken,Z as Dispenser,V as FixedRateCreateProgressStep,X as FixedRateExchange,N as GASLIMIT_DEFAULT,m as LogLevel,u as Logger,g as LoggerInstance,Q as Nft,tt as NftFactory,H as Pool,st as Provider,it as ProviderInstance,D as ZERO_ADDRESS,z as allowance,F as amountToUnits,j as approve,J as balance,O as configHelperNetworks,v as downloadFile,I as downloadFileBrowser,B as estApprove,k as fetchData,L as generateDid,f as generateDtName,S as getData,T as getErcCreationParams,A as getFairGasPrice,E as getFreCreationParams,P as getHash,C as getPoolCreationParams,G as postData,b as setContractDefaults,_ as signHash,W as signText,$ as signWithHash,R as unitsToAmount};
2
2
  //# sourceMappingURL=lib.modern.js.map