Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
convert trading sample to es7 syntax (#140)
Browse files Browse the repository at this point in the history
* convert trading sample to es7 syntax

Signed-off-by: andrew-coleman <[email protected]>

* changed promise.all to async/await

Signed-off-by: andrew-coleman <[email protected]>
  • Loading branch information
andrew-coleman authored and Dave Kelsey committed Jan 8, 2018
1 parent 7cad3f2 commit 46590cb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 179 deletions.
1 change: 0 additions & 1 deletion packages/trade-network/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
coverage
dist
go
lib
node_modules
out
3 changes: 1 addition & 2 deletions packages/trade-network/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ env:
mocha: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 2015
ecmaVersion: 8
sourceType:
- script
rules:
Expand All @@ -27,7 +27,6 @@ rules:
curly: error
eqeqeq: error
no-throw-literal: error
strict: error
no-var: error
dot-notation: error
no-tabs: error
Expand Down
50 changes: 0 additions & 50 deletions packages/trade-network/lib/.eslintrc.yml

This file was deleted.

58 changes: 23 additions & 35 deletions packages/trade-network/lib/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,44 @@
* limitations under the License.
*/

/* global getAssetRegistry getFactory emit query */

/**
* Track the trade of a commodity from one trader to another
* @param {org.acme.trading.Trade} trade - the trade to be processed
* @transaction
*/
function tradeCommodity(trade) {
async function tradeCommodity(trade) { // eslint-disable-line no-unused-vars

// set the new owner of the commodity
trade.commodity.owner = trade.newOwner;
return getAssetRegistry('org.acme.trading.Commodity')
.then(function (assetRegistry) {
const assetRegistry = await getAssetRegistry('org.acme.trading.Commodity');

// emit a notification that a trade has occurred
var tradeNotification = getFactory().newEvent('org.acme.trading', 'TradeNotification');
tradeNotification.commodity = trade.commodity;
emit(tradeNotification);
// emit a notification that a trade has occurred
const tradeNotification = getFactory().newEvent('org.acme.trading', 'TradeNotification');
tradeNotification.commodity = trade.commodity;
emit(tradeNotification);

// persist the state of the commodity
return assetRegistry.update(trade.commodity);
});
// persist the state of the commodity
await assetRegistry.update(trade.commodity);
}

/**
* Remove all high volume commodities
* @param {org.acme.trading.RemoveHighQuantityCommodities} remove - the remove to be processed
* @transaction
*/
function removeHighQuantityCommodities(remove) {

return getAssetRegistry('org.acme.trading.Commodity')
.then(function (assetRegistry) {
return query('selectCommoditiesWithHighQuantity')
.then(function (results) {

var promises = [];

for (var n = 0; n < results.length; n++) {
var trade = results[n];

// emit a notification that a trade was removed
var removeNotification = getFactory().newEvent('org.acme.trading', 'RemoveNotification');
removeNotification.commodity = trade;
emit(removeNotification);

// remove the commodity
promises.push(assetRegistry.remove(trade));
}

// we have to return all the promises
return Promise.all(promises);
});
});
async function removeHighQuantityCommodities(remove) { // eslint-disable-line no-unused-vars

const assetRegistry = await getAssetRegistry('org.acme.trading.Commodity');
const results = await query('selectCommoditiesWithHighQuantity');

// since all registry requests have to be serialized anyway, there is no benefit to calling Promise.all
// on an array of promises
results.forEach(async trade => {
const removeNotification = getFactory().newEvent('org.acme.trading', 'RemoveNotification');
removeNotification.commodity = trade;
emit(removeNotification);
await assetRegistry.remove(trade);
});
}
159 changes: 68 additions & 91 deletions packages/trade-network/test/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Commodity Trading', () => {
let adminConnection;
let businessNetworkConnection;

before(() => {
before(async () => {
// Embedded connection used for local testing
const connectionProfile = {
name: 'embedded',
Expand All @@ -55,46 +55,42 @@ describe('Commodity Trading', () => {
const deployerCardName = 'PeerAdmin';
adminConnection = new AdminConnection({ cardStore: cardStore });

return adminConnection.importCard(deployerCardName, deployerCard).then(() => {
return adminConnection.connect(deployerCardName);
});
await adminConnection.importCard(deployerCardName, deployerCard);
await adminConnection.connect(deployerCardName);
});

beforeEach(() => {
beforeEach(async () => {
businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });

const adminUserName = 'admin';
let adminCardName;
let businessNetworkDefinition;

return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..')).then(definition => {
businessNetworkDefinition = definition;
// Install the Composer runtime for the new business network
return adminConnection.install(businessNetworkDefinition.getName());
}).then(() => {
// Start the business network and configure an network admin identity
const startOptions = {
networkAdmins: [
{
userName: adminUserName,
enrollmentSecret: 'adminpw'
}
]
};
return adminConnection.start(businessNetworkDefinition, startOptions);
}).then(adminCards => {
// Import the network admin identity for us to use
adminCardName = `${adminUserName}@${businessNetworkDefinition.getName()}`;
return adminConnection.importCard(adminCardName, adminCards.get(adminUserName));
}).then(() => {
// Connect to the business network using the network admin identity
return businessNetworkConnection.connect(adminCardName);
});
const businessNetworkDefinition = await BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));

// Install the Composer runtime for the new business network
await adminConnection.install(businessNetworkDefinition.getName());

// Start the business network and configure an network admin identity
const startOptions = {
networkAdmins: [
{
userName: adminUserName,
enrollmentSecret: 'adminpw'
}
]
};
const adminCards = await adminConnection.start(businessNetworkDefinition, startOptions);

// Import the network admin identity for us to use
adminCardName = `${adminUserName}@${businessNetworkDefinition.getName()}`;
await adminConnection.importCard(adminCardName, adminCards.get(adminUserName));

// Connect to the business network using the network admin identity
await businessNetworkConnection.connect(adminCardName);
});

describe('#tradeCommodity', () => {

it('should be able to trade a commodity', () => {
it('should be able to trade a commodity', async () => {
const factory = businessNetworkConnection.getBusinessNetwork().getFactory();

// create the traders
Expand Down Expand Up @@ -134,66 +130,47 @@ describe('Commodity Trading', () => {
});

// Get the asset registry.
return businessNetworkConnection.getAssetRegistry(namespace + '.Commodity')
.then((assetRegistry) => {

// add the commodities to the asset registry.
return assetRegistry.addAll([commodity,commodity2])
.then(() => {
return businessNetworkConnection.getParticipantRegistry(namespace + '.Trader');
})
.then((participantRegistry) => {
// add the traders
return participantRegistry.addAll([dan, simon]);
})
.then(() => {
// submit the transaction
return businessNetworkConnection.submitTransaction(trade);
})
.then(() => {
return businessNetworkConnection.getAssetRegistry(namespace + '.Commodity');
})
.then((assetRegistry) => {
// re-get the commodity
return assetRegistry.get(commodity.$identifier);
})
.then((newCommodity) => {
// the owner of the commodity should now be simon
newCommodity.owner.$identifier.should.equal(simon.$identifier);
})
.then(() => {
// use a query
return businessNetworkConnection.query('selectCommoditiesByExchange', {exchange : 'Euronext'});
})
.then((results) => {
// check results
results.length.should.equal(1);
results[0].getIdentifier().should.equal('EMA');
})
.then(() => {
// use another query
return businessNetworkConnection.query('selectCommoditiesByOwner', {owner : 'resource:' + simon.getFullyQualifiedIdentifier()});
})
.then((results) => {
// check results
results.length.should.equal(1);
results[0].getIdentifier().should.equal('EMA');
})
.then(() => {
// submit the remove transaction
const remove = factory.newTransaction(namespace, 'RemoveHighQuantityCommodities');
return businessNetworkConnection.submitTransaction(remove);
})
.then(() => {
// use a query
return businessNetworkConnection.query('selectCommodities');
})
.then((results) => {
// check results, should only have 1 commodity left
results.length.should.equal(1);
results[0].getIdentifier().should.equal('XYZ');
});
});
const assetRegistry = await businessNetworkConnection.getAssetRegistry(namespace + '.Commodity');

// add the commodities to the asset registry.
await assetRegistry.addAll([commodity,commodity2]);

// add the traders
const participantRegistry = await businessNetworkConnection.getParticipantRegistry(namespace + '.Trader');
await participantRegistry.addAll([dan, simon]);

// submit the transaction
await businessNetworkConnection.submitTransaction(trade);

// re-get the commodity
const newCommodity = await assetRegistry.get(commodity.$identifier);
// the owner of the commodity should now be simon
newCommodity.owner.$identifier.should.equal(simon.$identifier);

// use a query
let results = await businessNetworkConnection.query('selectCommoditiesByExchange', {exchange : 'Euronext'});

// check results
results.length.should.equal(1);
results[0].getIdentifier().should.equal('EMA');

// use another query
results = await businessNetworkConnection.query('selectCommoditiesByOwner', {owner : 'resource:' + simon.getFullyQualifiedIdentifier()});

// check results
results.length.should.equal(1);
results[0].getIdentifier().should.equal('EMA');

// submit the remove transaction
const remove = factory.newTransaction(namespace, 'RemoveHighQuantityCommodities');
await businessNetworkConnection.submitTransaction(remove);

// use a query
results = await businessNetworkConnection.query('selectCommodities');

// check results, should only have 1 commodity left
results.length.should.equal(1);
results[0].getIdentifier().should.equal('XYZ');
});
});
});

0 comments on commit 46590cb

Please sign in to comment.