waitForTxInclusion waits for a transaction to be included in a block
(rpcURL, senderAddr, txHash string, timeout time.Duration)
| 270 | |
| 271 | // waitForTxInclusion waits for a transaction to be included in a block |
| 272 | func 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 |
| 310 | func checkTxNotFailed(rpcURL, senderAddr string) (int, error) { |
no test coverage detected