11import { TransactionRequest , ZeroAddress } from 'ethers' ;
2- import { TEST_FROM_ADDRESS , USDC_TEST_TOKEN , nativeTokenService } from '../test/utils' ;
3- import { decimalsFunctionSig , getTokenDecimals , isValidNonZeroAddress } from './utils' ;
2+ import {
3+ TEST_FROM_ADDRESS ,
4+ USDC_TEST_TOKEN ,
5+ WETH_TEST_TOKEN ,
6+ nativeTokenService ,
7+ } from '../test/utils' ;
8+ import {
9+ addAmount ,
10+ decimalsFunctionSig ,
11+ getTokenDecimals ,
12+ isValidNonZeroAddress ,
13+ newAmount ,
14+ subtractAmount ,
15+ } from './utils' ;
416
517jest . mock ( 'ethers' , ( ) => ( {
618 ...jest . requireActual ( 'ethers' ) ,
@@ -37,7 +49,11 @@ describe('utils', () => {
3749 describe ( 'getTokenDecimals' , ( ) => {
3850 describe ( 'when token is native' , ( ) => {
3951 it ( 'should return default native token decimals' , async ( ) => {
40- const decimals = await getTokenDecimals ( 'native' , provider as any , nativeTokenService . nativeToken ) ;
52+ const decimals = await getTokenDecimals (
53+ 'native' ,
54+ provider as any ,
55+ nativeTokenService . nativeToken ,
56+ ) ;
4157 expect ( decimals ) . toEqual ( 18 ) ;
4258 } ) ;
4359 } ) ;
@@ -53,4 +69,72 @@ describe('utils', () => {
5369 } ) ;
5470 } ) ;
5571 } ) ;
72+
73+ describe ( 'addAmount' , ( ) => {
74+ it ( 'adds ERC20 amounts' , ( ) => {
75+ const amount1 = newAmount ( 100n , USDC_TEST_TOKEN ) ;
76+ const amount2 = newAmount ( 200n , USDC_TEST_TOKEN ) ;
77+ const result = addAmount ( amount1 , amount2 ) ;
78+ expect ( result ) . toEqual ( { value : 300n , token : USDC_TEST_TOKEN } ) ;
79+ } ) ;
80+
81+ it ( 'throws when adding different ERC20 tokens' , ( ) => {
82+ const amount1 = newAmount ( 100n , USDC_TEST_TOKEN ) ;
83+ const amount2 = newAmount ( 200n , WETH_TEST_TOKEN ) ;
84+ expect ( ( ) => addAmount ( amount1 , amount2 ) ) . toThrow ( ) ;
85+ } ) ;
86+
87+ it ( 'adds amounts when the addresses differ only by case' , ( ) => {
88+ const amount1 = newAmount ( 100n , {
89+ ...USDC_TEST_TOKEN ,
90+ address : USDC_TEST_TOKEN . address . toUpperCase ( ) ,
91+ } ) ;
92+ const amount2 = newAmount ( 200n , {
93+ ...USDC_TEST_TOKEN ,
94+ address : USDC_TEST_TOKEN . address . toLowerCase ( ) ,
95+ } ) ;
96+ const result = addAmount ( amount1 , amount2 ) ;
97+ expect ( result ) . toEqual ( {
98+ value : 300n ,
99+ token : {
100+ ...USDC_TEST_TOKEN ,
101+ address : USDC_TEST_TOKEN . address . toUpperCase ( ) ,
102+ } ,
103+ } ) ;
104+ } ) ;
105+ } ) ;
106+
107+ describe ( 'subtractAmount' , ( ) => {
108+ it ( 'subtracts ERC20 amounts' , ( ) => {
109+ const amount1 = newAmount ( 200n , USDC_TEST_TOKEN ) ;
110+ const amount2 = newAmount ( 100n , USDC_TEST_TOKEN ) ;
111+ const result = subtractAmount ( amount1 , amount2 ) ;
112+ expect ( result ) . toEqual ( { value : 100n , token : USDC_TEST_TOKEN } ) ;
113+ } ) ;
114+
115+ it ( 'throws when subtracting different ERC20 tokens' , ( ) => {
116+ const amount1 = newAmount ( 200n , USDC_TEST_TOKEN ) ;
117+ const amount2 = newAmount ( 100n , WETH_TEST_TOKEN ) ;
118+ expect ( ( ) => subtractAmount ( amount1 , amount2 ) ) . toThrow ( ) ;
119+ } ) ;
120+
121+ it ( 'subtracts amounts when the addresses differ only by case' , ( ) => {
122+ const amount1 = newAmount ( 200n , {
123+ ...USDC_TEST_TOKEN ,
124+ address : USDC_TEST_TOKEN . address . toUpperCase ( ) ,
125+ } ) ;
126+ const amount2 = newAmount ( 100n , {
127+ ...USDC_TEST_TOKEN ,
128+ address : USDC_TEST_TOKEN . address . toLowerCase ( ) ,
129+ } ) ;
130+ const result = subtractAmount ( amount1 , amount2 ) ;
131+ expect ( result ) . toEqual ( {
132+ value : 100n ,
133+ token : {
134+ ...USDC_TEST_TOKEN ,
135+ address : USDC_TEST_TOKEN . address . toUpperCase ( ) ,
136+ } ,
137+ } ) ;
138+ } ) ;
139+ } ) ;
56140} ) ;
0 commit comments