Commit 177d87b5 authored by yann300's avatar yann300

return type directly instead of decode info

parent d53d2ad3
'use strict' 'use strict'
var AddressType = require('./types/Address')
var ArrayType = require('./types/ArrayType')
var BoolType = require('./types/Bool')
var BytesType = require('./types/DynamicByteArray')
var BytesXType = require('./types/FixedByteArray')
var EnumType = require('./types/Enum')
var StringType = require('./types/StringType')
var StructType = require('./types/Struct')
var IntType = require('./types/Int')
var UintType = require('./types/Uint')
/** /**
* Uint decode the given @arg type * Uint decode the given @arg type
* *
...@@ -7,11 +19,12 @@ ...@@ -7,11 +19,12 @@
*/ */
function Uint (type) { function Uint (type) {
type === 'uint' ? 'uint256' : type type === 'uint' ? 'uint256' : type
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: parseInt(type.replace('uint', '')) / 8, storageBytes: parseInt(type.replace('uint', '')) / 8,
typeName: 'uint' typeName: 'uint'
} }
return new UintType(decodeInfo)
} }
/** /**
...@@ -22,11 +35,12 @@ function Uint (type) { ...@@ -22,11 +35,12 @@ function Uint (type) {
*/ */
function Int (type) { function Int (type) {
type === 'int' ? 'int256' : type type === 'int' ? 'int256' : type
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: parseInt(type.replace('int', '')) / 8, storageBytes: parseInt(type.replace('int', '')) / 8,
typeName: 'int' typeName: 'int'
} }
return new IntType(decodeInfo)
} }
/** /**
...@@ -36,11 +50,12 @@ function Int (type) { ...@@ -36,11 +50,12 @@ function Int (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function Address (type) { function Address (type) {
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: 20, storageBytes: 20,
typeName: 'address' typeName: 'address'
} }
return new AddressType(decodeInfo)
} }
/** /**
...@@ -50,11 +65,12 @@ function Address (type) { ...@@ -50,11 +65,12 @@ function Address (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function Bool (type) { function Bool (type) {
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: 1, storageBytes: 1,
typeName: 'bool' typeName: 'bool'
} }
return new BoolType(decodeInfo)
} }
/** /**
...@@ -64,11 +80,12 @@ function Bool (type) { ...@@ -64,11 +80,12 @@ function Bool (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function DynamicByteArray (type) { function DynamicByteArray (type) {
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: 32, storageBytes: 32,
typeName: 'bytes' typeName: 'bytes'
} }
return new BytesType(decodeInfo)
} }
/** /**
...@@ -78,11 +95,12 @@ function DynamicByteArray (type) { ...@@ -78,11 +95,12 @@ function DynamicByteArray (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function FixedByteArray (type) { function FixedByteArray (type) {
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: parseInt(type.replace('bytes', '')), storageBytes: parseInt(type.replace('bytes', '')),
typeName: 'bytesX' typeName: 'bytesX'
} }
return new BytesXType(decodeInfo)
} }
/** /**
...@@ -91,12 +109,13 @@ function FixedByteArray (type) { ...@@ -91,12 +109,13 @@ function FixedByteArray (type) {
* @param {String} type - type given by the AST (e.g string storage ref) * @param {String} type - type given by the AST (e.g string storage ref)
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function StringType (type) { function String (type) {
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: 32, storageBytes: 32,
typeName: 'string' typeName: 'string'
} }
return new StringType(decodeInfo)
} }
/** /**
...@@ -105,7 +124,7 @@ function StringType (type) { ...@@ -105,7 +124,7 @@ function StringType (type) {
* @param {String} type - type given by the AST (e.g int256[] storage ref, int256[] storage ref[] storage ref) * @param {String} type - type given by the AST (e.g int256[] storage ref, int256[] storage ref[] storage ref)
* @return {Object} returns decoded info about the current type: { storageBytes, typeName, arraySize, subArray} * @return {Object} returns decoded info about the current type: { storageBytes, typeName, arraySize, subArray}
*/ */
function ArrayType (type, stateDefinitions) { function Array (type, stateDefinitions) {
var arraySize var arraySize
var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/) var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/)
...@@ -116,7 +135,7 @@ function ArrayType (type, stateDefinitions) { ...@@ -116,7 +135,7 @@ function ArrayType (type, stateDefinitions) {
arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2]) arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2])
var underlyingType = decode(match[1], stateDefinitions) var underlyingType = parseType(match[1], stateDefinitions)
if (underlyingType === null) { if (underlyingType === null) {
console.log('unable to parse type ' + type) console.log('unable to parse type ' + type)
return null return null
...@@ -134,13 +153,14 @@ function ArrayType (type, stateDefinitions) { ...@@ -134,13 +153,14 @@ function ArrayType (type, stateDefinitions) {
} }
} }
return { var decodeInfo = {
storageSlots: storageSlots, storageSlots: storageSlots,
storageBytes: 32, storageBytes: 32,
typeName: 'array', typeName: 'array',
arraySize: arraySize, arraySize: arraySize,
underlyingType: underlyingType underlyingType: underlyingType
} }
return new ArrayType(decodeInfo)
} }
/** /**
...@@ -161,12 +181,13 @@ function Enum (type, stateDefinitions) { ...@@ -161,12 +181,13 @@ function Enum (type, stateDefinitions) {
length = length / 256 length = length / 256
storageBytes++ storageBytes++
} }
return { var decodeInfo = {
storageSlots: 1, storageSlots: 1,
storageBytes: storageBytes, storageBytes: storageBytes,
typeName: 'enum', typeName: 'enum',
enum: enumDef enum: enumDef
} }
return new EnumType(decodeInfo)
} }
/** /**
...@@ -182,12 +203,13 @@ function Struct (type, stateDefinitions) { ...@@ -182,12 +203,13 @@ function Struct (type, stateDefinitions) {
} }
var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition
if (!memberDetails) return null if (!memberDetails) return null
return { var decodeInfo = {
storageSlots: Math.ceil(memberDetails.storageBytes / 32), storageSlots: Math.ceil(memberDetails.storageBytes / 32),
storageBytes: 32, storageBytes: 32,
typeName: 'struct', typeName: 'struct',
members: memberDetails.members members: memberDetails.members
} }
return new StructType(decodeInfo)
} }
/** /**
...@@ -222,7 +244,7 @@ function getStructMembers (typeName, stateDefinitions) { ...@@ -222,7 +244,7 @@ function getStructMembers (typeName, stateDefinitions) {
if (dec.name === 'StructDefinition' && typeName === dec.attributes.name) { if (dec.name === 'StructDefinition' && typeName === dec.attributes.name) {
for (var i in dec.children) { for (var i in dec.children) {
var member = dec.children[i] var member = dec.children[i]
var decoded = decode(member.attributes.type, stateDefinitions) var decoded = parseType(member.attributes.type, stateDefinitions)
if (!decoded) { if (!decoded) {
console.log('unable to retrieve decode info of ' + member.attributes.type) console.log('unable to retrieve decode info of ' + member.attributes.type)
return null return null
...@@ -266,12 +288,12 @@ function typeClass (fullType) { ...@@ -266,12 +288,12 @@ function typeClass (fullType) {
function parseType (type, stateDefinitions) { function parseType (type, stateDefinitions) {
var decodeInfos = { var decodeInfos = {
'address': Address, 'address': Address,
'array': ArrayType, 'array': Array,
'bool': Bool, 'bool': Bool,
'bytes': DynamicByteArray, 'bytes': DynamicByteArray,
'bytesX': FixedByteArray, 'bytesX': FixedByteArray,
'enum': Enum, 'enum': Enum,
'string': StringType, 'string': String,
'struct': Struct, 'struct': Struct,
'int': Int, 'int': Int,
'uint': Uint 'uint': Uint
...@@ -285,15 +307,15 @@ function parseType (type, stateDefinitions) { ...@@ -285,15 +307,15 @@ function parseType (type, stateDefinitions) {
} }
module.exports = { module.exports = {
decode: parseType, parseType: parseType,
Uint: Uint, Uint: Uint,
Address: Address, Address: Address,
Bool: Bool, Bool: Bool,
DynamicByteArray: DynamicByteArray, DynamicByteArray: DynamicByteArray,
FixedByteArray: FixedByteArray, FixedByteArray: FixedByteArray,
Int: Int, Int: Int,
StringType: StringType, String: String,
ArrayType: ArrayType, Array: Array,
Enum: Enum, Enum: Enum,
Struct: Struct Struct: Struct
} }
...@@ -41,8 +41,7 @@ function extractStateVariables (contractName, sourcesList) { ...@@ -41,8 +41,7 @@ function extractStateVariables (contractName, sourcesList) {
for (var k in stateDefinitions) { for (var k in stateDefinitions) {
var variable = stateDefinitions[k] var variable = stateDefinitions[k]
if (variable.name === 'VariableDeclaration') { if (variable.name === 'VariableDeclaration') {
var decoded = decodeInfo.parseType(variable.attributes.type, stateDefinitions) var type = decodeInfo.parseType(variable.attributes.type, stateDefinitions)
var type = new types[decoded.typeName](decoded)
if (location.offset + type.storageBytes > 32) { if (location.offset + type.storageBytes > 32) {
location.slot++ location.slot++
location.offset = 0 location.offset = 0
...@@ -85,15 +84,3 @@ module.exports = { ...@@ -85,15 +84,3 @@ module.exports = {
decodeState: decodeState decodeState: decodeState
} }
var types = {
'address': require('./types/Address'),
'array': require('./types/ArrayType'),
'bool': require('./types/Bool'),
'bytes': require('./types/DynamicByteArray'),
'bytesX': require('./types/FixedByteArray'),
'enum': require('./types/Enum'),
'string': require('./types/StringType'),
'struct': require('./types/Struct'),
'int': require('./types/Int'),
'uint': require('./types/Uint')
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment