This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
helpers.js
185 lines (155 loc) · 4.52 KB
/
helpers.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import debugModule from "debug";
const debug = debugModule("test:helpers");
import path from "path";
import fs from "fs-extra";
import Contracts from "truffle-workflow-compile";
import Debug from "truffle-debug-utils";
import Artifactor from "truffle-artifactor";
import Web3 from "web3";
import Migrate from "truffle-migrate";
import Box from "truffle-box";
import Resolver from "truffle-resolver";
export async function prepareContracts(provider, sources = {}, migrations) {
let config = await createSandbox();
let accounts = await getAccounts(provider);
config.networks["debugger"] = {
provider: provider,
network_id: "*",
from: accounts[0]
};
config.network = "debugger";
config.compilers = {
solc: {
version: "0.5.10",
settings: {
optimizer: { enabled: false, runs: 200 },
evmVersion: "constantinople"
}
}
};
await addContracts(config, sources);
let { contracts, files } = await compile(config);
let contractNames = Object.keys(contracts);
if (!migrations) {
migrations = await defaultMigrations(contractNames);
}
await addMigrations(config, migrations);
await migrate(config);
let artifacts = await gatherArtifacts(config);
debug("artifacts: %o", artifacts.map(a => a.contractName));
let abstractions = {};
contractNames.forEach(name => {
abstractions[name] = config.resolver.require(name);
});
return {
files,
abstractions,
artifacts,
config
};
}
export function getAccounts(provider) {
let web3 = new Web3(provider);
return new Promise(function(accept, reject) {
web3.eth.getAccounts(function(err, accounts) {
if (err) return reject(err);
accept(accounts);
});
});
}
export async function createSandbox() {
let config = await new Promise(function(accept, reject) {
Box.sandbox(
{
unsafeCleanup: true,
setGracefulCleanup: true,
name: "default"
},
function(err, result) {
if (err) return reject(err);
result.resolver = new Resolver(result);
result.artifactor = new Artifactor(result.contracts_build_directory);
result.networks = {};
accept(result);
}
);
});
await fs.remove(path.join(config.contracts_directory, "MetaCoin.sol"));
await fs.remove(path.join(config.contracts_directory, "ConvertLib.sol"));
await fs.remove(
path.join(config.migrations_directory, "2_deploy_contracts.js")
);
return config;
}
export async function addContracts(config, sources = {}) {
let promises = [];
for (let filename of Object.keys(sources)) {
let source = sources[filename];
promises.push(
fs.outputFile(path.join(config.contracts_directory, filename), source)
);
}
return await Promise.all(promises);
}
export async function addMigrations(config, migrations = {}) {
let promises = [];
for (let filename of Object.keys(migrations)) {
let source = migrations[filename];
promises.push(
fs.outputFile(path.join(config.migrations_directory, filename), source)
);
}
return await Promise.all(promises);
}
export async function defaultMigrations(contractNames) {
contractNames = contractNames.filter(name => name !== "Migrations");
let migrations = {};
contractNames.forEach((contractName, i) => {
let index = i + 2; // start at 2 cause Migrations migration
let filename = `${index}_migrate_${contractName}.js`;
let source = `
var ${contractName} = artifacts.require("${contractName}");
module.exports = function(deployer) {
deployer.deploy(${contractName});
};
`;
migrations[filename] = source;
});
return migrations;
}
export async function compile(config) {
return new Promise(function(accept, reject) {
Contracts.compile(
config.with({
all: true,
quiet: true
}),
function(err, result) {
if (err) return reject(err);
const { contracts, outputs } = result;
debug("result %O", result);
return accept({ contracts, files: outputs.solc });
}
);
});
}
export async function migrate(config) {
return new Promise(function(accept, reject) {
Migrate.run(
config.with({
quiet: true
}),
function(err, contracts) {
if (err) return reject(err);
accept(contracts);
}
);
});
}
export async function gatherArtifacts(config) {
return Debug.gatherArtifacts(config);
}
export function lineOf(searchString, source) {
const lines = source.split("\n");
return lines.findIndex(line => line.includes(searchString));
}