Skip to content

Commit dcc4be1

Browse files
committed
update headers setup for account API and balance formating
1 parent 6255a32 commit dcc4be1

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

packages/gator-permissions-snap/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MESSAGE_SIGNING_SNAP_ID=local:http://localhost:8080
2121
SNAP_ENV=production
2222
STORE_PERMISSIONS_ENABLED=false
2323
PRICE_API_BASE_URL=https://price.api.cx.metamask.io
24-
ACCOUNT_API_BASE_URL=https://account.api.cx.metamask.io
24+
ACCOUNT_API_BASE_URL=https://accounts.api.cx.metamask.io
2525
TOKENS_API_BASE_URL=https://tokens.api.cx.metamask.io
2626
KERNEL_SNAP_ID=npm:@metamask/permissions-kernel-snap
2727
MESSAGE_SIGNING_SNAP_ID=npm:@metamask/message-signing-snap

packages/gator-permissions-snap/src/clients/accountApiClient.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { z } from 'zod';
77
import { ZERO_ADDRESS } from '../constants';
88
import type { TokenBalanceAndMetadata, RetryOptions } from './types';
99
import { makeValidatedRequestWithRetry } from '../utils/httpClient';
10+
import { parseUnits } from '../utils/value';
1011

1112
/**
1213
* Zod schema for validating individual token balance item
@@ -185,10 +186,13 @@ export class AccountApiClient {
185186
}
186187

187188
// Convert balance string to BigInt
188-
// The balance comes as a decimal string in the smallest unit (e.g., wei for ETH, smallest unit for tokens)
189-
// We need to handle the decimal part properly
190-
const balanceValue = parseFloat(tokenData.balance);
191-
return BigInt(Math.floor(balanceValue));
189+
// The balance comes as a decimal string in human-readable format (e.g., "1.5" for 1.5 ETH)
190+
// We need to convert it to the smallest unit by multiplying by 10^decimals
191+
const balanceInSmallestUnit = parseUnits({
192+
formatted: tokenData.balance,
193+
decimals: tokenData.decimals,
194+
});
195+
return balanceInSmallestUnit;
192196
}
193197

194198
/**
@@ -299,6 +303,10 @@ export class AccountApiClient {
299303
timeoutMs: this.#timeoutMs,
300304
maxResponseSizeBytes: this.#maxResponseSizeBytes,
301305
fetch: this.#fetch,
306+
headers: {
307+
'x-mmcx-internal-application': 'gator-permissions-snap',
308+
'x-metamask-clientproduct': 'gator-permissions-snap',
309+
},
302310
},
303311
TokenBalanceResponseSchema,
304312
retryOptions,

packages/gator-permissions-snap/src/utils/httpClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type HttpClientConfig = {
1616
timeoutMs: number;
1717
maxResponseSizeBytes: number;
1818
fetch?: typeof globalThis.fetch;
19+
headers?: Record<string, string>;
1920
};
2021

2122
/**
@@ -89,9 +90,9 @@ async function makeValidatedRequest<
8990
response = await fetch(url, {
9091
signal: controller.signal,
9192
headers: {
93+
...config.headers,
9294
Accept: 'application/json',
9395
'User-Agent': 'MetaMask-Snap/1.0',
94-
'x-mmcx-internal-application': 'gator-permissions-snap',
9596
},
9697
});
9798
} catch (error) {

packages/gator-permissions-snap/test/client/accountApiClient.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('AccountApiClient', () => {
3737
name: 'Ethereum',
3838
type: 'native',
3939
occurrences: 100,
40-
balance: '1000000000000000000',
40+
balance: '1.0',
4141
chainId: mockChainId,
4242
},
4343
],
@@ -58,6 +58,7 @@ describe('AccountApiClient', () => {
5858
headers: {
5959
Accept: 'application/json',
6060
'User-Agent': 'MetaMask-Snap/1.0',
61+
'x-metamask-clientproduct': 'gator-permissions-snap',
6162
'x-mmcx-internal-application': 'gator-permissions-snap',
6263
},
6364
signal: expect.any(AbortSignal),
@@ -121,7 +122,6 @@ describe('AccountApiClient', () => {
121122
headers: {
122123
Accept: 'application/json',
123124
'User-Agent': 'MetaMask-Snap/1.0',
124-
'x-mmcx-internal-application': 'gator-permissions-snap',
125125
},
126126
signal: expect.any(AbortSignal),
127127
},
@@ -202,7 +202,7 @@ describe('AccountApiClient', () => {
202202
name: 'Ethereum',
203203
type: 'native',
204204
occurrences: 100,
205-
balance: '1000000000000000000',
205+
balance: '1.0',
206206
chainId: mockChainId,
207207
},
208208
],
@@ -258,7 +258,7 @@ describe('AccountApiClient', () => {
258258
name: 'Dai Stablecoin',
259259
type: 'erc20',
260260
occurrences: 100,
261-
balance: '2000000000000000000',
261+
balance: '2.0',
262262
chainId: mockChainId,
263263
},
264264
],
@@ -357,7 +357,7 @@ describe('AccountApiClient', () => {
357357
name: 'Dai Stablecoin',
358358
type: 'erc20',
359359
occurrences: 100,
360-
balance: '1000000000000000000',
360+
balance: '1.0',
361361
chainId: mockChainId,
362362
},
363363
],
@@ -437,7 +437,7 @@ describe('AccountApiClient', () => {
437437
name: 'Ethereum',
438438
type: 'native',
439439
occurrences: 100,
440-
balance: '1000000000000000000',
440+
balance: '1.0',
441441
chainId: mockChainId,
442442
},
443443
],
@@ -523,7 +523,7 @@ describe('AccountApiClient', () => {
523523
name: 'Some Token',
524524
type: 'unsupported_type',
525525
occurrences: 100,
526-
balance: '1000000000000000000',
526+
balance: '1.0',
527527
chainId: mockChainId,
528528
},
529529
],
@@ -603,7 +603,7 @@ describe('AccountApiClient', () => {
603603
name: 'Ethereum',
604604
type: 'native',
605605
occurrences: 100,
606-
balance: '1000000000000000000',
606+
balance: '1.0',
607607
chainId: mockChainId,
608608
},
609609
],
@@ -682,7 +682,7 @@ describe('AccountApiClient', () => {
682682
name: 'Ethereum',
683683
type: 'native',
684684
occurrences: 100,
685-
balance: '1000000000000000000',
685+
balance: '1.0',
686686
chainId: mockChainId,
687687
},
688688
],
@@ -830,7 +830,7 @@ describe('AccountApiClient', () => {
830830
name: 'Ethereum',
831831
type: 'native',
832832
occurrences: 100,
833-
balance: '1000000000000000000',
833+
balance: '1.0',
834834
chainId: mockChainId,
835835
},
836836
],
@@ -881,7 +881,7 @@ describe('AccountApiClient', () => {
881881
name: 'Ethereum',
882882
type: 'native',
883883
occurrences: 100,
884-
balance: '1000000000000000000',
884+
balance: '1.0',
885885
chainId: mockChainId,
886886
},
887887
],

0 commit comments

Comments
 (0)