How to read Ethereum contract storage

let contractAddress = '0xf1f5896ace3a78c347eb7eab503450bc93bd0c3b'
for (index = 0; index < 10; index++){
console.log(`[${index}]` +
web3.eth.getStorageAt(contractAddress, index))
}
result:
[0] 0x000000000000000000000000000000000000000000000000000000000000000f
[1] 0x00000000000000000000000059b92d9a0000000000000000000000000000429f
[2] 0x0000000000000000000000000000000074657374310000000000000000000000
[3] 0x7465737431323336000000000000000000000000000000000000000000000000
[4] 0x6c65747320737472696e6720736f6d657468696e67000000000000000000002a
[5] 0x0000000000000000000000000000000000000000000000000000000000000000
[6] 0x0000000000000000000000000000000000000000000000000000000000000000
[7] 0x0000000000000000000000000000000000000000000000000000000000000002
[8] 0x0000000000000000000000000000000000000000000000000000000000000002
[9] 0x0000000000000000000000000000000000000000000000000000000000000000

Index 0 — storeduint1

let contractAddress = '0xf1f5896ace3a78c347eb7eab503450bc93bd0c3b'
let index = 0
console.log(web3.eth.getStorageAt(contractAddress, index))
console.log('DEC:' + web3.toDecimal(web3.eth.getStorageAt(contractAddress, index)))
result:
0x000000000000000000000000000000000000000000000000000000000000000f
DEC:15

constuint

Constants are not stored in a storage. Available only in code.

Index 1 — investmentsLimit, investmentsDeadlineTimeStamp

let index = 1
console.log(web3.eth.getStorageAt(contractAddress, index))
result:
0x00000000000000000000000059b92d9a0000000000000000000000000000429f
DEC: 1505308058 and 17055

index 2 — string1

index = 2
console.log(web3.eth.getStorageAt(contractAddress, index))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, index)))
result:
0x0000000000000000000000000000000074657374310000000000000000000000
ASCII: test1

index 3 — string2

index = 3
console.log(web3.eth.getStorageAt(contractAddress, index))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, index)))
result:
0x7465737431323336000000000000000000000000000000000000000000000000
ASCII: test1236

index 4— string3

index = 4
console.log(web3.eth.getStorageAt(contractAddress, index))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, index)))
result:
0x6c65747320737472696e6720736f6d657468696e67000000000000000000002a
ASCII: lets string something * (42)

index 5 — uints1

index = 5
console.log(web3.eth.getStorageAt(contractAddress, index))
result:
0x0000000000000000000000000000000000000000000000000000000000000000
PROBLEM!!!!
index = '0000000000000000000000000000000000000000000000000000000000000005'
key = '00000000000000000000000xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6'
let newKey = web3.sha3(key + index, {"encoding":"hex"})console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('DEC: ' + web3.toDecimal(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x0000000000000000000000000000000000000000000000000000000000000058
DEC: 88

index6 — structs1

index = "0000000000000000000000000000000000000000000000000000000000000006"
key = "00000000000000000000000xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6"
let newKey = web3.sha3(key + index, {"encoding":"hex"})
console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x6465766963654272616e64000000000000000000000000000000000000000016
ASCII: deviceBrand
function increaseHexByOne(hex) {
let x = new BigNumber(hex)
let sum = x.add(1)
let result = '0x' + sum.toString(16)
return result
}
index = "0000000000000000000000000000000000000000000000000000000000000006"
key = "00000000000000000000000xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6"
let newKey = increaseHexByOne(
web3.sha3(key + index, {"encoding":"hex"}))
console.log(web3.eth.getStorageAt(contractAddress,newKey))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x6465766963655965617200000000000000000000000000000000000000000014
ASCII: deviceYear

index 7— uintarray

index = "7"
console.log(web3.eth.getStorageAt(contractAddress, index))
result:
0x0000000000000000000000000000000000000000000000000000000000000002
index = "0000000000000000000000000000000000000000000000000000000000000007"
let newKey = web3.sha3(index, {"encoding":"hex"})
console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('DEC: ' +
web3.toDecimal(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x0000000000000000000000000000000000000000000000000000000000001f40
DEC: 8000
newKey = increaseHexByOne(web3.sha3(index, {"encoding":"hex"}))console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('DEC: ' +
web3.toDecimal(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x0000000000000000000000000000000000000000000000000000000000002328
DEC: 9000

index 8— deviceDataArray

index = "0000000000000000000000000000000000000000000000000000000000000008"let newKey = web3.sha3(index, {"encoding":"hex"})console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x6465766963654272616e64000000000000000000000000000000000000000016
ASCII: deviceBrand
index = "0000000000000000000000000000000000000000000000000000000000000008"let newKey = increaseHexByOne(web3.sha3(index, {"encoding":"hex"}))console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('ASCII: ' +
web3.toAscii(web3.eth.getStorageAt(contractAddress, newKey)))
result:
0x6465766963655965617200000000000000000000000000000000000000000014
ASCII: deviceYear

Check out our testing ground — Aigang!

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store