diff --git a/packages/agoric-cli/template/contract/deploy-autoswap.js b/packages/agoric-cli/template/contract/deploy-autoswap.js new file mode 100644 index 00000000000..0bd8fe5889a --- /dev/null +++ b/packages/agoric-cli/template/contract/deploy-autoswap.js @@ -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 === + // ===================== + } +} diff --git a/packages/agoric-cli/template/contract/deploy.js b/packages/agoric-cli/template/contract/deploy.js index 2448024a7fc..709ff0d74cd 100644 --- a/packages/agoric-cli/template/contract/deploy.js +++ b/packages/agoric-cli/template/contract/deploy.js @@ -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@"; @@ -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(); @@ -71,7 +76,6 @@ export default async function deployContract(homeP, { bundleSource, pathResolve const { terms: { assays }} = await homeP~.zoe~.getInstance(instanceHandle); - // ===================== // === AWAITING TURN === // ===================== @@ -102,7 +106,7 @@ export default async function deployContract(homeP, { bundleSource, pathResolve }, }); - // 6. Liquidities. + // 6. Registration. const payments = [payment0, payment1]; @@ -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), ]); @@ -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)};`);