Skip to content

Commit

Permalink
cleanup README
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Apr 20, 2023
1 parent 3188bd5 commit a90e99a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions test/erc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit a90e99a

Please sign in to comment.