Skip to content

Commit

Permalink
Merge pull request #216 from near/task-212/custom-network-configuration
Browse files Browse the repository at this point in the history
#212 Custom Network Configuration (Main)
  • Loading branch information
lewis-sqa authored Mar 28, 2022
2 parents 3fe62e5 + ab615ec commit b52cbf4
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 85 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ const selector = new NearWalletSelector({

```ts
type BuiltInWalletId = "near-wallet" | "sender-wallet" | "ledger-wallet";
type NetworkId = "mainnet" | "betanet" | "testnet";
type NetworkId = "mainnet" | "betanet" | "testnet" | "customnet";
type Theme = "dark" | "light" | "auto";

interface NetworkConfiguration {
networkId: string;
nodeUrl: string;
helperUrl: string;
explorerUrl: string;
restApiUrl: string;
}

interface Options {
// List of wallets you want to support in your dApp.
wallets: Array<BuiltInWalletId>;
// Network ID matching that of your dApp.
networkId: NetworkId;
// Required when networkId is 'customnet'.
network?: NetworkConfiguration;
contract: {
// Account ID of the Smart Contract used for 'view' and 'signAndSendTransaction' calls.
contractId: string;
Expand Down
40 changes: 0 additions & 40 deletions src/config.spec.ts

This file was deleted.

38 changes: 0 additions & 38 deletions src/config.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/core/NearWalletSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import WalletController, {
import Contract from "./Contract";
import Modal from "../modal/Modal";
import EventHandler, { Emitter, EventList } from "../utils/EventsHandler";
import getConfig from "../config";
import { resolveNetwork } from "../network";
import ProviderService from "../services/provider/ProviderService";
import { updateState } from "../state/State";
import { MODAL_ELEMENT_ID } from "../constants";
Expand All @@ -22,10 +22,10 @@ export default class NearWalletSelector {
contract: Contract;

constructor(options: Options) {
const config = getConfig(options.networkId);
const network = resolveNetwork(options.networkId, options.network);

const emitter = new EventHandler();
const provider = new ProviderService(config.nodeUrl);
const provider = new ProviderService(network.nodeUrl);
const controller = new WalletController(options, provider, emitter);
const contract = new Contract(options, provider, controller);

Expand Down
5 changes: 4 additions & 1 deletion src/interfaces/Options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NetworkConfiguration } from "../network";

export type BuiltInWalletId = "near-wallet" | "sender-wallet" | "ledger-wallet";
export type NetworkId = "mainnet" | "betanet" | "testnet";
export type NetworkId = "mainnet" | "betanet" | "testnet" | "customnet";
export type Theme = "dark" | "light" | "auto";

export interface Options {
wallets: Array<BuiltInWalletId>;
networkId: NetworkId;
network?: NetworkConfiguration;
contract: {
contractId: string;
methodNames?: Array<string>;
Expand Down
78 changes: 78 additions & 0 deletions src/network.spec.ts
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}'`)
);
});
});
55 changes: 55 additions & 0 deletions src/network.ts
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);
};
25 changes: 23 additions & 2 deletions src/wallets/browser/NearWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WalletConnection, connect, keyStores } from "near-api-js";

import getConfig from "../../config";
import { NetworkConfiguration, resolveNetwork } from "../../network";
import { Options } from "../../interfaces/Options";
import { Emitter } from "../../utils/EventsHandler";
import { logger } from "../../services/logging.service";
Expand Down Expand Up @@ -39,11 +39,32 @@ class NearWallet implements BrowserWallet {
return true;
};

private getWalletUrl(network: NetworkConfiguration) {
switch (network.networkId) {
case "mainnet":
return "https://wallet.near.org";
case "testnet":
return "https://wallet.testnet.near.org";
case "betanet":
return "https://wallet.betanet.near.org";
default:
// TODO: Make this a parameter once we have wallet modules.
return "https://wallet.testnet.near.org";
}
}

init = async () => {
const network = resolveNetwork(
this.options.networkId,
this.options.network
);
const keyStore = new keyStores.BrowserLocalStorageKeyStore();
const near = await connect({
keyStore,
...getConfig(this.options.networkId),
networkId: network.networkId,
nodeUrl: network.nodeUrl,
helperUrl: network.helperUrl,
walletUrl: this.getWalletUrl(network),
headers: {},
});

Expand Down

0 comments on commit b52cbf4

Please sign in to comment.