Skip to content

Commit

Permalink
prepare npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
peiru.li@finbook.co committed Jan 29, 2019
1 parent d00159a commit 7397719
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ flow-typed
lilithEquities.json
sachielAssets.json
*.txt
keys
keys
*.tgz
33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "duo-contract-util",
"name": "@finbook/duo-contract-wrapper",
"version": "1.0.0",
"private": true,
"description": "DUO Contract Util",
"description": "DUO Contract Wrapper",
"author": "FinBook Pte Ltd",
"main": "dist/index.ts",
"types": "dist/index.d.ts",
"scripts": {
"test": "jest",
"prebuild": "rimraf dist",
"build": "tsc --declarationDir dist -d --emitDeclarationOnly --allowJs false && tsc",
"tslint": "tslint -c tslint.json -p tsconfig.json src/**/*.ts{,x} --fix"
},
"jest": {
Expand All @@ -27,12 +30,14 @@
},
"devDependencies": {
"@babel/polyfill": "^7.2.5",
"@types/jest": "^23.3.12",
"@types/node": "^10.12.18",
"@types/bn.js": "^4.11.4",
"@types/jest": "^23.3.13",
"@types/node": "^10.12.19",
"coveralls": "^3.0.2",
"ganache-core": "^2.3.3",
"jest": "^23.6.0",
"pre-commit": "^1.2.2",
"rimraf": "^2.6.3",
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"typescript": "^3.1.6"
Expand All @@ -43,6 +48,20 @@
"abi-decoder": "^1.2.0",
"bip39": "^2.5.0",
"ethereumjs-wallet": "^0.6.3",
"web3": "1.0.0-beta.35"
}
"web3": "^1.0.0-beta.37"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FinBook/duo-contract-wrapper.git"
},
"keywords": [
"DUO",
"Contract",
"Wrapper"
],
"license": "ISC",
"bugs": {
"url": "https://github.com/FinBook/duo-contract-wrapper/issues"
},
"homepage": "https://github.com/FinBook/duo-contract-wrapper#readme"
}
9 changes: 5 additions & 4 deletions src/BaseContractWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Contract } from 'web3/types';
import Web3Wrapper from './Web3Wrapper';
import { Web3Wrapper } from './Web3Wrapper';
const abiDecoder = require('abi-decoder');

export default abstract class BaseContractWrapper {
export abstract class BaseContractWrapper {
public readonly web3Wrapper: Web3Wrapper;
public readonly address: string;
public readonly abi: any[];
public readonly events: string[] = [];
public contract: Contract;
public contract: any;

constructor(web3Wrapper: Web3Wrapper, abi: any[], contractAddress: string) {
this.web3Wrapper = web3Wrapper;
Expand All @@ -32,3 +31,5 @@ export default abstract class BaseContractWrapper {
return abiDecoder.decodeMethod(input);
}
}

export default BaseContractWrapper;
2 changes: 1 addition & 1 deletion src/DualClassWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import '@babel/polyfill';

import * as CST from './constants';
import DualClassWrapper from './DualClassWrapper';
import {DualClassWrapper} from './DualClassWrapper';
import { ITransactionOption } from './types';

test('convertCustodianState', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/DualClassWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dualClassAbi from './static/DualClassCustodian.json';
import { ICustodianAddresses, IDualClassStates, ITransactionOption } from './types';
import Web3Wrapper from './Web3Wrapper';

export default class DualClassWrapper extends BaseContractWrapper {
export class DualClassWrapper extends BaseContractWrapper {
public readonly events = [
CST.EVENT_UPDATE_ROLE_MANAGER,
CST.EVENT_UPDATE_OPERATOR,
Expand Down Expand Up @@ -298,3 +298,5 @@ export default class DualClassWrapper extends BaseContractWrapper {
return this.contract.methods.users(index).call();
}
}

export default DualClassWrapper;
4 changes: 3 additions & 1 deletion src/EsplanadeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import esplanadeAbi from './static/Esplanade.json';
import { IEsplanadeStates, IVotingData } from './types';
import Web3Wrapper from './Web3Wrapper';

export default class EsplanadeWrapper extends BaseContractWrapper {
export class EsplanadeWrapper extends BaseContractWrapper {
public readonly events = [
CST.EVENT_ADD_ADDRESS,
CST.EVENT_REMOVE_ADDRESS,
Expand Down Expand Up @@ -184,3 +184,5 @@ export default class EsplanadeWrapper extends BaseContractWrapper {
});
}
}

export default EsplanadeWrapper;
8 changes: 6 additions & 2 deletions src/MagiWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import magiAbi from './static/Magi.json';
import { IContractPrice, ITransactionOption } from './types';
import Web3Wrapper from './Web3Wrapper';

export default class MagiWrapper extends BaseContractWrapper {
export class MagiWrapper extends BaseContractWrapper {
public readonly events = [
CST.EVENT_UPDATE_ROLE_MANAGER,
CST.EVENT_UPDATE_OPERATOR,
Expand Down Expand Up @@ -42,7 +42,9 @@ export default class MagiWrapper extends BaseContractWrapper {
if (!this.web3Wrapper.isLocal()) return this.web3Wrapper.wrongEnvReject();
return this.contract.methods
.commitPrice(this.web3Wrapper.toWei(price), timeInSecond)
.send(await this.web3Wrapper.getTransactionOption(account, CST.COMMIT_PRICE_GAS, option));
.send(
await this.web3Wrapper.getTransactionOption(account, CST.COMMIT_PRICE_GAS, option)
);
}

public async getLastPrice(): Promise<IContractPrice> {
Expand All @@ -53,3 +55,5 @@ export default class MagiWrapper extends BaseContractWrapper {
};
}
}

export default MagiWrapper;
40 changes: 32 additions & 8 deletions src/Web3Wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import Web3 from 'web3';
import { Contract, EventLog } from 'web3/types';
import * as CST from './constants';
import { kovan, mainnet } from './contractAddresses';
import erc20Abi from './static/ERC20.json';
import { IContractAddresses, IEvent, ITransactionOption, Wallet } from './types';

const Web3 = require('web3');
const HDWalletProvider = require('./external/HDWalletProvider');
const BigNumber = require('bignumber.js');
const ProviderEngine = require('web3-provider-engine');
const FetchSubprovider = require('web3-provider-engine/subproviders/fetch');
const createLedgerSubprovider = require('@ledgerhq/web3-subprovider').default;
const TransportU2F = require('@ledgerhq/hw-transport-u2f').default;

export default class Web3Wrapper {
public web3: Web3;
export class Web3Wrapper {
public web3: any;
public wallet: Wallet = Wallet.None;
public accountIndex: number = 0;
private live: boolean;
Expand Down Expand Up @@ -253,7 +252,20 @@ export default class Web3Wrapper {
return this.web3.eth.getTransactionReceipt(txHash);
}

public static parseEvent(eventLog: EventLog, timestamp: number): IEvent {
public static parseEvent(
eventLog: {
event: string;
address: string;
returnValues: any;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
raw?: { data: string; topics: string[] };
},
timestamp: number
): IEvent {
const returnValue = eventLog.returnValues;
const output: IEvent = {
contractAddress: eventLog.address,
Expand All @@ -272,18 +284,28 @@ export default class Web3Wrapper {
}

public static pullEvents(
contract: Contract,
contract: any,
start: number,
end: number,
event: string
): Promise<EventLog[]> {
): Promise<{
event: string
address: string
returnValues: any
logIndex: number
transactionIndex: number
transactionHash: string
blockHash: string
blockNumber: number
raw?: { data: string, topics: string[] }
}[]> {
return contract.getPastEvents(event, {
fromBlock: start,
toBlock: end
});
}

public createContract(abi: any[], address: string): Contract {
public createContract(abi: any[], address: string): any {
return new this.web3.eth.Contract(abi, address);
}

Expand All @@ -307,3 +329,5 @@ export default class Web3Wrapper {
};
}
}

export default Web3Wrapper;
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { BaseContractWrapper } from './BaseContractWrapper';
export { DualClassWrapper } from './DualClassWrapper';
export * from './contractAddresses';
import * as Constants from './constants';
export { Constants };
export { EsplanadeWrapper } from './EsplanadeWrapper';
export { MagiWrapper } from './MagiWrapper';
export * from './types';
export { Web3Wrapper } from './Web3Wrapper';
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export interface ICustodianStates extends IManagedStates {
}
export interface ICustodianWrappers {
[type: string]: {
[tenor: string]: DualClassWrapper
}
[tenor: string]: DualClassWrapper;
};
}

export interface IDualClassStates extends ICustodianStates {
Expand Down Expand Up @@ -112,12 +112,12 @@ export interface IContractAddress {
}

export interface ICustodianContractAddresses {
[tenor: string]: ICustodianContractAddress,
[tenor: string]: ICustodianContractAddress;
}

export interface IContractAddresses {
Custodians: {
[type: string]: ICustodianContractAddresses
[type: string]: ICustodianContractAddresses;
};
MultiSigManagers: IContractAddress[];
Oracles: IContractAddress[];
Expand Down
15 changes: 10 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "./dist/",
"outDir": "dist",
"target": "es5",
"sourceMap": true,
"sourceMap": false,
"lib": ["es2017", "es2015", "es6"],
"noImplicitAny": true,
"strict": true,
Expand All @@ -15,6 +15,11 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"allowJs": true
}
}
"allowJs": true,
"declarationMap": false
},
"exclude": [
"**/*.test.*",
"coverage"
]
}

0 comments on commit 7397719

Please sign in to comment.