From ce0c7efe52c8d4713021cf457fc5df3af8f4e0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Wed, 28 Jul 2021 00:39:54 +0100 Subject: [PATCH] fix: add crypto unit config when setting up a new wallet --- ...623975493095-add-crypto-units-to-config.js | 27 ++++++++++++------- new-lamassu-admin/src/pages/Wallet/Wizard.js | 10 ++++++- .../pages/Wizard/components/Wallet/AllSet.js | 10 ++++++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/migrations/1623975493095-add-crypto-units-to-config.js b/migrations/1623975493095-add-crypto-units-to-config.js index 8a5ec333..743850c0 100644 --- a/migrations/1623975493095-add-crypto-units-to-config.js +++ b/migrations/1623975493095-add-crypto-units-to-config.js @@ -1,15 +1,22 @@ -const { saveConfig } = require('../lib/new-settings-loader') +const { saveConfig, loadLatest } = require('../lib/new-settings-loader') +const { getAllCryptoCurrencies } = require('../lib/new-config-manager.js') +const { utils: coinUtils } = require('lamassu-coins') +const _ = require('lodash/fp') exports.up = function (next) { - const cryptoUnits = { - wallets_BTC_cryptoUnits: 'mili', - wallets_ETH_cryptoUnits: 'mili', - wallets_LTC_cryptoUnits: 'mili', - wallets_ZEC_cryptoUnits: 'mili', - wallets_BCH_cryptoUnits: 'mili' - } - - return saveConfig(cryptoUnits) + loadLatest() + .then(settings => { + const newSettings = {} + const activeCryptos = getAllCryptoCurrencies(settings.config) + if (_.head(activeCryptos)) { + _.map(crypto => { + const defaultUnit = _.head(_.keys(coinUtils.getCryptoCurrency(crypto).units)) + newSettings[`wallets_${crypto}_cryptoUnits`] = defaultUnit + return newSettings + }, activeCryptos) + } + return saveConfig(newSettings) + }) .then(() => next()) .catch(err => { console.log(err.message) diff --git a/new-lamassu-admin/src/pages/Wallet/Wizard.js b/new-lamassu-admin/src/pages/Wallet/Wizard.js index 4123f3fd..ea8a7c1a 100644 --- a/new-lamassu-admin/src/pages/Wallet/Wizard.js +++ b/new-lamassu-admin/src/pages/Wallet/Wizard.js @@ -1,3 +1,4 @@ +import { utils as coinUtils } from 'lamassu-coins' import * as R from 'ramda' import React, { useState } from 'react' @@ -57,7 +58,14 @@ const Wizard = ({ coin, onClose, accountsConfig, accounts, save, error }) => { : accountsToSave if (isLastStep) { - const configToSave = { ...newConfig, zeroConfLimit: 0 } + const defaultCryptoUnit = R.head( + R.keys(coinUtils.getCryptoCurrency(coin.code).units) + ) + const configToSave = { + ...newConfig, + zeroConfLimit: 0, + cryptoUnits: defaultCryptoUnit + } return save(toNamespace(coin.code, configToSave), newAccounts) } diff --git a/new-lamassu-admin/src/pages/Wizard/components/Wallet/AllSet.js b/new-lamassu-admin/src/pages/Wizard/components/Wallet/AllSet.js index a9c3cd8f..50f8e4d8 100644 --- a/new-lamassu-admin/src/pages/Wizard/components/Wallet/AllSet.js +++ b/new-lamassu-admin/src/pages/Wizard/components/Wallet/AllSet.js @@ -1,6 +1,7 @@ import { useQuery, useMutation } from '@apollo/react-hooks' import { makeStyles } from '@material-ui/core' import gql from 'graphql-tag' +import { utils as coinUtils } from 'lamassu-coins' import * as R from 'ramda' import React, { useState } from 'react' @@ -53,7 +54,14 @@ const AllSet = ({ data: currentData, doContinue }) => { const cryptoCurrencies = data?.cryptoCurrencies ?? [] const save = () => { - const adjustedData = { zeroConfLimit: 0, ...currentData } + const defaultCryptoUnit = R.head( + R.keys(coinUtils.getCryptoCurrency(coin).units) + ) + const adjustedData = { + zeroConfLimit: 0, + ...currentData, + cryptoUnits: defaultCryptoUnit + } if (!WalletSchema.isValidSync(adjustedData)) return setError(true) const withCoin = toNamespace(coin, R.omit('coin', adjustedData))