-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensea-api.min.js
1 lines (1 loc) · 1.81 KB
/
opensea-api.min.js
1
class OpenseaApi{constructor(t,e=null,s=.9,a="https://api.opensea.io/api/v1/"){this.etherUtils=t,this.baseUrl=a,this.apiKey=e,this.erc721Identifier="non-fungible",this.erc1155Identifier="semi-fungible",this.maxPercentagePriceDrop=s}async _doFetch(t,e=null){return null===e&&(e={method:"GET"},null!==this.apiKey&&(e.headers={"X-API-KEY":this.apiKey})),await fetch(this.baseUrl+t,e)}convertUsdToEth(t,e){return t=t.slice(0,t.length-e)+","+t.slice(t.length-e),parseFloat(t)/ethPriceInUsd}async getAssetsForOwnerByContract(t,e){let s=await this._doFetch("assets?owner="+t+"&asset_contract_address="+e+"&order_direction=desc&offset=0&limit=50");return await s.json()}async getLowestPriceOfAssetByContractAndId(t,e=null){let s=new URLSearchParams({asset_contract_address:t,order_by:"sale_date",order_direction:"desc",offset:0,limit:null!==e?1:20});null!==e&&s.append("token_ids",e);let a=await this._doFetch("assets?"+s.toString()),i=await a.json();if(0===i.assets.length)return 0;var r=0;return i.assets.forEach(function(t){var e;if(t.asset_contract.asset_contract_type!==this.erc721Identifier||-1===t.last_sale.payment_token.symbol.indexOf("WETH")){if(-1!==t.last_sale.payment_token.symbol.indexOf("USD"))return e=this.convertUsdToEth(t.last_sale.total_price,t.last_sale.payment_token.decimals)/parseInt(t.last_sale.quantity),void((0===r||e<r||(r-e)/r<=this.maxPercentagePriceDrop)&&(r=e));e=parseFloat(this.etherUtils.formatEther(t.last_sale.total_price))/parseInt(t.last_sale.quantity),(0===r||e<r||(r-e)/r<=this.maxPercentagePriceDrop)&&(r=e)}}.bind(this)),r}async getCollectionsByOwner(t){let e=await this._doFetch("collections?asset_owner="+t+"&offset=0&limit=300");return await e.json()}async getFloorPriceForCollectionBySlug(t,e=!1){let s=await this._doFetch("collection/"+t+"/stats"),a=await s.json();return e?a.stats.average_price:a.stats.floor_price}}