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

[TS] Refactor TS module #759

Merged
merged 5 commits into from
May 13, 2020
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
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "@gnosis.pm/dex-contracts",
"version": "0.2.15",
"description": "Contracts for dFusion multi-token batch auction exchange",
"main": "src/index.js",
"module": "build/esm/index.js",
"jsnext:main": "build/esm/index.js",
"types": "src/index.d.ts",
"main": "build/common/src/index.js",
"types": "build/common/src/index.d.ts",
"ts:main": "src/index.ts",
"module": "build/esm/src/index.js",
"jsnext:main": "build/esm/src/index.js",
"directories": {
"test": "test"
},
Expand All @@ -29,10 +30,9 @@
"prettier:solidity": "prettier --write 'contracts/**/*.sol'",
"prettier:ts": "prettier --write './**/*.{js,ts}'",
"spread-orders": "npx truffle exec scripts/place_spread_orders.js",
"ts": "yarn ts-contracts && yarn ts-check && yarn tsc:commonjs && yarn tsc:esm",
"ts": "yarn ts-contracts && yarn tsc:commonjs && yarn tsc:esm",
"tsc:commonjs": "rimraf build/common && tsc --outDir build/common",
"tsc:esm": "rimraf build/esm && tsc -m es6 -t esnext --outDir build/esm",
"ts-check": "tsc --esModuleInterop --noEmit src/index.d.ts",
"ts-contracts": "yarn ts-contracts:web3 && yarn ts-contracts:truffle",
"ts-contracts:truffle": "typechain --target trufflehotfix --outDir build/truffle-typings \"build/contracts/*.json\"",
"ts-contracts:web3": "typechain --target web3-v1 --outDir build/types \"build/contracts/{BatchExchange,BatchExchangeViewer}.json\"",
Expand Down
33 changes: 33 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* This module re-exports the contract type definitions and Truffle artifacts
* for the main Gnosis Protocol contracts:
* - `BatchExchange`: The main contract for the Gnosis Protocol that handles all
* the balances and auctions.
* - `BatchExchangeViewer`: A supplementary viewer contract with more efficient
* methods for reading EVM data.
*
* @packageDocumentation
*/

import type { AbiItem } from "web3-utils";
import type { BatchExchange } from "../build/types/BatchExchange";
import type { BatchExchangeViewer } from "../build/types/BatchExchangeViewer";
import BatchExchangeArtifact from "../build/contracts/BatchExchange.json";
import BatchExchangeViewerArtifact from "../build/contracts/BatchExchangeViewer.json";

export {
BatchExchange,
BatchExchangeArtifact,
BatchExchangeViewer,
BatchExchangeViewerArtifact,
};

export interface ContractArtifact {
abi: AbiItem[];
networks: {
[key: string]: {
address: string;
transactionHash: string;
};
};
}
79 changes: 0 additions & 79 deletions src/index.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/index.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* dex-contracts
*
* This NPM package provides smart contract artifacts used in the dFusion
* protocol. Additional tools for interacting with the dFusion contracts and
* performing migrations are also provided.
*
* @packageDocumentation
*/

export * from "./balance_reader";
export * from "./contracts";
export * from "./encoding";
export * from "./fraction";
export * from "./onchain_reading";
export * from "./orderbook";
export * from "./streamed";
9 changes: 4 additions & 5 deletions src/streamed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import Web3 from "web3";
import { BlockNumber, TransactionReceipt } from "web3-core";
import { Contract } from "web3-eth-contract";
import { AbiItem } from "web3-utils";
import {
BatchExchange,
BatchExchangeArtifact,
ContractArtifact,
IndexedOrder,
} from "..";
} from "../contracts";
import { IndexedOrder } from "../encoding";
import { AnyEvent } from "./events";
import { AuctionState } from "./state";

Expand Down Expand Up @@ -119,7 +118,7 @@ export class StreamedOrderbook {
): Promise<StreamedOrderbook> {
const [contract, tx] = await deployment<BatchExchange>(
web3,
BatchExchangeArtifact,
BatchExchangeArtifact as ContractArtifact,
);
const orderbook = new StreamedOrderbook(web3, contract, tx.blockNumber, {
...DEFAULT_ORDERBOOK_OPTIONS,
Expand Down Expand Up @@ -323,7 +322,7 @@ export async function deployment<C extends Contract>(
}

const tx = await web3.eth.getTransactionReceipt(network.transactionHash);
const contract = new web3.eth.Contract(abi as AbiItem[], network.address);
const contract = new web3.eth.Contract(abi, network.address);

return [contract as C, tx];
}
3 changes: 2 additions & 1 deletion src/streamed/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert";
import { EventData } from "web3-eth-contract";
import { BatchExchange, IndexedOrder } from "..";
import { BatchExchange } from "../contracts";
import { IndexedOrder } from "../encoding";
import { OrderbookOptions } from ".";
import { AnyEvent, Event } from "./events";

Expand Down
8 changes: 5 additions & 3 deletions test/models/streamed/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import Web3 from "web3";
import {
BatchExchangeViewerArtifact,
BatchExchangeViewer,
ContractArtifact,
IndexedOrder,
StreamedOrderbook,
deployment,
getOpenOrders,
} from "../../..";
import { StreamedOrderbook, deployment } from "../../../src/streamed";
} from "../../../src";

describe("Streamed Orderbook", () => {
describe("init", () => {
Expand Down Expand Up @@ -40,7 +42,7 @@ describe("Streamed Orderbook", () => {
const web3 = new Web3(url);
const [viewer] = await deployment<BatchExchangeViewer>(
web3,
BatchExchangeViewerArtifact,
BatchExchangeViewerArtifact as ContractArtifact,
);

console.debug("==> building streamed orderbook...");
Expand Down
3 changes: 1 addition & 2 deletions test/models/streamed/state.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from "chai";
import { BatchExchange } from "../../..";
import { DEFAULT_ORDERBOOK_OPTIONS } from "../../../src/streamed";
import { BatchExchange, DEFAULT_ORDERBOOK_OPTIONS } from "../../../src";
import { AuctionState } from "../../../src/streamed/state";
import { AnyEvent, EventName, EventValues } from "../../../src/streamed/events";

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
Copy link
Contributor

Choose a reason for hiding this comment

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

What are these .d.ts files anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type definitions. They allow other TypeScript projects to use type information from the original TS code.

// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
Expand Down