diff --git a/src/solidity/types/Int.js b/src/solidity/types/Int.js index 25df4c7f33..7604532466 100644 --- a/src/solidity/types/Int.js +++ b/src/solidity/types/Int.js @@ -1,4 +1,7 @@ 'use strict' +var util = require('./util') +var BN = require('ethereumjs-util').BN +var ethutil = require('ethereumjs-util') function Int (storageBytes) { this.storageSlots = 1 @@ -7,7 +10,22 @@ function Int (storageBytes) { } Int.prototype.decodeFromStorage = function (location, storageContent) { - return '' + var slot = ethutil.bufferToHex(ethutil.setLengthLeft(location.slot, 32)) + if (!storageContent[slot]) { + return '0' + } + var value = util.extractValue(storageContent[slot], this.storageBytes, location) + var bigNumber = new BN(value.replace('0x', ''), 16) + if (isNegative(bigNumber, this.storageBytes)) { + return ethutil.fromSigned(ethutil.toUnsigned(bigNumber)).toString(10) + } else { + return bigNumber.toString(10) + } } module.exports = Int + +function isNegative (value, storageBytes) { + var binary = value.toString(2) + return binary.length < storageBytes ? false : binary[0] === '1' +} diff --git a/src/solidity/types/Uint.js b/src/solidity/types/Uint.js index b80d336791..1b59b693f3 100644 --- a/src/solidity/types/Uint.js +++ b/src/solidity/types/Uint.js @@ -1,4 +1,7 @@ 'use strict' +var util = require('./util') +var ethutil = require('ethereumjs-util') +var BN = require('ethereumjs-util').BN function Uint (storageBytes) { this.storageSlots = 1 @@ -7,7 +10,13 @@ function Uint (storageBytes) { } Uint.prototype.decodeFromStorage = function (location, storageContent) { - return '' + var slot = ethutil.bufferToHex(ethutil.setLengthLeft(location.slot, 32)) + if (!storageContent[slot]) { + return '0' + } + var value = util.extractValue(storageContent[slot], this.storageBytes, location) + var bigNumber = new BN(value.replace('0x', ''), 16) + return bigNumber.toString(10) } module.exports = Uint diff --git a/src/solidity/types/util.js b/src/solidity/types/util.js new file mode 100644 index 0000000000..e4070cc9d3 --- /dev/null +++ b/src/solidity/types/util.js @@ -0,0 +1,13 @@ +module.exports = { + extractValue: function (slotValue, storageBytes, location) { + slotValue = slotValue.replace('0x', '') + var offset = slotValue.length - 2 * location.offset - storageBytes / 4 + if (offset >= 0) { + return '0x' + slotValue.substr(offset, storageBytes / 4) + } else if (offset + storageBytes > 0) { + return '0x' + slotValue.substr(0, storageBytes / 4 + offset) + } else { + return '0x0' + } + } +}