-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43f4e99
commit 5b9d084
Showing
2 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { fromMAS } from '@massalabs/web3-utils'; | ||
import { massaToken } from '../massa-react/const'; | ||
import { handlePercent } from './handlePercent'; | ||
|
||
describe('handlePercent', () => { | ||
it('should return correctly formatted amount when no conditions are met', () => { | ||
const result = handlePercent( | ||
1000000000000000000n, | ||
25n, | ||
0n, | ||
2000000000000000000n, | ||
18, | ||
'ETH', | ||
); | ||
expect(result).toBe('0.250000000000000000'); | ||
}); | ||
|
||
it('should return correctly formatted amount when symbol is massaToken and newAmount is within balance', () => { | ||
const result = handlePercent( | ||
1000000000n, | ||
50n, | ||
fromMAS(0.1), | ||
1000000000000n, | ||
9, | ||
massaToken, | ||
); | ||
expect(result).toBe('0.500000000'); | ||
}); | ||
|
||
it('should return 0 when balance minus fees is less than 0', () => { | ||
const result = handlePercent( | ||
1000000000n, | ||
100n, | ||
fromMAS(30000), | ||
2000n, | ||
9, | ||
massaToken, | ||
); | ||
expect(result).toBe('0.000000000'); | ||
}); | ||
|
||
it('should return balance minus fees when newAmount exceeds balance', () => { | ||
const result = handlePercent( | ||
1000000000n, | ||
100n, | ||
fromMAS(0.1), | ||
1000000000n, | ||
9, | ||
massaToken, | ||
); | ||
expect(result).toBe('0.900000000'); | ||
}); | ||
|
||
it('should handle zero amount correctly', () => { | ||
const result = handlePercent(0n, 10n, 0n, 1000n, 9, massaToken); | ||
expect(result).toBe('0.000000000'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { massaToken } from '../massa-react/const'; | ||
import { formatAmount } from './parseAmount'; | ||
|
||
export function handlePercent( | ||
amount = 0n, | ||
percent: bigint, | ||
fees: bigint, | ||
balance: bigint, | ||
decimals: number, | ||
symbol: string, | ||
): string { | ||
let newAmount = (amount * percent) / 100n; | ||
|
||
if (symbol === massaToken) { | ||
if (newAmount > balance - fees) { | ||
if (balance - fees < 0) { | ||
newAmount = 0n; | ||
} else { | ||
newAmount = balance - fees; | ||
} | ||
} | ||
} | ||
|
||
return formatAmount(newAmount.toString(), decimals).amountFormattedFull; | ||
} |