1+ import {
2+ createHash ,
3+ } from 'crypto' ;
4+
15import { logger , stats } from '@dydxprotocol-indexer/base' ;
26import {
37 BridgeInformationTable ,
@@ -45,6 +49,7 @@ import {
4549import {
4650 getSvmSigner , getSkipCallData , getKernelAccount ,
4751 buildUserAddresses ,
52+ limitAmount ,
4853} from '../../../helpers/skip-helper' ;
4954import { trackTurnkeyDepositSubmitted } from '../../../lib/amplitude-helpers' ;
5055import { handleControllerError } from '../../../lib/helpers' ;
@@ -257,14 +262,16 @@ class BridgeController extends Controller {
257262 throw new Error ( 'Failed to derive dYdX address' ) ;
258263 }
259264
265+ const amountIn = await limitAmount ( chainId , amount , sourceAssetDenom ) ;
266+
260267 const path = await route ( {
261268 goFast : true ,
262269 allowUnsafe : false ,
263270 smartRelay : true , // skip recommended to enable for better routes and less faults.
264271 smartSwapOptions : {
265272 splitRoutes : true ,
266273 } ,
267- amountIn : amount ,
274+ amountIn,
268275 sourceAssetDenom,
269276 sourceAssetChainId : chainId ,
270277 destAssetChainId : dydxChainId ,
@@ -319,7 +326,7 @@ class BridgeController extends Controller {
319326 logger . info ( {
320327 message : `Broadcasted on ${ c } : ${ txHash } ` ,
321328 from : fromAddress ,
322- amount,
329+ amount : amountIn ,
323330 sourceAssetDenom,
324331 chainId,
325332 toAddress : fromAddress ,
@@ -339,7 +346,7 @@ class BridgeController extends Controller {
339346 message : 'Bridge transaction completed' ,
340347 fromAddress,
341348 chainId : c ,
342- amount,
349+ amount : amountIn ,
343350 sourceAssetDenom,
344351 transactionHash : txHash ,
345352 status,
@@ -357,18 +364,20 @@ class BridgeController extends Controller {
357364 const bridgeRecord = {
358365 from_address : fromAddress ,
359366 chain_id : c ,
360- amount,
367+ amount : amountIn ,
361368 transaction_hash : txHash ,
362369 created_at : new Date ( ) . toISOString ( ) ,
363370 } ;
364371
365372 await BridgeInformationTable . create ( bridgeRecord ) ;
366-
373+ const email = record . email ?. trim ( ) . toLowerCase ( ) ;
374+ // sha256 hash email
375+ const emailHash = email ? createHash ( 'sha256' ) . update ( email ) . digest ( 'hex' ) : record . evm_address ;
367376 // Track TurnKey deposit confirmation event in Amplitude
368377 await trackTurnkeyDepositSubmitted (
369- dydxAddress ,
378+ emailHash ,
370379 c ,
371- amount ,
380+ amountIn ,
372381 txHash ,
373382 sourceAssetDenom ,
374383 ) ;
@@ -377,7 +386,7 @@ class BridgeController extends Controller {
377386 message : 'Bridge transaction tracked' ,
378387 fromAddress,
379388 chainId : c ,
380- amount,
389+ amount : amountIn ,
381390 sourceAssetDenom,
382391 transactionHash : txHash ,
383392 explorerLink,
@@ -389,7 +398,7 @@ class BridgeController extends Controller {
389398 message : 'Failed to create bridge information record on tracked' ,
390399 fromAddress,
391400 chainId : c ,
392- amount,
401+ amount : amountIn ,
393402 error : error . message || error ,
394403 } ) ;
395404 // Don't throw error to avoid breaking the bridge flow
@@ -457,13 +466,15 @@ class BridgeController extends Controller {
457466 if ( ! record || ! record . dydx_address ) {
458467 throw new Error ( 'Failed to derive dYdX address' ) ;
459468 }
469+ // we cannot bridge more than the max amount allowed through the bridge.
470+ const amountToUse = await limitAmount ( chainId , amount , sourceAssetDenom ) ;
460471 let callData : Parameters < SmartAccountImplementation [ 'encodeCalls' ] > [ 0 ] = [ ] ;
461472 try {
462473 callData = await getSkipCallData (
463474 srcAddress ,
464475 sourceAssetDenom ,
465476 record . dydx_address ,
466- amount ,
477+ amountToUse ,
467478 chainId ,
468479 ) ;
469480 } catch ( error ) {
@@ -534,7 +545,7 @@ class BridgeController extends Controller {
534545 const bridgeRecord : BridgeInformationCreateObject = {
535546 from_address : fromAddress ,
536547 chain_id : chainId ,
537- amount,
548+ amount : amountToUse ,
538549 transaction_hash : receipt . transactionHash ,
539550 created_at : new Date ( ) . toISOString ( ) ,
540551 } ;
@@ -543,12 +554,22 @@ class BridgeController extends Controller {
543554 ) ;
544555
545556 // Track TurnKey deposit confirmation event in Amplitude
546- const dydxAddress = await getDydxAddress ( fromAddress , chainId ) ;
547- if ( dydxAddress ) {
557+ if ( record . email ) {
558+ const email = record . email . trim ( ) . toLowerCase ( ) ;
559+ // sha256 hash email
560+ const emailHash = createHash ( 'sha256' ) . update ( email ) . digest ( 'hex' ) ;
561+ await trackTurnkeyDepositSubmitted (
562+ emailHash ,
563+ chainId ,
564+ amountToUse ,
565+ receipt . transactionHash ,
566+ sourceAssetDenom ,
567+ ) ;
568+ } else {
548569 await trackTurnkeyDepositSubmitted (
549- dydxAddress ,
570+ record . evm_address ,
550571 chainId ,
551- amount ,
572+ amountToUse ,
552573 receipt . transactionHash ,
553574 sourceAssetDenom ,
554575 ) ;
0 commit comments