refactor calldata decoding & fix nested array decoding

decodeCallData
yann300 4 years ago
parent 294280c178
commit fdf2cdf127
  1. 54
      libs/remix-debug/src/solidity-decoder/types/RefType.ts

@ -52,27 +52,45 @@ export class RefType {
offset = parseInt(offset, 16)
return this.decodeFromMemoryInternal(offset, memory, cursor)
} else if (this.isInCallData()) {
calldata = calldata.length > 0 ? calldata[0] : '0x'
const ethersAbi = new ethers.utils.Interface(variableDetails.abi)
const fnSign = calldata.substr(0, 10)
const decodedData = ethersAbi.decodeFunctionData(ethersAbi.getFunction(fnSign), calldata)
let decodedValue = decodedData[variableDetails.name]
const isArray = Array.isArray(decodedValue)
if (isArray) {
decodedValue = decodedValue.map((el) => {
return {
value: el.toString(),
type: this.underlyingType.typeName
}
})
}
return this._decodeFromCallData(variableDetails, calldata)
} else {
return { error: '<decoding failed - no decoder for ' + this.location + '>', type: this.typeName }
}
}
_decodeFromCallData (variableDetails, calldata) {
calldata = calldata.length > 0 ? calldata[0] : '0x'
const ethersAbi = new ethers.utils.Interface(variableDetails.abi)
const fnSign = calldata.substr(0, 10)
const decodedData = ethersAbi.decodeFunctionData(ethersAbi.getFunction(fnSign), calldata)
let decodedValue = decodedData[variableDetails.name]
const isArray = Array.isArray(decodedValue)
if (isArray) {
return this._decodeCallDataArray(decodedValue, this)
}
return {
length: isArray ? '0x' + decodedValue.length.toString(16) : undefined,
value: decodedValue,
type: this.typeName
}
}
_decodeCallDataArray (value, type) {
const isArray = Array.isArray(value)
if (isArray) {
value = value.map((el) => {
return this._decodeCallDataArray(el, this.underlyingType)
})
return {
length: Array.isArray(decodedValue) ? '0x' + decodedValue.length.toString(16) : undefined,
value: decodedValue,
type: this.typeName
length: value.length.toString(16),
value: value,
type: type.typeName
}
} else {
return { error: '<decoding failed - no decoder for ' + this.location + '>', type: this.typeName }
return {
value: value.toString(),
type: type.underlyingType.typeName
}
}
}

Loading…
Cancel
Save