From 12b370f211eef14474dcc402b2948193864f483a Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 26 Feb 2018 15:14:07 +0100 Subject: [PATCH] fix deploy and link libraries --- remix-lib/package.json | 1 + remix-lib/src/execution/txFormat.js | 48 ++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/remix-lib/package.json b/remix-lib/package.json index 263a38ea42..938e28cce3 100644 --- a/remix-lib/package.json +++ b/remix-lib/package.json @@ -14,6 +14,7 @@ ], "main": "./index.js", "dependencies": { + "async": "^2.1.2", "babel-preset-es2015": "^6.24.0", "babel-plugin-transform-object-assign": "^6.22.0", "babel-eslint": "^7.1.1", diff --git a/remix-lib/src/execution/txFormat.js b/remix-lib/src/execution/txFormat.js index 8959de4215..d32e5ad3fc 100644 --- a/remix-lib/src/execution/txFormat.js +++ b/remix-lib/src/execution/txFormat.js @@ -2,6 +2,7 @@ var ethJSABI = require('ethereumjs-abi') var helper = require('./txHelper') var executionContext = require('./execution-context') +var asyncJS = require('async') module.exports = { @@ -99,10 +100,37 @@ module.exports = { atAddress: function () {}, - linkBytecode: function (contract, contracts, udapp, callback, callbackStep) { - if (contract.evm.bytecode.object.indexOf('_') < 0) { - return callback(null, contract.evm.bytecode.object) - } + linkBytecodeStandard: function (contract, contracts, udapp, callback, callbackStep) { + asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => { + asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => { + var library = contracts[file][libName] + if (library) { + this.deployLibrary(file + ':' + libName, libName, library, contracts, udapp, (error, address) => { + if (error) { + return cbLibDeployed(error) + } + var hexAddress = address.toString('hex') + if (hexAddress.slice(0, 2) === '0x') { + hexAddress = hexAddress.slice(2) + } + contract.evm.bytecode.object = this.linkLibraryStandard(libName, hexAddress, contract) + cbLibDeployed() + }, callbackStep) + } else { + cbLibDeployed('Cannot find compilation data of library ' + libName) + } + }, (error) => { + cbFile(error) + }) + }, (error) => { + if (error) { + callbackStep(error) + } + callback(error, contract.evm.bytecode.object) + }) + }, + + linkBytecodeLegacy: function (contract, contracts, udapp, callback, callbackStep) { var libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/) if (!libraryRefMatch) { return callback('Invalid bytecode format.') @@ -129,12 +157,22 @@ module.exports = { if (hexAddress.slice(0, 2) === '0x') { hexAddress = hexAddress.slice(2) } - contract.evm.bytecode.object = this.linkLibraryStandard(libraryShortName, hexAddress, contract) contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object) this.linkBytecode(contract, contracts, udapp, callback, callbackStep) }, callbackStep) }, + linkBytecode: function (contract, contracts, udapp, callback, callbackStep) { + if (contract.evm.bytecode.object.indexOf('_') < 0) { + return callback(null, contract.evm.bytecode.object) + } + if (contract.evm.bytecode.linkReferences && Object.keys(contract.evm.bytecode.linkReferences).length) { + this.linkBytecodeStandard(contract, contracts, udapp, callback, callbackStep) + } else { + this.linkBytecodeLegacy(contract, contracts, udapp, callback, callbackStep) + } + }, + deployLibrary: function (libraryName, libraryShortName, library, contracts, udapp, callback, callbackStep) { var address = library.address if (address) {