88} from './v1_contract_sources'
99
1010import type { Signer } from 'ethers'
11- import type { TransactionReceipt } from '@ethersproject/abstract-provider'
11+ import type { TransactionReceipt , TransactionResponse } from '@ethersproject/abstract-provider'
1212
1313import type {
1414 SubConsumerConfig ,
@@ -21,6 +21,7 @@ import type {
2121 SubCreateConfig ,
2222 EstimateCostConfig ,
2323} from './types'
24+ import { FunctionsTopics } from './events'
2425
2526export class SubscriptionManager {
2627 private signer : Signer
@@ -109,24 +110,51 @@ export class SubscriptionManager {
109110 : await this . functionsRouter . createSubscriptionWithConsumer (
110111 subCreateConfig . consumerAddress ,
111112 )
112- const createSubWithConsumerTxReceipt = await createSubWithConsumerTx . wait (
113+
114+ const txReceipt : TransactionReceipt = await createSubWithConsumerTx . wait (
113115 subCreateConfig . txOptions ?. confirmations ,
114116 )
115117
116- const subscriptionId = createSubWithConsumerTxReceipt . events [ 0 ] . args [ 'subscriptionId' ]
118+ // Search through logs to find the topic that matches the SubscriptionCreated event
119+ if ( ! txReceipt . logs ) throw new Error ( 'No logs present within transaction receipt' )
120+
121+ const createSubscriptionLog = txReceipt . logs . find (
122+ log => log . topics [ 0 ] === FunctionsTopics . SubscriptionCreated ,
123+ )
117124
125+ // Sanity checking, ensure that the SubscriptionCreated event was found in the log
126+ if ( ! createSubscriptionLog ) throw new Error ( 'No SubscriptionCreated event found in logs' )
127+ if ( ! createSubscriptionLog . topics [ 1 ] ) throw new Error ( 'No subscriptionId found in logs' )
128+
129+ // The signature is SubscriptionCreated(uint64,address) so the subscriptionId is the second topic
130+ const subscriptionId = createSubscriptionLog . topics [ 1 ]
118131 return Number ( subscriptionId . toString ( ) )
119132 } catch ( error ) {
120133 throw Error ( `createSubscriptionWithConsumer failed\n${ error } ` )
121134 }
122135 }
123136
124137 try {
125- const createSubTx = subCreateConfig ?. txOptions ?. overrides
138+ const createSubTx : TransactionResponse = subCreateConfig ?. txOptions ?. overrides
126139 ? await this . functionsRouter . createSubscription ( subCreateConfig ?. txOptions . overrides )
127140 : await this . functionsRouter . createSubscription ( )
128- const createSubTxReceipt = await createSubTx . wait ( subCreateConfig ?. txOptions ?. confirmations )
129- const subscriptionId = createSubTxReceipt . events [ 0 ] . args [ 'subscriptionId' ]
141+
142+ const createSubTxReceipt : TransactionReceipt = await createSubTx . wait (
143+ subCreateConfig ?. txOptions ?. confirmations ,
144+ )
145+
146+ // Search through logs to find the topic that matches the SubscriptionCreated event
147+ if ( ! createSubTxReceipt . logs ) throw new Error ( 'No logs present within transaction receipt' )
148+ const createSubscriptionLog = createSubTxReceipt . logs . find (
149+ log => log . topics [ 0 ] === FunctionsTopics . SubscriptionCreated ,
150+ )
151+
152+ // Sanity checking, ensure that the SubscriptionCreated event was found in the log
153+ if ( ! createSubscriptionLog ) throw new Error ( 'No SubscriptionCreated event found in logs' )
154+ if ( ! createSubscriptionLog . topics [ 1 ] ) throw new Error ( 'No subscriptionId found in logs' )
155+
156+ // The signature is SubscriptionCreated(uint64,address) so the subscriptionId is the second topic
157+ const subscriptionId = createSubscriptionLog . topics [ 1 ]
130158 return Number ( subscriptionId . toString ( ) )
131159 } catch ( error ) {
132160 throw Error ( `createSubscription failed\n${ error } ` )
0 commit comments