Skip to content

Commit

Permalink
chore(contract): return result of contract execution directly
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleHovering committed Feb 20, 2019
1 parent 3076caa commit d29e74f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/contract/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ async function handleContractResult(contractId, contractAddr, callResult, trans,
}
}

callResult.transfers = undefined
}

/**
Expand Down Expand Up @@ -206,6 +207,7 @@ module.exports = {
timestamp: this.trs.timestamp,
})
}
return registerResult
},

/**
Expand Down Expand Up @@ -236,6 +238,7 @@ module.exports = {
contractInfo.id, contractInfo.address, callResult, this.trs,
this.block.height, checkResult.energy, checkResult.payer
)
return callResult
},

/**
Expand Down Expand Up @@ -285,7 +288,7 @@ module.exports = {
contractInfo.id, contractInfo.address, payResult, this.trs,
this.block.height, checkResult.useEnergy, checkResult.payer
)

return payResult
}

}
40 changes: 15 additions & 25 deletions src/interface/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ async function handleActionRequest(req) {
}

function convertBigintMemberToString(obj) {
Object.keys(obj).forEach(key=>{
Object.keys(obj).forEach( key => {
const value = obj[key]
const type = typeof value
if (type === 'bigint')
if (type === 'bigint') {
obj[key] = String(value)
else if (type === 'object')
}
else if (type === 'object') {
convertBigintMemberToString(value)
}
})
}

Expand Down Expand Up @@ -97,7 +99,7 @@ module.exports = (router) => {

/**
* Get contract details
* @param name name of contract
* @param name contract name
* @returns contract detail { contract : { id, name, tid, address, owner, vmVersion,
* desc, timestamp, metadata } }
*/
Expand All @@ -108,37 +110,25 @@ module.exports = (router) => {
return { success: true, contract: contracts[0] }
})

/**
* Get state of contract
* @param name name of contract
* @param stateName name of mapping state
* @param key key of mapping state
* @returns state value
*/
router.get('/:name/states/:stateName/:key', async (req) => {
const { name, stateName, key } = req.params
const result = await app.contract.queryState(name, stateName, key)
convertBigintMemberToString(result)
return result
})

/**
* Get state of contract
* @param name name of contract
* @param stateName state name
* @returns state value
* @param name contract name
* @param statePath path of state, separated by '.' , eg: 'holding.0' => contract['holding'][0]
* @returns state value if primitive, else return count of children states
*/
router.get('/:name/states/:stateName', async (req) => {
const { name, stateName } = req.params
const result = await app.contract.queryState(name, stateName)
router.get('/:name/states/:statePath', async (req) => {
const { name, statePath } = req.params
if (!statePath) throw new Error(`Invalid state path '${statePath}'`)

const result = await app.contract.queryState(name, String(statePath).split('.') )
convertBigintMemberToString(result)
return result
})


/**
* Get state of contract
* @param name name of contract
* @param name contract name
* @param method constant method name
* @param args stringified arguments, eg: ["name", 323]
* @returns constant method call result
Expand Down

0 comments on commit d29e74f

Please sign in to comment.