Git Product home page Git Product logo

wbnumber's Introduction

wbnumber

This is a bignumber.js-based library.

It is intended for use in web3 (crypto/blockchain) environments where it is important to always keep track of both "value" and "decimals".

Usage

import WBN from 'wbnumber'

const amount = WBN.fromEth('1.23456789', 18)

console.log(amount.toWei()) // 1234567890000000000

API

WBN.fromEth(valueEth: string, decimals: number): WBN

Creates an instance of WBN from a string representing a value in ETH (fractional value after applying decimals).

Use this to parse user input.

Example:

export const SendTokenForm: React.FC = () =>
{
	const [selectedToken, setSelectedToken] = useState({
		name: "Ether",
		symbol: "ETH",
		decimals: 18,
	})
	const [inputValue, setInputValue] = useState("")

	const amount = WBN.fromEth(inputValue, selectedToken.decimals)

	return (
		<div>
			<input
				type="text"
				value={inputValue}
				onChange={e => setInputValue(e.target.value)}
			/>
			<span>amount in wei: {amount.toWei()}</span>
		</div>
	)
}

WBN.fromWei(valueWei: string, decimals: number): WBN

Creates an instance of WBN from a string representing a value in WEI (integer value before applying decimals).

Use this to parse blockchain event values etc.

type TokenTransferEvent = {
	from: string
	to: string
	value: string
}
type TokenInfo = {
	name: string
	symbol: string
	decimals: number
}
export const EventInfo: React.FC<{ event: TokenTransferEvent, token: TokenInfo }> = ({ event, token }) =>
{
	const amount = WBN.fromWei(event.value, token.decimals)

	return (
		<div>
			Sent {amount.toEth()} {token.symbol} from {event.from} to {event.to}
		</div>
	)
}

wbnumber's People

Contributors

zlumer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.