UNPKG

6.04 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6var async_1 = __importDefault(require("async"));
7var remix_lib_1 = require("remix-lib");
8/**
9 * @dev Deploy all contracts from compilation result
10 * @param compileResult compilation result
11 * @param web3 web3 object
12 * @param withDoubleGas If true, try deployment with gas double of estimation (used for Out-of-gas error only)
13 * @param callback Callback
14 */
15function 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 } }); // TODO this will only work with JavaScriptV VM
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; // no need to set the abi for encoding the constructor
106 var params = ''; // we suppose that the test contract does not have any param in the constructor
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}
116exports.deployAll = deployAll;
117//# sourceMappingURL=deployer.js.map
\No newline at end of file