add utils

This commit is contained in:
Andrea Franz 2020-03-30 17:40:45 +02:00
parent fa1f796695
commit 003c875590
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
2 changed files with 21 additions and 18 deletions

View File

@ -12,9 +12,7 @@ import {
ERROR_LOADING_GIFT,
ERROR_GIFT_NOT_FOUND,
} from '../actions/bucket';
import Web3Utils from "web3-utils";
const BN = Web3Utils.BN;
import { toBaseUnit } from "../utils";
const errorMessage = (error: BucketError): string => {
switch (error.type) {
@ -29,21 +27,6 @@ const errorMessage = (error: BucketError): string => {
}
}
const toBaseUnit = (fullAmount: string, decimalsSize: number, roundDecimals: number) => {
const amount = new BN(fullAmount);
const base = new BN(10).pow(new BN(decimalsSize));
const whole = amount.div(base).toString();
let decimals = amount.mod(base).toString();
for (let i = decimals.length; i < decimalsSize; i++) {
decimals = `0${decimals}`;
}
const full = `${whole}.${decimals}`;
const rounded = `${whole}.${decimals.slice(0, roundDecimals)}`;
return [full, rounded];
}
export default function(ownProps: any) {
const dispatch = useDispatch()
const match = useRouteMatch({
@ -99,5 +82,7 @@ export default function(ownProps: any) {
Display Amount: {displayAmount} <br />
Rounded Display Amount: {roundedDisplayAmount} <br />
Receiver: {props.receiver} <br />
<br /><br /><br />
</>;
}

18
app/js/utils.ts Normal file
View File

@ -0,0 +1,18 @@
import Web3Utils from "web3-utils";
const BN = Web3Utils.BN;
export const toBaseUnit = (fullAmount: string, decimalsSize: number, roundDecimals: number) => {
const amount = new BN(fullAmount);
const base = new BN(10).pow(new BN(decimalsSize));
const whole = amount.div(base).toString();
let decimals = amount.mod(base).toString();
for (let i = decimals.length; i < decimalsSize; i++) {
decimals = `0${decimals}`;
}
const full = `${whole}.${decimals}`;
const rounded = `${whole}.${decimals.slice(0, roundDecimals)}`;
return [full, rounded];
}