Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Add fee-on-transfer token fees fields (#83)
Browse files Browse the repository at this point in the history
* Token construct supports FOT fees

* Fix code style issues with Prettier

* delete .idea folders

---------

Co-authored-by: Siyu Jiang <[email protected]>
Co-authored-by: Lint Action <[email protected]>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 134fbf5 commit 2f7a765
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/entities/token.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Token } from './token'
import { BigNumber } from '@ethersproject/bignumber'

describe('Token', () => {
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001'
Expand All @@ -20,6 +21,14 @@ describe('Token', () => {
it('fails with non-integer decimals', () => {
expect(() => new Token(3, ADDRESS_ONE, 1.5).address).toThrow('DECIMALS')
})
it('fails with negative FOT fees', () => {
expect(
() => new Token(3, ADDRESS_ONE, 18, undefined, undefined, undefined, BigNumber.from(-1), undefined)
).toThrow('NON-NEGATIVE FOT FEES')
expect(
() => new Token(3, ADDRESS_ONE, 18, undefined, undefined, undefined, undefined, BigNumber.from(-1))
).toThrow('NON-NEGATIVE FOT FEES')
})
})

describe('#constructor with bypassChecksum = true', () => {
Expand Down
22 changes: 21 additions & 1 deletion src/entities/token.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumber } from '@ethersproject/bignumber'
import invariant from 'tiny-invariant'
import { checkValidAddress, validateAndParseAddress } from '../utils/validateAndParseAddress'
import { BaseCurrency } from './baseCurrency'
Expand All @@ -15,6 +16,13 @@ export class Token extends BaseCurrency {
*/
public readonly address: string

/**
* Relevant for fee-on-transfer (FOT) token taxes,
* Not every ERC20 token is FOT token, so this field is optional
*/
public readonly buyFeeBps?: BigNumber
public readonly sellFeeBps?: BigNumber

/**
*
* @param chainId {@link BaseCurrency#chainId}
Expand All @@ -23,21 +31,33 @@ export class Token extends BaseCurrency {
* @param symbol {@link BaseCurrency#symbol}
* @param name {@link BaseCurrency#name}
* @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
* @param buyFeeBps Buy fee tax for FOT tokens, in basis points
* @param sellFeeBps Sell fee tax for FOT tokens, in basis points
*/
public constructor(
chainId: number,
address: string,
decimals: number,
symbol?: string,
name?: string,
bypassChecksum?: boolean
bypassChecksum?: boolean,
buyFeeBps?: BigNumber,
sellFeeBps?: BigNumber
) {
super(chainId, decimals, symbol, name)
if (bypassChecksum) {
this.address = checkValidAddress(address)
} else {
this.address = validateAndParseAddress(address)
}
if (buyFeeBps) {
invariant(buyFeeBps.gte(BigNumber.from(0)), 'NON-NEGATIVE FOT FEES')
}
if (sellFeeBps) {
invariant(sellFeeBps.gte(BigNumber.from(0)), 'NON-NEGATIVE FOT FEES')
}
this.buyFeeBps = buyFeeBps
this.sellFeeBps = sellFeeBps
}

/**
Expand Down

0 comments on commit 2f7a765

Please sign in to comment.