Skip to content

Commit

Permalink
Merge pull request #19 from torusresearch/feat/network-string
Browse files Browse the repository at this point in the history
feat: added network label support
  • Loading branch information
metallicalfa2 committed Jan 8, 2022
2 parents 5056163 + a50586c commit 0566625
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
FEATURES_CONFIRM_WINDOW,
FEATURES_DEFAULT_WALLET_WINDOW,
FEATURES_PROVIDER_CHANGE_WINDOW,
getNetworkConfig,
getPopupFeatures,
getTorusUrl,
getWindowId,
Expand Down Expand Up @@ -101,7 +102,7 @@ class Torus {
buildEnv = TORUS_BUILD_ENV.PRODUCTION,
enableLogging = false,
network,
showTorusButton = true,
showTorusButton = false,
useLocalStorage = false,
buttonPosition = BUTTON_POSITION.BOTTOM_LEFT,
apiKey = "torus-default",
Expand Down Expand Up @@ -156,7 +157,7 @@ class Torus {
{
buttonPosition,
apiKey,
network,
network: typeof network === "string" ? getNetworkConfig(network) : network,
dappMetadata,
extraParams,
},
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export interface LocaleLinks<T> {
es?: T;
}

export type NetworkLabel = "mainnet-beta" | "testnet" | "devnet";

export interface TorusParams {
/**
* Determines where the torus widget is visible on the page.
Expand All @@ -206,7 +208,7 @@ export interface TorusParams {
/**
* Torus Network Object
*/
network?: NetworkInterface;
network?: NetworkInterface | NetworkLabel;
/**
* Build Environment of Torus.
*
Expand Down
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethErrors } from "eth-rpc-errors";
import { LogLevelDesc } from "loglevel";

import config from "./config";
import { NetworkInterface, NetworkLabel } from "./interfaces";
import log from "./loglevel";

// utility functions
Expand Down Expand Up @@ -148,3 +149,43 @@ export function getPopupFeatures({ width: w, height: h }: { width: number; heigh
const features = `titlebar=0,toolbar=0,status=0,location=0,menubar=0,height=${h / systemZoom},width=${w / systemZoom},top=${top},left=${left}`;
return features;
}

export const getNetworkConfig = (label: NetworkLabel): NetworkInterface | undefined => {
switch (label) {
case "mainnet-beta":
return {
blockExplorerUrl: "https://explorer.solana.com",
chainId: "0x1",
displayName: "Solana Mainnet",
logo: "solana.svg",
rpcTarget: "https://api.mainnet-beta.solana.com",
ticker: "SOL",
tickerName: "Solana Token",
} as NetworkInterface;

case "testnet":
return {
blockExplorerUrl: "https://explorer.solana.com",
chainId: "0x2",
displayName: "Solana Testnet",
logo: "solana.svg",
rpcTarget: "https://api.testnet.solana.com",
ticker: "SOL",
tickerName: "Solana Token",
} as NetworkInterface;

case "devnet":
return {
blockExplorerUrl: "https://explorer.solana.com",
chainId: "0x3",
displayName: "Solana Devnet",
logo: "solana.svg",
rpcTarget: "https://api.devnet.solana.com",
ticker: "SOL",
tickerName: "Solana Token",
} as NetworkInterface;

default:
return undefined;
}
};

0 comments on commit 0566625

Please sign in to comment.