Skip to content

Commit

Permalink
fix: freshen the simple exchange dApp
Browse files Browse the repository at this point in the history
Refs #646

It should at least compile, deploy, and run.
  • Loading branch information
michaelfig committed Mar 12, 2020
1 parent d84ee30 commit 82f634f
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 312 deletions.
1 change: 1 addition & 0 deletions packages/dapp-simple-exchange/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dappConstants.js
14 changes: 12 additions & 2 deletions packages/dapp-simple-exchange/api/deploy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// Agoric Dapp api deployment script

export default async function deployApi(homeP, { bundleSource, pathResolve }) {
const { source, moduleFormat } = await bundleSource('./handler.js');
let overrideInstanceId;
const dc = `${process.cwd()}/dappConstants.js`;
try {
require(dc);
overrideInstanceId = __DAPP_CONSTANTS__.CONTRACT_ID;
} catch (e) {
console.log(`Proceeeding with defaults; cannot load ${dc}:`, e.message);
}

const { source, moduleFormat } = await bundleSource(pathResolve('./handler.js'));
const handlerInstall = homeP~.spawner~.install(source, moduleFormat);
const handler = handlerInstall~.spawn();
const [zoe, registrar] = await Promise.all([homeP~.zoe, homeP~.registrar]);
const handler = handlerInstall~.spawn({zoe, registrar, overrideInstanceId});
await homeP~.http~.registerCommandHandler(handler);
}
57 changes: 53 additions & 4 deletions packages/dapp-simple-exchange/api/handler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
import harden from '@agoric/harden';

export default harden((_terms, _inviteMaker) => {
export default harden(({zoe, registrar, overrideInstanceId = undefined}, _inviteMaker) => {
// If we have an overrideInstanceId, use it to assert the correct value in the RPC.
function coerceInstanceId(instanceId = undefined) {
if (instanceId === undefined) {
return overrideInstanceId;
}
if (overrideInstanceId === undefined || instanceId === overrideInstanceId) {
return instanceId;
}
throw TypeError(`instanceId ${JSON.stringify(instanceId)} must match ${JSON.stringify(overrideInstanceId)}`);
}

const registrarPCache = new Map();
function getRegistrarP(id) {
let regP = registrarPCache.get(id);
if (!regP) {
// Cache miss, so try the registrar.
regP = E(registrar).get(id);
registrarPCache.set(id, regP);
}
return regP;
}

const instancePCache = new Map();
function getInstanceP(id) {
let instanceP = instancePCache.get(id);
if (!instanceP) {
const instanceHandleP = getRegistrarP(id);
instanceP = instanceHandleP.then(instanceHandle =>
E(zoe).getInstance(instanceHandle));
instancePCache.set(id, instanceP);
}
return instanceP;
}

async function getOrderStatus(instanceRegKey, inviteHandles) {
const { publicAPI } = await getInstanceP(instanceRegKey);
return E(publicAPI).getOrderStatus(inviteHandles);
}

return harden({
getCommandHandler() {
return harden({
processInbound(obj, _home) {
async processInbound(obj, _home) {
switch (obj.type) {
case '@DIR@Message': {
return harden({ type: '@DIR@Response', orig: obj });
case 'simpleExchange/getOrderStatus': {
const { instanceRegKey } = obj;
const instanceId = coerceInstanceId(instanceRegKey);

// FIXME: Use a protocol to negotiate a sharingService.
const inviteHandles = [];

const data = await getOrderStatus(instanceId, inviteHandles);
return harden({
type: 'simpleExchange/orderStatus',
data,
});
}
default:
return undefined;
Expand Down
1 change: 0 additions & 1 deletion packages/dapp-simple-exchange/contract/autoswap.js

This file was deleted.

203 changes: 0 additions & 203 deletions packages/dapp-simple-exchange/contract/deploy-autoswap.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/dapp-simple-exchange/contract/deploy-myfirstdapp.js

This file was deleted.

Loading

0 comments on commit 82f634f

Please sign in to comment.