Skip to content

Commit

Permalink
Merge pull request #102 from thepower/feat/custom_chain
Browse files Browse the repository at this point in the history
updated README, bootstrap from localStorage nodeList
  • Loading branch information
jackkru69 authored Nov 4, 2023
2 parents 60edeae + efd60c0 commit 29c2680
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
9 changes: 9 additions & 0 deletions packages/tssdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ npm i @thepowereco/tssdk
- [GitHub](https://github.com/thepower/power_hub/tree/master/packages/tssdk/src/libs)
- [Report an issue](https://github.com/thepower/power_hub/issues)
[The Power API Reference](https://doc.thepower.io/docs/Build/api/api-reference)

## Custom chain

If you need to join to custom chain you can override chain id and bootstrap nodes this way:

localStorage.setItem("nodesList", JSON.stringify(
[{address:"nodeUrl", nodeId:"nodeId"}]
))
localStorage.setItem("chainId", "chainId")
46 changes: 31 additions & 15 deletions packages/tssdk/src/libs/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,45 @@ export class NetworkApi {
}

public bootstrap = async (isHTTPSNodesOnly = false) => {
const chainInfo = await NetworkApi.getChainGlobalConfig();
if (typeof localStorage !== 'undefined' && localStorage !== null && localStorage.getItem('nodesList')) {
const stringifiedNodesList = localStorage.getItem('nodesList');
const chainIdString = localStorage.getItem('chainId');
const chainId = chainIdString ? +chainIdString : this.currentChain;

if (stringifiedNodesList) {
this.currentChain = chainId;
const nodesList: ChainNode[] = JSON.parse(stringifiedNodesList);
await this.setCurrentConfig(nodesList);
info(`Bootstrapped chain ${chainId}`, nodesList);
await this.loadFeeGasSettings();
} else {
throw new NoNodesFoundException(chainId);
}
} else {
const chainInfo = await NetworkApi.getChainGlobalConfig();

const chainData = chainInfo.chains[this.currentChain];
const chainData = chainInfo.chains[this.currentChain];

if (chainData) {
this.isHTTPSNodesOnly = isHTTPSNodesOnly;
if (chainData) {
this.isHTTPSNodesOnly = isHTTPSNodesOnly;

const httpsRegExp = /^https:\/\//ig;
const httpsRegExp = /^https:\/\//ig;

const transformedNodeList = transformNodeList(chainData);
const nodesList = isHTTPSNodesOnly ? transformedNodeList.filter((node) => httpsRegExp.test(node.address)) : transformedNodeList;
const transformedNodeList = transformNodeList(chainData);
const nodesList = isHTTPSNodesOnly ? transformedNodeList.filter((node) => httpsRegExp.test(node.address)) : transformedNodeList;

if (!transformedNodeList.length) {
throw new NoNodesFoundException(this.currentChain);
if (!transformedNodeList.length) {
throw new NoNodesFoundException(this.currentChain);
}

await this.setCurrentConfig(nodesList);
info(`Bootstrapped chain ${this.currentChain}`, this.currentNodes);
await this.loadFeeGasSettings();
return;
}

await this.setCurrentConfig(nodesList);
info(`Bootstrapped chain ${this.currentChain}`, this.currentNodes);
await this.loadFeeGasSettings();
return;
throw new UnknownChainException(this.currentChain);
}

throw new UnknownChainException(this.currentChain);
};

private async loadRemoteSCInterface(interfaceData: any[]) {
Expand Down

0 comments on commit 29c2680

Please sign in to comment.