Skip to content

Commit

Permalink
improve(common): convert common to typescript (UMAprotocol#3268)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrice32 authored Aug 4, 2021
1 parent 09ee177 commit 22cf5fc
Show file tree
Hide file tree
Showing 87 changed files with 1,369 additions and 1,067 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ coverage
coverage.json
out.log
.GckmsOverride.js
.GckmsOverride.ts
docs
modules
ui
Expand Down
24 changes: 0 additions & 24 deletions packages/common/browser.js

This file was deleted.

15 changes: 0 additions & 15 deletions packages/common/index.js

This file was deleted.

19 changes: 10 additions & 9 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Common js utilities used by other UMA packages",
"homepage": "http://umaproject.org",
"license": "AGPL-3.0-or-later",
"main": "index.js",
"main": "./dist/index.js",
"browser": {
"./index.js": "./browser.js"
"./index.js": "./dist/browser.js"
},
"types": "./dist/index.d.ts",
"publishConfig": {
Expand All @@ -25,22 +25,20 @@
"url": "https://github.com/UMAprotocol/protocol/issues"
},
"files": [
"/src/**/*.js",
"/dist/**/*",
"browser.js"
"/dist/**/*"
],
"dependencies": {
"@ethersproject/bignumber": "^5.0.5",
"@google-cloud/kms": "^0.3.0",
"@google-cloud/storage": "^2.4.2",
"@google-cloud/kms": "^2.3.1",
"@google-cloud/storage": "^5.8.5",
"@maticnetwork/maticjs": "^2.0.40",
"@truffle/hdwallet-provider": "^1.2.3",
"@umaprotocol/truffle-ledger-provider": "^1.0.5",
"@umaprotocol/ynatm": "^0.0.1",
"abi-decoder": "github:UMAprotocol/abi-decoder",
"bignumber.js": "^8.0.1",
"chalk-pipe": "^3.0.0",
"dotenv": "^6.2.0",
"dotenv": "^9.0.0",
"eth-crypto": "^1.7.0",
"hardhat": "^2.5.0",
"hardhat-deploy": "^0.8.11",
Expand All @@ -64,6 +62,9 @@
"@tsconfig/node14": "^1.0.0",
"@types/ethereum-protocol": "^1.0.0",
"@types/mocha": "^5.2.7",
"ethers": "^5.4.2"
"web3-core": "^1.5.0",
"web3-eth-contract": "^1.5.0",
"ethers": "^5.4.2",
"@types/lodash.uniqby": "^4.7.6"
}
}
34 changes: 23 additions & 11 deletions packages/common/src/AbiUtils.js → packages/common/src/AbiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,39 @@
// pulled from the core/build/contracts directory. Example usage:
// getAbiDecoder().decodeMethod(data); // This decodes the txn data into the function name and arguments.

const abiDecoder = require("abi-decoder");
import abiDecoder from "abi-decoder";

function importAll(r) {
export type AbiDecoder = typeof abiDecoder;

interface Context {
keys: () => string[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(input: string): any;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function importAll(r: Context): any[] {
return r.keys().map(r);
}

function getAllContracts() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getAllContracts(): any[] {
let importedObjects;

// Note: we use a try here because we don't want to install the require-context package in node.js contexts where
// it won't work.
if (process.browser) {
const castedProcess = (process as unknown) as { browser: true };
if (castedProcess.browser) {
// This only works in webpack.
// eslint-disable-next-line no-unused-vars
const requireContext = require("require-context");
require("require-context");

// Note: all arguments must be hardcoded here for webpack to bundle the files correctly.
// This line also generates a few build warnings that should be ignored.
const contractContext = require.context("@uma/core/build/contracts/", true, /\.json$/);
const castedRequire = (require as unknown) as {
context: (path: string, useSubdirectories: boolean, regex: RegExp) => Context;
};
const contractContext = castedRequire.context("@uma/core/build/contracts/", true, /\.json$/);

importedObjects = importAll(contractContext);
} else {
Expand All @@ -34,8 +48,8 @@ function getAllContracts() {
const packageDir = path.dirname(require.resolve("@uma/core/package.json"));
const contractsPath = path.join(packageDir, "build/contracts/");

const fileList = fs.readdirSync(contractsPath).filter((name) => name.match(/\.json$/));
importedObjects = fileList.map((filename) => {
const fileList = fs.readdirSync(contractsPath).filter((name: string) => name.match(/\.json$/));
importedObjects = fileList.map((filename: string) => {
const fileContents = fs.readFileSync(path.join(contractsPath, filename));
return JSON.parse(fileContents);
});
Expand All @@ -44,13 +58,11 @@ function getAllContracts() {
return importedObjects;
}

function getAbiDecoder() {
export function getAbiDecoder(): typeof abiDecoder {
const contracts = getAllContracts();
for (const contract of contracts) {
abiDecoder.addABI(contract.abi);
}

return abiDecoder;
}

module.exports = { getAllContracts, getAbiDecoder };
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const { getAbiDecoder } = require("./AbiUtils.js");
import { getAbiDecoder, AbiDecoder } from "./AbiUtils";
import type { BN } from "./types";

let abiDecoder;
let abiDecoder: AbiDecoder;

function decodeTransaction(transaction) {
interface Transaction {
data?: string;
to: string;
value: string | BN;
}

export function decodeTransaction(transaction: Transaction): string {
let returnValue = "";

// Give to and value.
Expand Down Expand Up @@ -33,17 +40,17 @@ function decodeTransaction(transaction) {

const adminPrefix = "Admin ";

function isAdminRequest(identifierUtf8) {
export function isAdminRequest(identifierUtf8: string): boolean {
return identifierUtf8.startsWith(adminPrefix);
}

// Assumes that `identifierUtf8` is an admin request, i.e., `isAdminRequest()` returns true for it.
function getAdminRequestId(identifierUtf8) {
export function getAdminRequestId(identifierUtf8: string): number {
return parseInt(identifierUtf8.slice(adminPrefix.length), 10);
}

// Vote 1 for Yes, 0 for No. Any vote > 0 is technically a Yes, but the 1 is treated as the canonical yes.
const translateAdminVote = (voteValue) => {
export const translateAdminVote = (voteValue: string): string => {
if (!voteValue) {
return "No Vote";
} else {
Expand All @@ -61,5 +68,3 @@ const translateAdminVote = (voteValue) => {
}
}
};

module.exports = { decodeTransaction, isAdminRequest, getAdminRequestId, translateAdminVote };
47 changes: 0 additions & 47 deletions packages/common/src/Constants.js

This file was deleted.

42 changes: 42 additions & 0 deletions packages/common/src/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// The interface names that Finder.sol uses to refer to interfaces in the UMA system.
export const interfaceName = {
FinancialContractsAdmin: "FinancialContractsAdmin",
Oracle: "Oracle",
Registry: "Registry",
Store: "Store",
IdentifierWhitelist: "IdentifierWhitelist",
CollateralWhitelist: "CollateralWhitelist",
AddressWhitelist: "CollateralWhitelist",
OptimisticOracle: "OptimisticOracle",
Bridge: "Bridge",
GenericHandler: "GenericHandler",
MockOracleAncillary: "Oracle",
SinkOracle: "Oracle",
};

// These enforce the maximum number of transactions that can fit within one batch-commit and batch-reveal.
// Based off the current gas limit from Etherscan over the last 6 months of 9950000,
// the following maximum batchCommit, batchReveal and retrieveRewards are possible:
// - batchCommit: 28 commits, 6654676 gas used
// - batchReveal: 58 commits, 5828051 gas used
// - retrieveRewards: 129 commits, 3344083 gas used
// Practically, we set a safe upper bound of 25 batch commits & reveals and 100 retrievals.
export const BATCH_MAX_COMMITS = 25;
export const BATCH_MAX_REVEALS = 25;
export const BATCH_MAX_RETRIEVALS = 100;

// maximum uint256 value: 2^256 - 1
export const MAX_UINT_VAL = "115792089237316195423570985008687907853269984665640564039457584007913129639935";

// maximum allowance allowed by certain ERC20's like UNI: 2^96 - 1
export const MAX_SAFE_ALLOWANCE = "79228162514264337593543950335";

// Max integer that can be safely stored in a vanilla js int.
export const MAX_SAFE_JS_INT = 2147483647;

// 0x0 contract address
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

// Block number of first EMP created.
// https://etherscan.io/tx/0x741ccbf0f9655b0b71e3842d788d58770bd3eb80c8f5bdf4fdec7cd74a776ea3
export const UMA_FIRST_EMP_BLOCK = 10103723;
55 changes: 0 additions & 55 deletions packages/common/src/ContractUtils.js

This file was deleted.

Loading

0 comments on commit 22cf5fc

Please sign in to comment.