Skip to content

Commit b310f20

Browse files
authored
feat(config): add Chain.toString() (#346)
1 parent f2a859f commit b310f20

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/config/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ interface ChainJSON {
3434

3535
export class Chain implements ChainJSON {
3636
constructor(
37+
public readonly name: string,
3738
public readonly id: number,
3839
public rpcEndpoints: RPCEndpoint[],
3940
public contracts: Contracts,
4041
) {
42+
this.name = name
4143
this.id = id
4244
this.rpcEndpoints = new Array<RPCEndpoint>()
4345
for (const rpcEndpoint of rpcEndpoints) {
@@ -48,6 +50,7 @@ export class Chain implements ChainJSON {
4850
this.contracts[key] = contracts[key]
4951
}
5052
}
53+
5154
getRPCEndpointsByProtocol(protocol: RPCProtocol): RPCEndpoint[] {
5255
const endpoints = new Array<RPCEndpoint>()
5356
for (const rpcEndpoint of this.rpcEndpoints) {
@@ -63,6 +66,10 @@ export class Chain implements ChainJSON {
6366
}
6467
return endpoints
6568
}
69+
70+
toString(): string {
71+
return this.name
72+
}
6673
}
6774

6875
interface ChainsJSON {
@@ -92,7 +99,7 @@ class ChainsFactory {
9299
for (const key of Object.keys(chainJson.contracts)) {
93100
contracts[key] = chainJson.contracts[key]
94101
}
95-
chains[key] = new Chain(chainJson.id, rpcEndpoints, contracts)
102+
chains[key] = new Chain(key, chainJson.id, rpcEndpoints, contracts)
96103
}
97104
return chains
98105
}

packages/config/test/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ describe("Load configuration from JSON file", () => {
3333
assert.equal(endpoints.length, 1)
3434
assert.equal(endpoints[0].url, "https://bsc-dataseed.binance.org")
3535
})
36+
it("Chain.toString() returns the name of the chain", () => {
37+
const chains: config.Chains = config.Chains.load()
38+
const chain = chains["polygon"]
39+
assert.equal(chain.toString(), "polygon")
40+
})
3641
})

0 commit comments

Comments
 (0)