Merge pull request #1497 from ethereum/resolveContract

build a contract object instead of referencing contract by name
pull/5370/head
yann300 4 years ago committed by GitHub
commit 9269e3ae65
  1. 50
      remix-lib/src/execution/txListener.js

@ -181,7 +181,8 @@ class TxListener {
* @return {String} - contract name * @return {String} - contract name
*/ */
resolvedContract (address) { resolvedContract (address) {
return this._resolvedContracts[address] if (this._resolvedContracts[address]) return this._resolvedContracts[address].name
return null
} }
/** /**
@ -222,54 +223,53 @@ class TxListener {
_resolveTx (tx, receipt, cb) { _resolveTx (tx, receipt, cb) {
const contracts = this._api.contracts() const contracts = this._api.contracts()
if (!contracts) return cb() if (!contracts) return cb()
let contractName
let fun let fun
let contract
if (!tx.to || tx.to === '0x0') { // testrpc returns 0x0 in that case if (!tx.to || tx.to === '0x0') { // testrpc returns 0x0 in that case
// contract creation / resolve using the creation bytes code // contract creation / resolve using the creation bytes code
// if web3: we have to call getTransactionReceipt to get the created address // if web3: we have to call getTransactionReceipt to get the created address
// if VM: created address already included // if VM: created address already included
const code = tx.input const code = tx.input
contractName = this._tryResolveContract(code, contracts, true) contract = this._tryResolveContract(code, contracts, true)
if (contractName) { if (contract) {
let address = receipt.contractAddress let address = receipt.contractAddress
this._resolvedContracts[address] = contractName this._resolvedContracts[address] = contract
fun = this._resolveFunction(contractName, contracts, tx, true) fun = this._resolveFunction(contract, tx, true)
if (this._resolvedTransactions[tx.hash]) { if (this._resolvedTransactions[tx.hash]) {
this._resolvedTransactions[tx.hash].contractAddress = address this._resolvedTransactions[tx.hash].contractAddress = address
} }
return cb(null, {to: null, contractName: contractName, function: fun, creationAddress: address}) return cb(null, {to: null, contractName: contract.name, function: fun, creationAddress: address})
} }
return cb() return cb()
} else { } else {
// first check known contract, resolve against the `runtimeBytecode` if not known // first check known contract, resolve against the `runtimeBytecode` if not known
contractName = this._resolvedContracts[tx.to] contract = this._resolvedContracts[tx.to]
if (!contractName) { if (!contract) {
this.executionContext.web3().eth.getCode(tx.to, (error, code) => { this.executionContext.web3().eth.getCode(tx.to, (error, code) => {
if (error) return cb(error) if (error) return cb(error)
if (code) { if (code) {
const contractName = this._tryResolveContract(code, contracts, false) const contract = this._tryResolveContract(code, contracts, false)
if (contractName) { if (contract) {
this._resolvedContracts[tx.to] = contractName this._resolvedContracts[tx.to] = contract
const fun = this._resolveFunction(contractName, contracts, tx, false) const fun = this._resolveFunction(contract, tx, false)
return cb(null, {to: tx.to, contractName: contractName, function: fun}) return cb(null, {to: tx.to, contractName: contract.name, function: fun})
} }
} }
return cb() return cb()
}) })
return return
} }
if (contractName) { if (contract) {
fun = this._resolveFunction(contractName, contracts, tx, false) fun = this._resolveFunction(contract, tx, false)
return cb(null, {to: tx.to, contractName: contractName, function: fun}) return cb(null, {to: tx.to, contractName: contract.name, function: fun})
} }
return cb() return cb()
} }
} }
_resolveFunction (contractName, compiledContracts, tx, isCtor) { _resolveFunction (contract, tx, isCtor) {
const contract = txHelper.getContract(contractName, compiledContracts)
if (!contract) { if (!contract) {
console.log('txListener: cannot resolve ' + contractName) console.log('txListener: cannot resolve contract - contract is null')
return return
} }
const abi = contract.object.abi const abi = contract.object.abi
@ -280,7 +280,7 @@ class TxListener {
if (methodIdentifiers[fn] === inputData.substring(0, 8)) { if (methodIdentifiers[fn] === inputData.substring(0, 8)) {
const fnabi = txHelper.getFunction(abi, fn) const fnabi = txHelper.getFunction(abi, fn)
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contractName, contractName: contract.name,
to: tx.to, to: tx.to,
fn: fn, fn: fn,
params: this._decodeInputParams(inputData.substring(8), fnabi) params: this._decodeInputParams(inputData.substring(8), fnabi)
@ -294,7 +294,7 @@ class TxListener {
// receive function // receive function
if (!inputData && txHelper.getReceiveInterface(abi)) { if (!inputData && txHelper.getReceiveInterface(abi)) {
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contractName, contractName: contract.name,
to: tx.to, to: tx.to,
fn: '(receive)', fn: '(receive)',
params: null params: null
@ -302,7 +302,7 @@ class TxListener {
} else { } else {
// fallback function // fallback function
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contractName, contractName: contract.name,
to: tx.to, to: tx.to,
fn: '(fallback)', fn: '(fallback)',
params: null params: null
@ -315,7 +315,7 @@ class TxListener {
params = this._decodeInputParams(inputData.substring(bytecode.length), txHelper.getConstructorInterface(abi)) params = this._decodeInputParams(inputData.substring(bytecode.length), txHelper.getConstructorInterface(abi))
} }
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contractName, contractName: contract.name,
to: null, to: null,
fn: '(constructor)', fn: '(constructor)',
params: params params: params
@ -329,7 +329,7 @@ class TxListener {
txHelper.visitContracts(compiledContracts, (contract) => { txHelper.visitContracts(compiledContracts, (contract) => {
const bytes = isCreation ? contract.object.evm.bytecode.object : contract.object.evm.deployedBytecode.object const bytes = isCreation ? contract.object.evm.bytecode.object : contract.object.evm.deployedBytecode.object
if (codeUtil.compareByteCode(codeToResolve, '0x' + bytes)) { if (codeUtil.compareByteCode(codeToResolve, '0x' + bytes)) {
found = contract.name found = contract
return true return true
} }
}) })

Loading…
Cancel
Save