Bladeren bron

Merge pull request #87 from Relayz-io/silvestr/fix-nonce-update

Fix nonce update from the transaction outcome
develop
Predko Silvestr 2 jaren geleden
bovenliggende
commit
7c7e7dd67a
No account linked to committer's email address
2 gewijzigde bestanden met toevoegingen van 65 en 28 verwijderingen
  1. 28
    28
      near-rpc/src/client.rs
  2. 37
    0
      near-rpc/tests/rpc.rs

+ 28
- 28
near-rpc/src/client.rs Bestand weergeven

@@ -229,19 +229,7 @@ impl NearClient {
229 229
                     .map_err(Error::DeserializeExecutionOutcome)
230 230
             })?;
231 231
 
232
-        match execution_outcome.status {
233
-            FinalExecutionStatus::Failure(err) => Err(Error::TxExecution(err)),
234
-            FinalExecutionStatus::SuccessValue(data) => {
235
-                signer.update_nonce(execution_outcome.transaction.nonce);
236
-
237
-                Ok(Output {
238
-                    transaction: execution_outcome.transaction_outcome,
239
-                    logs: extract_logs(execution_outcome.receipts_outcome),
240
-                    data,
241
-                })
242
-            }
243
-            _ => Err(Error::TxNotStarted),
244
-        }
232
+        proceed_outcome(signer, execution_outcome)
245 233
     }
246 234
 
247 235
     /// Execute a transaction with a function call to the smart contract
@@ -441,6 +429,7 @@ pub struct FunctionCall<'a> {
441 429
 
442 430
 impl<'a> FunctionCall<'a> {
443 431
     /// Sends a transaction and waits until transaction is fully complete. (Has a 10 second timeout)
432
+    /// Also, possible that an output data will be empty if the transaction is still executing
444 433
     pub async fn commit(self, block_finality: Finality) -> Result<Output> {
445 434
         let transaction_bytes =
446 435
             serialize_transaction(&self.info, self.actions, block_finality).await?;
@@ -459,21 +448,7 @@ impl<'a> FunctionCall<'a> {
459 448
                     .map_err(Error::DeserializeExecutionOutcome)
460 449
             })?;
461 450
 
462
-        match execution_outcome.status {
463
-            FinalExecutionStatus::Failure(err) => Err(Error::TxExecution(err)),
464
-            FinalExecutionStatus::SuccessValue(data) => {
465
-                self.info
466
-                    .signer()
467
-                    .update_nonce(execution_outcome.transaction.nonce);
468
-
469
-                Ok(Output {
470
-                    transaction: execution_outcome.transaction_outcome,
471
-                    logs: extract_logs(execution_outcome.receipts_outcome),
472
-                    data,
473
-                })
474
-            }
475
-            _ => Err(Error::TxNotStarted),
476
-        }
451
+        proceed_outcome(self.info.signer(), execution_outcome)
477 452
     }
478 453
 
479 454
     /// Sends a transaction and immediately returns transaction hash.
@@ -493,3 +468,28 @@ impl<'a> FunctionCall<'a> {
493 468
             })
494 469
     }
495 470
 }
471
+
472
+#[allow(clippy::result_large_err)]
473
+pub(crate) fn proceed_outcome(
474
+    signer: &Signer,
475
+    execution_outcome: FinalExecutionOutcomeView,
476
+) -> Result<Output> {
477
+    signer.update_nonce(execution_outcome.transaction.nonce);
478
+    let transaction = execution_outcome.transaction_outcome;
479
+    let logs = extract_logs(execution_outcome.receipts_outcome);
480
+
481
+    match execution_outcome.status {
482
+        FinalExecutionStatus::Failure(err) => Err(Error::TxExecution(err)),
483
+        FinalExecutionStatus::SuccessValue(data) => Ok(Output {
484
+            transaction,
485
+            logs,
486
+            data,
487
+        }),
488
+        FinalExecutionStatus::NotStarted => Err(Error::TxNotStarted),
489
+        FinalExecutionStatus::Started => Ok(Output {
490
+            transaction,
491
+            logs,
492
+            data: vec![],
493
+        }),
494
+    }
495
+}

+ 37
- 0
near-rpc/tests/rpc.rs Bestand weergeven

@@ -135,6 +135,43 @@ async fn contract_function_call() {
135 135
         .unwrap();
136 136
 }
137 137
 
138
+#[tokio::test]
139
+async fn contract_function_call_failed() {
140
+    let worker = workspaces::sandbox().await.unwrap();
141
+    let client = near_client(&worker);
142
+    let signer_account_id = AccountId::from_str("alice.test.near").unwrap();
143
+    let signer = create_signer(&worker, &client, &signer_account_id).await;
144
+    let wasm = download_contract().await;
145
+
146
+    client
147
+        .deploy_contract(&signer, &signer_account_id, wasm)
148
+        .commit(Finality::None)
149
+        .await
150
+        .unwrap();
151
+
152
+    assert!(client
153
+        .function_call(&signer, &signer_account_id, "new_default_meta")
154
+        .args(json!({
155
+            "owner_id": &signer_account_id,
156
+            "total_suppl": "100",
157
+        }))
158
+        .gas(near_units::parse_gas!("300 T") as u64)
159
+        .commit(Finality::None)
160
+        .await
161
+        .is_err());
162
+
163
+    client
164
+        .function_call(&signer, &signer_account_id, "new_default_meta")
165
+        .args(json!({
166
+            "owner_id": &signer_account_id,
167
+            "total_supply": "100",
168
+        }))
169
+        .gas(near_units::parse_gas!("300 T") as u64)
170
+        .commit(Finality::None)
171
+        .await
172
+        .unwrap();
173
+}
174
+
138 175
 #[tokio::test]
139 176
 async fn multiple_tests() {
140 177
     let worker = workspaces::sandbox().await.unwrap();

Laden…
Annuleren
Opslaan