Skip to content

Commit c12fef5

Browse files
committed
feat: Wait until block isFinalized (keep isInBlock for substrate-contracts-node)
1 parent 4e4f353 commit c12fef5

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

src/helpers/contractCall.ts

+42-13
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,58 @@ export const contractTx = async (
100100
// Call actual query/tx & wrap it in a promise
101101
const gasLimit = dryResult.gasRequired
102102
return new Promise(async (resolve, reject) => {
103-
const tx = contract.tx[stringCamelCase(method)](
104-
{ ...options, gasLimit },
105-
...args,
106-
)
107103
try {
108-
const unsub = await tx.signAndSend(account, (result) => {
104+
const isDevelopment =
105+
(api.runtimeChain || '').toLowerCase() === 'development'
106+
? 'isInBlock'
107+
: 'isFinalized'
108+
const finalStatus = isDevelopment ? 'isInBlock' : 'isFinalized'
109+
const asFinalStatus = isDevelopment ? 'asInBlock' : 'asFinalized'
110+
111+
const tx = contract.tx[stringCamelCase(method)](
112+
{ ...options, gasLimit },
113+
...args,
114+
)
115+
116+
const unsub = await tx.signAndSend(account, async (result) => {
109117
statusCb?.(result)
110-
const isInBlock = result?.status?.isInBlock
111-
if (!isInBlock) return
112-
const errorEvent = result?.events.find(
113-
({ event: { method } }: any) => method === 'ExtrinsicFailed',
118+
119+
const isFinalized = result?.status?.[finalStatus]
120+
if (!isFinalized) return
121+
122+
// Determine extrinsic and block info
123+
const extrinsicHash = result.txHash.toHex()
124+
const extrinsicIndex = result.txIndex
125+
const blockHash = result.status[asFinalStatus].toHex()
126+
127+
const errorEvent = result?.events.find(({ event }) =>
128+
api.events.system.ExtrinsicFailed.is(event),
114129
)
115-
if (isInBlock && errorEvent) {
130+
if (errorEvent) {
116131
// Reject if `ExtrinsicFailed` event was found
117132
reject({
118133
dryResult,
119134
errorMessage: decodeOutput || 'ExtrinsicFailed',
120135
errorEvent,
136+
extrinsicHash,
137+
extrinsicIndex,
138+
blockHash,
121139
})
122140
unsub?.()
123-
} else if (isInBlock) {
124-
// Otherwise resolve succesfully if transaction is in block
125-
resolve({ dryResult, result })
141+
} else {
142+
// Resolve succesfully otherwise
143+
const successEvent = result?.events.find(({ event }) =>
144+
api.events.system.ExtrinsicSuccess.is(event),
145+
)
146+
147+
resolve({
148+
dryResult,
149+
result,
150+
successEvent,
151+
extrinsicHash,
152+
extrinsicIndex,
153+
blockHash,
154+
})
126155
unsub?.()
127156
}
128157
})

0 commit comments

Comments
 (0)