This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change how we represent currency amounts (#3)
* change how we represent currency amounts * fix unit tests * some unit tests for the printing methods * more tests * add 14.x * address comments * fix failing unit test * numerator
- Loading branch information
1 parent
42b315a
commit fd37337
Showing
9 changed files
with
179 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,115 @@ | ||
import JSBI from 'jsbi' | ||
import { ChainId, MaxUint256 } from '../../constants' | ||
import { ETHER } from '../ether' | ||
import { Token } from '../token' | ||
import CurrencyAmount from './currencyAmount' | ||
import Percent from './percent' | ||
|
||
describe('CurrencyAmount', () => { | ||
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001' | ||
|
||
describe('constructor', () => { | ||
it('works', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18) | ||
const amount = new CurrencyAmount(token, 100) | ||
expect(amount.raw).toEqual(JSBI.BigInt(100)) | ||
const amount = CurrencyAmount.fromRawAmount(token, 100) | ||
expect(amount.quotient).toEqual(JSBI.BigInt(100)) | ||
}) | ||
}) | ||
|
||
describe('#quotient', () => { | ||
it('returns the amount after multiplication', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18) | ||
const amount = CurrencyAmount.fromRawAmount(token, 100).multiply(new Percent(15, 100)) | ||
expect(amount.quotient).toEqual(JSBI.BigInt(15)) | ||
}) | ||
}) | ||
|
||
describe('#ether', () => { | ||
it('produces ether amount', () => { | ||
const amount = CurrencyAmount.ether(100) | ||
expect(amount.raw).toEqual(JSBI.BigInt(100)) | ||
expect(amount.quotient).toEqual(JSBI.BigInt(100)) | ||
expect(amount.currency).toEqual(ETHER) | ||
}) | ||
}) | ||
|
||
it('token amount can be max uint256', () => { | ||
const amount = new CurrencyAmount(new Token(ChainId.MAINNET, ADDRESS_ONE, 18), MaxUint256) | ||
expect(amount.raw).toEqual(MaxUint256) | ||
const amount = CurrencyAmount.fromRawAmount(new Token(ChainId.MAINNET, ADDRESS_ONE, 18), MaxUint256) | ||
expect(amount.quotient).toEqual(MaxUint256) | ||
}) | ||
it('token amount cannot exceed max uint256', () => { | ||
expect(() => | ||
CurrencyAmount.fromRawAmount(new Token(ChainId.MAINNET, ADDRESS_ONE, 18), JSBI.add(MaxUint256, JSBI.BigInt(1))) | ||
).toThrow('AMOUNT') | ||
}) | ||
it('token amount quotient cannot exceed max uint256', () => { | ||
expect(() => | ||
CurrencyAmount.fromFractionalAmount( | ||
new Token(ChainId.MAINNET, ADDRESS_ONE, 18), | ||
JSBI.add(JSBI.multiply(MaxUint256, JSBI.BigInt(2)), JSBI.BigInt(2)), | ||
JSBI.BigInt(2) | ||
) | ||
).toThrow('AMOUNT') | ||
}) | ||
it('token amount numerator can be gt. uint256 if denominator is gt. 1', () => { | ||
const amount = CurrencyAmount.fromFractionalAmount( | ||
new Token(ChainId.MAINNET, ADDRESS_ONE, 18), | ||
JSBI.add(MaxUint256, JSBI.BigInt(2)), | ||
2 | ||
) | ||
expect(amount.numerator).toEqual(JSBI.add(JSBI.BigInt(2), MaxUint256)) | ||
}) | ||
|
||
describe('#toFixed', () => { | ||
it('throws for decimals > currency.decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 1000) | ||
expect(() => amount.toFixed(3)).toThrow('DECIMALS') | ||
}) | ||
it('is correct for 0 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 123456) | ||
expect(amount.toFixed(0)).toEqual('123456') | ||
}) | ||
it('is correct for 18 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18) | ||
const amount = CurrencyAmount.fromRawAmount(token, 1e15) | ||
expect(amount.toFixed(9)).toEqual('0.001000000') | ||
}) | ||
}) | ||
|
||
describe('#toSignificant', () => { | ||
it('does not throw for sig figs > currency.decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 1000) | ||
expect(amount.toSignificant(3)).toEqual('1000') | ||
}) | ||
it('is correct for 0 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 123456) | ||
expect(amount.toSignificant(4)).toEqual('123400') | ||
}) | ||
it('is correct for 18 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18) | ||
const amount = CurrencyAmount.fromRawAmount(token, 1e15) | ||
expect(amount.toSignificant(9)).toEqual('0.001') | ||
}) | ||
}) | ||
|
||
describe('#toExact', () => { | ||
it('does not throw for sig figs > currency.decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 1000) | ||
expect(amount.toExact()).toEqual('1000') | ||
}) | ||
it('is correct for 0 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 0) | ||
const amount = CurrencyAmount.fromRawAmount(token, 123456) | ||
expect(amount.toExact()).toEqual('123456') | ||
}) | ||
it('is correct for 18 decimals', () => { | ||
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18) | ||
const amount = CurrencyAmount.fromRawAmount(token, 123e13) | ||
expect(amount.toExact()).toEqual('0.00123') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import { ChainId } from '../constants' | ||
import { CurrencyAmount, ETHER, Token, WETH9 } from '../entities' | ||
import { CurrencyAmount, Token, WETH9 } from '../entities' | ||
import { wrappedCurrencyAmount } from './wrappedCurrencyAmount' | ||
|
||
describe('#wrappedCurrencyAmount', () => { | ||
const token = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18) | ||
|
||
it('wraps ether', () => { | ||
expect(wrappedCurrencyAmount(new CurrencyAmount(ETHER, 10), ChainId.RINKEBY)).toEqual( | ||
new CurrencyAmount(WETH9[ChainId.RINKEBY], 10) | ||
expect(wrappedCurrencyAmount(CurrencyAmount.ether(10), ChainId.RINKEBY)).toEqual( | ||
CurrencyAmount.fromRawAmount(WETH9[ChainId.RINKEBY], 10) | ||
) | ||
}) | ||
it('does nothing to tokens', () => { | ||
expect(wrappedCurrencyAmount(new CurrencyAmount(token, 10), ChainId.MAINNET)).toEqual(new CurrencyAmount(token, 10)) | ||
expect(wrappedCurrencyAmount(CurrencyAmount.fromRawAmount(token, 10), ChainId.MAINNET)).toEqual( | ||
CurrencyAmount.fromRawAmount(token, 10) | ||
) | ||
}) | ||
it('throws if different network ', () => { | ||
expect(() => wrappedCurrencyAmount(new CurrencyAmount(token, 10), ChainId.RINKEBY)).toThrow('CHAIN_ID') | ||
expect(() => wrappedCurrencyAmount(CurrencyAmount.fromRawAmount(token, 10), ChainId.RINKEBY)).toThrow('CHAIN_ID') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters