Skip to content

Commit

Permalink
Switch to StellarSdk stream() from EventSource
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Sep 14, 2016
1 parent 3bb4651 commit c131fd0
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -13,7 +13,6 @@
"chai": "^3.5.0",
"classnames": "^2.2.0",
"css-loader": "^0.18.0",
"eventsource": "^0.2.1",
"extract-text-webpack-plugin": "^0.8.2",
"file-loader": "^0.8.4",
"gulp": "^3.9.0",
Expand All @@ -32,7 +31,8 @@
"sass-loader": "^3.0.0",
"solar-css": "git+https://github.com/stellar/solar#master",
"solar-stellarorg": "git+https://github.com/stellar/solar-stellarorg#master",
"stellar-base": "0.5.6",
"stellar-sdk": "0.6.0",
"urijs": "^1.16.0",
"uri-templates": "^0.1.9",
"webpack": "^1.13.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/accountCreator.js
@@ -1,6 +1,6 @@
import axios from 'axios';
import dispatchInNewStack from '../utilities/dispatchInNewStack';
import {Keypair} from 'stellar-base';
import {Keypair} from 'stellar-sdk';

export const GENERATE_NEW_KEYPAIR = 'GENERATE_NEW_KEYPAIR';
export function generateNewKeypair() {
Expand Down
21 changes: 11 additions & 10 deletions src/actions/endpointExplorer.js
@@ -1,7 +1,8 @@
import axios from 'axios';
import _ from 'lodash';
import dispatchInNewStack from '../utilities/dispatchInNewStack';
var EventSource = (typeof window === 'undefined') ? require('eventsource') : window.EventSource;
import {CallBuilder} from 'stellar-sdk/lib/call_builder';
import URI from 'urijs';

export const CHOOSE_ENDPOINT = "CHOOSE_ENDPOINT";
export function chooseEndpoint(resource, endpoint) {
Expand Down Expand Up @@ -32,13 +33,13 @@ export const START_REQUEST = "START_REQUEST"
export const ERROR_REQUEST = "ERROR_REQUEST"
export const UPDATE_REQUEST = "UPDATE_REQUEST"
let resultIdNonce = 0;
let openStream;
let closeStreamFn;
export function submitRequest(request) {
return dispatch => {
// Close old stream if it exists
if (typeof _.get(openStream, 'close') === 'function') {
openStream.close();
openStream = null;
if (typeof closeStreamFn === 'function') {
closeStreamFn();
closeStreamFn = null;
}

let id = resultIdNonce++;
Expand All @@ -48,12 +49,12 @@ export function submitRequest(request) {
});

if (request.streaming) {
openStream = streamingRequest(request.url, (message) => {
closeStreamFn = streamingRequest(request.url, (message) => {
// dispatchInNewStack is not needed for streaming since there is no catch here
dispatch({
type: UPDATE_REQUEST,
id,
body: JSON.parse(message.data),
body: message,
})
})
} else {
Expand Down Expand Up @@ -89,7 +90,7 @@ function httpRequest(request) {
}

function streamingRequest(url, onmessage) {
var es = new EventSource(url);
es.onmessage = onmessage;
return es;
var callBuilder = new CallBuilder();
callBuilder.url = URI(url);
return callBuilder.stream({onmessage});
}
2 changes: 1 addition & 1 deletion src/actions/transactionBuilder.js
@@ -1,5 +1,5 @@
import axios from 'axios';
import {UnsignedHyper} from 'stellar-base';
import {UnsignedHyper} from 'stellar-sdk';

// Resets everything to it's default state
export const RESET_TXBUILDER = 'RESET_TXBUILDER';
Expand Down
4 changes: 2 additions & 2 deletions src/app.js
Expand Up @@ -11,10 +11,10 @@ import logging from './middleware/logging';
import {routerMiddleware} from './utilities/simpleRouter';
import LaboratoryChrome from './components/LaboratoryChrome';

import StellarBase from 'stellar-base'
import StellarSdk from 'stellar-sdk'

if (typeof window !== "undefined") {
window.StellarBase = StellarBase;
window.StellarSdk = StellarSdk;
}

document.write('<div id="app"></div>');
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormComponents/MemoPicker.js
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import RadioButtonPicker from './RadioButtonPicker';
import TextPicker from './TextPicker';
import PickerError from './PickerError';
import {UnsignedHyper} from 'stellar-base';
import {UnsignedHyper} from 'stellar-sdk';

export default function MemoPicker(props) {
let {onUpdate} = props;
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormComponents/PubKeyPicker.js
@@ -1,6 +1,6 @@
import React from 'react';
import TextPicker from './TextPicker';
import {Keypair} from 'stellar-base';
import {Keypair} from 'stellar-sdk';

export default function PubKeyPicker(props) {
return <TextPicker
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormComponents/SecretKeyPicker.js
@@ -1,6 +1,6 @@
import React from 'react';
import TextPicker from './TextPicker';
import {Keypair} from 'stellar-base';
import {Keypair} from 'stellar-sdk';

export default function SecretKeyPicker(props) {
return <TextPicker
Expand Down
2 changes: 1 addition & 1 deletion src/components/TransactionSigner.js
@@ -1,6 +1,6 @@
import React from 'react';
import {connect} from 'react-redux';
import {Transaction, Keypair, Network} from 'stellar-base';
import {Transaction, Keypair, Network} from 'stellar-sdk';
import TransactionImporter from './TransactionImporter';
import {
importFromXdr,
Expand Down
2 changes: 1 addition & 1 deletion src/components/TxBuilderAttributes.js
Expand Up @@ -6,7 +6,7 @@ import SequencePicker from './FormComponents/SequencePicker';
import StroopsPicker from './FormComponents/StroopsPicker';
import MemoPicker from './FormComponents/MemoPicker';
import {connect} from 'react-redux';
import {Keypair} from 'stellar-base';
import {Keypair} from 'stellar-sdk';
import NETWORK from '../constants/network';
import {fetchSequence} from '../actions/transactionBuilder';

Expand Down
2 changes: 1 addition & 1 deletion src/components/XdrViewer.js
Expand Up @@ -7,7 +7,7 @@ import TreeView from './TreeView';
import validateBase64 from '../utilities/validateBase64';
import {updateXdrInput, updateXdrType, fetchLatestTx} from '../actions/xdrViewer';
import NETWORK from '../constants/network';
import {xdr} from 'stellar-base';
import {xdr} from 'stellar-sdk';

function XdrViewer(props) {
let {dispatch, state, baseURL} = props;
Expand Down
2 changes: 1 addition & 1 deletion src/constants/network.js
@@ -1,4 +1,4 @@
import {Network, Networks} from 'stellar-base';
import {Network, Networks} from 'stellar-sdk';

const NETWORK = {
available: {
Expand Down
2 changes: 1 addition & 1 deletion src/data/operations.js
Expand Up @@ -7,7 +7,7 @@ export function getOperation(opName) {
// Operations map documentation:
// [ // In an array because we really want this to be ordered correctly (whereas for params, it is not as important)
// {
// name: 'createAccount', // Corresponds to the operation key in js-stellar-base.Operation
// name: 'createAccount', // Corresponds to the operation key in js-stellar-sdk.Operation
// label: 'Create Account', // Human friendly name for the operation
// operationPane: require('../components/OperationPanes/CreateAccount'), // React component that contains the multiple pickers for this operation
// },
Expand Down
54 changes: 27 additions & 27 deletions src/utilities/Libify.js
@@ -1,14 +1,14 @@
// Libify is a utility that converts a wide variety of inputs into their stricter
// representations in Stellar libraries such as js-stellar-base and js-stellar-Base.
// representations in Stellar libraries such as js-stellar-sdk and js-stellar-sdk.
//
// The Libify api aims to look similar to that of js-stellar-base and Base. It
// The Libify api aims to look similar to that of js-stellar-sdk and Sdk. It
// will output better error messages in cases where helpful (instead of just
// undefined error messages).
//
// Libify could also be used to generate source code from input but might not be
// the best choice since source code differs based on content.

import Base from 'stellar-base';
import Sdk from 'stellar-sdk';
import _ from 'lodash';

// Helpers
Expand Down Expand Up @@ -66,29 +66,29 @@ let Libify = {};

Libify.Asset = function(opts) {
if (isEmpty(opts) || opts.type === 'native') {
return Base.Asset.native();
return Sdk.Asset.native();
}

assertNotEmpty(opts.code, 'Asset requires asset code');
return new Base.Asset(opts.code, opts.issuer);
return new Sdk.Asset(opts.code, opts.issuer);
}

Libify.Memo = function(opts) {
switch(opts.type) {
case '':
case 'MEMO_TEXT':
return Base.Memo.text(opts.content);
return Sdk.Memo.text(opts.content);
case 'MEMO_ID':
return Base.Memo.id(opts.content);
return Sdk.Memo.id(opts.content);
case 'MEMO_HASH':
return Base.Memo.hash(opts.content);
return Sdk.Memo.hash(opts.content);
case 'MEMO_RETURN':
return Base.Memo.returnHash(opts.content);
return Sdk.Memo.returnHash(opts.content);
}
}

// Takes in a type and a pile of options and attempts to turn it into a valid
// js-stellar-base operation. If not, it will throw an error.
// js-stellar-sdk operation. If not, it will throw an error.
Libify.Operation = function(type, opts) {
assertNotEmpty(type, 'Operation type is required');
let opFunction = Libify.Operation[type];
Expand All @@ -101,7 +101,7 @@ Libify.Operation = function(type, opts) {
Libify.Operation.createAccount = function(opts) {
assertNotEmpty(opts.destination, 'Create Account operation requires destination');
assertNotEmpty(opts.startingBalance, 'Create Account operation requires starting balance');
return Base.Operation.createAccount({
return Sdk.Operation.createAccount({
destination: opts.destination,
startingBalance: opts.startingBalance,
source: opts.sourceAccount,
Expand All @@ -112,7 +112,7 @@ Libify.Operation.payment = function(opts) {
assertNotEmpty(opts.destination, 'Payment operation requires destination');
assertNotEmpty(opts.asset, 'Payment operation requires asset');
assertNotEmpty(opts.amount, 'Payment operation requires amount');
return Base.Operation.payment({
return Sdk.Operation.payment({
destination: opts.destination,
asset: Libify.Asset(opts.asset),
amount: opts.amount,
Expand All @@ -134,7 +134,7 @@ Libify.Operation.pathPayment = function(opts) {
return Libify.Asset(hopAsset);
})

return Base.Operation.pathPayment({
return Sdk.Operation.pathPayment({
sendAsset: Libify.Asset(opts.sendAsset),
sendMax: opts.sendMax,
destination: opts.destination,
Expand All @@ -147,7 +147,7 @@ Libify.Operation.pathPayment = function(opts) {

Libify.Operation.changeTrust = function(opts) {
assertNotEmpty(opts.asset, 'Change Trust operation requires asset');
return Base.Operation.changeTrust({
return Sdk.Operation.changeTrust({
asset: Libify.Asset(opts.asset),
limit: (opts.limit === '') ? undefined : opts.limit,
source: opts.sourceAccount,
Expand All @@ -158,7 +158,7 @@ Libify.Operation.allowTrust = function(opts) {
assertNotEmpty(opts.trustor, 'Allow Trust operation requires trustor');
assertNotEmpty(opts.assetCode, 'Allow Trust operation requires asset code');
assertNotEmpty(opts.authorize, 'Allow Trust operation requires authorization setting');
return Base.Operation.allowTrust({
return Sdk.Operation.allowTrust({
trustor: opts.trustor,
assetCode: opts.assetCode,
authorize: isLooseTruthy(opts.authorize),
Expand All @@ -168,7 +168,7 @@ Libify.Operation.allowTrust = function(opts) {

Libify.Operation.accountMerge = function(opts) {
assertNotEmpty(opts.destination, 'Allow Trust operation requires destination');
return Base.Operation.accountMerge({
return Sdk.Operation.accountMerge({
destination: opts.destination,
source: opts.sourceAccount,
})
Expand All @@ -180,7 +180,7 @@ Libify.Operation.manageOffer = function(opts) {
assertNotEmpty(opts.amount, 'Manage Offer operation requires amount');
assertNotEmpty(opts.price, 'Manage Offer operation requires price');
assertNotEmpty(opts.offerId, 'Manage Offer operation requires Offer ID');
return Base.Operation.manageOffer({
return Sdk.Operation.manageOffer({
selling: Libify.Asset(opts.selling),
buying: Libify.Asset(opts.buying),
amount: opts.amount,
Expand All @@ -195,7 +195,7 @@ Libify.Operation.createPassiveOffer = function(opts) {
assertNotEmpty(opts.buying, 'Create Passive Offer operation requires buying asset');
assertNotEmpty(opts.amount, 'Create Passive Offer operation requires amount');
assertNotEmpty(opts.price, 'Create Passive Offer operation requires price');
return Base.Operation.manageOffer({
return Sdk.Operation.manageOffer({
selling: Libify.Asset(opts.selling),
buying: Libify.Asset(opts.buying),
amount: opts.amount,
Expand All @@ -205,7 +205,7 @@ Libify.Operation.createPassiveOffer = function(opts) {
}

Libify.Operation.inflation = function(opts) {
return Base.Operation.inflation({
return Sdk.Operation.inflation({
source: opts.sourceAccount,
})
}
Expand Down Expand Up @@ -235,7 +235,7 @@ Libify.Operation.setOptions = function(opts) {
assertIntOrEmpty(opts.medThreshold, 'Medium Threshold must be an integer');
assertIntOrEmpty(opts.highThreshold, 'High Threshold must be an integer');

return Base.Operation.setOptions({
return Sdk.Operation.setOptions({
inflationDest: opts.inflationDest,
clearFlags: castIntOrUndefined(opts.clearFlags),
setFlags: castIntOrUndefined(opts.setFlags),
Expand All @@ -252,7 +252,7 @@ Libify.Operation.setOptions = function(opts) {
Libify.Operation.manageData = function(opts) {
assertNotEmpty(opts.name, 'Manage Data operation requires entry name');

return Base.Operation.manageData({
return Sdk.Operation.manageData({
name: opts.name,
value: castStringOrNull(opts.value),
})
Expand All @@ -268,14 +268,14 @@ Libify.buildTransaction = function(attributes, operations) {
};

try {
var account = new Base.Account(attributes.sourceAccount, Base.UnsignedHyper.fromString(attributes.sequence).subtract(1).toString());
var account = new Sdk.Account(attributes.sourceAccount, Sdk.UnsignedHyper.fromString(attributes.sequence).subtract(1).toString());

let opts = {};
if (attributes.fee !== '') {
opts.fee = attributes.fee;
}

var transaction = new Base.TransactionBuilder(account, opts)
var transaction = new Sdk.TransactionBuilder(account, opts)

if (attributes.memoType !== 'MEMO_NONE' && attributes.memoType !== '') {
try {
Expand Down Expand Up @@ -307,7 +307,7 @@ Libify.buildTransaction = function(attributes, operations) {


Libify.signTransaction = function(txXdr, signers, networkObj) {
Base.Network.use(networkObj);
Sdk.Network.use(networkObj);

let validSecretKeys = [];
for (let i = 0; i < signers.length; i++) {
Expand All @@ -322,10 +322,10 @@ Libify.signTransaction = function(txXdr, signers, networkObj) {
}
}

let newTx = new Base.Transaction(txXdr);
let newTx = new Sdk.Transaction(txXdr);
let existingSigs = newTx.signatures.length;
_.each(validSecretKeys, (signer) => {
newTx.sign(Base.Keypair.fromSeed(signer));
newTx.sign(Sdk.Keypair.fromSeed(signer));
})

if (validSecretKeys.length === 0) {
Expand All @@ -342,7 +342,7 @@ Libify.signTransaction = function(txXdr, signers, networkObj) {

Libify.isValidSecret = function(key) {
try{
Base.Keypair.fromSeed(key);
Sdk.Keypair.fromSeed(key);
} catch (err) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/extrapolateFromXdr.js
Expand Up @@ -9,7 +9,7 @@
// - string: string values that appear as just plain text
// - object: typed values always with a type and value `{type: 'code', value: 'Foo();'}`

import {xdr, encodeCheck, Keypair, Operation} from 'stellar-base';
import {xdr, encodeCheck, Keypair, Operation} from 'stellar-sdk';
export default function extrapolateFromXdr(input, type) {
// TODO: Check to see if type exists
// TODO: input validation
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/validateTxXdr.js
@@ -1,5 +1,5 @@
import _ from 'lodash';
import {Transaction} from 'stellar-base';
import {Transaction} from 'stellar-sdk';
import validateBase64 from './validateBase64';

export default function validateTxXdr(input) {
Expand Down

0 comments on commit c131fd0

Please sign in to comment.