diff --git a/README.md b/README.md index 0192cca..9bee01b 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ import { Token, TokenAmount, expandToNDecimals } from 'erc-token-js' Create a new token from an object: ```javascript -const token = Token.from({ +const tknToken = Token.from({ chainId: 1, address: '0x...', name: 'Token Name', @@ -106,8 +106,8 @@ console.log(amountB.toString()) // "USDC 0.01" ### Perform arithmetic operations on token amounts ```javascript -const amountA = TokenAmount.from(token, 3) -const amountB = TokenAmount.from(token, 2) +const amountA = TokenAmount.from(tknToken, 3) +const amountB = TokenAmount.from(tknToken, 2) const sum = amountA.add(amountB) // '5.00 TKN' const difference = amountA.sub(amountB) // '1.00 TKN' @@ -137,7 +137,7 @@ In the previous example, using 6n or BigInt(6) provides the expected result, whi ### Format token amounts for display ```javascript -const formattedAmount = amount.toString() // '123.00 TKN' +const formattedAmount = amount.toString() // '123.00 USDC' ``` ## Serialization and Deserialization diff --git a/src/index.js b/src/index.js index 4bb3f10..e3308a3 100644 --- a/src/index.js +++ b/src/index.js @@ -55,6 +55,11 @@ export class Token { this.formatOptions = formatOptions } + // return number + asFloat(number, decimals = this.decimals) { + return ethers.formatUnits(number, decimals) + } + // return string format(number, decimals = this.decimals) { return this.#iformat @@ -167,6 +172,10 @@ export class TokenAmount { this.tags = [...this.tags, tag] } + toFloat() { + return this.token.asFloat(this.number) + } + toFormat() { return this.token.format(this.number) } diff --git a/test/erc.spec.js b/test/erc.spec.js index 5ff4b53..c4dad8f 100644 --- a/test/erc.spec.js +++ b/test/erc.spec.js @@ -157,6 +157,14 @@ describe('TokenERC20Amount', function () { }) // TODO +describe('Misc', function () { + it('xxx', async function () { + console.log(ethers.formatUnits('1000000000000', 18)) + const amountA = await TokenAmount.from(usdc, 3) + + console.log(amountA.toFloat()) + }) +}) xdescribe('TokenAmountSet', function () { it('xxx', async function () {