Skip to content

Commit

Permalink
Typecheck remaining files in src/api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Cohen committed Jul 29, 2015
1 parent 7fffbe0 commit e583eb4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/api/common/schema-validator.js
@@ -1,4 +1,6 @@
/* @flow */
'use strict';

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
Expand All @@ -16,9 +18,10 @@ function isValidLedgerHash(ledgerHash) {
return core.UInt256.is_valid(ledgerHash);
}

function loadSchema(filepath) {
function loadSchema(filepath: string): {} {
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
const schemaContent: any = fs.readFileSync(filepath, 'utf8');
return JSON.parse(schemaContent);
} catch (e) {
throw new Error('Failed to parse schema: ' + filepath);
}
Expand All @@ -43,7 +46,7 @@ function formatSchemaErrors(errors) {
return errors.map(formatSchemaError).join(', ');
}

function schemaValidate(schemaName, object) {
function schemaValidate(schemaName: string, object: any): void {
const formats = {address: isValidAddress,
ledgerHash: isValidLedgerHash};
const options = {schemas: SCHEMAS, formats: formats,
Expand Down
20 changes: 14 additions & 6 deletions src/api/common/utils.js
@@ -1,17 +1,20 @@
/* @flow */
'use strict';
const BigNumber = require('bignumber.js');
const core = require('../../core');
const errors = require('./errors');

function dropsToXrp(drops) {
type Amount = {currency: string, issuer: string, value: string}

function dropsToXrp(drops: string): string {
return (new BigNumber(drops)).dividedBy(1000000.0).toString();
}

function xrpToDrops(xrp) {
function xrpToDrops(xrp: string): string {
return (new BigNumber(xrp)).times(1000000.0).floor().toString();
}

function toRippledAmount(amount) {
function toRippledAmount(amount: Amount): string|Amount {
if (amount.currency === 'XRP') {
return xrpToDrops(amount.value);
}
Expand All @@ -22,7 +25,9 @@ function toRippledAmount(amount) {
};
}

function wrapCatch(asyncFunction: () => void): () => void {
type AsyncFunction = (...x: any) => void

function wrapCatch(asyncFunction: AsyncFunction): AsyncFunction {
return function() {
try {
asyncFunction.apply(this, arguments);
Expand All @@ -33,7 +38,10 @@ function wrapCatch(asyncFunction: () => void): () => void {
};
}

function composeAsync(wrapper, callback) {
type Callback = (err: any, data: any) => void
type Wrapper = (data: any) => any

function composeAsync(wrapper: Wrapper, callback: Callback): Callback {
return function(error, data) {
if (error) {
callback(error);
Expand All @@ -50,7 +58,7 @@ function composeAsync(wrapper, callback) {
};
}

function convertExceptions(f) {
function convertExceptions<T>(f: () => T): () => T {
return function() {
try {
return f.apply(this, arguments);
Expand Down
5 changes: 3 additions & 2 deletions src/api/common/validate.js
@@ -1,3 +1,4 @@
/* @flow */
'use strict';
const _ = require('lodash');
const core = require('./utils').core;
Expand All @@ -8,7 +9,7 @@ function error(text) {
return new ValidationError(text);
}

function validateAddressAndSecret(obj) {
function validateAddressAndSecret(obj: {address: string, secret: string}): void {
const address = obj.address;
const secret = obj.secret;
schemaValidate('address', address);
Expand All @@ -22,7 +23,7 @@ function validateAddressAndSecret(obj) {
}
}

function validateSecret(secret) {
function validateSecret(secret: string): void {
if (!secret) {
throw error('Parameter missing: secret');
}
Expand Down
28 changes: 20 additions & 8 deletions src/api/ledger/utils.js
@@ -1,23 +1,28 @@
/* @flow */
'use strict';
const _ = require('lodash');
const assert = require('assert');
const common = require('../common');
const dropsToXrp = common.dropsToXrp;
const composeAsync = common.composeAsync;

function clamp(value, min, max) {
type Callback = (err: any, data: any) => void

function clamp(value: number, min: number, max: number): number {
assert(min <= max, 'Illegal clamp bounds');
return Math.min(Math.max(value, min), max);
}

function getXRPBalance(remote, address, ledgerVersion, callback) {
function getXRPBalance(remote: any, address: string, ledgerVersion?: number, callback: Callback): void {
remote.requestAccountInfo({account: address, ledger: ledgerVersion},
composeAsync((data) => dropsToXrp(data.account_data.Balance), callback));
}

type Getter = (marker: ?string, limit: number, callback: Callback) => void

// If the marker is omitted from a response, you have reached the end
// getter(marker, limit, callback), callback(error, {marker, results})
function getRecursiveRecur(getter, marker, limit, callback) {
function getRecursiveRecur(getter: Getter, marker?: string, limit: number, callback: Callback): void {
getter(marker, limit, (error, data) => {
if (error) {
return callback(error);
Expand All @@ -34,11 +39,13 @@ function getRecursiveRecur(getter, marker, limit, callback) {
});
}

function getRecursive(getter, limit, callback) {
function getRecursive(getter: Getter, limit?: number, callback: Callback) {
getRecursiveRecur(getter, undefined, limit || Infinity, callback);
}

function renameCounterpartyToIssuer(amount) {
type Amount = {counterparty?: string, issuer?: string, value: string}

function renameCounterpartyToIssuer(amount?: Amount): ?{issuer?: string} {
if (amount === undefined) {
return undefined;
}
Expand All @@ -48,7 +55,9 @@ function renameCounterpartyToIssuer(amount) {
return _.omit(withIssuer, 'counterparty');
}

function renameCounterpartyToIssuerInOrder(order) {
type Order = {taker_gets: Amount, taker_pays: Amount}

function renameCounterpartyToIssuerInOrder(order: Order) {
const taker_gets = renameCounterpartyToIssuer(order.taker_gets);
const taker_pays = renameCounterpartyToIssuer(order.taker_pays);
const changes = {taker_gets: taker_gets, taker_pays: taker_pays};
Expand All @@ -70,7 +79,9 @@ function signum(num) {
* @returns {Number} [-1, 0, 1]
*/

function compareTransactions(first, second) {
type Outcome = {outcome: {ledgerVersion: string, indexInLedger: string}};

function compareTransactions(first: Outcome, second: Outcome): number {
if (first.outcome.ledgerVersion === second.outcome.ledgerVersion) {
return signum(Number(first.outcome.indexInLedger) -
Number(second.outcome.indexInLedger));
Expand All @@ -79,7 +90,8 @@ function compareTransactions(first, second) {
Number(second.outcome.ledgerVersion) ? -1 : 1;
}

function hasCompleteLedgerRange(remote, minLedgerVersion, maxLedgerVersion) {
function hasCompleteLedgerRange(remote: any, minLedgerVersion: number,
maxLedgerVersion: number): boolean {
const firstLedgerVersion = 32570; // earlier versions have been lost
return remote.getServer().hasLedgerRange(
minLedgerVersion || firstLedgerVersion,
Expand Down

0 comments on commit e583eb4

Please sign in to comment.