MCPcopy
hub / github.com/canopy-network/canopy / waitForTxInclusion

Function waitForTxInclusion

plugin/go/tutorial/rpc_test.go:272–307  ·  view source on GitHub ↗

waitForTxInclusion waits for a transaction to be included in a block

(rpcURL, senderAddr, txHash string, timeout time.Duration)

Source from the content-addressed store, hash-verified

270
271// waitForTxInclusion waits for a transaction to be included in a block
272func waitForTxInclusion(rpcURL, senderAddr, txHash string, timeout time.Duration) (bool, error) {
273 deadline := time.Now().Add(timeout)
274
275 for time.Now().Before(deadline) {
276 // Query transactions by sender
277 reqJSON := fmt.Sprintf(`{"address":"%s","perPage":20}`, senderAddr)
278 respBody, err := postRawJSON(rpcURL+"/v1/query/txs-by-sender", reqJSON)
279 if err != nil {
280 time.Sleep(1 * time.Second)
281 continue
282 }
283
284 var result struct {
285 Results []struct {
286 TxHash string `json:"txHash"`
287 Height uint64 `json:"height"`
288 } `json:"results"`
289 TotalCount int `json:"totalCount"`
290 }
291 if err := json.Unmarshal(respBody, &result); err != nil {
292 time.Sleep(1 * time.Second)
293 continue
294 }
295
296 // Check if our transaction is in the results
297 for _, tx := range result.Results {
298 if tx.TxHash == txHash {
299 return true, nil
300 }
301 }
302
303 time.Sleep(1 * time.Second)
304 }
305
306 return false, fmt.Errorf("transaction %s not included within timeout", txHash)
307}
308
309// checkTxNotFailed verifies that a transaction is not in the failed transactions list
310func checkTxNotFailed(rpcURL, senderAddr string) (int, error) {

Callers 1

TestPluginTransactionsFunction · 0.70

Calls 4

postRawJSONFunction · 0.70
UnmarshalMethod · 0.65
ErrorfMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected