Skip to content

Commit c2f7b14

Browse files
committed
fix: ensure toolkit works for non standard chains
1 parent 991ed27 commit c2f7b14

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/ResponseListener.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FunctionsRouterSource } from './v1_contract_sources'
44

55
import type { BigNumber, providers } from 'ethers'
66

7+
import { FunctionsTopics } from './events'
78
import { FulfillmentCode, type FunctionsResponse } from './types'
89

910
export class ResponseListener {
@@ -88,7 +89,20 @@ export class ResponseListener {
8889

8990
const check = async () => {
9091
const receipt = await this.provider.waitForTransaction(txHash, confirmations, timeoutMs)
91-
const updatedId = receipt.logs[0].topics[1]
92+
93+
// There must be logs in the receipt otherwise it's a chain that doesn't support logs or the tx was reverted
94+
if (!receipt.logs) throw new Error('No logs found in the transaction receipt')
95+
96+
// Find the RequestStart event in the logs
97+
const requestStartLog = receipt.logs.find(
98+
log => log.topics[0] === FunctionsTopics.RequestStart,
99+
)
100+
101+
// Ensure the requestID exists in the logs
102+
if (!requestStartLog) throw new Error('RequestStart event not found in the logs')
103+
if (!requestStartLog.topics[1]) throw new Error('Request ID not found in the logs')
104+
105+
const updatedId = requestStartLog.topics[1]
92106
if (updatedId !== requestId) {
93107
requestId = updatedId
94108
const response = await this.listenForResponse(requestId, timeoutMs)
@@ -119,7 +133,7 @@ export class ResponseListener {
119133
}
120134

121135
this.functionsRouter.on(
122-
'RequestProcessed',
136+
{ topics: [FunctionsTopics.RequestProcessed] },
123137
(
124138
requestId: string,
125139
_subscriptionId: BigNumber,

src/SubscriptionManager.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './v1_contract_sources'
99

1010
import type { Signer } from 'ethers'
11-
import type { TransactionReceipt } from '@ethersproject/abstract-provider'
11+
import type { TransactionReceipt, TransactionResponse } from '@ethersproject/abstract-provider'
1212

1313
import type {
1414
SubConsumerConfig,
@@ -21,6 +21,7 @@ import type {
2121
SubCreateConfig,
2222
EstimateCostConfig,
2323
} from './types'
24+
import { FunctionsTopics } from './events'
2425

2526
export 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}`)

src/events.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file contains a list of topics for events that are emitted by the Functions Contracts.
2+
export const FunctionsTopics = {
3+
SubscriptionCreated: '0x464722b4166576d3dcbba877b999bc35cf911f4eaf434b7eba68fa113951d0bf',
4+
RequestStart: '0xf67aec45c9a7ede407974a3e0c3a743dffeab99ee3f2d4c9a8144c2ebf2c7ec9',
5+
RequestProcessed: '0x64778f26c70b60a8d7e29e2451b3844302d959448401c0535b768ed88c6b505e',
6+
}

0 commit comments

Comments
 (0)