Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHANGED] - Testnet Ticker format update <network name>ETH #868

Merged
merged 14 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NetworkType } from './network/NetworkController';

export const MAINNET = 'mainnet';
export const RPC = 'rpc';
export const FALL_BACK_VS_CURRENCY = 'ETH';
Expand Down Expand Up @@ -39,6 +41,21 @@ export const TESTNET_TICKER_SYMBOLS = {
KOVAN: 'KovanETH',
};

// TYPED NetworkType TICKER SYMBOLS
export const TESTNET_NETWORK_TYPE_TO_TICKER_SYMBOL: {
[K in NetworkType]: string;
} = {
rinkeby: 'RinkebyETH',
goerli: 'GoerliETH',
ropsten: 'RopstenETH',
kovan: 'KovanETH',
mainnet: '',
rpc: '',
localhost: '',
optimism: '',
optimismTest: '',
};

// APIs
export const OPENSEA_PROXY_URL =
'https://proxy.metaswap.codefi.network/opensea/v1/api/v1';
Expand Down
18 changes: 18 additions & 0 deletions src/network/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ describe('NetworkController', () => {
expect(controller.state.isCustomNetwork).toBe(false);
});

it('should set new testnet provider type', () => {
const controller = new NetworkController();
controller.config.infuraProjectId = '0x0000';
controller.setProviderType('rinkeby' as NetworkType);
expect(controller.state.provider.type).toBe('rinkeby');
expect(controller.state.provider.ticker).toBe('RinkebyETH');
expect(controller.state.isCustomNetwork).toBe(false);
});

it('should set mainnet provider type', () => {
const controller = new NetworkController();
controller.config.infuraProjectId = '0x0000';
controller.setProviderType('mainnet' as NetworkType);
expect(controller.state.provider.type).toBe('mainnet');
expect(controller.state.provider.ticker).toBe('ETH');
expect(controller.state.isCustomNetwork).toBe(false);
});

it('should throw when setting an unrecognized provider type', () => {
const controller = new NetworkController();
expect(() => controller.setProviderType('junk' as NetworkType)).toThrow(
Expand Down
20 changes: 18 additions & 2 deletions src/network/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import createInfuraProvider from 'eth-json-rpc-infura/src/createProvider';
import createMetamaskProvider from 'web3-provider-engine/zero';
import { Mutex } from 'async-mutex';
import { BaseController, BaseConfig, BaseState } from '../BaseController';
import { MAINNET, RPC } from '../constants';
import {
MAINNET,
RPC,
TESTNET_NETWORK_TYPE_TO_TICKER_SYMBOL,
} from '../constants';

/**
* Human-readable network name
Expand Down Expand Up @@ -282,10 +286,22 @@ export class NetworkController extends BaseController<
setProviderType(type: NetworkType) {
const { rpcTarget, chainId, nickname, ...providerState } =
this.state.provider;

// If testnet the ticker symbol should use a testnet prefix
const ticker =
type in TESTNET_NETWORK_TYPE_TO_TICKER_SYMBOL &&
TESTNET_NETWORK_TYPE_TO_TICKER_SYMBOL[type].length > 0
? TESTNET_NETWORK_TYPE_TO_TICKER_SYMBOL[type]
: 'ETH';

this.update({
provider: {
...providerState,
...{ type, ticker: 'ETH', chainId: NetworksChainId[type] },
...{
type,
ticker,
chainId: NetworksChainId[type],
},
},
});
this.refreshNetwork();
Expand Down