forked from ChainSafe/dappeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.ts
75 lines (63 loc) · 2.39 KB
/
deploy.ts
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
import fs from 'fs';
import * as http from 'http';
import * as path from 'path';
import ganache, { Provider } from 'ganache';
import handler from 'serve-handler';
import Web3 from 'web3';
import { compileContracts } from './contract';
const counterContract: { address: string } | null = null;
export function getCounterContract(): { address: string } | null {
return counterContract;
}
async function deploy(): Promise<{ address: string }> {
const provider = await waitForGanache();
await startTestServer();
return await deployContract(provider);
}
async function waitForGanache(): Promise<Provider> {
console.log('Starting ganache...');
const server = ganache.server({ seed: 'asd123', logging: { quiet: true } });
await server.listen(8545);
console.log('Ganache running at http://localhost:8545');
return server.provider;
}
async function deployContract(provider: Provider): Promise<{ address: string } | null> {
console.log('Deploying test contract...');
const web3 = new Web3((provider as unknown) as Web3['currentProvider']);
const compiledContracts = compileContracts();
const counterContractInfo = compiledContracts['Counter.sol']['Counter'];
const counterContractDef = new web3.eth.Contract(counterContractInfo.abi);
const accounts = await web3.eth.getAccounts();
const counterContract = await counterContractDef
.deploy({ data: counterContractInfo.evm.bytecode.object })
.send({ from: accounts[0], gas: 4000000 });
console.log('Contract deployed at', counterContract.options.address);
// create file data for dapp
const dataJsPath = path.join(__dirname, 'dapp', 'data.js');
const data = `const ContractInfo = ${JSON.stringify(
{ ...counterContractInfo, ...counterContract.options },
null,
2,
)}`;
await new Promise((resolve) => {
fs.writeFile(dataJsPath, data, resolve);
});
console.log('path:', dataJsPath);
return { ...counterContract, ...counterContract, ...counterContract.options };
}
async function startTestServer(): Promise<void> {
console.log('Starting test server...');
const server = http.createServer((request, response) => {
return handler(request, response, {
public: path.join(__dirname, 'dapp'),
cleanUrls: true,
});
});
await new Promise<void>((resolve) => {
server.listen(8080, () => {
console.log('Server running at http://localhost:8080');
resolve();
});
});
}
export default deploy;