Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #271 from bodhiproject/dev-062419
Browse files Browse the repository at this point in the history
Dev 062419
  • Loading branch information
dwalintukan authored Jul 4, 2019
2 parents 88d0b81 + 0993b6c commit 4e8cbae
Show file tree
Hide file tree
Showing 24 changed files with 6,058 additions and 136 deletions.
40 changes: 35 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Server that parses, stores, sends, and receives Bodhi-related data from the blockchain.",
"author": "bodhi.network",
"license": "LGPL-3.0",
"version": "6.1.2",
"version": "6.1.3",
"repository": "git@github.com:bodhiproject/bodhi-server.git",
"keywords": [
"bodhi",
Expand All @@ -26,6 +26,7 @@
"babel-polyfill": "^6.26.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"crypto-random-string": "^3.0.1",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"express-winston": "^2.5.1",
Expand All @@ -45,6 +46,7 @@
"winston-daily-rotate-file": "^3.2.1"
},
"devDependencies": {
"chai-exclude": "^2.0.1",
"easygraphql-tester": "^5.1.3",
"eslint": "^4.16.0",
"eslint-config-airbnb": "^16.1.0",
Expand Down
16 changes: 9 additions & 7 deletions src/api/config-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const getContract = () => {
};

module.exports = {
getContract,

async bodhiTokenAddress() {
try {
return getContract().methods.bodhiTokenAddress().call();
return this.getContract().methods.bodhiTokenAddress().call();
} catch (err) {
logger.error(`Error ConfigManager.bodhiTokenAddress(): ${err.message}`);
throw err;
Expand All @@ -23,7 +25,7 @@ module.exports = {

async eventFactoryAddress() {
try {
return getContract().methods.eventFactoryAddress().call();
return this.getContract().methods.eventFactoryAddress().call();
} catch (err) {
logger.error(`Error ConfigManager.eventFactoryAddress(): ${err.message}`);
throw err;
Expand All @@ -32,7 +34,7 @@ module.exports = {

async eventEscrowAmount() {
try {
const res = await getContract().methods.eventEscrowAmount().call();
const res = await this.getContract().methods.eventEscrowAmount().call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
logger.error(`Error ConfigManager.eventEscrowAmount(): ${err.message}`);
Expand All @@ -42,7 +44,7 @@ module.exports = {

async arbitrationLength() {
try {
const res = await getContract().methods.arbitrationLength().call();
const res = await this.getContract().methods.arbitrationLength().call();
return map(res, len => web3.utils.toBN(len).toNumber());
} catch (err) {
logger.error(`Error ConfigManager.arbitrationLength(): ${err.message}`);
Expand All @@ -52,7 +54,7 @@ module.exports = {

async startingConsensusThreshold() {
try {
const res = await getContract().methods.startingConsensusThreshold().call();
const res = await this.getContract().methods.startingConsensusThreshold().call();
return map(res, len => web3.utils.toBN(len).toString(10));
} catch (err) {
logger.error(`Error ConfigManager.startingOracleThreshold(): ${err.message}`);
Expand All @@ -62,7 +64,7 @@ module.exports = {

async thresholdPercentIncrease() {
try {
const res = await getContract().methods.thresholdPercentIncrease().call();
const res = await this.getContract().methods.thresholdPercentIncrease().call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
logger.error(`Error ConfigManager.thresholdPercentIncrease(): ${err.message}`);
Expand All @@ -75,7 +77,7 @@ module.exports = {
const { address } = args;
if (isUndefined(address)) throw TypeError('address is not defined');

return getContract().methods.isWhitelisted(address).call();
return this.getContract().methods.isWhitelisted(address).call();
} catch (err) {
logger.error(`Error ConfigManager.isWhitelisted(): ${err.message}`);
throw err;
Expand Down
26 changes: 14 additions & 12 deletions src/api/multiple-results-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const getContract = async (eventAddress) => {
};

module.exports = {
getContract,

async calculateWinnings(args) {
try {
const { eventAddress, address } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');
if (isUndefined(address)) throw TypeError('address is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.calculateWinnings(address).call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
Expand All @@ -33,7 +35,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.version().call();
return web3.utils.toBN(res).toNumber();
} catch (err) {
Expand All @@ -47,7 +49,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.currentRound().call();
return web3.utils.toBN(res).toNumber();
} catch (err) {
Expand All @@ -61,7 +63,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.currentResultIndex().call();
return web3.utils.toBN(res).toNumber();
} catch (err) {
Expand All @@ -75,7 +77,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.currentConsensusThreshold().call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
Expand All @@ -89,7 +91,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.currentArbitrationEndTime().call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
Expand All @@ -104,7 +106,7 @@ module.exports = {
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const { utils: { toBN, hexToUtf8 } } = web3;
const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.eventMetadata().call();
return [
toBN(res[0]).toNumber(),
Expand All @@ -124,7 +126,7 @@ module.exports = {
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const { utils: { toBN } } = web3;
const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.centralizedMetadata().call();
return [
res[0],
Expand All @@ -145,7 +147,7 @@ module.exports = {
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const { utils: { toBN } } = web3;
const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.configMetadata().call();
return [
toBN(res[0]).toString(10),
Expand All @@ -164,7 +166,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
const res = await contract.methods.totalBets().call();
return web3.utils.toBN(res).toString(10);
} catch (err) {
Expand All @@ -179,7 +181,7 @@ module.exports = {
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');
if (isUndefined(address)) throw TypeError('address is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
return contract.methods.didWithdraw(address).call();
} catch (err) {
logger.error(`Error MultipleResultsEvent.didWithdraw(): ${err.message}`);
Expand All @@ -192,7 +194,7 @@ module.exports = {
const { eventAddress } = args;
if (isUndefined(eventAddress)) throw TypeError('eventAddress is not defined');

const contract = await getContract(eventAddress);
const contract = await this.getContract(eventAddress);
return contract.methods.didWithdrawEscrow().call();
} catch (err) {
logger.error(`Error MultipleResultsEvent.didWithdrawEscrow(): ${err.message}`);
Expand Down
72 changes: 36 additions & 36 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ const initDB = async () => {
await db.Bets.ensureIndex({ fieldName: 'txid', unique: true });
await db.ResultSets.ensureIndex({ fieldName: 'txid', unique: true });
await db.Withdraws.ensureIndex({ fieldName: 'txid', unique: true });
await db.TransactionReceipts.ensureIndex({ fieldName: 'transactionHash' });

await applyMigrations();
if (process.env.TEST_ENV !== 'true') {
await applyMigrations();
}
} catch (err) {
logger.error(`DB load Error: ${err.message}`);
throw err;
Expand Down Expand Up @@ -114,49 +117,46 @@ async function applyMigrations() {
logger.error(`Migration scripts load error: ${err.message}`);
throw err;
}

/**
* whilst source code: https://caolan.github.io/async/v3/whilst.js.html
* check(err, truth) and next(err, res) are built-in funcs in the source code
* whilst(test, iter, callback):
* 1. test: take check(err, truth), if truth is true, then call iter(next), otherwise, call callback to end the loop
* 2. iter: take next(err, res), which triggers test if no err, otherwise trigger callback(err)
* 3. callback: will only be triggered if test fails, or err encountered, reaching callback means end of the loop
*/
let i = 0;
async.whilst(
check => check(null, i < migrations.length), // trigger iter
(next) => {
const migration = migrations[i];
i++;
try {
if (Number(migration.number) > lastMigration) {
// Run migration
logger.info(`Running migration ${migration.number}...`);
// pass next() to migrate(), await not allowed, only callback func
migration.migrate(() => {
// Track the last migration number
lastMigration = migration.number;
fs.outputFileSync(migrationTrackPath, `LAST_MIGRATION=${lastMigration}\n`);
next(null, i); // trigger the next test
});
} else {
next(null, i); // if no migration here, trigger the next test
*/
try {
let i = 0;
await async.whilst(
check => check(null, i < migrations.length), // trigger iter
(next) => {
const migration = migrations[i];
i++;
try {
if (Number(migration.number) > lastMigration) {
// Run migration
logger.info(`Running migration ${migration.number}...`);
// pass next() to migrate(), await not allowed, only callback func
migration.migrate(() => {
// Track the last migration number
lastMigration = migration.number;
fs.outputFileSync(migrationTrackPath, `LAST_MIGRATION=${lastMigration}\n`);
next(null, i); // trigger the next test
});
} else {
next(null, i); // if no migration here, trigger the next test
}
} catch (err) {
next(err, i); // err met, trigger the callback to end this loop
}
} catch (err) {
next(err, i); // err met, trigger the callback to end this loop
}
},
(err, number) => {
// will only be called if should end this loop
if (err) {
logger.error(`Migration ${number} error: ${err.message}`);
return;
}

logger.info('Migrations complete.');
},
);
},
);
} catch (err) {
// will only be called if should end this loop
logger.error(err);
throw err;
}
logger.info('Migrations complete.');
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/queries/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const buildFilter = (rawFilter, version) => {
if (language) filter.language = language;

return filter;
}
};

module.exports = async (
parent,
Expand Down
Loading

0 comments on commit 4e8cbae

Please sign in to comment.