Skip to content

Commit

Permalink
initialize db
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhoucao committed Jan 22, 2019
1 parent d7b583e commit 57f3544
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 70 deletions.
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import '@babel/polyfill';
import * as CST from './common/constants';
import ContractService from './services/ContractService';
import MarketDataService from './services/MarketDataService';
import priceUtil from './utils/priceUtil';
import util from './utils/util';

const tool = process.argv[2];
Expand All @@ -25,33 +24,36 @@ util.logInfo(

switch (tool) {
case CST.TRADES:
new MarketDataService(tool, option).startFetching(tool, option);
const tradeMds = new MarketDataService(tool, option);
tradeMds.init().then(() => tradeMds.startFetching(tool, option));
break;
case CST.FETCH_EVENTS:
new ContractService(tool, option).fetchEvent();
const eventCs = new ContractService(tool, option);
eventCs.init().then(() => eventCs.fetchEvent());
break;
case CST.DB_PRICES:
priceUtil.startAggregate(option.period);
const priceMds = new MarketDataService(tool, option);
priceMds.init().then(() => priceMds.startFetching(tool, option));
break;
case CST.CLEAN_DB:
new MarketDataService(tool, option).cleanDb();
const dbMds = new MarketDataService(tool, option);
dbMds.init().then(() => dbMds.cleanDb());
break;
case CST.START_CUSTODIAN:
const kovanManagerAccount = require('./static/kovanManagerAccount.json');
const optAddr = kovanManagerAccount.Beethoven.operator.address;
new ContractService(tool, option).startCustodian(optAddr);
const startCs = new ContractService(tool, option);
startCs.init().then(() => startCs.startCustodian());
break;
case CST.TRIGGER:
util.logInfo('starting trigger process');
new ContractService(tool, option).trigger();
const triggerCs = new ContractService(tool, option);
triggerCs.init().then(() => triggerCs.trigger());
break;
case CST.COMMIT:
util.logInfo('starting commit process');
new ContractService(tool, option).commitPrice();
const commitCs = new ContractService(tool, option);
commitCs.init().then(() => commitCs.commitPrice());
break;
case CST.FETCH_PRICE:
util.logInfo('starting fetchPrice process');
new ContractService(tool, option).fetchPrice();
const fetchCs = new ContractService(tool, option);
fetchCs.init().then(() => fetchCs.fetchPrice());
break;
default:
util.logInfo('no such tool ' + tool);
Expand Down
5 changes: 5 additions & 0 deletions src/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import EsplanadeWrapper from '../../../duo-contract-wrapper/src/EsplanadeWrapper
import MagiWrapper from '../../../duo-contract-wrapper/src/MagiWrapper';
import Web3Wrapper from '../../../duo-contract-wrapper/src/Web3Wrapper';
import { IOption } from '../common/types';
import dbUtil from '../utils/dbUtil';

export default class BaseService {
public web3Wrapper: Web3Wrapper;
Expand All @@ -15,6 +16,10 @@ export default class BaseService {
this.web3Wrapper = new Web3Wrapper(null, option.provider, '', option.live);
}

public init() {
return dbUtil.init(this.tool, this.option, this.web3Wrapper);
}

public createDuoWrappers(): {[type: string]: {[tenor: string]: DualClassWrapper}} {
return {
Beethoven: {
Expand Down
4 changes: 2 additions & 2 deletions src/services/ContractService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test('fetchPrice', async () => {

test('startCustodian, worng type', async () => {
contractService.createDuoWrappers = jest.fn();
await contractService.startCustodian('account');
await contractService.startCustodian();
expect(contractService.createDuoWrappers as jest.Mock).not.toBeCalled();
});

Expand Down Expand Up @@ -151,6 +151,6 @@ test('startCustodian', async () => {
}
}
}));
await contractService1.startCustodian('account');
await contractService1.startCustodian();
expect((startCustodian as jest.Mock).mock.calls).toMatchSnapshot();
});
22 changes: 21 additions & 1 deletion src/services/ContractService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,27 @@ export default class ContractService extends BaseService {
setInterval(() => dbUtil.insertHeartbeat(), 30000);
}

public async startCustodian(account: string) {
public async startCustodian() {
let kovanManagerAccount = {
Beethoven: {
operator: {
address: 'account',
privateKey: ''
}
}
};
try {
kovanManagerAccount = require('../static/kovanManagerAccount.json');
} catch (error) {
console.log(error);
}
this.web3Wrapper = new Web3Wrapper(
null,
this.option.provider,
kovanManagerAccount.Beethoven.operator.privateKey,
this.option.live
);
const account = kovanManagerAccount.Beethoven.operator.address;
const type = this.option.contractType;
const tenor = this.option.tenor;
if (
Expand Down
14 changes: 14 additions & 0 deletions src/services/MarketDataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import child_process from 'child_process';
import { kovan } from '../../../duo-contract-wrapper/src/contractAddresses';
import geminiApi from '../apis/geminiApi';
import dbUtil from '../utils/dbUtil';
import dynamoUtil from '../utils/dynamoUtil';
import osUtil from '../utils/osUtil';
import priceUtil from '../utils/priceUtil';
import util from '../utils/util';
import MarketDataService from './MarketDataService';
jest.mock('../../../duo-contract-wrapper/src/Web3Wrapper', () =>
Expand Down Expand Up @@ -376,3 +378,15 @@ test('cleanDb', async () => {
(global.setInterval as jest.Mock).mock.calls[1][0]();
expect(dbUtil.insertHeartbeat as jest.Mock).toBeCalledTimes(1);
});

test('startAggregate', async () => {
global.setInterval = jest.fn();
dynamoUtil.insertHeartbeat = jest.fn();
priceUtil.aggregatePrice = jest.fn();
await marketDataService.startAggregate(10);

(global.setInterval as jest.Mock).mock.calls[0][0]();
(global.setInterval as jest.Mock).mock.calls[1][0]();
expect((dynamoUtil.insertHeartbeat as jest.Mock).mock.calls).toMatchSnapshot();
expect((priceUtil.aggregatePrice as jest.Mock).mock.calls).toMatchSnapshot();
});
14 changes: 14 additions & 0 deletions src/services/MarketDataService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import child_process from 'child_process';
import apis from '../apis';
import * as CST from '../common/constants';
import { IOption, ISubProcess } from '../common/types';
import dbUtil from '../utils/dbUtil';
import dynamoUtil from '../utils/dynamoUtil';
import osUtil from '../utils/osUtil';
import priceUtil from '../utils/priceUtil';
import util from '../utils/util';
import BaseService from './BaseService';

Expand Down Expand Up @@ -100,4 +103,15 @@ export default class MarketDataService extends BaseService {
global.setInterval(() => dbUtil.cleanDB(), 60000 * 60 * 24);
global.setInterval(() => dbUtil.insertHeartbeat(), 30000);
}

public async startAggregate(period: number) {
await dynamoUtil.insertHeartbeat({ period: { N: period + '' } });
await priceUtil.aggregatePrice(period);

global.setInterval(
() => dynamoUtil.insertHeartbeat({ period: { N: period + '' } }),
CST.STATUS_INTERVAL * 1000
);
global.setInterval(() => priceUtil.aggregatePrice(period), 30000);
}
}
30 changes: 30 additions & 0 deletions src/services/__snapshots__/MarketDataService.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,36 @@ Object {
}
`;

exports[`startAggregate 1`] = `
Array [
Array [
Object {
"period": Object {
"N": "10",
},
},
],
Array [
Object {
"period": Object {
"N": "10",
},
},
],
]
`;

exports[`startAggregate 2`] = `
Array [
Array [
10,
],
Array [
10,
],
]
`;

exports[`startFetching no source 1`] = `
Array [
Array [
Expand Down
30 changes: 0 additions & 30 deletions src/utils/__snapshots__/priceUtil.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -344,36 +344,6 @@ Object {
}
`;

exports[`startAggregate 1`] = `
Array [
Array [
Object {
"period": Object {
"N": "10",
},
},
],
Array [
Object {
"period": Object {
"N": "10",
},
},
],
]
`;

exports[`startAggregate 2`] = `
Array [
Array [
10,
],
Array [
10,
],
]
`;

exports[`startCommitPrices 1`] = `
Array [
Array [
Expand Down
12 changes: 0 additions & 12 deletions src/utils/priceUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,6 @@ test('startMagi gasPrice', async () => {
expect((magiWrapper.startMagi as jest.Mock).mock.calls).toMatchSnapshot();
});

test('startAggregate', async () => {
global.setInterval = jest.fn();
dynamoUtil.insertHeartbeat = jest.fn();
priceUtil.aggregatePrice = jest.fn();
await priceUtil.startAggregate(10);

(global.setInterval as jest.Mock).mock.calls[0][0]();
(global.setInterval as jest.Mock).mock.calls[1][0]();
expect((dynamoUtil.insertHeartbeat as jest.Mock).mock.calls).toMatchSnapshot();
expect((priceUtil.aggregatePrice as jest.Mock).mock.calls).toMatchSnapshot();
});

test('startCommitPrices', async () => {
schedule.RecurrenceRule = jest.fn().mockImplementation(() => ({}));
schedule.scheduleJob = jest.fn();
Expand Down
11 changes: 0 additions & 11 deletions src/utils/priceUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@ class PriceUtil {

util.logInfo('all source processed');
}

public async startAggregate(period: number) {
await dynamoUtil.insertHeartbeat({ period: { N: period + '' } });
await this.aggregatePrice(period);

global.setInterval(
() => dynamoUtil.insertHeartbeat({ period: { N: period + '' } }),
CST.STATUS_INTERVAL * 1000
);
global.setInterval(() => this.aggregatePrice(period), 30000);
}
}

const priceUtil = new PriceUtil();
Expand Down

0 comments on commit 57f3544

Please sign in to comment.