Skip to content

Commit

Permalink
toSatoshi: take into account the asset precision
Browse files Browse the repository at this point in the history
  • Loading branch information
tiero committed May 12, 2021
1 parent c96c4b7 commit 52d18df
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/presentation/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export function decimalCount(num: number) {
return 0;
}

export function toSatoshi(x: number, y: number = defaultPrecision): number {
return new Decimal(x).mul(Decimal.pow(10, y)).toNumber();
export function toSatoshi(fractional: number, precision: number = defaultPrecision): number {
return new Decimal(fractional).mul(Decimal.pow(10, precision)).toNumber();
}

// Converting to string will trim trailing zeros
Expand Down
9 changes: 7 additions & 2 deletions src/presentation/wallet/send/address-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
setAddressesAndAmount,
} from '../../../application/store/actions';
import {
defaultPrecision,
imgPathMapMainnet,
imgPathMapRegtest,
isValidAddressForNetwork,
Expand All @@ -27,6 +28,7 @@ interface AddressAmountFormValues {
address: string;
amount: number;
assetTicker: string;
assetPrecision: number;
balances: { [assetHash: string]: number };
}

Expand Down Expand Up @@ -130,6 +132,9 @@ const AddressAmountEnhancedForm = withFormik<AddressAmountFormProps, AddressAmou
assetTicker:
props.state.assets[props.state.app.network.value][props.state.transaction.asset]?.ticker ??
'',
assetPrecision:
props.state.assets[props.state.app.network.value][props.state.transaction.asset]?.precision ??
defaultPrecision,
balances: props.balances,
}),

Expand Down Expand Up @@ -166,7 +171,7 @@ const AddressAmountEnhancedForm = withFormik<AddressAmountFormProps, AddressAmou
setAddressesAndAmount(
Address.create(values.address),
Address.create(changeAddress.value, changeAddress.derivationPath),
toSatoshi(values.amount)
toSatoshi(values.amount, values.assetPrecision)
)
);
props.history.push({
Expand All @@ -183,7 +188,7 @@ const AddressAmount: React.FC = () => {
const [balances, setBalances] = useState<{ [assetHash: string]: number }>({});
const assetTicker = state.assets[state.app.network.value][state.transaction.asset]?.ticker ?? '';
const assetPrecision =
state.assets[state.app.network.value][state.transaction.asset]?.precision ?? '';
state.assets[state.app.network.value][state.transaction.asset]?.precision ?? defaultPrecision;

const handleBackBtn = () => {
dispatch(
Expand Down
6 changes: 4 additions & 2 deletions src/presentation/wallet/send/confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ const Confirmation: React.FC = () => {
<div className="bg-gradient-to-r from-secondary to-primary flex flex-row items-center justify-between h-12 px-4 mt-4 rounded-full">
<span className="text-lg font-medium">Amount</span>
<span className="text-base font-medium text-white">
{fromSatoshiStr(sendAmount)} {assets[app.network.value][sendAsset]?.ticker}
{fromSatoshiStr(sendAmount, assets[app.network.value][sendAsset]?.precision)}{' '}
{assets[app.network.value][sendAsset]?.ticker}
</span>
</div>

<div className="flex flex-row items-end justify-between px-3 mt-10">
<span className="text-lg font-medium">Fee</span>
<span className="font-regular text-base">
{fromSatoshiStr(feeAmount)} {assets[app.network.value][feeAsset]?.ticker}
{fromSatoshiStr(feeAmount, assets[app.network.value][feeAsset]?.precision)}{' '}
{assets[app.network.value][feeAsset]?.ticker}
</span>
</div>

Expand Down
3 changes: 2 additions & 1 deletion src/presentation/wallet/transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ const Transactions: React.FC = () => {
<div>
<p className="text-base font-medium">Amount</p>
<p className="text-xs font-light">
{fromSatoshiStr(modalTxDetails?.amount ?? 0)} {state.assetTicker}
{fromSatoshiStr(modalTxDetails?.amount ?? 0, state.assetPrecision)}{' '}
{state.assetTicker}
</p>
</div>
<div>
Expand Down

0 comments on commit 52d18df

Please sign in to comment.