Skip to content

Commit

Permalink
fix(deploy): Remove mints in deploy scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jfparadis committed Nov 3, 2019
1 parent 60855f2 commit 04de7b0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
4 changes: 1 addition & 3 deletions template/api/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import harden from '@agoric/harden';
export default async function deployApi(homeP, { bundleSource, pathResolve }) {
const { source, moduleFormat } = await bundleSource('./handler.js');
const handlerInstall = homeP~.spawner~.install(source, moduleFormat);
const cjson = await fs.promises.readFile(pathResolve('./contracts.json'));
const contracts = JSON.parse(cjson);
const handler = handlerInstall~.spawn(harden({ contracts }));
const handler = handlerInstall~.spawn();
await homeP~.http~.registerCommandHandler(handler);
}
3 changes: 1 addition & 2 deletions template/api/handler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import harden from '@agoric/harden';

export default harden((terms, _inviteMaker) => {
const {contracts} = terms;
return harden({
getCommandHandler() {
return harden({
processInbound(obj, home) {
switch (obj.type) {
case '@DIR@Message': {
return harden({type: '@DIR@Response', orig: obj, contracts});
return harden({type: '@DIR@Response', orig: obj});
}
}
return undefined;
Expand Down
110 changes: 75 additions & 35 deletions template/contract/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,123 @@ import fs from 'fs';
import harden from '@agoric/harden';

const DAPP_NAME = "@DIR@";
const CONTRACT_NAME = 'myFirstDapp';

export default async function deployContract(homeP, { bundleSource, pathResolve }) {

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

const installationHandle = await homeP~.zoe~.install(source, moduleFormat);
const contractId = await homeP~.registrar~.register(DAPP_NAME, installationHandle);
// =====================
// === AWAITING TURN ===
// =====================

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

console.log('- myFirstDapp installed', CONTRACT_NAME, '=>', installationHandle);
// =====================
// === AWAITING TURN ===
// =====================

// 1. Assays
const assays = await Promise.all([
homeP~.moolaMint~.getAssay(),
homeP~.simoleanMint~.getAssay(),
// 1. Assays and payments
const purse0P = homeP~.wallet~.getPurse('Moola purse');
const purse1P = homeP~.wallet~.getPurse('Simolean purse');
const assay0P = purse0P~.getAssay();
const assay1P = purse1P~.getAssay();
const payment0P = purse0P~.withdraw(1);
const payment1P = purse1P~.withdraw(1);

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

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

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

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

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

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

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 (from mint, not from purse)
// 4. Liquidities.

const faucets = await Promise.all([
homeP~.moolaMint~.mint(units[0]),
homeP~.simoleanMint~.mint(units[1]),
]);
const payments = [payment0, payment1];

const payments = await Promise.all([
faucets[0]~.withdrawAll(),
faucets[1]~.withdrawAll(),
const { escrowReceipt } = await homeP~.zoe~.escrow(offerRules, payments);

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

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

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

if (liquidityOk) {
// Only store if the contract instance has liquidity.
const instanceId = await homeP~.registrar~.register(CONTRACT_NAME, instanceHandle);
console.log('- Autoswap instance', CONTRACT_NAME, '=>', instanceId);
console.log('- myFirstDapp installation made', CONTRACT_NAME, '=>', installationHandle);
console.log('- myFirstDapp 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 ===
// =====================
}
}

0 comments on commit 04de7b0

Please sign in to comment.