-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.ts
201 lines (166 loc) · 5.55 KB
/
index.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {
TASK_COMPILE_GET_SOURCE_PATHS,
TASK_TEST_SETUP_TEST_ENVIRONMENT
} from "@nomiclabs/buidler/builtin-tasks/task-names";
import {
extendEnvironment,
internalTask,
usePlugin
} from "@nomiclabs/buidler/config";
import { glob } from "@nomiclabs/buidler/internal/util/glob";
import {
BUIDLEREVM_NETWORK_NAME,
BuidlerPluginError,
lazyFunction,
lazyObject
} from "@nomiclabs/buidler/plugins";
import { BuidlerNetworkConfig } from "@nomiclabs/buidler/types";
import { join } from "path";
import { TruffleEnvironmentArtifacts } from "./artifacts";
import {
getTruffleFixtureFunction,
hasMigrations,
hasTruffleFixture
} from "./fixture";
import { LazyTruffleContractProvisioner } from "./provisioner";
import { RUN_TRUFFLE_FIXTURE_TASK } from "./task-names";
// See buidler-core's CONTRIBUTING.md
let originalFormatter: any;
let originalGetGasEstimate: any;
let originalPrepareCall: any;
export default function() {
usePlugin("@nomiclabs/buidler-web3");
let accounts: string[] | undefined;
extendEnvironment(env => {
env.artifacts = lazyObject(() => {
const networkConfig = env.network.config;
const provisioner = new LazyTruffleContractProvisioner(
env.web3,
networkConfig
);
const artifacts = new TruffleEnvironmentArtifacts(
env.config.paths.artifacts,
provisioner
);
const execute = require("@nomiclabs/truffle-contract/lib/execute");
let noDefaultAccounts = false;
let defaultAccount: string | undefined = networkConfig.from;
async function addFromIfNeededAndAvailable(params: any) {
if (noDefaultAccounts) {
return;
}
if (params.from === undefined) {
if (defaultAccount === undefined) {
accounts = await env.web3.eth.getAccounts();
if (accounts!.length === 0) {
noDefaultAccounts = true;
return;
}
defaultAccount = accounts![0];
}
params.from = defaultAccount;
}
}
const web3Path = require.resolve("web3");
const formattersPath = require.resolve(
"web3-core-helpers/src/formatters",
{
paths: [web3Path]
}
);
const formatters = require(formattersPath);
if (originalFormatter === undefined) {
originalFormatter = formatters.inputTransactionFormatter;
}
formatters.inputTransactionFormatter = function(options: any) {
if (options.from === undefined) {
throw new BuidlerPluginError(
"There's no account available in the selected network."
);
}
return originalFormatter(options);
};
if (originalGetGasEstimate === undefined) {
originalGetGasEstimate = execute.getGasEstimate;
}
execute.getGasEstimate = async function(params: any, ...others: any[]) {
await addFromIfNeededAndAvailable(params);
return originalGetGasEstimate.call(this, params, ...others);
};
if (originalPrepareCall === undefined) {
originalPrepareCall = execute.prepareCall;
}
execute.prepareCall = async function(...args: any[]) {
const ret = await originalPrepareCall.apply(this, args);
await addFromIfNeededAndAvailable(ret.params);
return ret;
};
return artifacts;
});
env.assert = lazyFunction(() => require("chai").assert);
env.expect = lazyFunction(() => require("chai").expect);
env.contract = (
description: string,
definition: (accounts: string[]) => any
) => {
if (env.network.name === BUIDLEREVM_NETWORK_NAME) {
if (accounts === undefined) {
const {
privateToAddress,
toChecksumAddress,
bufferToHex
} = require("ethereumjs-util");
const netConfig = env.network.config as Required<
BuidlerNetworkConfig
>;
accounts = netConfig.accounts.map(acc =>
toChecksumAddress(bufferToHex(privateToAddress(acc.privateKey)))
);
}
} else if (accounts === undefined) {
throw new BuidlerPluginError(
`To run your tests that use Truffle's "contract()" function with the network "${env.network.name}", you need to use Buidler's CLI`
);
}
describe(`Contract: ${description}`, () => {
before("Running truffle fixture if available", async function() {
await env.run(RUN_TRUFFLE_FIXTURE_TASK);
});
definition(accounts!);
});
};
});
internalTask(
TASK_TEST_SETUP_TEST_ENVIRONMENT,
async (_, { web3, network }) => {
if (network.name !== BUIDLEREVM_NETWORK_NAME) {
accounts = await web3.eth.getAccounts();
}
}
);
internalTask(
TASK_COMPILE_GET_SOURCE_PATHS,
async (_, { config }, runSuper) => {
const sources = await runSuper();
const testSources = await glob(join(config.paths.tests, "**", "*.sol"));
return [...sources, ...testSources];
}
);
let wasWarningShown = false;
internalTask(RUN_TRUFFLE_FIXTURE_TASK, async (_, env) => {
const paths = env.config.paths;
const hasFixture = await hasTruffleFixture(paths);
if (!wasWarningShown) {
if ((await hasMigrations(paths)) && !hasFixture) {
console.warn(
"Your project has Truffle migrations, which have to be turn into a fixture to run your tests with Buidler"
);
wasWarningShown = true;
}
}
if (hasFixture) {
const fixture = await getTruffleFixtureFunction(paths);
await fixture(env);
}
});
}