Skip to content

Commit

Permalink
feat: convert amount back to base
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrorezende authored and emccorson committed Oct 11, 2024
1 parent 70391e3 commit 2b36b2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
14 changes: 11 additions & 3 deletions apps/namadillo/src/App/Transfer/TransferModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionButton, Stack } from "@namada/components";
import BigNumber from "bignumber.js";
import { useState } from "react";
import { WalletProvider } from "types";
import { formatDisplayValue } from "utils";
import { toBaseAmount, toDisplayAmount } from "utils";
import { SelectAssetModal } from "./SelectAssetModal";
import { SelectChainModal } from "./SelectChainModal";
import { SelectWalletModal } from "./SelectWalletModal";
Expand Down Expand Up @@ -66,7 +66,7 @@ export const TransferModule = ({

const availableAmount =
source.selectedAsset ?
formatDisplayValue(
toDisplayAmount(
source.selectedAsset,
new BigNumber(source.availableAmount || 0)
)
Expand All @@ -93,7 +93,15 @@ export const TransferModule = ({
throw new Error("Address is not provided");
}

onSubmitTransfer?.(amount, address, memo);
if (!source.selectedAsset) {
throw new Error("Asset is not selected");
}

onSubmitTransfer?.(
toBaseAmount(source.selectedAsset, amount),
address,
memo
);
};

const onChangeWallet = (config: TransferModuleConfig) => (): void => {
Expand Down
21 changes: 15 additions & 6 deletions apps/namadillo/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset } from "@chain-registry/types";
import { Asset, AssetDenomUnit } from "@chain-registry/types";
import { ProposalStatus, ProposalTypeString } from "@namada/types";
import BigNumber from "bignumber.js";
import * as fns from "date-fns";
Expand Down Expand Up @@ -98,14 +98,23 @@ export const secondsToTimeRemainingString = (
.replace("minute", "Min");
};

export const formatDisplayValue = (
asset: Asset,
amount: BigNumber
): BigNumber => {
const findDisplayUnit = (asset: Asset): AssetDenomUnit | undefined => {
const { display, denom_units } = asset;
const displayUnit = denom_units.find((unit) => unit.denom === display);
return denom_units.find((unit) => unit.denom === display);
};

export const toDisplayAmount = (asset: Asset, amount: BigNumber): BigNumber => {
const displayUnit = findDisplayUnit(asset);
if (!displayUnit) {
return amount;
}
return amount.shiftedBy(-displayUnit.exponent);
};

export const toBaseAmount = (asset: Asset, amount: BigNumber): BigNumber => {
const displayUnit = findDisplayUnit(asset);
if (!displayUnit) {
return amount;
}
return amount.shiftedBy(displayUnit.exponent);
};

0 comments on commit 2b36b2a

Please sign in to comment.