11import { ContractLookup , FAssetType } from "fasset-indexer-core"
22import * as Entities from "fasset-indexer-core/entities"
3- import * as ExplorerType from "./interface "
3+ import * as ExplorerType from "./types "
44import * as SQL from "./utils/raw-sql"
5- import type { EntityManager , ORM } from "fasset-indexer-core/orm"
6- import { unixnow } from "src/shared/utils"
5+ import { PaymentReference } from "fasset-indexer-core/utils"
76import { EVENTS } from "fasset-indexer-core/config"
7+ import { unixnow } from "../shared/utils"
8+ import type { EntityManager , ORM } from "fasset-indexer-core/orm"
9+ import type { FilterQuery } from "@mikro-orm/core"
810
9- const TRANSACTION_TYPE_EVENTS = [
10- EVENTS . ASSET_MANAGER . REDEMPTION_REQUESTED ,
11- EVENTS . ASSET_MANAGER . COLLATERAL_RESERVED ,
12- EVENTS . ASSET_MANAGER . TRANSFER_TO_CORE_VAULT_STARTED ,
13- EVENTS . ASSET_MANAGER . RETURN_FROM_CORE_VAULT_REQUESTED
14- ]
11+ const SELF_MINT_CONFIRMATION_OFFSET = 15 * 60
1512
1613const ALL_TRANSACTION_TYPES = Object . values ( ExplorerType . TransactionType )
1714 . filter ( v => typeof v === "number" )
@@ -132,7 +129,7 @@ export class ExplorerAnalytics {
132129
133130 async returnFromCoreVaultTransactionDetails (
134131 hash : string
135- ) : Promise < ExplorerType . RetrunFromCoreVaultTransactionDetails > {
132+ ) : Promise < ExplorerType . ReturnFromCoreVaultTransactionDetails > {
136133 const em = this . orm . em . fork ( )
137134 const returnFromCoreVaultRequests = await em . find ( Entities . ReturnFromCoreVaultRequested ,
138135 { evmLog : { transaction : { hash } } } , { populate : [
@@ -148,13 +145,31 @@ export class ExplorerAnalytics {
148145 return { flows }
149146 }
150147
148+ async selfMintTransactionDetails (
149+ hash : string
150+ ) : Promise < ExplorerType . SelfMintTransactionDetails > {
151+ const em = this . orm . em . fork ( )
152+ const selfMints = await em . find ( Entities . SelfMint ,
153+ { evmLog : { transaction : { hash } } } , { populate : [
154+ 'evmLog.block' , 'evmLog.transaction.source' ,
155+ 'agentVault.address' , 'agentVault.underlyingAddress' , 'agentVault.owner.manager'
156+ ] }
157+ )
158+ const flows : ExplorerType . SelfMintEventDetails [ ] = [ ]
159+ for ( const selfMint of selfMints ) {
160+ const details = await this . selfMintEventDetails ( em , selfMint )
161+ flows . push ( details )
162+ }
163+ return { flows }
164+ }
165+
151166 protected async mintingEventDetails (
152167 em : EntityManager , collateralReserved : Entities . CollateralReserved
153168 ) : Promise < ExplorerType . MintEventDetails > {
154169 const underlyingTransaction = await this . getUnderlyingTransaction (
155170 em , collateralReserved . fasset ,
156171 collateralReserved . paymentReference ,
157- collateralReserved . paymentAddress . text
172+ { transaction : { target : collateralReserved . paymentAddress } }
158173 )
159174 const resp : ExplorerType . MintEventDetails = {
160175 events : { original : collateralReserved } , underlyingTransaction
@@ -187,8 +202,10 @@ export class ExplorerAnalytics {
187202 const underlyingTransaction = await this . getUnderlyingTransaction (
188203 em , redemptionRequested . fasset ,
189204 redemptionRequested . paymentReference ,
190- redemptionRequested . paymentAddress . text ,
191- redemptionRequested . agentVault . underlyingAddress . text
205+ { transaction : {
206+ source : redemptionRequested . agentVault . underlyingAddress ,
207+ target : redemptionRequested . paymentAddress
208+ } }
192209 )
193210 const resp : ExplorerType . RedeemEventDetails = {
194211 events : { original : redemptionRequested } , underlyingTransaction
@@ -237,8 +254,10 @@ export class ExplorerAnalytics {
237254 const underlyingTransaction = await this . getUnderlyingTransaction (
238255 em , transferToCoreVaultStarted . fasset ,
239256 redemptionRequested . paymentReference ,
240- redemptionRequested . paymentAddress . text ,
241- transferToCoreVaultStarted . agentVault . underlyingAddress . text
257+ { transaction : {
258+ source : transferToCoreVaultStarted . agentVault . underlyingAddress ,
259+ target : redemptionRequested . paymentAddress
260+ } }
242261 )
243262 const resp : ExplorerType . TransferToCoreVaultEventDetails = {
244263 events : { original : transferToCoreVaultStarted } , underlyingTransaction
@@ -266,8 +285,10 @@ export class ExplorerAnalytics {
266285 const underlyingTransaction = await this . getUnderlyingTransaction (
267286 em , returnFromCoreVaultRequested . fasset ,
268287 returnFromCoreVaultRequested . paymentReference ,
269- returnFromCoreVaultRequested . agentVault . underlyingAddress . text ,
270- coreVault
288+ { transaction : {
289+ source : { text : coreVault } ,
290+ target : returnFromCoreVaultRequested . agentVault . underlyingAddress
291+ } }
271292 )
272293 const resp : ExplorerType . ReturnFromCoreVaultEventDetails = {
273294 events : { original : returnFromCoreVaultRequested } , underlyingTransaction
@@ -288,14 +309,20 @@ export class ExplorerAnalytics {
288309 return resp
289310 }
290311
291- protected async getUnderlyingTransaction (
292- em : EntityManager , fasset : FAssetType , reference : string , target : string , source ?: string
293- ) : Promise < Entities . UnderlyingVoutReference | null > {
294- const obj = source == null ? { } : { source : { text : source } }
295- return em . findOne ( Entities . UnderlyingVoutReference ,
296- { fasset, reference, transaction : { target : { text : target } , ...obj } } ,
297- { populate : [ 'transaction.block' , 'transaction.source' , 'transaction.target' ] }
312+ protected async selfMintEventDetails (
313+ em : EntityManager , selfMint : Entities . SelfMint
314+ ) : Promise < ExplorerType . SelfMintEventDetails > {
315+ const lastTs = selfMint . evmLog . block . timestamp
316+ const firstTs = lastTs - SELF_MINT_CONFIRMATION_OFFSET
317+ const underlyingTransaction = await this . getUnderlyingTransaction (
318+ em , selfMint . fasset ,
319+ PaymentReference . selfMint ( selfMint . agentVault . address . hex ) ,
320+ {
321+ transaction : { value : selfMint . depositedUBA , target : selfMint . agentVault . underlyingAddress } ,
322+ block : { timestamp : { $gte : firstTs , $lte : lastTs } }
323+ }
298324 )
325+ return { events : { original : selfMint } , underlyingTransaction }
299326 }
300327
301328 protected async nativeTransactionClassification ( em : EntityManager , hash : string ) : Promise < ExplorerType . GenericTransactionClassification > {
@@ -308,6 +335,7 @@ export class ExplorerAnalytics {
308335 || log . name == EVENTS . ASSET_MANAGER . COLLATERAL_RESERVED
309336 || log . name == EVENTS . ASSET_MANAGER . TRANSFER_TO_CORE_VAULT_STARTED
310337 || log . name == EVENTS . ASSET_MANAGER . RETURN_FROM_CORE_VAULT_REQUESTED
338+ || log . name == EVENTS . ASSET_MANAGER . SELF_MINT
311339 ) {
312340 oglog = log
313341 // core vault transfers
@@ -375,6 +403,18 @@ export class ExplorerAnalytics {
375403 return resp . map ( ( { hash, name } ) => ( { transactionHash : hash , eventName : name } ) )
376404 }
377405
406+ protected async getUnderlyingTransaction (
407+ em : EntityManager ,
408+ fasset : FAssetType ,
409+ reference : string ,
410+ filters : FilterQuery < Entities . UnderlyingVoutReference > = { }
411+ ) : Promise < Entities . UnderlyingVoutReference | null > {
412+ return em . findOne ( Entities . UnderlyingVoutReference ,
413+ { fasset, reference, ...filters as object } ,
414+ { populate : [ 'transaction.block' , 'transaction.source' , 'transaction.target' ] }
415+ )
416+ }
417+
378418 protected eventNameToTransactionType ( name : string ) : ExplorerType . TransactionType {
379419 switch ( name ) {
380420 case 'CollateralReserved' :
@@ -385,6 +425,8 @@ export class ExplorerAnalytics {
385425 return ExplorerType . TransactionType . TransferToCV
386426 case 'ReturnFromCoreVaultRequested' :
387427 return ExplorerType . TransactionType . ReturnFromCV
428+ case 'SelfMint' :
429+ return ExplorerType . TransactionType . SelfMint
388430 default :
389431 throw new Error ( `event name ${ name } cannot be mapped to transaction type` )
390432 }
0 commit comments