1 import {btmAmountUnit} from '../features/core/reducers'
7 transactionId: 'Transaction ID',
10 assetAlias: 'Asset Alias',
11 assetDefinition: 'Asset Definition',
12 assetTags: 'Asset Tags',
13 assetIsLocal: 'Asset Is Local?',
15 accountId: 'Account ID',
16 accountAlias: 'Account Alias',
17 accountTags: 'Account Tags',
18 controlProgram: 'Control Program',
20 programIndex: 'Program Index',
21 spentOutputId: 'Spent Output ID',
22 refData: 'Reference Data',
23 sourceId: 'Source ID',
24 sourcePos: 'Source Postion',
25 issuanceProgram: 'Issuance Program',
27 referenceData: 'Reference Data',
35 transactionId: '交易ID',
39 assetDefinition: '资产定义',
40 assetTags: 'Asset Tags',
41 assetIsLocal: 'Asset Is Local?',
45 accountTags: 'Account Tags',
46 controlProgram: '合约程序',
49 spentOutputId: '花费输出ID',
50 refData: 'Reference Data',
53 issuanceProgram: '资产发布程序',
55 referenceData: 'Reference Data',
59 const txInputFields = [
78 const txOutputFields = [
98 const unspentFields = [
122 const balanceFields = Object.keys(mappings)
124 const buildDisplay = (item, fields, btmAmountUnit, lang) => {
126 fields.forEach(key => {
127 if (item.hasOwnProperty(key)) {
128 if(key === 'amount'){
129 details.push({label: ( lang === 'zh'? mappings_ZH[key]: mappings[key] ), value: normalizeGlobalBTMAmount(item['assetId'], item[key], btmAmountUnit)})
131 details.push({label: ( lang === 'zh'? mappings_ZH[key]: mappings[key] ), value: item[key]})
138 const addZeroToDecimalPos = (src,pos) => {
140 let srcString = src.toString()
141 var rs = srcString.indexOf('.')
143 rs = srcString.length
146 while (srcString.length <= rs + pos) {
154 const formatIntNumToPosDecimal = (neu,pos) => {
156 let neuString = neu.toString()
157 if(neuString.length<=8){
158 return addZeroToDecimalPos((neu/Math.pow(10, pos)), pos)
160 return neuString.slice(0, -pos) + '.' + neuString.slice(-pos)
166 export const normalizeGlobalBTMAmount = (assetID, amount, btmAmountUnit) => {
167 //normalize BTM Amount
168 if (assetID === 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') {
169 switch (btmAmountUnit){
171 return formatIntNumToPosDecimal(amount, 8)+' BTM'
173 return formatIntNumToPosDecimal(amount, 5)+' mBTM'
181 export const normalizeBTM = (amount, btmAmountUnit) => {
182 switch (btmAmountUnit){
184 return formatIntNumToPosDecimal(amount, 8)+' BTM'
186 return formatIntNumToPosDecimal(amount, 5)+' mBTM'
192 export function formatBTMAmount(value, pos) {
197 const onlyNums = value.toString().replace(/[^0-9.]/g, '')
199 // Create an array with sections split by .
200 const sections = onlyNums.split('.')
202 // Remove any leading 0s apart from single 0
203 if (sections[0] !== '0' && sections[0] !== '00') {
204 sections[0] = sections[0].replace(/^0+/, '')
209 // If numbers exist after first .
211 return sections[0] + '.' + sections[1].slice(0, pos)
212 } else if (onlyNums.indexOf('.') !== -1) {
213 return sections[0] + '.'
219 export function parseBTMAmount(value, pos){
224 const onlyNums = value.replace(/[^0-9.]/g, '')
225 const sections = onlyNums.split('.')
230 numDecimal = sections[1].slice(0, pos)
232 while (numDecimal.length < pos) {
236 //remove all the leading 0s
237 let amountNum = sections[0] + numDecimal
238 if(/^0*$/.test(amountNum)){
241 amountNum = amountNum.replace(/^0+/, '')
247 export function normalizeBTMAmountUnit(assetID, amount, btmAmountUnit) {
248 return normalizeGlobalBTMAmount(assetID, amount, btmAmountUnit)
251 export function addZeroToDecimalPosition(value, deciPoint){
252 return addZeroToDecimalPos(value, deciPoint)
255 export function converIntToDec(int, deciPoint){
256 return formatIntNumToPosDecimal(int, deciPoint)
259 export function buildTxInputDisplay(input, btmAmountUnit, lang) {
260 return buildDisplay(input, txInputFields, btmAmountUnit, lang)
263 export function buildTxOutputDisplay(output, btmAmountUnit, lang) {
264 return buildDisplay(output, txOutputFields, btmAmountUnit, lang)
267 export function buildUnspentDisplay(output, btmAmountUnit, lang) {
269 amount: output.amount,
270 accountId: output.accountId,
271 accountAlias: output.accountAlias,
272 assetId: output.assetId,
273 assetAlias: output.assetAlias,
274 controlProgram: output.program,
275 programIndex: output.controlProgramIndex,
276 sourceId: output.sourceId,
277 sourcePos: output.sourcePos,
278 change: output.change + ''
280 return buildDisplay(normalized, unspentFields, btmAmountUnit, lang)
283 export function buildBalanceDisplay(balance, btmAmountUnit, lang) {
284 return buildDisplay({
285 amount: balance.amount,
286 assetId: balance.assetId,
287 assetAlias: balance.assetAlias,
288 accountAlias: balance.accountAlias
289 }, balanceFields, btmAmountUnit, lang)