Skip to content

Commit

Permalink
Merge pull request #614 from Agoric/deployForSimpleExchange
Browse files Browse the repository at this point in the history
remove more references to liquidity tokens and other cleanups
  • Loading branch information
Chris-Hibbert committed Feb 28, 2020
2 parents ef8a13d + 47a0ebb commit c963123
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 10 deletions.
149 changes: 149 additions & 0 deletions packages/agoric-cli/template/contract/deploy-autoswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Agoric Dapp contract deployment script for autoswap
import fs from 'fs';

import harden from '@agoric/harden';

// This javascript source file uses the "tildot" syntax (foo~.bar()) for
// eventual sends. Tildot is standards track with TC39, the JavaScript standards
// committee.
// TODO: improve this comment. https://github.com/Agoric/agoric-sdk/issues/608

const DAPP_NAME = "@DIR@";

export default async function deployContract(homeP, { bundleSource, pathResolve },
CONTRACT_NAME = 'autoswap') {

// Create a source bundle for the "myFirstDapp" smart contract.
const { source, moduleFormat } = await bundleSource(`./${CONTRACT_NAME}.js`);

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

const installationHandle = await homeP~.zoe~.install(source, moduleFormat);

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

// 1. Assays and payments
const purse0P = homeP~.wallet~.getPurse('moola');
const purse1P = homeP~.wallet~.getPurse('simolean');
const assay0P = purse0P~.getAssay();
const assay1P = purse1P~.getAssay();
const payment0P = purse0P~.withdraw(900);
const payment1P = purse1P~.withdraw(900);

const [
purse0,
purse1,
assay0,
assay1,
payment0,
payment1
] = await Promise.all([
purse0P,
purse1P,
assay0P,
assay1P,
payment0P,
payment1P
]);

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

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

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

// 3. Get the instanceHandle

const {
extent: { instanceHandle },
} = await invite~.getBalance();

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

// 4. Get the contract terms and assays

const { terms: { assays }} = await homeP~.zoe~.getInstance(instanceHandle);


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

// 5. Offer rules
const [unit0, unit1, unit2] = await Promise.all([
assays~.[0]~.makeUnits(900),
assays~.[1]~.makeUnits(900),
assays~.[2]~.makeUnits(0),
]);

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

const offerRules = harden({
payoutRules: [
{
kind: 'offerAtMost',
units: unit0,
},
{
kind: 'offerAtMost',
units: unit1,
},
{
kind: 'wantAtLeast',
units: unit2,
},
],
exitRule: {
kind: 'onDemand',
},
});

// 6. Liquidities.

const payments = [payment0, payment1];

const { seat, payout } = await homeP~.zoe~.redeem(invite, offerRules, payments);

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

const [liquidityOk, contractId, instanceId] = await Promise.all([
seat~.addLiquidity(),
homeP~.registrar~.register(DAPP_NAME, installationHandle),
homeP~.registrar~.register(CONTRACT_NAME, instanceHandle),
]);

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

console.log('- installation made', CONTRACT_NAME, '=>', installationHandle);
console.log('- instance made', CONTRACT_NAME, '=>', instanceId);
console.log(liquidityOk);

// Save the instanceId somewhere where the UI can find it.
if (liquidityOk) {
const cjfile = pathResolve(`../ui/src/utils/contractID.js`);
console.log('writing', cjfile);
await fs.promises.writeFile(cjfile, `export default ${JSON.stringify(instanceId)};`);

// =====================
// === AWAITING TURN ===
// =====================
}
}
22 changes: 12 additions & 10 deletions packages/agoric-cli/template/contract/deploy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Agoric Dapp contract deployment script
// Agoric Dapp contract deployment script for myFirstDapp/simpleExchange
import fs from 'fs';

// This javascript source file uses the "tildot" syntax (foo~.bar()) for
// eventual sends. Tildot is standards track with TC39, the JavaScript standards
// committee.
// TODO: improve this comment. https://github.com/Agoric/agoric-sdk/issues/608

import harden from '@agoric/harden';

const DAPP_NAME = "@DIR@";
Expand Down Expand Up @@ -50,15 +55,15 @@ export default async function deployContract(homeP, { bundleSource, pathResolve
// =====================

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

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

// 3. Get the instanceHandle

const {
extent: { instanceHandle },
} = await invite~.getBalance();
Expand All @@ -71,7 +76,6 @@ export default async function deployContract(homeP, { bundleSource, pathResolve

const { terms: { assays }} = await homeP~.zoe~.getInstance(instanceHandle);


// =====================
// === AWAITING TURN ===
// =====================
Expand Down Expand Up @@ -102,7 +106,7 @@ export default async function deployContract(homeP, { bundleSource, pathResolve
},
});

// 6. Liquidities.
// 6. Registration.

const payments = [payment0, payment1];

Expand All @@ -112,8 +116,7 @@ export default async function deployContract(homeP, { bundleSource, pathResolve
// === AWAITING TURN ===
// =====================

const [liquidityOk, contractId, instanceId] = await Promise.all([
seat~.addLiquidity(),
const [contractId, instanceId] = await Promise.all([
homeP~.registrar~.register(DAPP_NAME, installationHandle),
homeP~.registrar~.register(CONTRACT_NAME, instanceHandle),
]);
Expand All @@ -124,10 +127,9 @@ export default async function deployContract(homeP, { bundleSource, pathResolve

console.log('- installation made', CONTRACT_NAME, '=>', installationHandle);
console.log('- instance made', CONTRACT_NAME, '=>', instanceId);
console.log(liquidityOk);

// Save the instanceId somewhere where the UI can find it.
if (liquidityOk) {
if (instanceId) {
const cjfile = pathResolve(`../ui/src/utils/contractID.js`);
console.log('writing', cjfile);
await fs.promises.writeFile(cjfile, `export default ${JSON.stringify(instanceId)};`);
Expand Down

0 comments on commit c963123

Please sign in to comment.