HackerNoon.com
Published in

HackerNoon.com

Migrating from Hyperledger Composer to Convector Framework — Marbles Example

Right now Convector supports Hyperledger Fabric already, and we're planning to add support for multiple Blockchain techs soon, stay tuned!

  • Assets: houses and listings
  • Participants: buyers and homeowners
  • Transactions: buying or selling houses, and creating and closing listings
  • Models: A reference to something in the real world, tangible or intangible, describing its properties. It might as well be a participant in the network.
  • Controllers: A set of rules on how you can interact with the models.
namespace org.hyperledger_composer.marblesenum MarbleColor {
o RED
o GREEN
o BLUE
o PURPLE
o ORANGE
}
enum MarbleSize {
o SMALL
o MEDIUM
o LARGE
}
asset Marble identified by marbleId {
o String marbleId
o MarbleSize size
o MarbleColor color
--> Player owner
}
participant Player identified by email {
o String email
o String firstName
o String lastName
}
export enum MarbleColor {
RED,
GREEN,
BLUE,
PURPLE,
ORANGE
}
export enum MarbleSize {
SMALL,
MEDIUM,
LARGE
}
export class Marble extends ConvectorModel<Marble> {
@ReadOnly()
@Required()
public type = 'io.worldsibu.marbles.marble';
@Validate(yup.number())
@Default(MarbleSize.MEDIUM)
public size: MarbleSize;
@Validate(yup.number())
public color: MarbleColor;
@Validate(yup.string().email())
public owner: string;
}
export class Player extends ConvectorModel<Player> {
@Required()
@ReadOnly()
public type = 'io.worldsibu.marbles.player';
@Validate(yup.string().email())
public id: string;
@Validate(yup.string())
public firstName: string;
@Validate(yup.string())
public lastName: string;
}

Chaincode Logic

rule Default {
description: "Allow all participants access to all resources"
participant: "org.hyperledger_composer.marbles.player"
operation: ALL
resource: "org.hyperledger_composer.marbles.*"
action: ALLOW
}
transaction TradeMarble {
--> Marble marble
--> Player newOwner
}
async function tradeMarble(tradeMarble) {
tradeMarble.marble.owner = tradeMarble.newOwner;
const assetRegistry = await getAssetRegistry('org.hyperledger_composer.marbles.Marble'); await assetRegistry.update(tradeMarble.marble);
}
@Controller('marble')
export class MarbleController extends ConvectorController {

@Invokable()
public async create(@Param(Marble) marble: Marble) {
await marble.save();
}
@Invokable()
public async trade(
@Param(yup.string()) marbleId: string,
@Param(yup.string().email()) newOwner: string
): Promise<void> {
// use this.sender for authorization checks
const marble = await Marble.getOne(marbleId); marble.owner = newOwner; await marble.save();
}
}

Client Access

const namespace = 'org.hyperledger_composer.marbles';
const factory = businessNetworkConnection.getBusinessNetwork().getFactory();
const dan = factory.newResource(namespace, 'Player', 'daniel.selman@example.com');
dan.firstName = 'Dan';
dan.lastName = 'Selman';
const simon = factory.newResource(namespace, 'Player', 'sstone1@example.com');
simon.firstName = 'Simon';
simon.lastName = 'Stone';
const playerRegistry = await businessNetworkConnection.getParticipantRegistry(namespace + '.Player');
await playerRegistry.addAll([dan, simon]);
const marble = factory.newResource(namespace, 'Marble', 'MARBLE_001');
marble.size = 'SMALL';
marble.color = 'RED';
marble.owner = factory.newRelationship(namespace, 'Player', dan.$identifier);
const marbleRegistry = await businessNetworkConnection.getAssetRegistry(namespace + '.Marble');
await marbleRegistry.add(marble);
const tradeMarble = factory.newTransaction(namespace, 'TradeMarble');
tradeMarble.newOwner = factory.newRelationship(namespace, 'Player', simon.$identifier);
tradeMarble.marble = factory.newRelationship(namespace, 'Marble', marble.$identifier);
await businessNetworkConnection.submitTransaction(tradeMarble);
const adapter = new FabricControllerAdapter(configuration);
const playerCtrl = new PlayerControllerClient(adapter);
const marbleCtrl = new MarbleControllerClient(adapter);
const dan = new Player({
id: 'daniel.selman@example.com',
firstName: 'Dan',
lastName: 'Selman'
});
await playerCtrl.register(dan);
const simon = new Player({
id: 'sstone1@example.com',
firstName: 'Simon',
lastName: 'Stone'
});
await playerCtrl.register(simon);
const marble = new Marble({
id: '1',
size: MarbleSize.SMALL,
color: MarbleColor.RED,
owner: dan.id
});
await marbleCtrl.create(marble);
await marbleCtrl.trade(marble.id, simon.id);

Conclusion

--

--

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store