Skip to content

Commit 208d78c

Browse files
fix(stripe): paymentIntent amount is now scale 2 (#1987)
* fix(stripe): paymentIntent amount is now scale 2 * refactor(stripe): applyScale util function now uses default scale * chore(stripe): format
1 parent 741647d commit 208d78c

4 files changed

Lines changed: 41 additions & 6 deletions

File tree

packages/wallet/backend/src/stripe-integration/service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { StripeWebhookType } from './validation'
77
import { WalletAddressService } from '../walletAddress/service'
88
import { AccountService } from '../account/service'
99
import { Transaction } from '../transaction/model'
10-
import { transformBalance } from '../utils/helpers'
10+
import { transformBalance, applyScale } from '../utils/helpers'
1111

1212
export enum EventType {
1313
payment_intent_canceled = 'payment_intent.canceled',
@@ -49,7 +49,8 @@ export class StripeService implements IStripeService {
4949
const metadata = paymentIntent.metadata
5050
const receiving_address: string = metadata.receiving_address
5151
const currency: string = paymentIntent.currency
52-
const amount: number = paymentIntent.amount
52+
53+
const scaledAmount = applyScale(paymentIntent.amount)
5354

5455
try {
5556
const walletAddress =
@@ -63,7 +64,7 @@ export class StripeService implements IStripeService {
6364
await this.accountService.getGateHubWalletAddress(walletAddress)
6465

6566
await this.gateHubClient.createTransaction({
66-
amount,
67+
amount: scaledAmount,
6768
vault_uuid: this.gateHubClient.getVaultUuid(currency.toUpperCase()),
6869
receiving_address: gateHubWalletId,
6970
sending_address: this.env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
@@ -76,7 +77,7 @@ export class StripeService implements IStripeService {
7677
accountId: walletAddress.accountId,
7778
paymentId: paymentIntent.id,
7879
assetCode: currency.toUpperCase(),
79-
value: transformBalance(Number(amount), 2),
80+
value: transformBalance(scaledAmount, 2),
8081
type: 'INCOMING',
8182
status: 'COMPLETED',
8283
description: 'Stripe Payment',

packages/wallet/backend/src/utils/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ObjectWithAnyKeys } from '@/shared/types'
33
import { createHash, randomBytes } from 'crypto'
44
import NodeCache from 'node-cache'
55
import { env } from '@/config/env'
6+
import { DEFAULT_ASSET_SCALE } from './consts'
67

78
export const transformAmount = (
89
amount: string | bigint,
@@ -24,6 +25,16 @@ export const transformBalance = (value: number, scale: number): bigint => {
2425
return BigInt(Math.floor(value * 10 ** scale))
2526
}
2627

28+
export const applyScale = (
29+
value: number,
30+
scale: number = DEFAULT_ASSET_SCALE
31+
): number => {
32+
const factor = 10 ** scale
33+
const scaledValue = value * 10 ** -scale
34+
const truncatedValue = Math.floor(scaledValue * factor) / factor
35+
return truncatedValue
36+
}
37+
2738
export function extractUuidFromUrl(url: string): string {
2839
const { pathname } = new URL(url)
2940
const id = pathname.match(
@@ -36,6 +47,7 @@ export function extractUuidFromUrl(url: string): string {
3647

3748
return id
3849
}
50+
3951
export const incomingPaymentRegexp =
4052
/\/incoming-payments\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
4153

packages/wallet/backend/tests/stripe-integration/service.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Stripe Service', (): void => {
141141
await stripeService.onWebHook(webhook)
142142

143143
expect(mockGateHubClient.createTransaction).toHaveBeenCalledWith({
144-
amount: webhook.data.object.amount,
144+
amount: 10,
145145
vault_uuid: 'vault-uuid-123',
146146
receiving_address: 'gatehub-wallet-123',
147147
sending_address: env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
@@ -226,7 +226,7 @@ describe('Stripe Service', (): void => {
226226
)
227227

228228
expect(mockGateHubClient.createTransaction).toHaveBeenCalledWith({
229-
amount: webhook.data.object.amount,
229+
amount: 10,
230230
vault_uuid: 'vault-uuid-123',
231231
receiving_address: 'gatehub-wallet-123',
232232
sending_address: env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { applyScale } from '@/utils/helpers'
2+
3+
describe('Utils Helper Functions', (): void => {
4+
describe('applyScale', (): void => {
5+
it('should correctly scale amounts with scale of 2', (): void => {
6+
// Test various cases
7+
expect(applyScale(1000, 2)).toBe(10)
8+
expect(applyScale(1500, 2)).toBe(15)
9+
expect(applyScale(1550, 2)).toBe(15.5)
10+
expect(applyScale(1555, 2)).toBe(15.55)
11+
expect(applyScale(1, 2)).toBe(0.01)
12+
})
13+
14+
it('should handle zero correctly', (): void => {
15+
expect(applyScale(0, 2)).toBe(0)
16+
})
17+
18+
it('should use default scale if not provided', (): void => {
19+
expect(applyScale(1000)).toBe(10)
20+
})
21+
})
22+
})

0 commit comments

Comments
 (0)