duco
Version:
Implement a duco miner and interact with the duco api
1 lines • 4.14 kB
JavaScript
export class Duco{constructor(e){this.username=e.username||"axorax",this.threads="max"==String(e.threads).toLowerCase()?window.navigator.hardwareConcurrency:e.threads||1,this.rigid=e.rigid||"duco.js",this.key=e.key||"",this.id=e.id||"duco-js-"+String(Math.random().toString(36).slice(2,12)),this.createdTimestamp=new Date,this.startedTimestamp=null,this.stoppedTimestamp=null,this.running=!1,this.changed=!1,this.data=null,this.url=`webminer.html?username=${this.username}&threads=${this.threads}&rigid=${this.rigid}&keyinput=${this.key}`}start(){let e=document.createElement("iframe");e.src=this.url,e.id=this.id,e.style.cssText="display:none;",document.body.append(e),this.running=!0,this.startedTimestamp=new Date}startAfter(e){setTimeout(()=>{this.start()},e)}stop(){document.querySelectorAll(`#${this.id}`).forEach(e=>e.remove()),this.running=!1,this.stoppedTimestamp=new Date}stopAfter(e){setTimeout(()=>{this.stop()},e)}delete(){document.querySelectorAll(`#${this.id}`).forEach(e=>e.remove())}deleteAfter(e){setTimeout(()=>{this.delete()},e)}addStyle(e){document.querySelectorAll(`#${this.id}`).forEach(t=>t.style.cssText=e)}change(e){this.username=e.username||this.username,this.threads="hardwareconcurrency"==String(e.threads).toLowerCase()?window.navigator.hardwareConcurrency:e.threads||this.threads,this.rigid=e.rigid||this.rigid,this.key=e.key||this.key,this.id=e.id||this.id,this.url=`webminer.html?username=${this.username}&threads=${this.threads}&rigid=${this.rigid}&keyinput=${this.key}`,this.changed=!0,this.stop(),e.start&&this.start()}changeAfter(e,t){setTimeout(()=>{this.change(e)},t)}onRemove(e){let t=this.id,s=new MutationObserver(function(a){a.forEach(function(a){a.removedNodes.forEach(function(a){a.id==t&&(s.disconnect(),e())})})});s.observe(document.querySelector("body"),{subtree:!1,childList:!0})}onRemoveCreateNew(){this.onRemove(()=>{let e=new Duco({username:this.username,threads:this.threads,rigid:this.rigid,key:this.key,id:this.id});e.start(),e.onRemoveCreateNew()})}get(e="",t=""){let s="";switch(e){case"":s=`v2/users/${this.username}`;break;case"transactions":s=`transactions?username=${this.username}`;break;case"user":s=`users/${this.username}`;break;case"balance":s=`balances/${this.username}`;break;case"miners":s=`miners/${this.username}`;break;case"pools":s="all_pools";break;case"stats":s="statistics";break;case"shop":s="shop_items";break;case"latestTransactions":s=`user_transactions/${this.username}`;break;case"hash":s=`transactions/${t}`;break;case"id":s=`id_transactions/${t}`}return new Promise((e,t)=>{fetch("https://server.duinocoin.com/"+String(s)).then(e=>{if(!e.ok)throw Error("Failed to fetch data");return e.json()}).then(t=>{this.data=t,e(t)}).catch(()=>{t(`Failed to fetch data about '${this.username}'`)})})}auth(e){return new Promise((t,s)=>{fetch(`https://server.duinocoin.com/auth/${this.username}?password=${e.password}`).then(e=>{if(!e.ok)throw Error("Failed to fetch data");return e.json()}).then(e=>{this.data=e,t(e)}).catch(()=>{s(`Failed to fetch data about '${this.username}'`)})})}buy(e){return new Promise((t,s)=>{fetch(`https://server.duinocoin.com/shop_buy/${this.username}?item=${e.item}&password=${e.password}`).then(e=>{if(!e.ok)throw Error("Failed to fetch data");return e.json()}).then(e=>{this.data=e,t(e)}).catch(()=>{s(`Failed to fetch data about '${this.username}'`)})})}send(e){return new Promise((t,s)=>{fetch(`https://server.duinocoin.com/transaction?username=${this.username}&password=${e.password}&recipient=${e.recipient}&amount=${String(e.amount)}&memo=${e.memo}`).then(e=>{if(!e.ok)throw Error("Failed to fetch data");return e.json()}).then(e=>{this.data=e,t(e)}).catch(()=>{s(`Failed to fetch data about '${this.username}'`)})})}exchange(e){return new Promise((t,s)=>{fetch(`https://server.duinocoin.com/exchange_request/?username=${this.username}&password=${e.password}&email=${e.email}&type=${e.type}&amount=${e.amount}&coin=${e.coin}&address=${e.address}`).then(e=>{if(!e.ok)throw Error("Failed to fetch data");return e.json()}).then(e=>{this.data=e,t(e)}).catch(()=>{s(`Failed to fetch data about '${this.username}'`)})})}}export default Duco;