Skip to content

Commit e24e35f

Browse files
authored
feat(exchange-vat): Reduce the number of round-trips (#112)
* feat(exchange-vat): Reduce the number of round-trips in getPrice() * feat(exchange-vat): Reduce the number of round-trips in getOfferRules()
1 parent e0488a1 commit e24e35f

File tree

2 files changed

+87
-92
lines changed

2 files changed

+87
-92
lines changed

lib/ag-solo/vats/lib-exchange.js

+84-89
Original file line numberDiff line numberDiff line change
@@ -12,99 +12,94 @@ function checkOrder(a0, a1, b0, b1) {
1212
throw new TypeError('Canot resove asset ordering');
1313
}
1414

15-
export async function makeExchange(E, log, host, zoe, registrar) {
16-
// === API
17-
18-
async function getPrice(instanceId, extent0, assayId0, assayId1) {
19-
const instanceHandle = await E(registrar).get(instanceId);
20-
21-
// Find the assays in the registrar
22-
const registrarAssays = await Promise.all([
23-
E(registrar).get(assayId0),
24-
E(registrar).get(assayId1),
25-
]);
26-
27-
// Get the assays in the contract.
28-
// Get the contract instance.
29-
const {
30-
terms: { assays: contractAssays },
31-
instance,
32-
} = await E(zoe).getInstance(instanceHandle);
33-
34-
// Check whether we sell on contract assay 0 or 1.
35-
const normal = checkOrder(
36-
registrarAssays[0],
37-
registrarAssays[1],
38-
contractAssays[0],
39-
contractAssays[1],
15+
export function makeExchange(E, log, host, zoe, registrar) {
16+
17+
function getPrice(instanceId, extent0, assayId0, assayId1) {
18+
const instanceHandleP = E(registrar).get(instanceId);
19+
const regAssay0P = E(registrar).get(assayId0);
20+
const regAssay1P = E(registrar).get(assayId1);
21+
const unit0P = E(regAssay0P).makeUnits(extent0);
22+
23+
return Promise.all([instanceHandleP, regAssay0P, regAssay1P, unit0P]).then(
24+
([instanceHandle, regAssay0, regAssay1, unit0]) =>
25+
E(zoe)
26+
.getInstance(instanceHandle)
27+
.then(
28+
({
29+
terms: {
30+
assays: [contractAssay0, contractAssay1],
31+
},
32+
instance,
33+
}) => {
34+
// Check whether we sell on contract assay 0 or 1.
35+
const normal = checkOrder(
36+
regAssay0,
37+
regAssay1,
38+
contractAssay0,
39+
contractAssay1,
40+
);
41+
// Order the units accordingly.
42+
const units = [
43+
normal ? unit0 : undefined,
44+
normal ? undefined : unit0,
45+
undefined,
46+
];
47+
return E(instance)
48+
.getPrice(units)
49+
.then(unit1 => unit1.extent);
50+
},
51+
),
4052
);
41-
42-
// Units of the input amount.
43-
const unit0 = await E(registrarAssays[0]).makeUnits(extent0);
44-
45-
// Order the units accordingly.
46-
const units = [
47-
normal ? unit0 : undefined,
48-
normal ? undefined : unit0,
49-
undefined,
50-
];
51-
52-
// Extract the price (multi steps for debugging).
53-
const unit1 = await E(instance).getPrice(units);
54-
const { extent } = unit1;
55-
return extent;
5653
}
5754

58-
async function getOfferRules(instanceId, extent, assayId0, assayId1) {
59-
const instanceHandle = await E(registrar).get(instanceId);
60-
61-
// Find the assays by id in the registrar.
62-
const registrarAssays = await Promise.all([
63-
E(registrar).get(assayId0),
64-
E(registrar).get(assayId1),
65-
]);
66-
67-
// Get the assays in the contract.
68-
const {
69-
terms: { assays: contractAssays },
70-
} = await E(zoe).getInstance(instanceHandle);
71-
72-
// Check whether we sell on contract assay 0 or 1.
73-
const normal = checkOrder(
74-
registrarAssays[0],
75-
registrarAssays[1],
76-
contractAssays[0],
77-
contractAssays[1],
55+
function getOfferRules(instanceId, extent0, assayId0, assayId1) {
56+
const instanceHandleP = E(registrar).get(instanceId);
57+
const regAssay0P = E(registrar).get(assayId0);
58+
const regAssay1P = E(registrar).get(assayId1);
59+
60+
return Promise.all([instanceHandleP, regAssay0P, regAssay1P]).then(
61+
([instanceHandle, regAssay0, regAssay1]) =>
62+
E(zoe)
63+
.getInstance(instanceHandle)
64+
.then(({ terms: { assays: [contractAssay0, contractAssay1] } }) => {
65+
// Check whether we sell on contract assay 0 or 1.
66+
const normal = checkOrder(
67+
regAssay0,
68+
regAssay1,
69+
contractAssay0,
70+
contractAssay1,
71+
);
72+
73+
// Contrust the rules for serialization (no instance).
74+
// This rule is the payment
75+
const rule0 = {
76+
kind: 'offerExactly',
77+
units: { assayId: assayId0, extent: extent0 },
78+
};
79+
// This rule is the payout
80+
const rule1 = {
81+
kind: 'wantAtLeast',
82+
units: { assayId: assayId1 },
83+
};
84+
85+
// Order the rules accordingly.
86+
const offerRules = harden({
87+
payoutRules: [
88+
normal ? rule0 : rule1,
89+
normal ? rule1 : rule0,
90+
{
91+
kind: 'wantAtLeast',
92+
units: {},
93+
},
94+
],
95+
exitRule: {
96+
kind: 'onDemand',
97+
},
98+
});
99+
100+
return offerRules;
101+
}),
78102
);
79-
80-
// Contrust the rules for serialization (no instance).
81-
// This rule is the payment
82-
const rule0 = {
83-
kind: 'offerExactly',
84-
units: { assayId: assayId0, extent },
85-
};
86-
// This rule is the payout
87-
const rule1 = {
88-
kind: 'wantAtLeast',
89-
units: { assayId: assayId1 },
90-
};
91-
92-
// Order the rules accordingly.
93-
const offerRules = harden({
94-
payoutRules: [
95-
normal ? rule0 : rule1,
96-
normal ? rule1 : rule0,
97-
{
98-
kind: 'wantAtLeast',
99-
units: {},
100-
},
101-
],
102-
exitRule: {
103-
kind: 'onDemand',
104-
},
105-
});
106-
107-
return offerRules;
108103
}
109104

110105
const autoswap = harden({

lib/ag-solo/vats/vat-exchange.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { makeExchange } from './lib-exchange';
44
function build(E, log) {
55
let userFacet;
66

7-
async function startup(host, zoe, registrar, uploads) {
8-
const exchange = await makeExchange(E, log, host, zoe, registrar, uploads);
7+
function startup(host, zoe, registrar, uploads) {
8+
const exchange = makeExchange(E, log, host, zoe, registrar, uploads);
99
userFacet = exchange.userFacet;
1010
}
1111

12-
async function getExchange() {
12+
function getExchange() {
1313
return harden(userFacet);
1414
}
1515

0 commit comments

Comments
 (0)