Skip to content

Commit

Permalink
refactor: not parse response data to object any more
Browse files Browse the repository at this point in the history
  • Loading branch information
yAmanda committed Oct 22, 2019
1 parent 871b97c commit ad2987d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 150 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,24 @@ None

##### Returns
`Promise` - Call `then` method to get the address list of current deputy nodes. The address can be used to [connect](#submodule-net-connect) LemoChain node.
`minerAddress` - (string)The address of miner account who receive miner benefit
`incomeAddress` - (string)Nodal incomeAddress
`nodeID` - (string)The LemoChain node ID, it is from the public key whose private key is using for sign blocks. The length should be 130 characters with `0x`
`rank` - (string)The rank of the outgoing node
`votes` - (string)Node votes
`host` - (string)The LemoChain node ID, it is from the public key whose private key is using for sign blocks. The length should be 130 characters with `0x`
`port` - (string)Port of the candidate node server
`depositAmount` - (string)deposit amount
`introduction` - (string)Introduction of node

##### Example
```js
lemo.getDeputyNodeList().then(function(nodeList) {
console.log(nodeList.length) // 1
console.log(nodeList[0]) // "0x5e3600755f9b512a65603b38e30885c98cbac70259c3235c9b3f42ee563b480edea351ba0ff5748a638fe0aeff5d845bf37a3b437831871b48fd32f33cd9a3c0@149.28.68.93:7003"
lemo.net.connect(nodeList[0])
console.log(JSON.stringify(nodeList[0]))
// "{"minerAddress":"Lemo83DZ5J99JSK5ZH89TCW7T6ZZCWJ8H7FDGA7W","incomeAddress":"Lemo83DZ5J99JSK5ZH89TCW7T6ZZCWJ8H7FDGA7W","nodeID":"0x0e7dcd418dbe7717eb0e83162f8810a3c7725e0d386b324dc5f3ef5a27a2a83e393a193f6ab53d3a51b490adeee362357676f50eed3d188824ef1fb3af02b2d0","rank":"0","votes":"50000","host":"127.0.0.1","port":"8080","depositAmount":"5000000000000000000000000","introduction":"ddf"}"
const nodeName = `${nodeList[0].nodeID}@${nodeList[0].host}:${nodeList[0].port}`
lemo.net.connect(nodeName)
})
```

Expand Down
15 changes: 13 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,24 @@ lemo.getDeputyNodeList()

##### Returns
`Promise` - 通过`then`可以获取当前所有共识节点的地址列表。该地址可用于[连接节点](#submodule-net-connect)
`minerAddress` - (string)节点出块账号
`incomeAddress` - (string)节点收益账号
`nodeID` - (string)节点ID
`rank` - (string)出块节点所在的排名
`votes` - (string)节点所得票数
`host` - (string)节点域名
`port` - (string)端口号
`depositAmount` - (string)质押金额
`introduction` - (string)节点简介

##### Example
```js
lemo.getDeputyNodeList().then(function(nodeList) {
console.log(nodeList.length) // 1
console.log(nodeList[0]) // "5e3600755f9b512a65603b38e30885c98cbac70259c3235c9b3f42ee563b480edea351ba0ff5748a638fe0aeff5d845bf37a3b437831871b48fd32f33cd9a3c0@149.28.68.93:7003"
lemo.net.connect(nodeList[0])
console.log(JSON.stringify(nodeList[0]))
// "{"minerAddress":"Lemo83DZ5J99JSK5ZH89TCW7T6ZZCWJ8H7FDGA7W","incomeAddress":"Lemo83DZ5J99JSK5ZH89TCW7T6ZZCWJ8H7FDGA7W","nodeID":"0x0e7dcd418dbe7717eb0e83162f8810a3c7725e0d386b324dc5f3ef5a27a2a83e393a193f6ab53d3a51b490adeee362357676f50eed3d188824ef1fb3af02b2d0","rank":"0","votes":"50000","host":"127.0.0.1","port":"8080","depositAmount":"5000000000000000000000000","introduction":"ddf"}"
const nodeName = `${nodeList[0].nodeID}@${nodeList[0].host}:${nodeList[0].port}`
lemo.net.connect(nodeName)
})
```

Expand Down
25 changes: 17 additions & 8 deletions lib/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ export const TxType = {
export const ChangeLogTypes = {
BalanceLog: 1,
StorageLog: 2,
CodeLog: 3,
AddEventLog: 4,
SuicideLog: 5,
VoteForLog: 6,
VotesLog: 7,
CandidateProfileLog: 8,
TxCountLog: 9,
SignersLog: 10,
StorageRootLog: 3,
AssetCodeLog: 4,
AssetCodeStateLog: 5,
AssetCodeRootLog: 6,
AssetCodeTotalSupplyLog: 7,
AssetIdLog: 8,
AssetIdRootLog: 9,
EquityLog: 10,
EquityRootLog: 11,
CandidateLog: 12,
CandidateStateLog: 13,
CodeLog: 14,
AddEventLog: 15,
SuicideLog: 16,
VoteForLog: 17,
VotesLog: 18,
SignerLog: 19,
}

// module name
Expand Down
14 changes: 6 additions & 8 deletions lib/modules/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ const apis = {
/**
* Get account information
* @param {string} address
* @return {Promise<object>}
* @return {Promise<string>}
*/
async getAccount(address) {
getAccount(address) {
if (!isLemoAddress(address)) {
throw new Error(error.InvalidAddress(address))
}
const result = await this.requester.send(`${ACCOUNT_NAME}_getAccount`, [address])
return this.parser.parseAccount(result)
return this.requester.send(`${ACCOUNT_NAME}_getAccount`, [address])
},
/**
* Get candidate information
Expand All @@ -30,14 +29,13 @@ const apis = {
/**
* Get balance from account
* @param {string} address
* @return {Promise<BigNumber>}
* @return {Promise<string>}
*/
async getBalance(address) {
getBalance(address) {
if (!isLemoAddress(address)) {
throw new Error(error.InvalidAddress(address))
}
const result = await this.requester.send(`${ACCOUNT_NAME}_getBalance`, [address])
return this.parser.parseMoney(result)
return this.requester.send(`${ACCOUNT_NAME}_getBalance`, [address])
},
}

Expand Down
68 changes: 13 additions & 55 deletions lib/network/data_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ export function parseBlock(block, withBody) {
}

export function parseAccount(account) {
account.balance = parseMoney(account.balance)
account.txCount = parseNumber(account.txCount)

const oldRecords = account.records || {}
account.records = {}
Object.entries(oldRecords).forEach(([logType, record]) => {
record.height = parseNumber(record.height)
record.version = parseNumber(record.version)
account.records[parseChangeLogType(logType)] = record
account.records[parseNumber(logType)] = record
})

if (account.candidate) {
Expand All @@ -47,100 +44,62 @@ export function parseAccount(account) {
export function parseCandidate(candidate) {
if (candidate.profile) {
candidate.profile.isCandidate = /true/i.test(candidate.profile.isCandidate)
candidate.profile.port = parseNumber(candidate.profile.port)
}

return candidate
}

export function parseChangeLog(changeLog) {
changeLog.type = parseChangeLogType(changeLog.type)
changeLog.type = parseNumber(changeLog.type)
changeLog.version = parseNumber(changeLog.version)
return changeLog
}

export function parseChangeLogType(logType) {
logType = parseInt(logType, 10)
const typeInfo = Object.entries(ChangeLogTypes).find(item => logType === item[1])
if (!typeInfo) {
return `UnknonwType(${logType})`
}
return typeInfo[0]
}


function parseTx(tx) {
// new Tx will fill default fields such as gasPrice. So we couldn't return it directly
const txObj = new LemoTx(tx)
tx.from = txObj.from
tx.type = txObj.type
tx.typeText = parseTxType(txObj.type)
tx.version = txObj.version
tx.amountText = String(tx.amount)
tx.gasPriceText = String(tx.gasPrice)
tx.amount = parseMoney(tx.amount)
tx.type = parseNumber(tx.type)
tx.version = parseNumber(tx.version)
tx.expirationTime = parseNumber(tx.expirationTime)
tx.gasPrice = parseMoney(tx.gasPrice)
tx.gasLimit = parseNumber(tx.gasLimit)
tx.data = parseTxData(tx.type, tx.data)
tx.parsedData = parseTxData(tx.type, tx.data)
tx.gasUsed = parseNumber(tx.gasUsed)

return tx
}

function parseTxType(txType) {
txType = parseInt(txType, 10)
const typeInfo = Object.entries(TxType).find(item => txType === item[1])
if (!typeInfo) {
return `UnknonwType(${txType})`
}
return typeInfo[0]
}

function parseNumber(str) {
return parseInt(str, 10) || 0
}

export function parseTxData(type, data) {
type = parseNumber(type)
let result
switch (type) {
case TxType.ORDINARY:
break
case TxType.CREATE_CONTRACT:
case TxType.VOTE:
result = undefined
break
case TxType.BOX_TX:
data = JSON.parse(toBuffer(data).toString())
data = data.map(parseTx)
result = data.map(parseTx)
break
default:
data = JSON.parse(toBuffer(data).toString())
result = JSON.parse(toBuffer(data).toString())
break
}
return data
}

export function parseBigNumber(str) {
return new BigNumber(str)
}

export function parseMoney(str) {
const result = new BigNumber(str)
Object.defineProperty(result, 'toMoney', {
enumerable: false,
value: formatLEMO.bind(null, result),
})
return result
}

export function parseAsset(result) {
result.equities.forEach(item => {
item.equity = parseBigNumber(item.equity)
item.equity = parseNumber(item.equity)
})
return result
}

export function parseAssetInfo(result) {
result.decimal = parseNumber(result.decimal)
result.totalSupply = parseBigNumber(result.totalSupply)
return result
}

Expand All @@ -151,13 +110,12 @@ export function parseMetaData(result) {
return result
}


export default {
parseBlock,
parseAccount,
parseCandidate,
parseTx,
parseBigNumber,
parseMoney,
parseAsset,
parseAssetInfo,
parseMetaData,
Expand Down

0 comments on commit ad2987d

Please sign in to comment.