This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
deploy.js
170 lines (135 loc) · 5.92 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// @ts-check
// Agoric Dapp api deployment script
import fs from 'fs';
import { E } from '@endo/eventual-send';
import '@agoric/zoe/exported.js';
import installationConstants from '../ui/public/conf/installationConstants.js';
// deploy.js runs in an ephemeral Node.js outside of swingset. The
// spawner runs within ag-solo, so is persistent. Once the deploy.js
// script ends, connections to any of its objects are severed.
/**
* @typedef {object} DeployPowers The special powers that `agoric deploy` gives us
* @property {(path: string) => Promise<{ moduleFormat: string, source: string }>} bundleSource
* @property {(path: string) => string} pathResolve
* @property {(path: string, opts?: any) => Promise<any>} installUnsafePlugin
* @typedef {object} Board
* @property {(id: string) => any} getValue
* @property {(value: any) => string} getId
* @property {(value: any) => boolean} has
* @property {() => [string]} ids
*/
const API_PORT = process.env.API_PORT || '8000';
/**
* @typedef {{ zoe: ZoeService, board: Board, spawner, wallet, uploads, http }} Home
* @param {Promise<Home>} homePromise
* A promise for the references available from REPL home
* @param {DeployPowers} powers
*/
export default async function deployApi(
homePromise,
{ bundleSource, pathResolve },
) {
// Let's wait for the promise to resolve.
const home = await homePromise;
// Unpack the references.
const {
// *** LOCAL REFERENCES ***
// The spawner persistently runs scripts within ag-solo, off-chain.
spawner,
// *** ON-CHAIN REFERENCES ***
// Zoe lives on-chain and is shared by everyone who has access to
// the chain. In this demo, that's just you, but on our testnet,
// everyone has access to the same Zoe.
zoe,
// The http service allows registered handlers that are executed as part of
// the ag-solo web server.
http,
// This is a scratch pad specific to the current ag-solo and inaccessible
// from the chain.
uploads: scratch,
// The board is an on-chain object that is used to make private
// on-chain objects public to everyone else on-chain. These
// objects get assigned a unique string id. Given the id, other
// people can access the object through the board. Ids and values
// have a one-to-one bidirectional mapping. If a value is added a
// second time, the original id is just returned.
board,
} = home;
// To get the backend of our dapp up and running, first we need to
// grab the installation that our contract deploy script put
// in the public board.
const { INSTALLATION_BOARD_ID, CONTRACT_NAME } = installationConstants;
const installation = await E(board).getValue(INSTALLATION_BOARD_ID);
// Second, we can use the installation to create a new instance of
// our contract code on Zoe. A contract instance is a running
// program that can take offers through Zoe. Making an instance will
// give us a `creatorFacet` that will let us make invitations we can
// send to users.
const { creatorFacet, instance, publicFacet } = await E(zoe).startInstance(
installation,
);
console.log('- SUCCESS! contract instance is running on Zoe');
console.log('Retrieving Board IDs for issuers and brands');
const invitationIssuerP = E(zoe).getInvitationIssuer();
const invitationBrandP = E(invitationIssuerP).getBrand();
const tokenIssuer = await E(publicFacet).getTokenIssuer();
// Set up our token to be used by other dapps (like our card store).
E(scratch).set('faucetTokenIssuer', tokenIssuer);
const tokenBrand = await E(tokenIssuer).getBrand();
const invitationIssuer = await invitationIssuerP;
const [INSTANCE_BOARD_ID, TOKEN_BRAND_BOARD_ID, TOKEN_ISSUER_BOARD_ID] =
await Promise.all([
E(board).getId(instance),
E(board).getId(tokenBrand),
E(board).getId(tokenIssuer),
]);
console.log(`-- Contract Name: ${CONTRACT_NAME}`);
console.log(`-- INSTANCE_BOARD_ID: ${INSTANCE_BOARD_ID}`);
console.log(`-- TOKEN_ISSUER_BOARD_ID: ${TOKEN_ISSUER_BOARD_ID}`);
console.log(`-- TOKEN_BRAND_BOARD_ID: ${TOKEN_BRAND_BOARD_ID}`);
// We want the handler to run persistently. (Scripts such as this
// deploy.js script are ephemeral and all connections to objects
// within this script are severed when the script is done running.)
const installURLHandler = async () => {
// To run the URL handler persistently, we must use the spawner to run
// the code on our ag-solo even after the deploy script exits.
// Bundle up the handler code
const bundle = await bundleSource(pathResolve('./src/handler.js'));
// Install it on the spawner
const handlerInstall = E(spawner).install(bundle);
// Spawn the installed code to create an URL handler.
const handler = E(handlerInstall).spawn({
creatorFacet,
board,
http,
invitationIssuer,
});
// Have our ag-solo wait on ws://localhost:8000/api/fungible-faucet for
// websocket connections.
await E(http).registerURLHandler(handler, '/api/fungible-faucet');
};
await installURLHandler();
const invitationBrand = await invitationBrandP;
const INVITE_BRAND_BOARD_ID = await E(board).getId(invitationBrand);
const API_URL = process.env.API_URL || `http://127.0.0.1:${API_PORT || 8000}`;
// Re-save the constants somewhere where the UI and api can find it.
const dappConstants = {
INSTANCE_BOARD_ID,
INSTALLATION_BOARD_ID,
INVITE_BRAND_BOARD_ID,
// BRIDGE_URL: 'agoric-lookup:https://local.agoric.com?append=/bridge',
brandBoardIds: {
Token: TOKEN_BRAND_BOARD_ID,
},
issuerBoardIds: { Token: TOKEN_ISSUER_BOARD_ID },
BRIDGE_URL: 'http://127.0.0.1:8000',
API_URL,
};
const defaultsFile = pathResolve(`../ui/public/conf/defaults.js`);
console.log('writing', defaultsFile);
const defaultsContents = `\
// GENERATED FROM ${pathResolve('./deploy.js')}
export default ${JSON.stringify(dappConstants, undefined, 2)};
`;
await fs.promises.writeFile(defaultsFile, defaultsContents);
}