Skip to content

Commit

Permalink
fix: Start separating functions for unit testing (#350)
Browse files Browse the repository at this point in the history
* toWeiSafe works

Signed-off-by: Tulun <[email protected]>

* Addresses can be checked independently; odd import is screwing up other test.

Signed-off-by: Tulun <[email protected]>

* save wip

Signed-off-by: Tulun <[email protected]>

* address.test.ts

Signed-off-by: Tulun <[email protected]>

* update imports

Signed-off-by: Tulun <[email protected]>

* Fixed import issue

Signed-off-by: Tulun <[email protected]>

* test env

Signed-off-by: Tulun <[email protected]>

* removed unnec jest config stuff

Signed-off-by: Tulun <[email protected]>

* add to GH actions

Signed-off-by: Tulun <[email protected]>

* syntax

Signed-off-by: Tulun <[email protected]>

Signed-off-by: Tulun <[email protected]>
  • Loading branch information
Tulun authored Oct 7, 2022
1 parent dd33147 commit 7da0ffb
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_V_ETH=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
REACT_APP_UMA_ADDRESS=0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828
10 changes: 9 additions & 1 deletion .github/workflows/lint-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup
- run: yarn test
- name: Unit tests
env:
REACT_APP_PUBLIC_INFURA_ID: ${{ secrets.CYPRESS_PUBLIC_INFURA_ID }}
REACT_APP_PUBLIC_ONBOARD_API_KEY: ${{ secrets.CYPRESS_PUBLIC_ONBOARD_API_KEY }}
REACT_APP_REWARDS_API_URL: ${{ secrets.CYPRESS_REWARDS_API_URL }}
REACT_APP_CHAIN_137_PROVIDER_URL: ${{ secrets.CYPRESS_CHAIN_137_PROVIDER_URL }}
REACT_APP_CHAIN_42161_PROVIDER_URL: ${{ secrets.CYPRESS_CHAIN_42161_PROVIDER_URL }}
REACT_APP_MOCK_SERVERLESS: true
run: yarn test

cypress-tests:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

module.exports = {
preset: "ts-jest",
testEnvironment: "node",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"cypress": "^10.4.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.0",
"jest-environment-hardhat": "^1.1.8",
"lint-staged": "^12.4.1",
"prettier": "^2.4.1",
"serve": "^14.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/state/poolsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as acrossSdk from "@across-protocol/sdk-v2";
import { update } from "./pools";
import { store } from "../state";
import {
getProvider,
getConfigStoreAddress,
ChainId,
hubPoolAddress,
hubPoolChainId,
getConfig,
} from "utils";
import { ethers } from "ethers";
import { getProvider } from "utils/providers";

const provider = getProvider();

Expand Down
11 changes: 7 additions & 4 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ChainId, getProvider } from "utils";
import { ethers } from "ethers";
import { utils } from "ethers";
// import { ChainId, getProvider } from "utils";
import { ChainId } from "./utils.d";
import { getProvider } from "./providers";

export function isValidAddress(address: string) {
return ethers.utils.isAddress(address);
return utils.isAddress(address);
}

export function getAddress(address: string) {
return ethers.utils.getAddress(address);
return utils.getAddress(address);
}

export const noContractCode = "0x";
Expand Down
3 changes: 1 addition & 2 deletions src/utils/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ChainId,
hubPoolChainId,
hubPoolAddress,
getProvider,
getConfigStoreAddress,
queriesTable,
FLAT_RELAY_CAPITAL_FEE,
Expand All @@ -25,7 +24,7 @@ import {
wbtcLpCushion,
daiLpCushion,
} from "./constants";

import { getProvider } from "./providers";
import { parseEther, tagAddress } from "./format";
import { getConfig } from "utils";
import getApiEndpoint from "./serverless-api";
Expand Down
6 changes: 4 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert";
import { Signer } from "./ethers";
import * as constants from "./constants";
import * as providerUtils from "./providers";
import {
HubPool,
HubPool__factory,
Expand Down Expand Up @@ -73,7 +74,7 @@ export class ConfigClient {
}
getSpokePool(chainId: constants.ChainId, signer?: Signer): SpokePool {
const address = this.getSpokePoolAddress(chainId);
const provider = signer ?? constants.getProvider(chainId);
const provider = signer ?? providerUtils.getProvider(chainId);
return SpokePool__factory.connect(address, provider);
}
getHubPoolChainId(): constants.ChainId {
Expand All @@ -90,7 +91,8 @@ export class ConfigClient {
}
getHubPool(signer?: Signer): HubPool {
const address = this.getHubPoolAddress();
const provider = signer ?? constants.getProvider(this.getHubPoolChainId());
const provider =
signer ?? providerUtils.getProvider(this.getHubPoolChainId());
return HubPool__factory.connect(address, provider);
}
filterRoutes(query: Partial<constants.Route>): constants.Routes {
Expand Down
40 changes: 0 additions & 40 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,46 +465,6 @@ assert(
export function isSupportedChainId(chainId: number): chainId is ChainId {
return chainId in ChainId;
}
export const providerUrls: [ChainId, string][] = [
[ChainId.MAINNET, `https://mainnet.infura.io/v3/${infuraId}`],
[ChainId.ARBITRUM, ArbitrumProviderUrl],
[ChainId.POLYGON, PolygonProviderUrl],
[ChainId.OPTIMISM, `https://optimism-mainnet.infura.io/v3/${infuraId}`],
[ChainId.BOBA, `https://mainnet.boba.network`],
[ChainId.RINKEBY, `https://rinkeby.infura.io/v3/${infuraId}`],
[ChainId.KOVAN, `https://kovan.infura.io/v3/${infuraId}`],
[ChainId.KOVAN_OPTIMISM, `https://optimism-kovan.infura.io/v3/${infuraId}`],
[
ChainId.ARBITRUM_RINKEBY,
`https://arbitrum-rinkeby.infura.io/v3/${infuraId}`,
],
[ChainId.GOERLI, `https://goerli.infura.io/v3/${infuraId}`],
[ChainId.MUMBAI, `https://polygon-mumbai.infura.io/v3/${infuraId}`],
];
export const providerUrlsTable: Record<number, string> =
Object.fromEntries(providerUrls);

export const providers: [number, ethers.providers.StaticJsonRpcProvider][] =
providerUrls.map(([chainId, url]) => {
return [chainId, new ethers.providers.StaticJsonRpcProvider(url)];
});
export const providersTable: Record<
number,
ethers.providers.StaticJsonRpcProvider
> = Object.fromEntries(providers);

export function getProvider(
chainId: ChainId = hubPoolChainId
): ethers.providers.StaticJsonRpcProvider {
// Requires for Cypress testing. Only use the injected test provider if isCypress flag has been added to the window object..
if ((window as any).isCypress) {
const provider: ethers.providers.JsonRpcProvider = (window as any).ethereum
.provider;

return provider;
}
return providersTable[chainId];
}

export function getConfigStoreAddress(
chainId: ChainId = hubPoolChainId
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from "./query-keys";
export * from "./ethers";
export * from "./config";
export * from "./matomo";
export * from "./providers";
53 changes: 53 additions & 0 deletions src/utils/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ethers } from "ethers";
import { ChainId } from "./utils.d";
import { hubPoolChainId } from "./constants";
const infuraId = process.env.REACT_APP_PUBLIC_INFURA_ID || "";
const ArbitrumProviderUrl =
process.env.REACT_APP_CHAIN_42161_PROVIDER_URL ||
`https://arbitrum-mainnet.infura.io/v3/${infuraId}`;

const PolygonProviderUrl =
process.env.REACT_APP_CHAIN_137_PROVIDER_URL ||
`https://polygon-mainnet.infura.io/v3/${infuraId}`;

export const providerUrls: [ChainId, string][] = [
[ChainId.MAINNET, `https://mainnet.infura.io/v3/${infuraId}`],
[ChainId.ARBITRUM, ArbitrumProviderUrl],
[ChainId.POLYGON, PolygonProviderUrl],
[ChainId.OPTIMISM, `https://optimism-mainnet.infura.io/v3/${infuraId}`],
[ChainId.BOBA, `https://mainnet.boba.network`],
[ChainId.RINKEBY, `https://rinkeby.infura.io/v3/${infuraId}`],
[ChainId.KOVAN, `https://kovan.infura.io/v3/${infuraId}`],
[ChainId.KOVAN_OPTIMISM, `https://optimism-kovan.infura.io/v3/${infuraId}`],
[
ChainId.ARBITRUM_RINKEBY,
`https://arbitrum-rinkeby.infura.io/v3/${infuraId}`,
],
[ChainId.GOERLI, `https://goerli.infura.io/v3/${infuraId}`],
[ChainId.MUMBAI, `https://polygon-mumbai.infura.io/v3/${infuraId}`],
];

export const providerUrlsTable: Record<number, string> =
Object.fromEntries(providerUrls);

export const providers: [number, ethers.providers.StaticJsonRpcProvider][] =
providerUrls.map(([chainId, url]) => {
return [chainId, new ethers.providers.StaticJsonRpcProvider(url)];
});
export const providersTable: Record<
number,
ethers.providers.StaticJsonRpcProvider
> = Object.fromEntries(providers);

export function getProvider(
chainId: ChainId = hubPoolChainId
): ethers.providers.StaticJsonRpcProvider {
// Requires for Cypress testing. Only use the injected test provider if isCypress flag has been added to the window object..
if ((window as any).isCypress) {
const provider: ethers.providers.JsonRpcProvider = (window as any).ethereum
.provider;

return provider;
}
return providersTable[chainId];
}
14 changes: 14 additions & 0 deletions src/utils/tests/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getCode } from "../address";

const V_ETH = process.env.REACT_APP_V_ETH || "";
const UMA_ADDRESS = process.env.REACT_APP_UMA_ADDRESS || "";

describe("#getCode", () => {
it("return 0x when address is not a Contract", () => {
return expect(getCode(V_ETH, 1)).resolves.toBe("0x");
});

it("returns a value different than 0x when address is a Contract", () => {
return expect(getCode(UMA_ADDRESS, 1)).resolves.not.toBe("0x");
});
});
8 changes: 8 additions & 0 deletions src/utils/tests/weiMath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toWeiSafe } from "../weiMath";

describe("toWeiSafe", () => {
it("Converts the value without an error", () => {
const toBigNum = toWeiSafe("1").toString();
expect(toBigNum).toEqual("1000000000000000000");
});
});
16 changes: 16 additions & 0 deletions src/utils/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Chains and Tokens section */
export enum ChainId {
MAINNET = 1,
OPTIMISM = 10,
ARBITRUM = 42161,
BOBA = 288,
POLYGON = 137,
// testnets
RINKEBY = 4,
KOVAN = 42,
KOVAN_OPTIMISM = 69,
ARBITRUM_RINKEBY = 421611,
GOERLI = 5,
// Polygon testnet
MUMBAI = 80001,
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "src"
"baseUrl": "src",
},
"include": ["src", "test", "api"]
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19659,6 +19659,13 @@ jest-each@^26.6.0, jest-each@^26.6.2:
jest-util "^26.6.2"
pretty-format "^26.6.2"

jest-environment-hardhat@^1.1.8:
version "1.1.8"
resolved "https://registry.yarnpkg.com/jest-environment-hardhat/-/jest-environment-hardhat-1.1.8.tgz#36e08cb087a8f0ca7e64cf60c221f898fac4d79f"
integrity sha512-1D5dix/cKR3IgS4MMImMbvvWXClUeFRvAo+E17JY6UBhmP29Kc8yrUCMG7r3g1JSvdBYBe64Nl7cqpydJmZMog==
dependencies:
setimmediate "^1.0.5"

jest-environment-jsdom@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/jest-environment-jsdom/-/jest-environment-jsdom-26.6.2.tgz#78d09fe9cf019a357009b9b7e1f101d23bd1da3e"
Expand Down

2 comments on commit 7da0ffb

@vercel
Copy link

@vercel vercel bot commented on 7da0ffb Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

goerli-frontend-v2 – ./

goerli-frontend-v2.vercel.app
goerli-frontend-v2-uma.vercel.app
goerli-frontend-v2-git-master-uma.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 7da0ffb Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.