@@ -100,20 +100,24 @@ export class TokensService {
100100 async init ( ctx : Context ) {
101101 this . stream = await this . getStream ( ctx ) ;
102102 if ( this . factoryAddress !== '' ) {
103- const eventABI = this . mapper . getCreateEvent ( ) ;
104- const methodABI = this . mapper . getCreateMethod ( ) ;
105- if ( eventABI !== undefined && methodABI !== undefined ) {
106- await this . eventstream . getOrCreateSubscription (
107- ctx ,
108- this . baseUrl ,
109- eventABI ,
110- this . stream . id ,
111- packSubscriptionName ( this . factoryAddress , eventABI . name ) ,
112- this . factoryAddress ,
113- [ methodABI ] ,
114- '0' ,
115- ) ;
116- }
103+ await this . createFactorySubscription ( ctx , this . factoryAddress ) ;
104+ }
105+ }
106+
107+ private async createFactorySubscription ( ctx : Context , address : string ) {
108+ const eventABI = this . mapper . getCreateEvent ( ) ;
109+ const methodABI = this . mapper . getCreateMethod ( ) ;
110+ if ( eventABI !== undefined && methodABI !== undefined ) {
111+ await this . eventstream . getOrCreateSubscription (
112+ ctx ,
113+ this . baseUrl ,
114+ eventABI ,
115+ this . stream . id ,
116+ packSubscriptionName ( address , eventABI . name ) ,
117+ address ,
118+ [ methodABI ] ,
119+ '0' ,
120+ ) ;
117121 }
118122 }
119123
@@ -148,6 +152,7 @@ export class TokensService {
148152 return false ;
149153 }
150154
155+ const createABI = this . mapper . getCreateEvent ( ) ;
151156 const foundEvents = new Map < string , string [ ] > ( ) ;
152157 for ( const sub of subscriptions ) {
153158 const parts = unpackSubscriptionName ( sub . name ) ;
@@ -158,7 +163,8 @@ export class TokensService {
158163 ) ;
159164 return true ;
160165 }
161- if ( parts . poolLocator === this . factoryAddress ) {
166+ if ( parts . event === createABI ?. name ) {
167+ // Skip "create" subscriptions (assume they are factories)
162168 continue ;
163169 }
164170 const key = packSubscriptionName ( parts . poolLocator , '' , parts . poolData ) ;
@@ -230,17 +236,20 @@ export class TokensService {
230236 }
231237
232238 async createPool ( ctx : Context , dto : TokenPool ) : Promise < TokenPoolEvent | AsyncResponse > {
233- if ( dto . config ?. address !== undefined && dto . config . address !== '' ) {
234- this . logger . log ( `Create token pool from existing: '${ dto . config . address } '` ) ;
235- return this . createFromExisting ( ctx , dto . config . address , dto ) ;
239+ const contractAddress = dto . config ?. address || undefined ;
240+ if ( contractAddress !== undefined ) {
241+ this . logger . log ( `Create token pool from existing: '${ contractAddress } '` ) ;
242+ return this . createFromExisting ( ctx , contractAddress , dto ) ;
236243 }
237- if ( this . factoryAddress === '' ) {
244+
245+ const factoryAddress = dto . config ?. factoryAddress || this . factoryAddress || undefined ;
246+ if ( factoryAddress === undefined ) {
238247 throw new BadRequestException (
239248 'config.address was unspecified, and no token factory is configured!' ,
240249 ) ;
241250 }
242- this . logger . log ( `Create token pool from factory: '${ this . factoryAddress } '` ) ;
243- return this . createFromFactory ( ctx , dto ) ;
251+ this . logger . log ( `Create token pool from factory: '${ factoryAddress } '` ) ;
252+ return this . createFromFactory ( ctx , factoryAddress , dto ) ;
244253 }
245254
246255 private async createFromExisting (
@@ -281,16 +290,17 @@ export class TokensService {
281290 } ;
282291 }
283292
284- private async createFromFactory ( ctx : Context , dto : TokenPool ) : Promise < AsyncResponse > {
285- const { method, params } = await this . mapper . getCreateMethodAndParams (
286- ctx ,
287- this . factoryAddress ,
288- dto ,
289- ) ;
293+ private async createFromFactory (
294+ ctx : Context ,
295+ address : string ,
296+ dto : TokenPool ,
297+ ) : Promise < AsyncResponse > {
298+ await this . createFactorySubscription ( ctx , address ) ;
299+ const { method, params } = await this . mapper . getCreateMethodAndParams ( ctx , address , dto ) ;
290300 const response = await this . blockchain . sendTransaction (
291301 ctx ,
292302 dto . signer ,
293- this . factoryAddress ,
303+ address ,
294304 dto . requestId ,
295305 method ,
296306 params ,
0 commit comments