Skip to content

Commit 82be6e3

Browse files
authored
Fix: payment values should use bigint (#132)
* Update payment and spend permission interfaces with subscription support * Apply code formatting * Minimize diff: Keep only hex to bigint value conversions - Reverted unnecessary refactoring of pay.test.ts - Reverted unnecessary type annotation in translatePayment.ts - Kept only the essential changes converting value from hex strings ('0x0') to bigint (0n) - Reduced diff from 511 lines to 79 lines * Use toHex(0n) in translatePayment for better clarity - Use bigint internally (0n) and convert to hex at JSON-RPC boundary - Add comment explaining the conversion for JSON-RPC compatibility - This follows modern best practices while maintaining compatibility * format
1 parent 1ce77ae commit 82be6e3

File tree

9 files changed

+34
-35
lines changed

9 files changed

+34
-35
lines changed

packages/account-sdk/src/interface/payment/charge.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('charge', () => {
4242
{
4343
to: '0xabc123' as any,
4444
data: '0xdef456' as any,
45-
value: '0x0' as any,
45+
value: 0n,
4646
},
4747
];
4848

@@ -205,12 +205,12 @@ describe('charge', () => {
205205
{
206206
to: '0xabc123' as any,
207207
data: '0xdef456' as any,
208-
value: '0x0' as any,
208+
value: 0n,
209209
},
210210
{
211211
to: '0xfed321' as any,
212212
data: '0xcba987' as any,
213-
value: '0x0' as any,
213+
value: 0n,
214214
},
215215
];
216216

@@ -252,7 +252,7 @@ describe('charge', () => {
252252
{
253253
to: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913' as any, // USDC address
254254
data: '0xtransferData' as any,
255-
value: '0x0' as any,
255+
value: 0n,
256256
},
257257
];
258258

@@ -306,7 +306,7 @@ describe('charge', () => {
306306
{
307307
to: '0x036CbD53842c5426634e7929541eC2318f3dCF7e' as any, // USDC testnet address
308308
data: '0xtransferData' as any,
309-
value: '0x0' as any,
309+
value: 0n,
310310
},
311311
];
312312

@@ -344,7 +344,7 @@ describe('charge', () => {
344344
{
345345
to: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913' as any, // USDC address
346346
data: '0xtransferData' as any,
347-
value: '0x0' as any,
347+
value: 0n,
348348
},
349349
];
350350

packages/account-sdk/src/interface/payment/charge.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,10 @@ export async function charge(options: ChargeOptions): Promise<ChargeResult> {
148148

149149
try {
150150
// Build the calls array for the smart wallet
151-
// Convert value from hex string to bigint if needed
152151
const calls = chargeCalls.map((call) => ({
153152
to: call.to,
154153
data: call.data,
155-
value: BigInt(call.value || '0x0'),
154+
value: call.value,
156155
}));
157156

158157
// For smart wallets, we can send all calls in a single user operation

packages/account-sdk/src/interface/payment/prepareCharge.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('prepareCharge', () => {
2222
} as SpendPermission;
2323

2424
const mockCallData: PrepareChargeResult = [
25-
{ to: '0xmock', data: '0xapprove', value: '0x0' },
26-
{ to: '0xmock', data: '0xspend', value: '0x0' },
25+
{ to: '0xmock', data: '0xapprove', value: 0n },
26+
{ to: '0xmock', data: '0xspend', value: 0n },
2727
];
2828

2929
it('should prepare charge for specific amount', async () => {

packages/account-sdk/src/interface/payment/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ export interface PrepareChargeCall {
215215
to: Address;
216216
/** The encoded call data */
217217
data: Hex;
218-
/** The value to send (always 0x0 for spend permissions) */
219-
value: '0x0';
218+
/** The value to send (always 0n for spend permissions) */
219+
value: bigint;
220220
}
221221

222222
/**

packages/account-sdk/src/interface/payment/utils/translatePayment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeFunctionData, parseUnits, type Address, type Hex } from 'viem';
1+
import { encodeFunctionData, parseUnits, toHex, type Address, type Hex } from 'viem';
22
import { CHAIN_IDS, ERC20_TRANSFER_ABI, TOKENS } from '../constants.js';
33
import type { PayerInfo } from '../types.js';
44

@@ -35,7 +35,7 @@ export function buildSendCallsRequest(transferData: Hex, testnet: boolean, payer
3535
const call = {
3636
to: usdcAddress as Address,
3737
data: transferData,
38-
value: '0x0' as Hex, // No ETH value for ERC20 transfer
38+
value: toHex(0n), // No ETH value for ERC20 transfer
3939
};
4040

4141
// Build the capabilities object

packages/account-sdk/src/interface/public-utilities/spend-permission/methods/prepareRevokeCallData.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('prepareRevokeCallData', () => {
7575
expect(result).toEqual({
7676
to: spendPermissionManagerAddress,
7777
data: mockEncodedData,
78-
value: '0x0',
78+
value: 0n,
7979
});
8080
});
8181

@@ -106,7 +106,7 @@ describe('prepareRevokeCallData', () => {
106106
it('should always set value to 0x0', async () => {
107107
const result = await prepareRevokeCallData(mockSpendPermission);
108108

109-
expect(result.value).toBe('0x0');
109+
expect(result.value).toBe(0n);
110110
});
111111

112112
it('should handle different spend permission structures', async () => {
@@ -143,7 +143,7 @@ describe('prepareRevokeCallData', () => {
143143
expect(result).toEqual({
144144
to: spendPermissionManagerAddress,
145145
data: differentEncodedData,
146-
value: '0x0',
146+
value: 0n,
147147
});
148148
});
149149

packages/account-sdk/src/interface/public-utilities/spend-permission/methods/prepareRevokeCallData.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { withTelemetry } from '../withTelemetry.js';
1010
type RevokeSpendPermissionResponse = {
1111
to: Address;
1212
data: Hex;
13-
value: '0x0'; // explicitly set to 0x0
13+
value: bigint;
1414
};
1515

1616
/**
@@ -40,7 +40,7 @@ type RevokeSpendPermissionResponse = {
4040
* const call = {
4141
* to,
4242
* data,
43-
* value: '0x0'
43+
* value: 0n
4444
* };
4545
* ```
4646
*/
@@ -57,7 +57,7 @@ const prepareRevokeCallDataFn = async (
5757
const response: RevokeSpendPermissionResponse = {
5858
to: spendPermissionManagerAddress,
5959
data,
60-
value: '0x0', // explicitly set to 0x0
60+
value: 0n,
6161
};
6262

6363
return response;

packages/account-sdk/src/interface/public-utilities/spend-permission/methods/prepareSpendCallData.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ describe('prepareSpendCallData', () => {
9292
expect(result[0]).toEqual({
9393
to: spendPermissionManagerAddress,
9494
data: '0xapprovedata123456',
95-
value: '0x0',
95+
value: 0n,
9696
});
9797
expect(result[1]).toEqual({
9898
to: spendPermissionManagerAddress,
9999
data: '0xspenddata789abc',
100-
value: '0x0',
100+
value: 0n,
101101
});
102102
});
103103

@@ -110,7 +110,7 @@ describe('prepareSpendCallData', () => {
110110
expect(result[0]).toEqual({
111111
to: spendPermissionManagerAddress,
112112
data: '0xspenddata789abc',
113-
value: '0x0',
113+
value: 0n,
114114
});
115115

116116
// Verify approve call is not made when permission is active
@@ -206,13 +206,13 @@ describe('prepareSpendCallData', () => {
206206
expect(typedResult[0]).toHaveProperty('to');
207207
expect(typedResult[0]).toHaveProperty('data');
208208
expect(typedResult[0]).toHaveProperty('value');
209-
expect(typedResult[0].value).toBe('0x0');
209+
expect(typedResult[0].value).toBe(0n);
210210

211211
// Check spend call structure
212212
expect(typedResult[1]).toHaveProperty('to');
213213
expect(typedResult[1]).toHaveProperty('data');
214214
expect(typedResult[1]).toHaveProperty('value');
215-
expect(typedResult[1].value).toBe('0x0');
215+
expect(typedResult[1].value).toBe(0n);
216216
});
217217

218218
it('should return calls with correct structure when permission is active', async () => {
@@ -228,7 +228,7 @@ describe('prepareSpendCallData', () => {
228228
expect(typedResult[0]).toHaveProperty('to');
229229
expect(typedResult[0]).toHaveProperty('data');
230230
expect(typedResult[0]).toHaveProperty('value');
231-
expect(typedResult[0].value).toBe('0x0');
231+
expect(typedResult[0].value).toBe(0n);
232232
});
233233

234234
it('should handle zero amount', async () => {
@@ -323,14 +323,14 @@ describe('prepareSpendCallData', () => {
323323
expect(result[0]).toEqual({
324324
to: spendPermissionManagerAddress,
325325
data: '0xspenddata789abc',
326-
value: '0x0',
326+
value: 0n,
327327
});
328328

329329
// Second call should be the ERC20 transfer
330330
expect(result[1]).toEqual({
331331
to: mockSpendPermission.permission.token,
332332
data: '0xtransferdata123',
333-
value: '0x0',
333+
value: 0n,
334334
});
335335

336336
// Verify encodeFunctionData was called with correct args for transfer
@@ -398,14 +398,14 @@ describe('prepareSpendCallData', () => {
398398

399399
const result = await prepareSpendCallData(mockSpendPermission, 'max-remaining-allowance');
400400

401-
expect(result[0].value).toBe('0x0');
402-
expect(result[1].value).toBe('0x0');
401+
expect(result[0].value).toBe(0n);
402+
expect(result[1].value).toBe(0n);
403403
});
404404

405405
it('should set value to 0x0 for spend call when permission is active', async () => {
406406
const result = await prepareSpendCallData(mockSpendPermission, 'max-remaining-allowance');
407407

408-
expect(result[0].value).toBe('0x0');
408+
expect(result[0].value).toBe(0n);
409409
});
410410

411411
it('should handle remaining spend of zero', async () => {

packages/account-sdk/src/interface/public-utilities/spend-permission/methods/prepareSpendCallData.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getPermissionStatus } from './getPermissionStatus.js';
1212
type Call = {
1313
to: Address;
1414
data: Hex;
15-
value: '0x0'; // explicitly set to 0x0
15+
value: bigint;
1616
};
1717

1818
export type PrepareSpendCallDataResponseType = Call[];
@@ -133,7 +133,7 @@ const prepareSpendCallDataFn = async (
133133
approveCall = {
134134
to: spendPermissionManagerAddress,
135135
data: approveData,
136-
value: '0x0', // explicitly set to 0x0
136+
value: 0n,
137137
};
138138
}
139139

@@ -145,7 +145,7 @@ const prepareSpendCallDataFn = async (
145145
const spendCall: Call = {
146146
to: spendPermissionManagerAddress,
147147
data: spendData,
148-
value: '0x0', // explicitly set to 0x0
148+
value: 0n,
149149
};
150150

151151
const calls: Call[] = [approveCall, spendCall].filter((item) => item !== null);
@@ -163,7 +163,7 @@ const prepareSpendCallDataFn = async (
163163
calls.push({
164164
to: permission.permission.token as Address,
165165
data: transferCallData,
166-
value: '0x0',
166+
value: 0n,
167167
});
168168
}
169169

0 commit comments

Comments
 (0)