change types ctor

pull/7/head
yann300 8 years ago
parent cf10206c9f
commit 5a833953ff
  1. 99
      src/solidity/decodeInfo.js
  2. 7
      src/solidity/types/Address.js
  3. 21
      src/solidity/types/ArrayType.js
  4. 7
      src/solidity/types/Bool.js
  5. 7
      src/solidity/types/DynamicByteArray.js
  6. 14
      src/solidity/types/Enum.js
  7. 7
      src/solidity/types/FixedByteArray.js
  8. 7
      src/solidity/types/Int.js
  9. 7
      src/solidity/types/StringType.js
  10. 9
      src/solidity/types/Struct.js
  11. 7
      src/solidity/types/Uint.js
  12. 8
      src/solidity/types/baseType.js

@ -19,12 +19,8 @@ var UintType = require('./types/Uint')
*/
function Uint (type) {
type === 'uint' ? 'uint256' : type
var decodeInfo = {
storageSlots: 1,
storageBytes: parseInt(type.replace('uint', '')) / 8,
typeName: 'uint'
}
return new UintType(decodeInfo)
var storageBytes = parseInt(type.replace('uint', '')) / 8
return new UintType(storageBytes)
}
/**
@ -35,12 +31,8 @@ function Uint (type) {
*/
function Int (type) {
type === 'int' ? 'int256' : type
var decodeInfo = {
storageSlots: 1,
storageBytes: parseInt(type.replace('int', '')) / 8,
typeName: 'int'
}
return new IntType(decodeInfo)
var storageBytes = parseInt(type.replace('int', '')) / 8
return new IntType(storageBytes)
}
/**
@ -50,12 +42,7 @@ function Int (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/
function Address (type) {
var decodeInfo = {
storageSlots: 1,
storageBytes: 20,
typeName: 'address'
}
return new AddressType(decodeInfo)
return new AddressType()
}
/**
@ -65,12 +52,7 @@ function Address (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/
function Bool (type) {
var decodeInfo = {
storageSlots: 1,
storageBytes: 1,
typeName: 'bool'
}
return new BoolType(decodeInfo)
return new BoolType()
}
/**
@ -80,12 +62,7 @@ function Bool (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/
function DynamicByteArray (type) {
var decodeInfo = {
storageSlots: 1,
storageBytes: 32,
typeName: 'bytes'
}
return new BytesType(decodeInfo)
return new BytesType()
}
/**
@ -95,12 +72,8 @@ function DynamicByteArray (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/
function FixedByteArray (type) {
var decodeInfo = {
storageSlots: 1,
storageBytes: parseInt(type.replace('bytes', '')),
typeName: 'bytesX'
}
return new BytesXType(decodeInfo)
var storageBytes = parseInt(type.replace('bytes', ''))
return new BytesXType(storageBytes)
}
/**
@ -110,12 +83,7 @@ function FixedByteArray (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/
function String (type) {
var decodeInfo = {
storageSlots: 1,
storageBytes: 32,
typeName: 'string'
}
return new StringType(decodeInfo)
return new StringType()
}
/**
@ -126,41 +94,18 @@ function String (type) {
*/
function Array (type, stateDefinitions) {
var arraySize
var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/)
if (!match || match.length < 3) {
console.log('unable to parse type ' + type)
return null
}
arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2])
var underlyingType = parseType(match[1], stateDefinitions)
if (underlyingType === null) {
console.log('unable to parse type ' + type)
return null
}
var storageSlots
if (arraySize === 'dynamic') {
storageSlots = 1
} else {
if (underlyingType.storageBytes < 32) {
var itemPerSlot = Math.floor(32 / underlyingType.storageBytes)
storageSlots = Math.ceil(arraySize / itemPerSlot)
} else {
storageSlots = arraySize * underlyingType.storageSlots
}
}
var decodeInfo = {
storageSlots: storageSlots,
storageBytes: 32,
typeName: 'array',
arraySize: arraySize,
underlyingType: underlyingType
}
return new ArrayType(decodeInfo)
return new ArrayType(underlyingType, arraySize)
}
/**
@ -175,19 +120,7 @@ function Enum (type, stateDefinitions) {
console.log('unable to retrieve decode info of ' + type)
return null
}
var length = enumDef.children.length
var storageBytes = 0
while (length > 1) {
length = length / 256
storageBytes++
}
var decodeInfo = {
storageSlots: 1,
storageBytes: storageBytes,
typeName: 'enum',
enum: enumDef
}
return new EnumType(decodeInfo)
return new EnumType(enumDef)
}
/**
@ -203,13 +136,7 @@ function Struct (type, stateDefinitions) {
}
var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition
if (!memberDetails) return null
var decodeInfo = {
storageSlots: Math.ceil(memberDetails.storageBytes / 32),
storageBytes: 32,
typeName: 'struct',
members: memberDetails.members
}
return new StructType(decodeInfo)
return new StructType(memberDetails)
}
/**

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function Address (decoder) {
baseType(this, decoder)
function Address () {
this.storageSlots = 1
this.storageBytes = 20
this.typeName = 'address'
}
Address.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,10 +1,21 @@
'use strict'
var baseType = require('./baseType')
function ArrayType (decoder) {
baseType(this, decoder)
this.arraySize = decoder.arraySize
this.subArray = decoder.subArray
function ArrayType (underlyingType, arraySize) {
this.typeName = 'array'
this.storageBytes = 32
this.underlyingType = underlyingType
this.arraySize = arraySize
this.storageSlots = null
if (arraySize === 'dynamic') {
this.storageSlots = 1
} else {
if (underlyingType.storageBytes < 32) {
var itemPerSlot = Math.floor(32 / underlyingType.storageBytes)
this.storageSlots = Math.ceil(arraySize / itemPerSlot)
} else {
this.storageSlots = arraySize * underlyingType.storageSlots
}
}
}
ArrayType.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function Bool (decoder) {
baseType(this, decoder)
function Bool () {
this.storageSlots = 1
this.storageBytes = 1
this.typeName = 'bool'
}
Bool.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function DynamicByteArray (decoder) {
baseType(this, decoder)
function DynamicByteArray () {
this.storageSlots = 1
this.storageBytes = 32
this.typeName = 'bytes'
}
DynamicByteArray.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,9 +1,15 @@
'use strict'
var baseType = require('./baseType')
function Enum (decoder) {
baseType(this, decoder)
this.enum = decoder.enum
function Enum (enumDef) {
this.enumDef = enumDef
this.typeName = 'enum'
this.storageSlots = 1
var length = enumDef.children.length
this.storageBytes = 0
while (length > 1) {
length = length / 256
this.storageBytes++
}
}
Enum.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function FixedByteArray (decoder) {
baseType(this, decoder)
function FixedByteArray (storageBytes) {
this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'bytesX'
}
FixedByteArray.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function Int (decoder) {
baseType(this, decoder)
function Int (storageBytes) {
this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'int'
}
Int.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function StringType (decoder) {
baseType(this, decoder)
function StringType () {
this.storageSlots = 1
this.storageBytes = 32
this.typeName = 'string'
}
StringType.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,9 +1,10 @@
'use strict'
var baseType = require('./baseType')
function Struct (decoder) {
baseType(this, decoder)
this.members = decoder.members
function Struct (memberDetails) {
this.storageSlots = Math.ceil(memberDetails.storageBytes / 32)
this.storageBytes = 32
this.members = memberDetails.members
this.typeName = 'struct'
}
Struct.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +1,9 @@
'use strict'
var baseType = require('./baseType')
function Uint (decoder) {
baseType(this, decoder)
function Uint (storageBytes) {
this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'uint'
}
Uint.prototype.decodeFromStorage = function (location, storageContent) {

@ -1,8 +0,0 @@
'use strict'
module.exports = function (type, decoder) {
type.storageSlots = decoder.storageSlots
type.storageBytes = decoder.storageBytes
type.typeName = decoder.typeName
type.decoder = decoder.decoder
}
Loading…
Cancel
Save