@windingtree/wt-read-api
Version:
API to interact with the Winding Tree platform
82 lines (72 loc) • 2.97 kB
JavaScript
const Web3 = require('web3');
const lib = require('zos-lib');
const { getSchemaVersion } = require('../../test/utils/schemas');
const provider = new Web3.providers.HttpProvider('http://localhost:8545');
const web3 = new Web3(provider);
const Contracts = lib.Contracts;
const Organization = Contracts.getFromNodeModules('@windingtree/wt-contracts', 'Organization');
const deployFullHotel = async (deploymentOptions, hotelDescription, ratePlans, availability, malformedGuarantee = 0) => {
const dataFormatVersion = deploymentOptions.schemaVersion;
const offChainDataAdapter = deploymentOptions.offChainDataClient;
const accounts = await web3.eth.getAccounts();
const indexFile = {};
if (hotelDescription) {
indexFile.descriptionUri = await offChainDataAdapter.upload(JSON.stringify(hotelDescription));
}
if (ratePlans) {
indexFile.ratePlansUri = await offChainDataAdapter.upload(JSON.stringify(ratePlans));
}
if (availability) {
indexFile.availabilityUri = await offChainDataAdapter.upload(JSON.stringify(availability));
}
indexFile.notificationsUri = 'https://notifications.example';
indexFile.bookingUri = 'https://booking.example';
indexFile.dataFormatVersion = dataFormatVersion;
indexFile.defaultLocale = 'en';
const dataUri = await offChainDataAdapter.upload(JSON.stringify(indexFile));
const orgJsonContent = {
dataFormatVersion: getSchemaVersion('@windingtree/wt-hotel-schemas'),
name: hotelDescription.name,
hotel: {
name: hotelDescription.name,
apis: [
{
entrypoint: dataUri,
format: 'windingtree',
},
{
entrypoint: 'http://dummy.restapiexample.com/api/v1/employees',
format: 'coolapi',
},
],
},
};
const orgJsonString = JSON.stringify(orgJsonContent);
const orgJsonUri = await offChainDataAdapter.upload(orgJsonString);
const orgJsonHash = web3.utils.soliditySha3(orgJsonString);
const hotelEvent = await deploymentOptions.factory.methods.createAndAddToDirectory(orgJsonUri, orgJsonHash, deploymentOptions.directoryAddress).send({ from: accounts[0] });
const hotel = Organization.at(hotelEvent.events.OrganizationCreated.returnValues.organization);
hotel.setProvider('http://localhost:8545');
await hotel.methods.addAssociatedKey(accounts[0]).send({ from: accounts[0] });
const monthFromNow = new Date();
monthFromNow.setMonth(monthFromNow.getMonth() + 1);
const rawClaim = {
hotel: hotel.address,
guarantor: accounts[0],
expiresAt: monthFromNow.getTime(),
};
if (malformedGuarantee) {
delete rawClaim.expiresAt;
}
const hexClaim = web3.utils.utf8ToHex(JSON.stringify(rawClaim));
const signature = await web3.eth.sign(hexClaim, accounts[0]);
indexFile.guarantee = {
claim: hexClaim,
signature: signature,
};
await offChainDataAdapter.update(dataUri, JSON.stringify(indexFile));
return hotel;
};
module.exports = {
deployFullHotel,
};