@@ -13,6 +13,7 @@ import type { Provider } from '@ethersproject/providers'
1313import { TrancheBalanceService } from '../services/trancheBalanceService'
1414import { escrows , userEscrows } from '../../config'
1515import { InvestorPositionService } from '../services/investorPositionService'
16+ import { getPeriodStart } from '../../helpers/timekeeperService'
1617
1718const _ethApi = api as unknown as Provider
1819//const networkPromise = typeof ethApi.getNetwork === 'function' ? ethApi.getNetwork() : null
@@ -62,6 +63,7 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
6263 const [ fromEvmAddress , toEvmAddress , amount ] = event . args
6364 logger . info ( `Transfer ${ fromEvmAddress } -${ toEvmAddress } of ${ amount . toString ( ) } at block: ${ event . blockNumber } ` )
6465
66+ const timestamp = new Date ( Number ( event . block . timestamp ) * 1000 )
6567 const evmTokenAddress = event . address
6668 const chainId = await getNodeEvmChainId ( ) //(await networkPromise).chainId.toString(10)
6769 const evmBlockchain = await BlockchainService . getOrInit ( chainId )
@@ -74,12 +76,16 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
7476 const isFromEscrow = fromEvmAddress === escrowAddress
7577 const _isFromUserEscrow = fromEvmAddress === userEscrowAddress
7678
79+ const trancheId = evmToken . trancheId . split ( '-' ) [ 1 ]
80+ const tranche = await TrancheService . getById ( evmToken . poolId , trancheId )
81+
7782 const orderData : Omit < InvestorTransactionData , 'address' > = {
7883 poolId : evmToken . poolId ,
79- trancheId : evmToken . trancheId . split ( '-' ) [ 1 ] ,
84+ trancheId : trancheId ,
8085 hash : event . transactionHash ,
81- timestamp : new Date ( Number ( event . block . timestamp ) * 1000 ) ,
86+ timestamp : timestamp ,
8287 amount : amount . toBigInt ( ) ,
88+ price : tranche . snapshot ?. tokenPrice ,
8389 }
8490
8591 const isLpTokenMigrationDay =
@@ -128,27 +134,40 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
128134
129135 // Handle Transfer In and Out
130136 if ( isFromUserAddress && isToUserAddress ) {
131- const txIn = InvestorTransactionService . transferIn ( { ...orderData , address : toAccount . id } )
132- if ( ! isLpTokenMigrationDay )
133- await InvestorPositionService . buy (
134- txIn . accountId ,
135- txIn . trancheId ,
136- txIn . hash ,
137- txIn . timestamp ,
138- txIn . tokenAmount ,
139- txIn . tokenPrice
140- )
141- await txIn . save ( )
137+ await tranche . loadSnapshot ( getPeriodStart ( timestamp ) )
138+ const price = tranche . tokenPrice
142139
143- const txOut = InvestorTransactionService . transferOut ( { ...orderData , address : fromAccount . id } )
140+ const txIn = InvestorTransactionService . transferIn ( { ...orderData , address : toAccount . id , price } )
141+ await txIn . save ( )
142+ if ( ! isLpTokenMigrationDay )
143+ try {
144+ await InvestorPositionService . buy (
145+ txIn . accountId ,
146+ txIn . trancheId ,
147+ txIn . hash ,
148+ txIn . timestamp ,
149+ txIn . tokenAmount ,
150+ txIn . tokenPrice
151+ )
152+ } catch ( error ) {
153+ logger . error ( `Unable to save buy investor position: ${ error } ` )
154+ // TODO: Fallback use PoolManager Contract to read price
155+ }
156+
157+ const txOut = InvestorTransactionService . transferOut ( { ...orderData , address : fromAccount . id , price } )
144158 if ( ! isLpTokenMigrationDay ) {
145- const profit = await InvestorPositionService . sellFifo (
146- txOut . accountId ,
147- txOut . trancheId ,
148- txOut . tokenAmount ,
149- txOut . tokenPrice
150- )
151- await txOut . setRealizedProfitFifo ( profit )
159+ try {
160+ const profit = await InvestorPositionService . sellFifo (
161+ txOut . accountId ,
162+ txOut . trancheId ,
163+ txOut . tokenAmount ,
164+ txOut . tokenPrice
165+ )
166+ await txOut . setRealizedProfitFifo ( profit )
167+ } catch ( error ) {
168+ logger . error ( `Unable to save sell investor position: ${ error } ` )
169+ // TODO: Fallback use PoolManager Contract to read price
170+ }
152171 }
153172 await txOut . save ( )
154173 }
0 commit comments