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