@compass/utils

This package provides utility functions for handling Ether values. The functions are bound from the ethers.js package and are used to parse and format Ether and unit values as well as get the current gas fee data.

Usage

import utils from "@compass/utils";

API Reference

utils.parseEther(value: string)

Parse a decimal value to a BigNumber.

  • value (required, string): The value to parse.

Returns a string representation of the BigNumber.

utils.parseUnits(value: string, unit: string)

Parse a decimal value to a BigNumber.

  • value (required, string): The value to parse.

  • unit (required, string): The unit to parse the value to.

Returns a string representation of the BigNumber.

utils.formatEther(value: string)

Format a BigNumber to a decimal value.

  • value (required, string): The value to format.

Returns a string representation of the formatted value.

utils.formatUnits(value: string, unit: string)

Format a BigNumber to a decimal value.

  • value (required, string): The value to format.

  • unit (required, string): The unit to format the value to.

Returns a string representation of the formatted value.

utils.getFeeData()

Get the current gas fee data.

Returns a Promise that resolves with an object containing gasPrice, maxFeePerGas, and maxPriorityFeePerGas.

Examples

To parse a decimal value to a BigNumber:

import utils from "@compass/utils";

export default async function() {
    const value = '1.5';
    const parsedEther = utils.parseEther(value);
    return { parsedEther };
}

To parse a decimal value to a specific unit:

import utils from "@compass/utils";

export default async function() {
    const value = '1000';
    const unit = 'wei';
    const parsedUnits = utils.parseUnits(value, unit);
    return { parsedUnits };
}

To format a BigNumber to a decimal value:

import utils from "@compass/utils";

export default async function() {
    const value = '1.5';
    const formattedEther = utils.formatEther(value);
    return { formattedEther };
}

To format a BigNumber to a specific unit:

import utils from "@compass/utils";

export default async function() {
    const value = '1000';
    const unit = 'wei';
    const formattedUnits = utils.formatUnits(value, unit);
    return { formattedUnits };
}

To get the current gas fee data:

import utils from "@compass/utils";

export default async function() {
    const feeData = await utils.getFeeData();
    return { feeData };
}

Last updated