@@ -5,14 +5,14 @@ import { and, DrizzleQueryError, eq, isNull } from "drizzle-orm";
55import { Hono } from "hono" ;
66import { validator } from "hono/validator" ;
77import { createHash , createVerify } from "node:crypto" ;
8- import { literal , object , parse , picklist , string , unknown , variant } from "valibot" ;
8+ import { check , literal , object , parse , picklist , pipe , string , unknown , variant } from "valibot" ;
99
1010import { Address } from "@exactly/common/validation" ;
1111
1212import database , { credentials } from "../database" ;
1313import { sendPushNotification } from "../utils/onesignal" ;
1414import { searchAccounts } from "../utils/persona" ;
15- import { BridgeCurrency , getCustomer , publicKey } from "../utils/ramps/bridge" ;
15+ import { BridgeCurrency , feeRule , getCustomer , publicKey } from "../utils/ramps/bridge" ;
1616import { track } from "../utils/segment" ;
1717import validatorHook from "../utils/validatorHook" ;
1818
@@ -52,6 +52,10 @@ export default new Hono().post(
5252 object ( {
5353 event_type : literal ( "liquidation_address.drain.updated.status_transitioned" ) ,
5454 event_object : object ( {
55+ created_at : pipe (
56+ string ( ) ,
57+ check ( ( v ) => ! Number . isNaN ( new Date ( v ) . getTime ( ) ) , "invalid date" ) ,
58+ ) ,
5559 currency : picklist ( BridgeCurrency ) ,
5660 customer_id : string ( ) ,
5761 id : string ( ) ,
@@ -70,6 +74,10 @@ export default new Hono().post(
7074 object ( { type : literal ( "in_review" ) , id : string ( ) , customer_id : string ( ) } ) ,
7175 object ( { type : literal ( "microdeposit" ) , id : string ( ) , customer_id : string ( ) } ) , // cspell:ignore microdeposit
7276 object ( {
77+ created_at : pipe (
78+ string ( ) ,
79+ check ( ( v ) => ! Number . isNaN ( new Date ( v ) . getTime ( ) ) , "invalid created at date" ) ,
80+ ) ,
7381 customer_id : string ( ) ,
7482 currency : picklist ( BridgeCurrency ) ,
7583 id : string ( ) ,
@@ -190,6 +198,18 @@ export default new Hono().post(
190198 } ) . catch ( ( error : unknown ) => captureException ( error , { level : "error" } ) ) ;
191199 }
192200 if ( payload . event_object . type === "payment_processed" ) {
201+ const usdcAmount = Number ( payload . event_object . receipt . final_amount ) ;
202+ if ( Number . isNaN ( usdcAmount ) ) {
203+ captureException ( new Error ( "invalid amount" ) , {
204+ level : "error" ,
205+ contexts : { details : { usdcAmount : payload . event_object . receipt . final_amount } } ,
206+ } ) ;
207+ return c . json ( { code : "invalid amount" } , 200 ) ;
208+ }
209+ await feeRule . report (
210+ { bridgeId, eventId : payload . event_object . id , amount : Math . round ( usdcAmount * 100 ) } ,
211+ new Date ( payload . event_object . created_at ) ,
212+ ) ;
193213 track ( {
194214 userId : account ,
195215 event : "Onramp" ,
@@ -198,13 +218,25 @@ export default new Hono().post(
198218 amount : Number ( payload . event_object . receipt . initial_amount ) ,
199219 provider : "bridge" ,
200220 source : credential . source ,
201- usdcAmount : Number ( payload . event_object . receipt . final_amount ) ,
221+ usdcAmount,
202222 } ,
203223 } ) ;
204224 }
205225 return c . json ( { code : "ok" } , 200 ) ;
206- case "liquidation_address.drain.updated.status_transitioned" :
226+ case "liquidation_address.drain.updated.status_transitioned" : {
207227 if ( payload . event_object . state !== "payment_submitted" ) return c . json ( { code : "ok" } , 200 ) ;
228+ const usdcAmount = Number ( payload . event_object . receipt . outgoing_amount ) ;
229+ if ( Number . isNaN ( usdcAmount ) ) {
230+ captureException ( new Error ( "invalid amount" ) , {
231+ level : "error" ,
232+ contexts : { details : { usdcAmount : payload . event_object . receipt . outgoing_amount } } ,
233+ } ) ;
234+ return c . json ( { code : "invalid amount" } , 200 ) ;
235+ }
236+ await feeRule . report (
237+ { bridgeId, eventId : payload . event_object . id , amount : Math . round ( usdcAmount * 100 ) } ,
238+ new Date ( payload . event_object . created_at ) ,
239+ ) ;
208240 sendPushNotification ( {
209241 userId : account ,
210242 headings : { en : "Deposited funds" } ,
@@ -220,10 +252,11 @@ export default new Hono().post(
220252 amount : Number ( payload . event_object . receipt . initial_amount ) ,
221253 provider : "bridge" ,
222254 source : credential . source ,
223- usdcAmount : Number ( payload . event_object . receipt . outgoing_amount ) ,
255+ usdcAmount,
224256 } ,
225257 } ) ;
226258 return c . json ( { code : "ok" } , 200 ) ;
259+ }
227260 }
228261 } ,
229262) ;
0 commit comments