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

kosu.js: move kosu helper class to separate file #183

Merged
merged 4 commits into from Jul 24, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

move Kosu class to separate file

  • Loading branch information
hrharder committed Jul 23, 2019
commit 56debb6e66aceff7be7bdcd1a5ea49eb49294493
@@ -0,0 +1,166 @@
import { Web3Wrapper } from "@0x/web3-wrapper";
import { artifacts } from "@kosu/system-contracts";
import Web3 from "web3";

import { EventEmitter } from "./EventEmitter";
import { KosuToken } from "./KosuToken";
import { OrderGateway } from "./OrderGateway";
import { OrderHelper } from "./OrderHelper";
import { PosterRegistry } from "./PosterRegistry";
import { Signature } from "./Signature";
import { Treasury } from "./Treasury";
import { NULL_ADDRESS, toBytes32 } from "./utils";
import { ValidatorRegistry } from "./ValidatorRegistry";
import { Voting } from "./Voting";

// tslint:disable-next-line: no-var-requires
const version = process.env.npm_package_version || require("../package.json").version;

/**
* The `Kosu` class assists in interacting with the Kosu contract system and
* network.
*
* Instances of the `Kosu` class (`kosu`) provide access to read and write
* access to the Kosu contract system via the `web3` library and connection to
* an Ethereum node, or remote provider (read-only).
*
* If a compatible Ethereum network is detected (there has been a deployment of
* the Kosu contract system with the same `networkId`), JavaScript wrappers of
* each contract will be instantiated and can used to interact will the deployed
* contracts.
*/
export class Kosu {
// WEB3

/**
* The primary `web3` instance provides access to an Ethereum node's JSONRPC
* API and utilities. This instance is used to interact with each Kosu contract
* wrapper and can be used to directly access the `web3` API.
*/
public readonly web3: Web3;

/**
* The `web3Wrapper` is used for some extended features, and provides
* a higher-level API to the same underlying `web3` provider. It it used to
* manage the various deployed contracts ABI's.
*/
public readonly web3Wrapper: Web3Wrapper;

// KOSU CONTRACTS

/**
* The `orderGateway` instance provides access to the `OrderGateway` contract
* which is used to direct settlement of orders to their corresponding
* `SubContract` and underlying settlement logic.
*
* It can also be used to load the required `arguments` for a specific order
* type, based on a `SubContract` address.
*/
public readonly orderGateway: OrderGateway;

/**
* The `orderHelper` instance provides methods for generating and signing
* maker orders, signing orders for submission to the Kosu network, and
* submitting orders for settlement on the Ethereum blockchain via the
* `OrderGateway` and the `SubContract` specified in the maker order.
*/
public readonly orderHelper: OrderHelper;

/**
* The `kosuToken` provides methods for interacting with the KOSU ERC-20 token.
*/
public readonly kosuToken: KosuToken;

/**
* The `treasury` instance provides access to functionality of the Kosu
* `Treasury` contract, such as deposits/withdrawals and allowance management.
*/
public readonly treasury: Treasury;

/**
* The `voting` instance provides access to the Kosu `voting` contract and
* allows users to participate in voting on governance measures, and claiming
* rewards for correctly participating in winning polls.
*/
public readonly voting: Voting;

/**
* The `posterRegistry` instance enables users to interact with the Kosu
* `PosterRegistry` contract to bond and un-bond KOSU tokens to access the
* order relay feature of the Kosu network.
*/
public readonly posterRegistry: PosterRegistry;

/**
* The `validatorRegistry` instance enables interaction with the `ValidatorRegistry`
* token-curated registry system. The system enables users to submit proposals
* to become validators and challenge existing validators and pending proposals.
*
* It also provides query access to past challenges and the current listings.
*/
public readonly validatorRegistry: ValidatorRegistry;

/**
* The `eventEmitter` enables the query of and subscription to decoded event
* logs from the Kosu `EventEmitter` contract.
*/
public readonly eventEmitter: EventEmitter;

// UTILS

/**
* Common and helpful utility methods and constants for interacting with the
* Kosu contract system and Ethereum blockchain.
*/
public readonly utils: KosuUtils;

/**
* Utilities for generating and recovering signatures for use within the
* Kosu system.
*/
public readonly Signature: Signature;

/**
* The current `@kosu/kosu.js` package version.
*/
public readonly version: string;

/**
* Create a new `kosu` instance and instantiate wrappers for each Kosu system
* contract.
*
* If provided with no arguments, `kosu` will be generated in a read-only
* state, with a default Ropsten test-network provider. Methods that query
* constants and the state of the contract system will work, the submission
* of transactions and generation of signatures will not be possible.
*
* @param options An options object used to configure `kosu` and the configured
* contract instances. A custom `provider` may be supplied.
*/
constructor(options: KosuOptions = { provider: "https://ropsten.infura.io" }) {
// Configuring web3
this.web3 = new Web3(options.provider);
this.web3Wrapper = new Web3Wrapper(this.web3.currentProvider);
options.web3 = this.web3;
options.web3Wrapper = this.web3Wrapper;

for (const contractName of Object.keys(artifacts)) {
this.web3Wrapper.abiDecoder.addABI(artifacts[contractName].compilerOutput.abi, contractName);
}

// Initializing contract objects
this.orderGateway = new OrderGateway(options);
this.kosuToken = new KosuToken(options);
this.treasury = new Treasury(options, this.kosuToken);
this.voting = new Voting(options, this.treasury);
this.posterRegistry = new PosterRegistry(options, this.treasury);
this.validatorRegistry = new ValidatorRegistry(options, this.treasury);
this.orderHelper = new OrderHelper(this.web3, this.orderGateway);
this.eventEmitter = new EventEmitter(options);

// Utilities
this.utils = { toBytes32, NULL_ADDRESS };
this.Signature = Signature;
this.version = version;
}
}
@@ -1,181 +1,13 @@
import { Web3Wrapper } from "@0x/web3-wrapper";
import { artifacts } from "@kosu/system-contracts";
import Web3 from "web3";

import { EventEmitter } from "./EventEmitter";
import { KosuToken } from "./KosuToken";
import { OrderGateway } from "./OrderGateway";
import { OrderHelper } from "./OrderHelper";
import { PosterRegistry } from "./PosterRegistry";
import { Signature } from "./Signature";
import { Treasury } from "./Treasury";
import { NULL_ADDRESS, toBytes32 } from "./utils";
import { ValidatorRegistry } from "./ValidatorRegistry";
import { Voting } from "./Voting";

// tslint:disable-next-line: no-var-requires
const version = process.env.npm_package_version || require("../package.json").version;

/**
* The `Kosu` class assists in interacting with the Kosu contract system and
* network.
*
* Instances of the `Kosu` class (`kosu`) provide access to read and write
* access to the Kosu contract system via the `web3` library and connection to
* an Ethereum node, or remote provider (read-only).
*
* If a compatible Ethereum network is detected (there has been a deployment of
* the Kosu contract system with the same `networkId`), JavaScript wrappers of
* each contract will be instantiated and can used to interact will the deployed
* contracts.
*/
export class Kosu {
// WEB3

/**
* The primary `web3` instance provides access to an Ethereum node's JSONRPC
* API and utilities. This instance is used to interact with each Kosu contract
* wrapper and can be used to directly access the `web3` API.
*/
public readonly web3: Web3;

/**
* The `web3Wrapper` is used for some extended features, and provides
* a higher-level API to the same underlying `web3` provider. It it used to
* manage the various deployed contracts ABI's.
*/
public readonly web3Wrapper: Web3Wrapper;

// KOSU CONTRACTS

/**
* The `orderGateway` instance provides access to the `OrderGateway` contract
* which is used to direct settlement of orders to their corresponding
* `SubContract` and underlying settlement logic.
*
* It can also be used to load the required `arguments` for a specific order
* type, based on a `SubContract` address.
*/
public readonly orderGateway: OrderGateway;

/**
* The `orderHelper` instance provides methods for generating and signing
* maker orders, signing orders for submission to the Kosu network, and
* submitting orders for settlement on the Ethereum blockchain via the
* `OrderGateway` and the `SubContract` specified in the maker order.
*/
public readonly orderHelper: OrderHelper;

/**
* The `kosuToken` provides methods for interacting with the KOSU ERC-20 token.
*/
public readonly kosuToken: KosuToken;

/**
* The `treasury` instance provides access to functionality of the Kosu
* `Treasury` contract, such as deposits/withdrawals and allowance management.
*/
public readonly treasury: Treasury;

/**
* The `voting` instance provides access to the Kosu `voting` contract and
* allows users to participate in voting on governance measures, and claiming
* rewards for correctly participating in winning polls.
*/
public readonly voting: Voting;

/**
* The `posterRegistry` instance enables users to interact with the Kosu
* `PosterRegistry` contract to bond and un-bond KOSU tokens to access the
* order relay feature of the Kosu network.
*/
public readonly posterRegistry: PosterRegistry;

/**
* The `validatorRegistry` instance enables interaction with the `ValidatorRegistry`
* token-curated registry system. The system enables users to submit proposals
* to become validators and challenge existing validators and pending proposals.
*
* It also provides query access to past challenges and the current listings.
*/
public readonly validatorRegistry: ValidatorRegistry;

/**
* The `eventEmitter` enables the query of and subscription to decoded event
* logs from the Kosu `EventEmitter` contract.
*/
public readonly eventEmitter: EventEmitter;

// UTILS

/**
* Common and helpful utility methods and constants for interacting with the
* Kosu contract system and Ethereum blockchain.
*/
public readonly utils: KosuUtils;

/**
* Utilities for generating and recovering signatures for use within the
* Kosu system.
*/
public readonly Signature: Signature;

/**
* The current `@kosu/kosu.js` package version.
*/
public readonly version: string;

/**
* Create a new `kosu` instance and instantiate wrappers for each Kosu system
* contract.
*
* If provided with no arguments, `kosu` will be generated in a read-only
* state, with a default Ropsten test-network provider. Methods that query
* constants and the state of the contract system will work, the submission
* of transactions and generation of signatures will not be possible.
*
* @param options An options object used to configure `kosu` and the configured
* contract instances. A custom `provider` may be supplied.
*/
constructor(options: KosuOptions = { provider: "https://ropsten.infura.io" }) {
// Configuring web3
this.web3 = new Web3(options.provider);
this.web3Wrapper = new Web3Wrapper(this.web3.currentProvider);
options.web3 = this.web3;
options.web3Wrapper = this.web3Wrapper;

for (const contractName of Object.keys(artifacts)) {
this.web3Wrapper.abiDecoder.addABI(artifacts[contractName].compilerOutput.abi, contractName);
}

// Initializing contract objects
this.orderGateway = new OrderGateway(options);
this.kosuToken = new KosuToken(options);
this.treasury = new Treasury(options, this.kosuToken);
this.voting = new Voting(options, this.treasury);
this.posterRegistry = new PosterRegistry(options, this.treasury);
this.validatorRegistry = new ValidatorRegistry(options, this.treasury);
this.orderHelper = new OrderHelper(this.web3, this.orderGateway);
this.eventEmitter = new EventEmitter(options);

// Utilities
this.utils = { toBytes32, NULL_ADDRESS };
this.Signature = Signature;
this.version = version;
}
}

export { Kosu } from "./Kosu";

export { EventEmitter } from "./EventEmitter";
export { KosuToken } from "./KosuToken";
export { OrderGateway } from "./OrderGateway";
export { OrderHelper } from "./OrderHelper";
export { PosterRegistry } from "./PosterRegistry";
export { Signature } from "./Signature";
export { Treasury } from "./Treasury";
export { NULL_ADDRESS, toBytes32 } from "./utils";
export { ValidatorRegistry } from "./ValidatorRegistry";
export { Voting } from "./Voting";
export { OrderSerializer } from "./OrderSerializer";

export {
KosuToken,
OrderGateway,
OrderHelper,
PosterRegistry,
Signature,
Treasury,
ValidatorRegistry,
Voting,
toBytes32,
NULL_ADDRESS,
};
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.