From dceeae3eb736cdffdeb1b3c8cf4a0a0cf344a8ed Mon Sep 17 00:00:00 2001 From: lenilsonjr Date: Wed, 16 Mar 2022 18:43:27 +0100 Subject: [PATCH] :bug: Fix fungible transactions sum --- .../Dashboard/Portfolio/Portfolio.tsx | 4 +--- src/electron/package.json | 2 +- src/electron/worker.js | 19 +++++++++++++++++-- src/store/models/wallet.ts | 8 ++++++-- src/util/helpers.ts | 6 ++++-- src/util/links.ts | 1 + 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/components/Dashboard/Portfolio/Portfolio.tsx b/src/components/Dashboard/Portfolio/Portfolio.tsx index 35592352..2e35b3d4 100644 --- a/src/components/Dashboard/Portfolio/Portfolio.tsx +++ b/src/components/Dashboard/Portfolio/Portfolio.tsx @@ -46,9 +46,7 @@ const Portfolio = (): ReactElement => { const tokenCount = useSelector(selectTokenCount); const tokelPriceUSD = useSelector(selectTokelPriceUSD); - const priceString = tokelPriceUSD - ? ` ~ $${Math.round(balance * tokelPriceUSD * 100) / 100}` - : null; + const priceString = tokelPriceUSD ? ` ~ $${Math.round(balance * tokelPriceUSD * 100) / 100}` : ''; useEffect(() => { fetchTokelPrice(); diff --git a/src/electron/package.json b/src/electron/package.json index cefb4d18..9bfbacfa 100644 --- a/src/electron/package.json +++ b/src/electron/package.json @@ -1,7 +1,7 @@ { "name": "tokel_app", "productName": "tokelPlatform", - "version": "1.2.1", + "version": "1.2.2", "description": "Komodo ecosystem’s Token Platform", "main": "./main.js", "author": { diff --git a/src/electron/worker.js b/src/electron/worker.js index d4e27d15..df5bd208 100644 --- a/src/electron/worker.js +++ b/src/electron/worker.js @@ -2,6 +2,17 @@ const { parentPort } = require('worker_threads'); const sb = require('satoshi-bitcoin'); const BN = require('bn.js'); +// Same as parseBigNumObject in helpers.ts TODO: don't repeat myself +const parseBigNumObject = bnObj => { + if (!bnObj) return new BN(0); + + const bn = new BN(0); + Object.entries(bnObj).forEach(([propName, propValue]) => { + bn[propName] = propValue; + }); + return bn; +}; + const { ECPair, ccutils, @@ -154,10 +165,14 @@ class BitgoSingleton { const res = {}; ccUtxos.forEach(utxo => { if (utxo?.tokendata?.tokenid) { - res[utxo.tokendata.tokenid.reverse().toString('hex')] = utxo.satoshis; + const tokenId = utxo.tokendata.tokenid.reverse().toString('hex'); + const currentSatoshis = parseBigNumObject(res[tokenId]); + res[tokenId] = currentSatoshis.add(parseBigNumObject(utxo.satoshis)); } else if (!!utxo?.tokendata?.name && utxo?.tokendata?.funcid === 'c') { // In case token was created by the wallet, but has no transactions - res[utxo.txid.reverse().toString('hex')] = utxo.satoshis; + const txId = utxo.txid.reverse().toString('hex'); + const currentSatoshis = parseBigNumObject(res[txId]); + res[txId] = currentSatoshis.add(parseBigNumObject(utxo.satoshis)); } }); return { diff --git a/src/store/models/wallet.ts b/src/store/models/wallet.ts index 460b3feb..6dc0be4d 100644 --- a/src/store/models/wallet.ts +++ b/src/store/models/wallet.ts @@ -1,6 +1,8 @@ import { createModel } from '@rematch/core'; +import BN from 'bn.js'; import dp from 'dot-prop-immutable'; +import { parseBigNumObject } from 'util/helpers'; import { TICKER, TokenFilter } from 'vars/defines'; import type { RootModel } from './models'; @@ -45,7 +47,9 @@ export default createModel()({ ...state, tokenBalances, }), - UPDATE_TOKEN_BALANCE: (state, tokenid: string, balance: number) => - dp.set(state, `tokenBalances.${tokenid}`, v => v - balance), + UPDATE_TOKEN_BALANCE: (state, tokenid: string, amount: number) => + dp.set(state, `tokenBalances.${tokenid}`, balance => + parseBigNumObject(balance).sub(new BN(amount)) + ), }, }); diff --git a/src/util/helpers.ts b/src/util/helpers.ts index 3886e17a..25a92aea 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -2,8 +2,8 @@ import BN from 'bn.js'; import format from 'date-fns/format'; import fromUnixTime from 'date-fns/fromUnixTime'; import getUnixTime from 'date-fns/getUnixTime'; -import BigNumObject from 'util/types/BigNum'; +import BigNumObject from 'util/types/BigNum'; import { Config, EXTRACT_IPFS_HASH_REGEX, SATOSHIS, WindowSize } from 'vars/defines'; interface ResponsiveType { @@ -26,7 +26,9 @@ export const Responsive = { export const randomColor = () => `hsla(${(360 * Math.random()).toString()}, 70%, 80%, 1)`; -const parseBigNumObject = (bnObj: BigNumObject) => { +export const parseBigNumObject = (bnObj: BigNumObject) => { + if (!bnObj) return new BN(0); + const bn = new BN(0); Object.entries(bnObj).forEach(([propName, propValue]) => { bn[propName] = propValue; diff --git a/src/util/links.ts b/src/util/links.ts index bfe6ce08..0b0f788c 100644 --- a/src/util/links.ts +++ b/src/util/links.ts @@ -3,6 +3,7 @@ export default { KMD: () => 'https://kmd.explorer.dexstats.info', TKL: path => `https://explorer.tokel.io/${path}`, TKLTEST: path => `http://explorer.komodoplatform.com:20000/${path}/TKLTEST`, + TKLTEST2: path => `http://explorer.komodoplatform.com:20000/${path}/TKLTEST2`, }, insightApi: { TKL: 'https://tokel.explorer.dexstats.info/insight-api-komodo',