Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freshen the simple exchange dApp #676

Merged
merged 2 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

This file was deleted.

Loading