Skip to content

Commit

Permalink
Merge pull request #153 from Synthetixio/switch-from-enum-to-const
Browse files Browse the repository at this point in the history
renamed imports for networkId enum
  • Loading branch information
fritzschoff authored Jan 27, 2022
2 parents b6a9cf2 + 6d7e954 commit d74de5c
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 192 deletions.
34 changes: 16 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions packages/contracts-interface/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { ethers } from 'ethers';
import findIndex from 'lodash/findIndex';

import synthetix, { SynthetixJS } from '../src';
import { Network, NetworkId } from '../src/types';
import { NetworkNameById, NetworkIdByName } from '../src/types';
import { ERRORS } from '../src/constants';

describe('@synthetixio/js tests', () => {
let snxjs: SynthetixJS;

beforeAll(() => {
snxjs = synthetix({ network: Network.Kovan });
snxjs = synthetix({ network: NetworkNameById[42] });
});

test('should return contracts', () => {
expect(Object.keys(snxjs.targets).length).toBeGreaterThan(0);
});

test('should return different contracts for the OVM', () => {
const snxjsGoerli = synthetix({ network: Network.Goerli });
const snxjsGoerliOvm = synthetix({ network: Network['Goerli-Ovm'] });
const snxjsGoerli = synthetix({ network: NetworkNameById[5] });
const snxjsGoerliOvm = synthetix({ network: NetworkNameById[42] });
const synthetixContractGoerli = snxjsGoerli.contracts['Synthetix'];
const synthetixContractGoerliOvm = snxjsGoerliOvm.contracts['Synthetix'];
expect(synthetixContractGoerli.address).not.toEqual(synthetixContractGoerliOvm.address);
Expand All @@ -34,7 +34,7 @@ describe('@synthetixio/js tests', () => {
});

test('should have the right mapping with the contracts for the OVM', () => {
const snxjsGoerliOvm = synthetix({ network: Network.Goerli, useOvm: true });
const snxjsGoerliOvm = synthetix({ network: NetworkNameById[5], useOvm: true });
const synthetixContractGoerliOvm = snxjsGoerliOvm.contracts['Synthetix'];
const sUSDContractGoerliOvm = snxjsGoerliOvm.contracts['SynthsUSD'];
expect(synthetixContractGoerliOvm.address).toEqual(snxjsGoerliOvm.targets.ProxyERC20.address);
Expand All @@ -46,14 +46,13 @@ describe('@synthetixio/js tests', () => {
});

test('should include the supported networks', () => {
expect(snxjs.networkToChainId[Network.Mainnet]).toBe(NetworkId.Mainnet.toString());
expect(snxjs.networkToChainId[Network.Kovan]).toBe(NetworkId.Kovan.toString());
expect(snxjs.networkToChainId[Network.Rinkeby]).not.toBe(NetworkId.Ropsten.toString());
expect(snxjs.networkToChainId[NetworkNameById[1]]).toBe(NetworkIdByName.mainnet.toString());
expect(snxjs.networkToChainId[NetworkNameById[42]]).toBe(NetworkIdByName.kovan.toString());
});

test('should include the current network', () => {
expect(snxjs.network.name).toBe(Network.Kovan);
expect(snxjs.network.id).toBe(NetworkId.Kovan);
expect(snxjs.network.name).toBe(NetworkNameById[42]);
expect(snxjs.network.id).toBe(NetworkIdByName.kovan.toString());
});

test('should return users', () => {
Expand Down Expand Up @@ -87,12 +86,13 @@ describe('@synthetixio/js tests', () => {
});

test('should not include invalid synths data', () => {
// @ts-ignore
const invalidSynthIndex = findIndex(snxjs.synths, ({ name }) => name === 'mETH1234');
expect(invalidSynthIndex).toBe(-1);
});

test('should have a list of staking rewards', () => {
const mainnetSnxjs = synthetix({ network: Network.Mainnet });
const mainnetSnxjs = synthetix({ network: NetworkNameById[1] });
expect(mainnetSnxjs.stakingRewards[0].name).toBeTruthy();
});

Expand Down Expand Up @@ -134,6 +134,7 @@ describe('@synthetixio/js tests', () => {
// @ts-ignore
synthetix({ network: 'wrongnetwork' });
} catch (e) {
// @ts-ignore
expect(e.message).toEqual(ERRORS.badNetworkArg);
}
});
Expand Down
63 changes: 42 additions & 21 deletions packages/contracts-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ import {
decode,
defaults,
getFeeds,
Token,
TargetsRecord,
Target,
} from 'synthetix';
import { ethers } from 'ethers';

import {
Config,
CurrencyKey,
CurrencyCategory,
Network,
NetworkId,
Target,
TargetsRecord,
NetworkIdByNameType,
ContractsMap,
SynthetixJS,
Synth,
Token,
NetworkName,
NetworkIdByName,
NetworkNameById,
} from './types';

import { Synths } from '../generated/mainnet';
Expand Down Expand Up @@ -62,41 +65,47 @@ const synthetix = ({ networkId, network, signer, provider }: Config): SynthetixJ
};
};

const selectNetwork = (networkId?: NetworkId, network?: Network): [Network, NetworkId, boolean] => {
let currentNetwork: Network = Network.Mainnet;
let currentNetworkId: NetworkId = NetworkId.Mainnet;
const selectNetwork = (
networkId?: NetworkId,
network?: NetworkName
): [NetworkName, NetworkId, boolean] => {
let currentNetworkId: NetworkId;
let currentNetworkName: NetworkName;
let useOvm = false;
if (
(network && !networkToChainId[network]) ||
(networkId && !getNetworkFromId({ id: networkId }))
) {
throw new Error(ERRORS.badNetworkArg);
} else if (network && networkToChainId[network]) {
const networkToId = Number(networkToChainId[network]);
const networkToId = NetworkIdByName[network];
const networkFromId = getNetworkFromId({ id: networkToId });
currentNetworkId = networkToId;
currentNetwork = networkFromId.network;
useOvm = networkFromId.useOvm;
currentNetworkId = networkToChainId[network];
currentNetworkName = networkFromId.network;
useOvm = !!networkFromId.useOvm;
} else if (networkId) {
const networkFromId = getNetworkFromId({ id: networkId });
currentNetworkId = networkId;
currentNetwork = networkFromId.network;
useOvm = networkFromId.useOvm;
currentNetworkName = networkFromId.network;
useOvm = Boolean(networkFromId.useOvm);
} else {
currentNetworkId = NetworkIdByName.mainnet;
currentNetworkName = NetworkNameById[1];
}
return [currentNetwork, currentNetworkId, useOvm];
return [currentNetworkName, currentNetworkId, useOvm];
};

const getSynthetixContracts = (
network: Network,
network: NetworkName,
signer?: ethers.Signer,
provider?: ethers.providers.Provider,
useOvm?: boolean
): ContractsMap => {
const sources = getSource({ network, useOvm });
const targets: TargetsRecord = getTarget({ network, useOvm });
const targets = getTarget({ network, useOvm });

return Object.values(targets)
.map((target: Target) => {
.map((target) => {
if (target.name === 'Synthetix') {
target.address = targets.ProxyERC20.address;
} else if (target.name === 'SynthsUSD') {
Expand All @@ -109,7 +118,7 @@ const getSynthetixContracts = (
}
return target;
})
.reduce((acc: ContractsMap, { name, source, address }: Target) => {
.reduce((acc: ContractsMap, { name, source, address }) => {
acc[name] = new ethers.Contract(
address,
sources[source].abi,
Expand All @@ -121,12 +130,24 @@ const getSynthetixContracts = (

export {
synthetix,
Network,
NetworkId,
NetworkNameById,
NetworkIdByName,
Synths,
CurrencyCategory,
networkToChainId,
getNetworkFromId,
};
export type { Config, CurrencyKey, Target, TargetsRecord, ContractsMap, SynthetixJS, Synth, Token };
export type {
Config,
CurrencyKey,
Target,
TargetsRecord,
ContractsMap,
SynthetixJS,
Synth,
Token,
NetworkName,
NetworkId,
NetworkIdByNameType,
};
export default synthetix;
1 change: 0 additions & 1 deletion packages/contracts-interface/src/missing-types.d.ts

This file was deleted.

Loading

0 comments on commit d74de5c

Please sign in to comment.