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

Commit

Permalink
Added flow sample
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Feb 9, 2018
1 parent 3122e4d commit 43798a3
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .babelrc
@@ -1,5 +1,5 @@
{
"presets": ["env"],
"presets": ["env", "flow"],
"plugins": ["transform-runtime"],
"env": {
"test": {
Expand Down
7 changes: 6 additions & 1 deletion .eslintrc.json
Expand Up @@ -3,11 +3,16 @@
"globals": {
"naclInstance": true
},
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"arrow-parens": ["error", "as-needed"],
"curly": "off",
"indent": "off",
"no-confusing-arrow": "off",
"no-mixed-operators": "off"
"no-mixed-operators": "off",
"flowtype/define-flow-type": 2
}
}
10 changes: 10 additions & 0 deletions .flowconfig
@@ -0,0 +1,10 @@
[include]

[ignore]
.*/node_modules/cypress/dist/*

[libs]
flow/

[version]
^0.65.0
41 changes: 41 additions & 0 deletions flow/types.js
@@ -0,0 +1,41 @@
/*
* Copyright © 2017 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
* @flow
*/

declare type Options = {
ssl: boolean,
node: string,
randomPeer: boolean,
testnet: boolean,
port: string,
bannedPeers: Array<string>,
nethash: NethashOption,
};

declare type NethashOption = {
'Content-Type': string,
nethash: string,
broadhash: string,
os: string,
version: string,
minVersion: string,
port: string,
Accept: string,
};

declare type NethashOptions = {
mainnet: NethashOption,
testnet: NethashOption,
};
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"prettier": "prettier --write \"browsertest/{setup,runTests}.js\" \"{cypress/integration,src,test}/**/*.js\"",
"serve:browsertest": "http-server browsertest",
"jenkins": "grunt jenkins --verbose",
"flow": "flow",
"build": "grunt build",
"build:check": "node -e \"require('./dist-node')\"",
"build:browsertest": "grunt build-browsertest",
Expand All @@ -40,16 +41,20 @@
},
"devDependencies": {
"babel-cli": "=6.24.1",
"babel-eslint": "=8.2.1",
"babel-plugin-istanbul": "=4.1.4",
"babel-plugin-rewire": "=1.1.0",
"babel-plugin-transform-runtime": "=6.23.0",
"babel-preset-env": "=1.6.0",
"babel-preset-flow": "=6.23.0",
"chokidar-cli": "=1.2.0",
"coveralls": "=2.13.1",
"cypress": "=0.20.3",
"eslint-config-airbnb-base": "=11.3.1",
"eslint-config-lisk-base": "=0.1.0",
"eslint-plugin-flowtype": "=2.42.0",
"eslint-plugin-import": "=2.7.0",
"flow-bin": "=0.65.0",
"grunt": "=1.0.1",
"grunt-browserify": "=5.0.0",
"grunt-contrib-uglify": "=3.0.1",
Expand Down
59 changes: 49 additions & 10 deletions src/api/liskApi.js
Expand Up @@ -11,14 +11,16 @@
*
* Removal or modification of this copyright notice is prohibited.
*
* @flow
*
*/
import config from '../../config.json';
import { LIVE_PORT, TEST_PORT, GET, POST } from '../constants';
import * as privateApi from './privateApi';
import * as utils from './utils';

export default class LiskAPI {
constructor(providedOptions) {
constructor(providedOptions: Options) {
const options = Object.assign({}, config.options, providedOptions);

this.defaultNodes = options.nodes || config.nodes.mainnet;
Expand All @@ -40,7 +42,35 @@ export default class LiskAPI {
this.nethash = this.getNethash(options.nethash);
}

getNethash(providedNethash) {
defaultNodes: Array<string>;
defaultSSLNodes: Array<string>;
defaultTestnetNodes: Array<string>;
options: Options;
ssl: boolean;
randomNode: boolean;
testnet: boolean;
bannedNodes: Array<string>;
node: string;
port: string;
nethash: NethashOption;

getAccount: Function;
getActiveDelegates: Function;
getStandbyDelegates: Function;
searchDelegatesByUsername: Function;
getBlocks: Function;
getForgedBlocks: Function;
getBlock: Function;
getTransactions: Function;
getTransaction: Function;
getVotes: Function;
getVoters: Function;
getUnsignedMultisignatureTransactions: Function;
getDapp: Function;
getDapps: Function;
getDappsByCategory: Function;

getNethash(providedNethash: string): NethashOption {
const { port } = this;
const NetHash = this.testnet
? utils.netHashOptions({ port }).testnet
Expand All @@ -65,12 +95,12 @@ export default class LiskAPI {
};
}

setNode(node) {
setNode(node: string) {
this.node = node || privateApi.selectNewNode.call(this);
return this.node;
}

setTestnet(testnet) {
setTestnet(testnet: boolean) {
if (this.testnet !== testnet) {
this.bannedNodes = [];
}
Expand All @@ -80,31 +110,35 @@ export default class LiskAPI {
privateApi.selectNewNode.call(this);
}

setSSL(ssl) {
setSSL(ssl: boolean) {
if (this.ssl !== ssl) {
this.ssl = ssl;
this.bannedNodes = [];
privateApi.selectNewNode.call(this);
}
}

broadcastTransactions(transactions) {
broadcastTransactions(transactions: Array<Object>) {
return privateApi.sendRequestPromise
.call(this, POST, 'transactions', transactions)
.then(result => result.body);
}

broadcastTransaction(transaction) {
broadcastTransaction(transaction: Object) {
return this.broadcastTransactions([transaction]);
}

broadcastSignatures(signatures) {
broadcastSignatures(signatures: Object) {
return privateApi.sendRequestPromise
.call(this, POST, 'signatures', { signatures })
.then(result => result.body);
}

sendRequest(requestMethod, requestType, options) {
sendRequest(
requestMethod: string,
requestType: string,
options: Object | Array<Object>,
) {
const checkedOptions = utils.checkOptions(options);

return privateApi.sendRequestPromise
Expand All @@ -128,7 +162,12 @@ export default class LiskAPI {
);
}

transferLSK(recipientId, amount, passphrase, secondPassphrase) {
transferLSK(
recipientId: string,
amount: number,
passphrase: string,
secondPassphrase: string,
) {
return this.sendRequest(POST, 'transactions', {
recipientId,
amount,
Expand Down
46 changes: 28 additions & 18 deletions src/api/privateApi.js
Expand Up @@ -11,22 +11,24 @@
*
* Removal or modification of this copyright notice is prohibited.
*
* @flow
*
*/
import * as popsicle from 'popsicle';
import { GET } from '../constants';
import * as utils from './utils';

export function getNodes() {
export function getNodes(): Array<string> {
if (this.testnet) return this.defaultTestnetNodes;
if (this.ssl) return this.defaultSSLNodes;
return this.defaultNodes;
}

export function isBanned(node) {
export function isBanned(node: string): boolean {
return this.bannedNodes.includes(node);
}

export function getRandomNode() {
export function getRandomNode(): string {
const nodes = getNodes.call(this).filter(node => !isBanned.call(this, node));

if (!nodes.length) {
Expand All @@ -39,7 +41,7 @@ export function getRandomNode() {
return nodes[randomIndex];
}

export function selectNewNode() {
export function selectNewNode(): string {
const providedNode = this.options.node;

if (this.randomNode) {
Expand All @@ -58,23 +60,27 @@ export function selectNewNode() {
);
}

export function banActiveNode() {
export function banActiveNode(): boolean {
if (!isBanned.call(this, this.node)) {
this.bannedNodes.push(this.node);
return true;
}
return false;
}

export function hasAvailableNodes() {
export function hasAvailableNodes(): boolean {
const nodes = getNodes.call(this);

return this.randomNode
? nodes.some(node => !isBanned.call(this, node))
: false;
}

export function createRequestObject(method, requestType, providedOptions) {
export function createRequestObject(
method: string,
requestType: string,
providedOptions: Object | Array<Object>,
) {
const options = providedOptions || {};
const baseURL = utils.getFullURL(this);
const url =
Expand All @@ -90,7 +96,11 @@ export function createRequestObject(method, requestType, providedOptions) {
};
}

export function sendRequestPromise(requestMethod, requestType, options) {
export function sendRequestPromise(
requestMethod: string,
requestType: string,
options: Object | Array<Object>,
) {
const requestObject = createRequestObject.call(
this,
requestMethod,
Expand All @@ -104,11 +114,11 @@ export function sendRequestPromise(requestMethod, requestType, options) {
}

export function handleTimestampIsInFutureFailures(
requestMethod,
requestType,
options,
result,
) {
requestMethod: string,
requestType: string,
options: any,
result: Object,
): Promise<*> {
if (
!result.success &&
result.message &&
Expand All @@ -125,11 +135,11 @@ export function handleTimestampIsInFutureFailures(
}

export function handleSendRequestFailures(
requestMethod,
requestType,
options,
error,
) {
requestMethod: string,
requestType: string,
options: Object | Array<Object>,
error: Error,
): Promise<*> {
const that = this;
if (hasAvailableNodes.call(that)) {
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 43798a3

Please sign in to comment.