1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | var async_1 = __importDefault(require("async"));
|
7 | var remix_lib_1 = require("remix-lib");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function deployAll(compileResult, web3, withDoubleGas, callback) {
|
16 | var compiledObject = {};
|
17 | var contracts = {};
|
18 | var accounts = [];
|
19 | async_1.default.waterfall([
|
20 | function getAccountList(next) {
|
21 | web3.eth.getAccounts(function (_err, _accounts) {
|
22 | accounts = _accounts;
|
23 | next();
|
24 | });
|
25 | },
|
26 | function getContractData(next) {
|
27 | for (var contractFile in compileResult) {
|
28 | for (var contractName in compileResult[contractFile]) {
|
29 | var contract = compileResult[contractFile][contractName];
|
30 | var className = contractName;
|
31 | var filename = contractFile;
|
32 | var abi = contract.abi;
|
33 | var code = contract.evm.bytecode.object;
|
34 | compiledObject[className] = {};
|
35 | compiledObject[className].abi = abi;
|
36 | compiledObject[className].code = code;
|
37 | compiledObject[className].filename = filename;
|
38 | compiledObject[className].className = className;
|
39 | compiledObject[className].raw = contract;
|
40 | if (contractFile.endsWith('_test.sol')) {
|
41 | compiledObject[className].isTest = true;
|
42 | }
|
43 | }
|
44 | }
|
45 | next();
|
46 | },
|
47 | function determineContractsToDeploy(next) {
|
48 | var contractsToDeploy = ['Assert'];
|
49 | var allContracts = Object.keys(compiledObject);
|
50 | for (var _i = 0, allContracts_1 = allContracts; _i < allContracts_1.length; _i++) {
|
51 | var contractName = allContracts_1[_i];
|
52 | if (contractName === 'Assert') {
|
53 | continue;
|
54 | }
|
55 | if (compiledObject[contractName].isTest) {
|
56 | contractsToDeploy.push(contractName);
|
57 | }
|
58 | }
|
59 | next(null, contractsToDeploy);
|
60 | },
|
61 | function deployContracts(contractsToDeploy, next) {
|
62 | var deployRunner = function (deployObject, contractObject, contractName, filename, callback) {
|
63 | deployObject.estimateGas().then(function (gasValue) {
|
64 | var gasBase = Math.ceil(gasValue * 1.2);
|
65 | var gas = withDoubleGas ? gasBase * 2 : gasBase;
|
66 | deployObject.send({
|
67 | from: accounts[0],
|
68 | gas: gas
|
69 | }).on('receipt', function (receipt) {
|
70 | contractObject.options.address = receipt.contractAddress;
|
71 | contractObject.options.from = accounts[0];
|
72 | contractObject.options.gas = 5000 * 1000;
|
73 | compiledObject[contractName].deployedAddress = receipt.contractAddress;
|
74 | contracts[contractName] = contractObject;
|
75 | contracts[contractName].filename = filename;
|
76 | callback(null, { result: { createdAddress: receipt.contractAddress } });
|
77 | }).on('error', function (err) {
|
78 | console.error(err);
|
79 | callback(err);
|
80 | });
|
81 | });
|
82 | };
|
83 | async_1.default.eachOfLimit(contractsToDeploy, 1, function (contractName, index, nextEach) {
|
84 | var contract = compiledObject[contractName];
|
85 | var encodeDataFinalCallback = function (error, contractDeployData) {
|
86 | if (error)
|
87 | return nextEach(error);
|
88 | try {
|
89 | var contractObject = new web3.eth.Contract(contract.abi);
|
90 | var deployObject = contractObject.deploy({ arguments: [], data: '0x' + contractDeployData.dataHex });
|
91 | deployRunner(deployObject, contractObject, contractName, contract.filename, function (error) { nextEach(error); });
|
92 | }
|
93 | catch (e) {
|
94 | throw e;
|
95 | }
|
96 | };
|
97 | var encodeDataStepCallback = function (msg) { console.dir(msg); };
|
98 | var encodeDataDeployLibraryCallback = function (libData, callback) {
|
99 | var abi = compiledObject[libData.data.contractName].abi;
|
100 | var code = compiledObject[libData.data.contractName].code;
|
101 | var libraryObject = new web3.eth.Contract(abi);
|
102 | var deployObject = libraryObject.deploy({ arguments: [], data: '0x' + code });
|
103 | deployRunner(deployObject, libraryObject, libData.data.contractName, contract.filename, callback);
|
104 | };
|
105 | var funAbi = null;
|
106 | var params = '';
|
107 | remix_lib_1.execution.txFormat.encodeConstructorCallAndDeployLibraries(contractName, contract.raw, compileResult, params, funAbi, encodeDataFinalCallback, encodeDataStepCallback, encodeDataDeployLibraryCallback);
|
108 | }, function (err) {
|
109 | if (err)
|
110 | next(err);
|
111 | next(null, contracts);
|
112 | });
|
113 | }
|
114 | ], callback);
|
115 | }
|
116 | exports.deployAll = deployAll;
|
117 |
|
\ | No newline at end of file |