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

txFormat and txHelper

parent 1678b0e9
'use strict' 'use strict'
const ethers = require('ethers') import { ethers } from 'ethers'
const helper = require('./txHelper') const helper = require('./txHelper')
const asyncJS = require('async') import { eachOfSeries } from 'async'
const solcLinker = require('solc/linker') import { linkBytecode } from 'solc/linker'
const ethJSUtil = require('ethereumjs-util') import { isValidAddress, addHexPrefix } from 'ethereumjs-util'
module.exports = { module.exports = {
...@@ -38,7 +38,7 @@ module.exports = { ...@@ -38,7 +38,7 @@ module.exports = {
* @param {Function} callback - callback * @param {Function} callback - callback
*/ */
encodeParams: function (params, funAbi, callback) { encodeParams: function (params, funAbi, callback) {
let data = '' let data: any = ''
let dataHex = '' let dataHex = ''
let funArgs let funArgs
if (params.indexOf('raw:0x') === 0) { if (params.indexOf('raw:0x') === 0) {
...@@ -104,7 +104,7 @@ module.exports = { ...@@ -104,7 +104,7 @@ module.exports = {
for (let libFile in linkLibraries) { for (let libFile in linkLibraries) {
for (let lib in linkLibraries[libFile]) { for (let lib in linkLibraries[libFile]) {
const address = linkLibraries[libFile][lib] const address = linkLibraries[libFile][lib]
if (!ethJSUtil.isValidAddress(address)) return callback(address + ' is not a valid address. Please check the provided address is valid.') if (!isValidAddress(address)) return callback(address + ' is not a valid address. Please check the provided address is valid.')
bytecodeToDeploy = this.linkLibraryStandardFromlinkReferences(lib, address.replace('0x', ''), bytecodeToDeploy, linkReferences) bytecodeToDeploy = this.linkLibraryStandardFromlinkReferences(lib, address.replace('0x', ''), bytecodeToDeploy, linkReferences)
} }
} }
...@@ -168,7 +168,7 @@ module.exports = { ...@@ -168,7 +168,7 @@ module.exports = {
*/ */
buildData: function (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) { buildData: function (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) {
let funArgs = [] let funArgs = []
let data = '' let data: any = ''
let dataHex = '' let dataHex = ''
if (params.indexOf('raw:0x') === 0) { if (params.indexOf('raw:0x') === 0) {
...@@ -223,8 +223,8 @@ module.exports = { ...@@ -223,8 +223,8 @@ module.exports = {
linkBytecodeStandard: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { linkBytecodeStandard: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) {
let contractBytecode = contract.evm.bytecode.object let contractBytecode = contract.evm.bytecode.object
asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => { eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => {
asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => { eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => {
const library = contracts[file][libName] const library = contracts[file][libName]
if (library) { if (library) {
this.deployLibrary(file + ':' + libName, libName, library, contracts, (error, address) => { this.deployLibrary(file + ':' + libName, libName, library, contracts, (error, address) => {
...@@ -351,7 +351,7 @@ module.exports = { ...@@ -351,7 +351,7 @@ module.exports = {
}, },
linkLibrary: function (libraryName, address, bytecodeToLink) { linkLibrary: function (libraryName, address, bytecodeToLink) {
return solcLinker.linkBytecode(bytecodeToLink, { [libraryName]: ethJSUtil.addHexPrefix(address) }) return linkBytecode(bytecodeToLink, { [libraryName]: addHexPrefix(address) })
}, },
decodeResponse: function (response, fnabi) { decodeResponse: function (response, fnabi) {
......
'use strict' 'use strict'
import { ethers } from 'ethers' import { ethers } from 'ethers'
module.exports = { export function makeFullTypeDefinition (typeDef) {
makeFullTypeDefinition: function (typeDef) {
if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) { if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) {
const innerTypes = typeDef.components.map((innerType) => { return this.makeFullTypeDefinition(innerType) }) const innerTypes = typeDef.components.map((innerType) => { return this.makeFullTypeDefinition(innerType) })
return `tuple(${innerTypes.join(',')})${this.extractSize(typeDef.type)}` return `tuple(${innerTypes.join(',')})${this.extractSize(typeDef.type)}`
} }
return typeDef.type return typeDef.type
}, }
encodeParams: function (funABI, args) { export function encodeParams (funABI, args) {
const types = [] const types = []
if (funABI.inputs && funABI.inputs.length) { if (funABI.inputs && funABI.inputs.length) {
for (let i = 0; i < funABI.inputs.length; i++) { for (let i = 0; i < funABI.inputs.length; i++) {
...@@ -31,15 +30,15 @@ module.exports = { ...@@ -31,15 +30,15 @@ module.exports = {
// it could be done here too for consistency // it could be done here too for consistency
const abiCoder = new ethers.utils.AbiCoder() const abiCoder = new ethers.utils.AbiCoder()
return abiCoder.encode(types, args) return abiCoder.encode(types, args)
}, }
encodeFunctionId: function (funABI) { export function encodeFunctionId (funABI) {
if (funABI.type === 'fallback' || funABI.type === 'receive') return '0x' if (funABI.type === 'fallback' || funABI.type === 'receive') return '0x'
let abi = new ethers.utils.Interface([funABI]) let abi = new ethers.utils.Interface([funABI])
return abi.getSighash(funABI.name) return abi.getSighash(funABI.name)
}, }
sortAbiFunction: function (contractabi) { export function sortAbiFunction (contractabi) {
// Check if function is constant (introduced with Solidity 0.6.0) // Check if function is constant (introduced with Solidity 0.6.0)
const isConstant = ({stateMutability}) => stateMutability === 'view' || stateMutability === 'pure' const isConstant = ({stateMutability}) => stateMutability === 'view' || stateMutability === 'pure'
// Sorts the list of ABI entries. Constant functions will appear first, // Sorts the list of ABI entries. Constant functions will appear first,
...@@ -59,9 +58,9 @@ module.exports = { ...@@ -59,9 +58,9 @@ module.exports = {
return 1 return 1
} }
}) })
}, }
getConstructorInterface: function (abi) { export function getConstructorInterface (abi) {
const funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'payable': false, 'outputs': [] } const funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'payable': false, 'outputs': [] }
if (typeof abi === 'string') { if (typeof abi === 'string') {
try { try {
...@@ -82,23 +81,23 @@ module.exports = { ...@@ -82,23 +81,23 @@ module.exports = {
} }
return funABI return funABI
}, }
serializeInputs: function (fnAbi) { export function serializeInputs (fnAbi) {
let serialized = '(' let serialized = '('
if (fnAbi.inputs && fnAbi.inputs.length) { if (fnAbi.inputs && fnAbi.inputs.length) {
serialized += fnAbi.inputs.map((input) => { return input.type }).join(',') serialized += fnAbi.inputs.map((input) => { return input.type }).join(',')
} }
serialized += ')' serialized += ')'
return serialized return serialized
}, }
extractSize: function (type) { export function extractSize (type) {
const size = type.match(/([a-zA-Z0-9])(\[.*\])/) const size = type.match(/([a-zA-Z0-9])(\[.*\])/)
return size ? size[2] : '' return size ? size[2] : ''
}, }
getFunction: function (abi, fnName) { export function getFunction (abi, fnName) {
for (let i = 0; i < abi.length; i++) { for (let i = 0; i < abi.length; i++) {
const fn = abi[i] const fn = abi[i]
if (fn.type === 'function' && fnName === fn.name + '(' + fn.inputs.map((value) => { if (fn.type === 'function' && fnName === fn.name + '(' + fn.inputs.map((value) => {
...@@ -113,55 +112,53 @@ module.exports = { ...@@ -113,55 +112,53 @@ module.exports = {
} }
} }
return null return null
}, }
getFallbackInterface: function (abi) { export function getFallbackInterface (abi) {
for (let i = 0; i < abi.length; i++) { for (let i = 0; i < abi.length; i++) {
if (abi[i].type === 'fallback') { if (abi[i].type === 'fallback') {
return abi[i] return abi[i]
} }
} }
}, }
getReceiveInterface: function (abi) { export function getReceiveInterface (abi) {
for (let i = 0; i < abi.length; i++) { for (let i = 0; i < abi.length; i++) {
if (abi[i].type === 'receive') { if (abi[i].type === 'receive') {
return abi[i] return abi[i]
} }
} }
}, }
/** /**
* return the contract obj of the given @arg name. Uses last compilation result. * return the contract obj of the given @arg name. Uses last compilation result.
* return null if not found * return null if not found
* @param {String} name - contract name * @param {String} name - contract name
* @returns contract obj and associated file: { contract, file } or null * @returns contract obj and associated file: { contract, file } or null
*/ */
getContract: (contractName, contracts) => { export function getContract(contractName, contracts) {
for (let file in contracts) { for (let file in contracts) {
if (contracts[file][contractName]) { if (contracts[file][contractName]) {
return { object: contracts[file][contractName], file: file } return { object: contracts[file][contractName], file: file }
} }
} }
return null return null
}, }
/** /**
* call the given @arg cb (function) for all the contracts. Uses last compilation result * call the given @arg cb (function) for all the contracts. Uses last compilation result
* stop visiting when cb return true * stop visiting when cb return true
* @param {Function} cb - callback * @param {Function} cb - callback
*/ */
visitContracts: (contracts, cb) => { export function visitContracts (contracts, cb) {
for (let file in contracts) { for (let file in contracts) {
for (let name in contracts[file]) { for (let name in contracts[file]) {
if (cb({ name: name, object: contracts[file][name], file: file })) return if (cb({ name: name, object: contracts[file][name], file: file })) return
} }
} }
}, }
inputParametersDeclarationToString: function (abiinputs) { export function inputParametersDeclarationToString (abiinputs) {
const inputs = (abiinputs || []).map((inp) => inp.type + ' ' + inp.name) const inputs = (abiinputs || []).map((inp) => inp.type + ' ' + inp.name)
return inputs.join(', ') return inputs.join(', ')
}
} }
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