Skip to content

Commit

Permalink
feat(autoswap): Reduce the number of awaits in init-autoswap.js (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfparadis authored Nov 3, 2019
1 parent 1978e4c commit 32aaf84
Showing 1 changed file with 91 additions and 40 deletions.
131 changes: 91 additions & 40 deletions lib/ag-solo/init-autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,128 @@ export default async ({ home, bundle }) => {

console.log('*** AUTOSWAP');

// *********************
// AUTOSWAP INSTALL
// *********************

// 1. Load & install the autoswap contract.

await upload(home, bundle, [ CONTRACT_NAME ]);

// 2. Get the autoswap contract installation.
const installationHandle = await home~.uploads~.get(CONTRACT_NAME);
// =====================
// === AWAITING TURN ===
// =====================

// 2. Get the autoswap contract installation.
// 3. Store the contract installation in the registry.
const installationId = await home~.registrar~.register(CONTRACT_NAME, installationHandle);

const installationHandleP = home~.uploads~.get(CONTRACT_NAME);
const installationIdP = home~.registrar~.register(CONTRACT_NAME, installationHandleP);

const [installationHandle, installationId]
= await Promise.all([installationHandleP, installationIdP]);

// =====================
// === AWAITING TURN ===
// =====================

console.log('- Autoswap intallation', CONTRACT_NAME, '=>', installationId);

// *********************
// AUTOSWAP INSTANCE

// 1. Wallet has (at least 2?) purses. Get their assays
const purses = await Promise.all([
home~.wallet~.getPurse('Moola purse'),
home~.wallet~.getPurse('Simolean purse'),
// *********************

// 1. Purses, assays, payments.
const purse0P = home~.wallet~.getPurse('Moola purse');
const purse1P = home~.wallet~.getPurse('Simolean purse');
const assay0P = purse0P~.getAssay();
const assay1P = purse1P~.getAssay();
const payment0P = purse0P~.withdraw(INITIAL_LIQUIDITY);
const payment1P = purse1P~.withdraw(INITIAL_LIQUIDITY);

const [
purse0,
purse1,
assay0,
assay1,
payment0,
payment1
] = await Promise.all([
purse0P,
purse1P,
assay0P,
assay1P,
payment0P,
payment1P
]);
const assays = await Promise.all(
purses.map(purse => purse~.getAssay()),
)

// 2. Contract instance.
try {
await home~.zoe~.makeInstance(installationHandle, { assays });
} catch(e) {}
const { instance, instanceHandle, terms } = await home~.zoe~.makeInstance(installationHandle, { assays });

// 3. Offer rules
const units = await Promise.all([
terms~.assays~.[0]~.makeUnits(INITIAL_LIQUIDITY),
terms~.assays~.[1]~.makeUnits(INITIAL_LIQUIDITY),
terms~.assays~.[2]~.makeUnits(0),

// =====================
// === AWAITING TURN ===
// =====================

// 2. Contract instance, contract assays.
const { instance, instanceHandle, terms: { assays } } =
await home~.zoe~.makeInstance(installationHandle, { assays: [assay0, assay1] });

// =====================
// === AWAITING TURN ===
// =====================

// 3. Offer rules.
const unit0P = assays~.[0]~.makeUnits(INITIAL_LIQUIDITY);
const unit1P = assays~.[1]~.makeUnits(INITIAL_LIQUIDITY);
const unit2P = assays~.[2]~.makeUnits(0);

const [
unit0,
unit1,
unit2,
] = await Promise.all([
unit0P,
unit1P,
unit2P,
]);

// =====================
// === AWAITING TURN ===
// =====================

// 5.
const offerRules = harden({
payoutRules: [
{
kind: 'offerExactly',
units: units[0],
units: unit0,
},
{
kind: 'offerExactly',
units: units[1],
units: unit1,
},
{
kind: 'wantAtLeast',
units: units[2],
units: unit2,
},
],
exitRule: {
kind: 'onDemand',
},
});

// 4. Payments
const payments = await Promise.all([
purses[0]~.withdraw(INITIAL_LIQUIDITY),
purses[1]~.withdraw(INITIAL_LIQUIDITY),
]);
const { escrowReceipt } = await home~.zoe~.escrow(offerRules, [payment0, payment1]);

// 5. Liquidities.
const { escrowReceipt } = await home~.zoe~.escrow(offerRules, payments);
const liquidityOk = await instance~.addLiquidity(escrowReceipt);
console.log(liquidityOk);
// =====================
// === AWAITING TURN ===
// =====================

const liquidityOkP = instance~.addLiquidity(escrowReceipt);
const instanceIdP = home~.registrar~.register(CONTRACT_NAME, instanceHandle);

if (liquidityOk) {
// Only store if the contract instance has liquidities.
const instanceId = await home~.registrar~.register(CONTRACT_NAME, instanceHandle);
console.log('- Autoswap instance', CONTRACT_NAME, '=>', instanceId);
}
const [liquidityOk, instanceId] = await Promise.all([liquidityOkP, instanceIdP]);

// =====================
// === AWAITING TURN ===
// =====================

console.log('- Autoswap instance', CONTRACT_NAME, '=>', instanceId);
console.log(liquidityOk);
}

0 comments on commit 32aaf84

Please sign in to comment.