Commit a4a16863 authored by aniket-engg's avatar aniket-engg Committed by Aniket

txFormat methods export improved

parent db23f70a
...@@ -5,16 +5,14 @@ import { eachOfSeries } from 'async' ...@@ -5,16 +5,14 @@ import { eachOfSeries } from 'async'
import { linkBytecode } from 'solc/linker' import { linkBytecode } from 'solc/linker'
import { isValidAddress, addHexPrefix } from 'ethereumjs-util' import { isValidAddress, addHexPrefix } from 'ethereumjs-util'
module.exports = { /**
/**
* build the transaction data * build the transaction data
* *
* @param {Object} function abi * @param {Object} function abi
* @param {Object} values to encode * @param {Object} values to encode
* @param {String} contractbyteCode * @param {String} contractbyteCode
*/ */
encodeData: function (funABI, values, contractbyteCode) { export function encodeData (funABI, values, contractbyteCode) {
let encoded let encoded
let encodedHex let encodedHex
try { try {
...@@ -28,16 +26,16 @@ module.exports = { ...@@ -28,16 +26,16 @@ module.exports = {
} else { } else {
return { data: helper.encodeFunctionId(funABI) + encodedHex.replace('0x', '') } return { data: helper.encodeFunctionId(funABI) + encodedHex.replace('0x', '') }
} }
}, }
/** /**
* encode function / constructor parameters * encode function / constructor parameters
* *
* @param {Object} params - input paramater of the function to call * @param {Object} params - input paramater of the function to call
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. * @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor.
* @param {Function} callback - callback * @param {Function} callback - callback
*/ */
encodeParams: function (params, funAbi, callback) { export function encodeParams (params, funAbi, callback) {
let data: any = '' let data: any = ''
let dataHex = '' let dataHex = ''
let funArgs let funArgs
...@@ -69,33 +67,33 @@ module.exports = { ...@@ -69,33 +67,33 @@ module.exports = {
} }
} }
callback(null, { data: data, dataHex: dataHex, funArgs: funArgs }) callback(null, { data: data, dataHex: dataHex, funArgs: funArgs })
}, }
/** /**
* encode function call (function id + encoded parameters) * encode function call (function id + encoded parameters)
* *
* @param {Object} params - input paramater of the function to call * @param {Object} params - input paramater of the function to call
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. * @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor.
* @param {Function} callback - callback * @param {Function} callback - callback
*/ */
encodeFunctionCall: function (params, funAbi, callback) { export function encodeFunctionCall (params, funAbi, callback) {
this.encodeParams(params, funAbi, (error, encodedParam) => { this.encodeParams(params, funAbi, (error, encodedParam) => {
if (error) return callback(error) if (error) return callback(error)
callback(null, { dataHex: helper.encodeFunctionId(funAbi) + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs }) callback(null, { dataHex: helper.encodeFunctionId(funAbi) + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs })
}) })
}, }
/** /**
* encode constructor creation and link with provided libraries if needed * encode constructor creation and link with provided libraries if needed
* *
* @param {Object} contract - input paramater of the function to call * @param {Object} contract - input paramater of the function to call
* @param {Object} params - input paramater of the function to call * @param {Object} params - input paramater of the function to call
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. * @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor.
* @param {Object} linkLibraries - contains {linkReferences} object which list all the addresses to be linked * @param {Object} linkLibraries - contains {linkReferences} object which list all the addresses to be linked
* @param {Object} linkReferences - given by the compiler, contains the proper linkReferences * @param {Object} linkReferences - given by the compiler, contains the proper linkReferences
* @param {Function} callback - callback * @param {Function} callback - callback
*/ */
encodeConstructorCallAndLinkLibraries: function (contract, params, funAbi, linkLibraries, linkReferences, callback) { export function encodeConstructorCallAndLinkLibraries (contract, params, funAbi, linkLibraries, linkReferences, callback) {
this.encodeParams(params, funAbi, (error, encodedParam) => { this.encodeParams(params, funAbi, (error, encodedParam) => {
if (error) return callback(error) if (error) return callback(error)
let bytecodeToDeploy = contract.evm.bytecode.object let bytecodeToDeploy = contract.evm.bytecode.object
...@@ -115,22 +113,22 @@ module.exports = { ...@@ -115,22 +113,22 @@ module.exports = {
} }
return callback(null, { dataHex: bytecodeToDeploy + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs, contractBytecode: contract.evm.bytecode.object }) return callback(null, { dataHex: bytecodeToDeploy + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs, contractBytecode: contract.evm.bytecode.object })
}) })
}, }
/** /**
* encode constructor creation and deploy librairies if needed * encode constructor creation and deploy librairies if needed
* *
* @param {String} contractName - current contract name * @param {String} contractName - current contract name
* @param {Object} contract - input paramater of the function to call * @param {Object} contract - input paramater of the function to call
* @param {Object} contracts - map of all compiled contracts. * @param {Object} contracts - map of all compiled contracts.
* @param {Object} params - input paramater of the function to call * @param {Object} params - input paramater of the function to call
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. * @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor.
* @param {Function} callback - callback * @param {Function} callback - callback
* @param {Function} callbackStep - callbackStep * @param {Function} callbackStep - callbackStep
* @param {Function} callbackDeployLibrary - callbackDeployLibrary * @param {Function} callbackDeployLibrary - callbackDeployLibrary
* @param {Function} callback - callback * @param {Function} callback - callback
*/ */
encodeConstructorCallAndDeployLibraries: function (contractName, contract, contracts, params, funAbi, callback, callbackStep, callbackDeployLibrary) { export function encodeConstructorCallAndDeployLibraries (contractName, contract, contracts, params, funAbi, callback, callbackStep, callbackDeployLibrary) {
this.encodeParams(params, funAbi, (error, encodedParam) => { this.encodeParams(params, funAbi, (error, encodedParam) => {
if (error) return callback(error) if (error) return callback(error)
let dataHex = '' let dataHex = ''
...@@ -151,22 +149,22 @@ module.exports = { ...@@ -151,22 +149,22 @@ module.exports = {
} }
callback(null, {dataHex: bytecodeToDeploy, funAbi, funArgs: encodedParam.funArgs, contractBytecode, contractName: contractName}) callback(null, {dataHex: bytecodeToDeploy, funAbi, funArgs: encodedParam.funArgs, contractBytecode, contractName: contractName})
}) })
}, }
/** /**
* (DEPRECATED) build the transaction data * (DEPRECATED) build the transaction data
* *
* @param {String} contractName * @param {String} contractName
* @param {Object} contract - abi definition of the current contract. * @param {Object} contract - abi definition of the current contract.
* @param {Object} contracts - map of all compiled contracts. * @param {Object} contracts - map of all compiled contracts.
* @param {Bool} isConstructor - isConstructor. * @param {Bool} isConstructor - isConstructor.
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. * @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor.
* @param {Object} params - input paramater of the function to call * @param {Object} params - input paramater of the function to call
* @param {Function} callback - callback * @param {Function} callback - callback
* @param {Function} callbackStep - callbackStep * @param {Function} callbackStep - callbackStep
* @param {Function} callbackDeployLibrary - callbackDeployLibrary * @param {Function} callbackDeployLibrary - callbackDeployLibrary
*/ */
buildData: function (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) { export function buildData (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) {
let funArgs = [] let funArgs = []
let data: any = '' let data: any = ''
let dataHex = '' let dataHex = ''
...@@ -217,11 +215,11 @@ module.exports = { ...@@ -217,11 +215,11 @@ module.exports = {
dataHex = helper.encodeFunctionId(funAbi) + dataHex dataHex = helper.encodeFunctionId(funAbi) + dataHex
} }
callback(null, { dataHex, funAbi, funArgs, contractBytecode, contractName: contractName }) callback(null, { dataHex, funAbi, funArgs, contractBytecode, contractName: contractName })
}, }
atAddress: function () {}, export function atAddress () {}
linkBytecodeStandard: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { export function linkBytecodeStandard (contract, contracts, callback, callbackStep, callbackDeployLibrary) {
let contractBytecode = contract.evm.bytecode.object let contractBytecode = contract.evm.bytecode.object
eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => { eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => {
eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => { eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => {
...@@ -250,9 +248,9 @@ module.exports = { ...@@ -250,9 +248,9 @@ module.exports = {
} }
callback(error, contractBytecode) callback(error, contractBytecode)
}) })
}, }
linkBytecodeLegacy: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { export function linkBytecodeLegacy (contract, contracts, callback, callbackStep, callbackDeployLibrary) {
const libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/) const libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/)
if (!libraryRefMatch) { if (!libraryRefMatch) {
return callback('Invalid bytecode format.') return callback('Invalid bytecode format.')
...@@ -282,9 +280,9 @@ module.exports = { ...@@ -282,9 +280,9 @@ module.exports = {
contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object) contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object)
this.linkBytecode(contract, contracts, callback, callbackStep, callbackDeployLibrary) this.linkBytecode(contract, contracts, callback, callbackStep, callbackDeployLibrary)
}, callbackStep, callbackDeployLibrary) }, callbackStep, callbackDeployLibrary)
}, }
linkBytecode: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { export function linkBytecode (contract, contracts, callback?, callbackStep?, callbackDeployLibrary?) {
if (contract.evm.bytecode.object.indexOf('_') < 0) { if (contract.evm.bytecode.object.indexOf('_') < 0) {
return callback(null, contract.evm.bytecode.object) return callback(null, contract.evm.bytecode.object)
} }
...@@ -293,9 +291,9 @@ module.exports = { ...@@ -293,9 +291,9 @@ module.exports = {
} else { } else {
this.linkBytecodeLegacy(contract, contracts, callback, callbackStep, callbackDeployLibrary) this.linkBytecodeLegacy(contract, contracts, callback, callbackStep, callbackDeployLibrary)
} }
}, }
deployLibrary: function (libraryName, libraryShortName, library, contracts, callback, callbackStep, callbackDeployLibrary) { export function deployLibrary (libraryName, libraryShortName, library, contracts, callback, callbackStep, callbackDeployLibrary) {
const address = library.address const address = library.address
if (address) { if (address) {
return callback(null, address) return callback(null, address)
...@@ -321,9 +319,9 @@ module.exports = { ...@@ -321,9 +319,9 @@ module.exports = {
callback(err, address) callback(err, address)
}) })
} }
}, }
linkLibraryStandardFromlinkReferences: function (libraryName, address, bytecode, linkReferences) { export function linkLibraryStandardFromlinkReferences (libraryName, address, bytecode, linkReferences) {
for (let file in linkReferences) { for (let file in linkReferences) {
for (let libName in linkReferences[file]) { for (let libName in linkReferences[file]) {
if (libraryName === libName) { if (libraryName === libName) {
...@@ -332,13 +330,13 @@ module.exports = { ...@@ -332,13 +330,13 @@ module.exports = {
} }
} }
return bytecode return bytecode
}, }
linkLibraryStandard: function (libraryName, address, bytecode, contract) { export function linkLibraryStandard (libraryName, address, bytecode, contract) {
return this.linkLibraryStandardFromlinkReferences(libraryName, address, bytecode, contract.evm.bytecode.linkReferences) return this.linkLibraryStandardFromlinkReferences(libraryName, address, bytecode, contract.evm.bytecode.linkReferences)
}, }
setLibraryAddress: function (address, bytecodeToLink, positions) { export function setLibraryAddress (address, bytecodeToLink, positions) {
if (positions) { if (positions) {
for (let pos of positions) { for (let pos of positions) {
const regpos = bytecodeToLink.match(new RegExp(`(.{${2 * pos.start}})(.{${2 * pos.length}})(.*)`)) const regpos = bytecodeToLink.match(new RegExp(`(.{${2 * pos.start}})(.{${2 * pos.length}})(.*)`))
...@@ -348,13 +346,13 @@ module.exports = { ...@@ -348,13 +346,13 @@ module.exports = {
} }
} }
return bytecodeToLink return bytecodeToLink
}, }
linkLibrary: function (libraryName, address, bytecodeToLink) { export function linkLibrary (libraryName, address, bytecodeToLink) {
return linkBytecode(bytecodeToLink, { [libraryName]: addHexPrefix(address) }) return linkBytecode(bytecodeToLink, { [libraryName]: addHexPrefix(address) })
}, }
decodeResponse: function (response, fnabi) { export function decodeResponse (response, fnabi) {
// Only decode if there supposed to be fields // Only decode if there supposed to be fields
if (fnabi.outputs && fnabi.outputs.length > 0) { if (fnabi.outputs && fnabi.outputs.length > 0) {
try { try {
...@@ -383,9 +381,9 @@ module.exports = { ...@@ -383,9 +381,9 @@ module.exports = {
} }
} }
return {} return {}
}, }
parseFunctionParams: function (params) { export function parseFunctionParams (params) {
let args = [] let args = []
// Check if parameter string starts with array or string // Check if parameter string starts with array or string
let startIndex = this.isArrayOrStringStart(params, 0) ? -1 : 0 let startIndex = this.isArrayOrStringStart(params, 0) ? -1 : 0
...@@ -445,10 +443,10 @@ module.exports = { ...@@ -445,10 +443,10 @@ module.exports = {
} }
}) })
return args return args
}, }
isArrayOrStringStart: function (str, index) { export function isArrayOrStringStart (str, index) {
return str.charAt(index) === '"' || str.charAt(index) === '[' return str.charAt(index) === '"' || str.charAt(index) === '['
}
} }
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