Skip to content

Commit c0d077d

Browse files
authored
fix(checkout): GFI-98: Fix token mismatch errors (#2554)
1 parent 2c6f5ef commit c0d077d

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

packages/internal/dex/sdk/src/lib/utils.test.ts

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { 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

517
jest.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
});

packages/internal/dex/sdk/src/lib/utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ export const isNativeAmount = (amount: CoinAmount<Coin>): amount is CoinAmount<N
109109

110110
export const isNative = (token: Coin): token is Native => token.type === 'native';
111111

112-
export const addERC20Amount = (a: CoinAmount<ERC20>, b: CoinAmount<ERC20>) => {
112+
const addERC20Amount = (a: CoinAmount<ERC20>, b: CoinAmount<ERC20>) => {
113113
// Make sure the ERC20s have the same address
114-
if (a.token.address !== b.token.address) throw new Error('Token mismatch: token addresses must be the same');
114+
if (a.token.address.toLowerCase() !== b.token.address.toLowerCase()) {
115+
throw new Error('Token mismatch: token addresses must be the same');
116+
}
115117
return { value: a.value + b.value, token: a.token };
116118
};
117119

@@ -132,9 +134,11 @@ export const addAmount = <T extends Coin>(a: CoinAmount<T>, b: CoinAmount<T>) =>
132134
throw new Error('Token mismatch: token types must be the same');
133135
};
134136

135-
export const subtractERC20Amount = (a: CoinAmount<ERC20>, b: CoinAmount<ERC20>) => {
137+
const subtractERC20Amount = (a: CoinAmount<ERC20>, b: CoinAmount<ERC20>) => {
136138
// Make sure the ERC20s have the same address
137-
if (a.token.address !== b.token.address) throw new Error('Token mismatch: token addresses must be the same');
139+
if (a.token.address.toLowerCase() !== b.token.address.toLowerCase()) {
140+
throw new Error('Token mismatch: token addresses must be the same');
141+
}
138142
return { value: a.value - b.value, token: a.token };
139143
};
140144

0 commit comments

Comments
 (0)