8
8
} from './v1_contract_sources'
9
9
10
10
import type { Signer } from 'ethers'
11
- import type { TransactionReceipt } from '@ethersproject/abstract-provider'
11
+ import type { TransactionReceipt , TransactionResponse } from '@ethersproject/abstract-provider'
12
12
13
13
import type {
14
14
SubConsumerConfig ,
@@ -21,6 +21,7 @@ import type {
21
21
SubCreateConfig ,
22
22
EstimateCostConfig ,
23
23
} from './types'
24
+ import { FunctionsTopics } from './events'
24
25
25
26
export class SubscriptionManager {
26
27
private signer : Signer
@@ -109,24 +110,51 @@ export class SubscriptionManager {
109
110
: await this . functionsRouter . createSubscriptionWithConsumer (
110
111
subCreateConfig . consumerAddress ,
111
112
)
112
- const createSubWithConsumerTxReceipt = await createSubWithConsumerTx . wait (
113
+
114
+ const txReceipt : TransactionReceipt = await createSubWithConsumerTx . wait (
113
115
subCreateConfig . txOptions ?. confirmations ,
114
116
)
115
117
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
+ )
117
124
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 ]
118
131
return Number ( subscriptionId . toString ( ) )
119
132
} catch ( error ) {
120
133
throw Error ( `createSubscriptionWithConsumer failed\n${ error } ` )
121
134
}
122
135
}
123
136
124
137
try {
125
- const createSubTx = subCreateConfig ?. txOptions ?. overrides
138
+ const createSubTx : TransactionResponse = subCreateConfig ?. txOptions ?. overrides
126
139
? await this . functionsRouter . createSubscription ( subCreateConfig ?. txOptions . overrides )
127
140
: 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 ]
130
158
return Number ( subscriptionId . toString ( ) )
131
159
} catch ( error ) {
132
160
throw Error ( `createSubscription failed\n${ error } ` )
0 commit comments