-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from near/task-212/custom-network-configuration
#212 Custom Network Configuration (Main)
- Loading branch information
Showing
8 changed files
with
174 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { getNetwork, NetworkConfiguration, resolveNetwork } from "./network"; | ||
|
||
describe("getNetwork", () => { | ||
it("returns the correct config for 'mainnet'", () => { | ||
const networkId = "mainnet"; | ||
const config = getNetwork(networkId); | ||
|
||
expect(config).toEqual({ | ||
networkId, | ||
nodeUrl: "https://rpc.mainnet.near.org", | ||
helperUrl: "https://helper.mainnet.near.org", | ||
explorerUrl: "https://explorer.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}); | ||
}); | ||
|
||
it("returns the correct config for 'testnet'", () => { | ||
const networkId = "testnet"; | ||
const config = getNetwork(networkId); | ||
|
||
expect(config).toEqual({ | ||
networkId, | ||
nodeUrl: "https://rpc.testnet.near.org", | ||
helperUrl: "https://helper.testnet.near.org", | ||
explorerUrl: "https://explorer.testnet.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}); | ||
}); | ||
|
||
it("returns the correct config for 'betanet'", () => { | ||
const networkId = "betanet"; | ||
const config = getNetwork(networkId); | ||
|
||
expect(config).toEqual({ | ||
networkId, | ||
nodeUrl: "https://rpc.betanet.near.org", | ||
helperUrl: "https://helper.betanet.near.org", | ||
explorerUrl: "https://explorer.betanet.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}); | ||
}); | ||
|
||
it("throws for 'customnet'", () => { | ||
const networkId = "customnet"; | ||
|
||
expect(() => getNetwork(networkId)).toThrowError( | ||
new Error(`Failed to find network configuration for '${networkId}'`) | ||
); | ||
}); | ||
}); | ||
|
||
describe("resolveNetwork", () => { | ||
it("resolves to network configuration matching the preset", () => { | ||
const networkId = "testnet"; | ||
|
||
expect(resolveNetwork(networkId)).toEqual(getNetwork(networkId)); | ||
}); | ||
|
||
it("resolves to custom network configuration for 'customnet'", () => { | ||
const network: NetworkConfiguration = { | ||
networkId: "localnet", | ||
nodeUrl: "http://127.0.0.1:52993", | ||
helperUrl: "http://127.0.0.1:52997", | ||
explorerUrl: "http://127.0.0.1:53009", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}; | ||
|
||
expect(resolveNetwork("customnet", network)).toEqual(network); | ||
}); | ||
|
||
it("throws if no custom network configuration is defined for 'customnet'", () => { | ||
const networkId = "customnet"; | ||
|
||
expect(() => resolveNetwork(networkId)).toThrowError( | ||
new Error(`You must define network configuration for '${networkId}'`) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { NetworkId } from "./interfaces/Options"; | ||
|
||
export interface NetworkConfiguration { | ||
networkId: string; | ||
nodeUrl: string; | ||
helperUrl: string; | ||
explorerUrl: string; | ||
restApiUrl: string; | ||
} | ||
|
||
export const getNetwork = (networkId: NetworkId): NetworkConfiguration => { | ||
switch (networkId) { | ||
case "mainnet": | ||
return { | ||
networkId, | ||
nodeUrl: "https://rpc.mainnet.near.org", | ||
helperUrl: "https://helper.mainnet.near.org", | ||
explorerUrl: "https://explorer.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}; | ||
case "testnet": | ||
return { | ||
networkId, | ||
nodeUrl: "https://rpc.testnet.near.org", | ||
helperUrl: "https://helper.testnet.near.org", | ||
explorerUrl: "https://explorer.testnet.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}; | ||
case "betanet": | ||
return { | ||
networkId, | ||
nodeUrl: "https://rpc.betanet.near.org", | ||
helperUrl: "https://helper.betanet.near.org", | ||
explorerUrl: "https://explorer.betanet.near.org", | ||
restApiUrl: "https://rest.nearapi.org", | ||
}; | ||
default: | ||
throw Error(`Failed to find network configuration for '${networkId}'`); | ||
} | ||
}; | ||
|
||
export const resolveNetwork = ( | ||
networkId: NetworkId, | ||
network?: NetworkConfiguration | ||
): NetworkConfiguration => { | ||
if (networkId === "customnet") { | ||
if (!network) { | ||
throw new Error("You must define network configuration for 'customnet'"); | ||
} | ||
|
||
return network; | ||
} | ||
|
||
return getNetwork(networkId); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters