This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
deploy.ts
78 lines (67 loc) · 2.65 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
76
77
78
import * as path from "path";
import { exec } from "child_process";
import ganache, { EthereumProvider, Server, ServerOptions } from "ganache";
import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { compileContracts } from "./contract";
import { ContractInfo } from "./contract/contractInfo";
const counterContract: { address: string } | null = null;
export function getCounterContract(): { address: string } | null {
return counterContract;
}
export async function startLocalEthereum(
opts?: ServerOptions
): Promise<Server<"ethereum">> {
console.log("Starting ganache...");
opts = opts ?? {};
const server = ganache.server({ ...opts, logging: { quiet: true } });
await server.listen(8545);
console.log("Ganache running at http://localhost:8545");
return server;
}
export type TestContract = Contract<typeof ContractInfo.abi>;
export async function deployContract(
provider: EthereumProvider
): Promise<TestContract> {
console.log("Deploying test contract...");
const web3 = new Web3(provider);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const compiledContracts = compileContracts();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const counterContractInfo = compiledContracts["Counter.sol"]["Counter"];
const counterContractDef = new Contract(ContractInfo.abi, web3);
const accounts = await web3.eth.getAccounts();
const counterContract = await counterContractDef
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
.deploy({ data: counterContractInfo.evm.bytecode.object })
.send({ from: accounts[0], gas: "4000000" });
console.log("Contract deployed at", counterContract.options.address);
return counterContract;
}
export enum Snaps {
BASE_SNAP = "base-snap",
KEYS_SNAP = "keys-snap",
PERMISSIONS_SNAP = "permissions-snap",
METHODS_SNAP = "methods-snap",
}
export async function buildSnaps(): Promise<Record<Snaps, string>> {
return {
[Snaps.BASE_SNAP]: await buildSnap(Snaps.BASE_SNAP),
[Snaps.KEYS_SNAP]: await buildSnap(Snaps.KEYS_SNAP),
[Snaps.PERMISSIONS_SNAP]: await buildSnap(Snaps.PERMISSIONS_SNAP),
[Snaps.METHODS_SNAP]: await buildSnap(Snaps.METHODS_SNAP),
};
}
async function buildSnap(snap: Snaps): Promise<string> {
console.log(`Building ${snap}...`);
await new Promise((resolve, reject) => {
exec(`cd ./test/flask/${snap} && npx mm-snap build`, (error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
return `${path.resolve("./test/flask", snap)}`;
}