@hashgraph/sdk
Version:
3 lines (2 loc) • 3.36 kB
JavaScript
import e from"./Client.js";import t from"../channel/WebChannel.js";import r from"../LedgerId.js";import{WebNetwork as s,WebMirrorNetwork as o}from"../constants/ClientConstants.js";import n from"../network/AddressBookQueryWeb.js";import i from"../file/FileId.js";class a extends e{constructor(t){if(super(t),null!=t)if("string"==typeof t.network)switch(t.network){case"mainnet":this.setNetwork(s.MAINNET),this.setMirrorNetwork(o.MAINNET),this.setLedgerId(r.MAINNET);break;case"testnet":this.setNetwork(s.TESTNET),this.setLedgerId(r.TESTNET),this.setMirrorNetwork(o.TESTNET);break;case"previewnet":this.setNetwork(s.PREVIEWNET),this.setLedgerId(r.PREVIEWNET),this.setMirrorNetwork(o.PREVIEWNET);break;case"local-node":this.setNetwork(s.LOCAL_NODE),this.setLedgerId(r.LOCAL_NODE),this.setMirrorNetwork(o.LOCAL_NODE);break;default:throw new Error(`unknown network: ${t.network}`)}else if(null!=t.network){e._validateNetworkConsistency(t.network);const{shard:r,realm:s}=e._extractShardRealm(t.network);this._shard=r,this._realm=s,this.setNetwork(t.network)}}static fromConfig(e){return new a("string"==typeof e?JSON.parse(e):e)}static forNetwork(e){return new a({network:e})}static forName(e){return new a({network:e})}static forMainnet(){return new a({network:"mainnet"})}static forTestnet(){return new a({network:"testnet"})}static forPreviewnet(){return new a({network:"previewnet"})}static forLocalNode(e={scheduleNetworkUpdate:!1}){return new a({network:"local-node",...e})}static async forMainnetAsync(){return new a({network:"mainnet"}).updateNetwork()}static async forTestnetAsync(){return new a({network:"testnet"}).updateNetwork()}static async forPreviewnetAsync(){return new a({network:"previewnet"}).updateNetwork()}static async forNameAsync(e){const t=new a({network:e});return"local-node"!==e&&await t.updateNetwork(),t}static async forMirrorNetwork(e){const t=new a({mirrorNetwork:e});return await t.updateNetwork(),t}setNetwork(e){if("string"==typeof e)switch(e){case"previewnet":this._network.setNetwork(s.PREVIEWNET);break;case"testnet":this._network.setNetwork(s.TESTNET);break;case"mainnet":this._network.setNetwork(s.MAINNET);break;case"local-node":this._network.setNetwork(s.LOCAL_NODE)}else this._network.setNetwork(e)}setMirrorNetwork(e){if("string"==typeof e)switch(e){case"local-node":this._mirrorNetwork.setNetwork(o.LOCAL_NODE);break;case"previewnet":this._mirrorNetwork.setNetwork(o.PREVIEWNET);break;case"testnet":this._mirrorNetwork.setNetwork(o.TESTNET);break;case"mainnet":this._mirrorNetwork.setNetwork(o.MAINNET);break;default:this._mirrorNetwork.setNetwork([e])}else this._mirrorNetwork.setNetwork(e);return this}async updateNetwork(){if(this._isUpdatingNetwork)return this;this._isUpdatingNetwork=!0;try{const e=await(new n).setFileId(i.getAddressBookFileIdFor(this.shard,this.realm)).execute(this),t={};for(const r of e.nodeAddresses)for(const e of r.addresses)null!=r.accountId&&(t[e.toString()]=r.accountId);this.setNetwork(t)}catch(e){if(this._logger){const t=e instanceof Error?e.message:String(e);this._logger.trace(`failed to update client address book: ${t}`)}}finally{this._isUpdatingNetwork=!1}return this}_createNetworkChannel(){return e=>new t(e)}_createMirrorNetworkChannel(){return()=>{throw new Error("mirror support is not supported in browsers")}}}export{a as default};
//# sourceMappingURL=WebClient.js.map