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

fix flow type errors #811

Merged
merged 2 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ class RestrictedConnection extends common.Connection {
}

class RippleAPI extends EventEmitter {

_feeCushion: number;
connection: RestrictedConnection;

// these are exposed only for use by unit tests; they are not part of the API.
static _PRIVATE = {
validate: common.validate,
RangeSet: require('./common/rangeset').RangeSet,
ledgerUtils: require('./ledger/utils'),
schemaValidator: require('./common/schema-validator')
};

constructor(options: APIOptions = {}) {
common.validate.apiOptions(options)
super()
Expand Down Expand Up @@ -146,14 +158,6 @@ _.assign(RippleAPI.prototype, {
errors
})

// these are exposed only for use by unit tests; they are not part of the API
RippleAPI._PRIVATE = {
validate: common.validate,
RangeSet: require('./common/rangeset').RangeSet,
ledgerUtils: require('./ledger/utils'),
schemaValidator: require('./common/schema-validator')
}

module.exports = {
RippleAPI
}
12 changes: 7 additions & 5 deletions src/common/rangeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict' // eslint-disable-line strict
const _ = require('lodash')
const assert = require('assert')
const ranges = Symbol()

function mergeIntervals(intervals: Array<[number, number]>) {
const stack = [[-Infinity, -Infinity]]
Expand All @@ -19,22 +18,25 @@ function mergeIntervals(intervals: Array<[number, number]>) {
}

class RangeSet {

ranges: Array<[number, number]>;

constructor() {
this.reset()
}

reset() {
this[ranges] = []
this.ranges = []
}

serialize() {
return this[ranges].map(range =>
return this.ranges.map(range =>
range[0].toString() + '-' + range[1].toString()).join(',')
}

addRange(start: number, end: number) {
assert(start <= end, 'invalid range')
this[ranges] = mergeIntervals(this[ranges].concat([[start, end]]))
this.ranges = mergeIntervals(this.ranges.concat([[start, end]]))
}

addValue(value: number) {
Expand All @@ -50,7 +52,7 @@ class RangeSet {
}

containsRange(start: number, end: number) {
return _.some(this[ranges], range => range[0] <= start && range[1] >= end)
return _.some(this.ranges, range => range[0] <= start && range[1] >= end)
}

containsValue(value: number) {
Expand Down
1 change: 1 addition & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function toRippledAmount(amount: Amount): RippledAmount {
if (amount.currency === 'XRP') {
return xrpToDrops(amount.value)
}
// $FlowFixMe: amount.issuer is not a Amount type property. Safe to remove?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. issuer is actually the term that we use in rippled. We should add issuer to the Amount type, and keep using it here. (No hurry, but in a future release, we may deprecate counterparty.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(There are other areas in the code that will need to be updated to accommodate this)

Copy link
Contributor Author

@FredKSchott FredKSchott Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@intelliot Say more about the other areas that need to be changed? They'll need to accommodate by setting issuer? Read from issuer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FredKSchott Right. For example, here are places where I think we'll need to set issuer or read from issuer. We can do this in a later PR though.

return {
currency: amount.currency,
issuer: amount.counterparty ? amount.counterparty :
Expand Down
2 changes: 1 addition & 1 deletion src/ledger/orderbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type GetOrderbook = {
function getBookOffers(connection: Connection, account: string,
ledgerVersion?: number, limit?: number, takerGets: Issue,
takerPays: Issue
): Promise {
): Promise<Object[]> {
return connection.request(utils.renameCounterpartyToIssuerInOrder({
command: 'book_offers',
taker_gets: takerGets,
Expand Down
2 changes: 1 addition & 1 deletion src/ledger/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type GetOrders = Array<Order>

function requestAccountOffers(connection: Connection, address: string,
ledgerVersion: number, marker: string, limit: number
): Promise {
): Promise<Object> {
return connection.request({
command: 'account_offers',
account: address,
Expand Down
2 changes: 1 addition & 1 deletion src/ledger/parse/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function parseTransaction(tx: Object): Object {
'feeUpdate': parseFeeUpdate,
'amendment': parseAmendment
}
const parser = mapping[type]
const parser: Function = (mapping: Object)[type]
assert(parser !== undefined, 'Unrecognized transaction type')
const specification = parser(tx)
const outcome = utils.parseOutcome(tx)
Expand Down
4 changes: 2 additions & 2 deletions src/ledger/parse/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const utils = require('../utils')
const BigNumber = require('bignumber.js')
const parseAmount = require('./amount')

import type {Amount} from '../common/types.js'
import type {Amount} from '../../common/types.js'

function adjustQualityForXRP(
quality: string, takerGetsCurrency: string, takerPaysCurrency: string
Expand Down Expand Up @@ -51,7 +51,7 @@ function removeEmptyCounterpartyInOrderbookChanges(orderbookChanges) {
})
}

function isPartialPayment(tx) {
function isPartialPayment(tx: Object) {
return (tx.Flags & utils.common.txFlags.Payment.PartialPayment) !== 0
}

Expand Down
8 changes: 5 additions & 3 deletions src/ledger/pathfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import type {GetPaths, PathFind, RippledPathsResponse, PathFindRequest}
from './pathfind-types.js'


function addParams(request: PathFindRequest, result: RippledPathsResponse) {
function addParams(request: PathFindRequest, result: RippledPathsResponse
): RippledPathsResponse {
return _.defaults(_.assign({}, result, {
source_account: request.source_account,
source_currencies: request.source_currencies
}), {destination_amount: request.destination_amount})
}

function requestPathFind(connection: Connection, pathfind: PathFind): Promise {
function requestPathFind(connection: Connection, pathfind: PathFind
): Promise<RippledPathsResponse> {
const destinationAmount = _.assign({value: -1}, pathfind.destination.amount)
const request: PathFindRequest = {
command: 'ripple_path_find',
Expand Down Expand Up @@ -76,7 +78,7 @@ function isRippledIOUAmount(amount: RippledAmount) {

function conditionallyAddDirectXRPPath(connection: Connection, address: string,
paths: RippledPathsResponse
): Promise {
): Promise<RippledPathsResponse> {
if (isRippledIOUAmount(paths.destination_amount)
|| !_.includes(paths.destination_currencies, 'XRP')) {
return Promise.resolve(paths)
Expand Down
8 changes: 4 additions & 4 deletions src/ledger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function clamp(value: number, min: number, max: number): number {

function getXRPBalance(connection: Connection, address: string,
ledgerVersion?: number
): Promise<number> {
): Promise<string> {
const request = {
command: 'account_info',
account: address,
Expand All @@ -34,7 +34,7 @@ function getXRPBalance(connection: Connection, address: string,

// If the marker is omitted from a response, you have reached the end
function getRecursiveRecur(getter: Getter, marker?: string, limit: number
): Promise {
): Promise<Array<any>> {
return getter(marker, limit).then(data => {
const remaining = limit - data.results.length
if (remaining > 0 && data.marker !== undefined) {
Expand All @@ -46,7 +46,7 @@ function getRecursiveRecur(getter: Getter, marker?: string, limit: number
})
}

function getRecursive(getter: Getter, limit?: number): Promise {
function getRecursive(getter: Getter, limit?: number): Promise<Array<any>> {
return getRecursiveRecur(getter, undefined, limit || Infinity)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ function isPendingLedgerVersion(connection: Connection,
}

function ensureLedgerVersion(options: Object
): Promise<number> {
): Promise<Object> {
if (Boolean(options) && options.ledgerVersion !== undefined &&
options.ledgerVersion !== null
) {
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/payment-channel-claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PaymentChannelClaim = {
function createPaymentChannelClaimTransaction(account: string,
claim: PaymentChannelClaim
): Object {
const txJSON = {
const txJSON: Object = {
Account: account,
TransactionType: 'PaymentChannelClaim',
Channel: claim.channel,
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/payment-channel-fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PaymentChannelFund = {
function createPaymentChannelFundTransaction(account: string,
fund: PaymentChannelFund
): Object {
const txJSON = {
const txJSON: Object = {
Account: account,
TransactionType: 'PaymentChannelFund',
Channel: fund.channel,
Expand Down
53 changes: 0 additions & 53 deletions src/transaction/settings-types.js

This file was deleted.

25 changes: 22 additions & 3 deletions src/transaction/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,36 @@ const validate = utils.common.validate
const AccountFlagIndices = utils.common.constants.AccountFlagIndices
const AccountFields = utils.common.constants.AccountFields
import type {Instructions, Prepare} from './types.js'
import type {Settings} from './settings-types.js'

type Settings = {
passwordSpent?: boolean,
requireDestinationTag?: boolean,
requireAuthorization?: boolean,
disallowIncomingXRP?: boolean,
disableMasterKey?: boolean,
enableTransactionIDTracking?: boolean,
noFreeze?: boolean,
globalFreeze?: boolean,
defaultRipple?: boolean,
emailHash?: ?string,
messageKey?: string,
domain?: string,
transferRate?: ?number,
regularKey?: string,
signers?: {
threshold?: number,
weights: {address: string, weight: number}[],
},
}

// Emptry string passed to setting will clear it
const CLEAR_SETTING = null


function setTransactionFlags(txJSON: Object, values: Settings) {
const keys = Object.keys(values)
assert(keys.length === 1, 'ERROR: can only set one setting per transaction')
const flagName = keys[0]
const value = values[flagName]
const value = (values: Object)[flagName]
const index = AccountFlagIndices[flagName]
if (index !== undefined) {
if (value) {
Expand Down