Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Consensys/abi-decoder
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: UMAprotocol/abi-decoder
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 4 commits
  • 1 file changed
  • 3 contributors

Commits on Jan 28, 2020

  1. Fix method decoding for ABIEncoderV2 incompatibility

    Signed-off-by: Matt Rice <[email protected]>
    mrice32 committed Jan 28, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    2f9ed1a View commit details

Commits on Feb 28, 2024

  1. fix: cleanobject function

    Signed-off-by: Pablo Maldonado <[email protected]>
    md0x committed Feb 28, 2024
    Copy the full SHA
    f346e03 View commit details
  2. refactor: format

    Signed-off-by: Pablo Maldonado <[email protected]>
    md0x committed Feb 28, 2024
    Copy the full SHA
    f678249 View commit details
  3. Merge pull request #1 from UMAprotocol/pablo/fix-clean-object

    fix: cleanobject function
    md0x authored Feb 28, 2024
    Copy the full SHA
    1f18b70 View commit details
Showing with 57 additions and 62 deletions.
  1. +57 −62 index.js
119 changes: 57 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
@@ -15,16 +15,13 @@ function _addABI(abiArray) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
const signature = sha3(
abi.name +
"(" +
abi.inputs
.map(function(input) {
return input.type;
})
.join(",") +
")"
);
let signature;
if (abi.type === "event") {
signature = abiCoder.encodeFunctionSignature(abi);
} else {
signature = abiCoder.encodeEventSignature(abi);
}

if (abi.type === "event") {
state.methodIDs[signature.slice(2)] = abi;
} else {
@@ -44,16 +41,14 @@ function _removeABI(abiArray) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
const signature = sha3(
abi.name +
"(" +
abi.inputs
.map(function(input) {
return input.type;
})
.join(",") +
")"
);
let signature;
if (abi.type === "event") {
signature = abiCoder.encodeFunctionSignature(abi);
} else {
signature = abiCoder.encodeEventSignature(abi);
}


if (abi.type === "event") {
if (state.methodIDs[signature.slice(2)]) {
delete state.methodIDs[signature.slice(2)];
@@ -74,56 +69,56 @@ function _getMethodIDs() {
return state.methodIDs;
}

function _decodeMethod(data) {
const methodID = data.slice(2, 10);
const abiItem = state.methodIDs[methodID];
if (abiItem) {
const params = abiItem.inputs.map(function(item) {
return item.type;
});
let decoded = abiCoder.decodeParameters(params, data.slice(10));
function _isArrayKey(key) {
return /^\d+$/.test(key) || key === "__length__";
}

let retData = {
name: abiItem.name,
params: [],
};
function _isArray(obj) {
if (!Array.isArray(obj)) {
return false;
}

for (let i = 0; i < decoded.__length__; i++) {
let param = decoded[i];
let parsedParam = param;
const isUint = abiItem.inputs[i].type.indexOf("uint") === 0;
const isInt = abiItem.inputs[i].type.indexOf("int") === 0;
const isAddress = abiItem.inputs[i].type.indexOf("address") === 0;
return Object.keys(obj).every(value => {
return _isArrayKey(value);
});
}

if (isUint || isInt) {
const isArray = Array.isArray(param);
function _cleanObject(obj) {
if (_isArray(obj)) {

if (isArray) {
parsedParam = param.map(val => new BN(val).toString());
} else {
parsedParam = new BN(param).toString();
}
// Return an array with all of the elements recursively cleaned.
const cleanedArray = [];
for (const elt of obj) {
cleanedArray.push(_cleanObject(elt));
}
return cleanedArray;
} else if (typeof obj === "object" && obj !== null) {

// Remove all of the array-style keys, and clean the values for the non array-style keys.
const cleanedObject = {};
for (const [key, value] of Object.entries(obj)) {
// Only keep keys that are not array keys.
if (!_isArrayKey(key)) {
cleanedObject[key] = _cleanObject(value);
}
}

// Addresses returned by web3 are randomly cased so we need to standardize and lowercase all
if (isAddress) {
const isArray = Array.isArray(param);

if (isArray) {
parsedParam = param.map(_ => _.toLowerCase());
} else {
parsedParam = param.toLowerCase();
}
}
return cleanedObject;
} else {
// If the object is not an array or an object, we assume it's a primative and return it.
return obj;
}
}

retData.params.push({
name: abiItem.inputs[i].name,
value: parsedParam,
type: abiItem.inputs[i].type,
});
}
function _decodeMethod(data) {
const methodID = data.slice(2, 10);
const abiItem = state.methodIDs[methodID];

return retData;
if (abiItem) {
return {
name: abiItem.name,
params: _cleanObject(abiCoder.decodeParameters(abiItem.inputs, data.slice(10)))
};
}
}