Update nonce during withdraw flow
We are no longer constantly updating our nonce, meaning we need
special handling if we need to update it during a withdraw.
+use crate::rita_common::oracle::update_nonce;
use crate::rita_common::rita_loop::get_web3_server;
use crate::SETTING;
use ::actix_web::http::StatusCode;
@@ -18,6 +19,16 @@ pub fn withdraw(path: Path<(Address, u64)>) -> Box<dyn Future<Item = HttpRespons
let full_node = get_web3_server();
let web3 = Web3::new(&full_node);
let payment_settings = SETTING.get_payment();
+ let our_address = match payment_settings.eth_address {
+ Some(address) => address,
+ None => {
+ return Box::new(future::ok(
+ HttpResponse::new(StatusCode::from_u16(504u16).unwrap())
+ .into_builder()
+ .json("No Address configured, withdraw impossible!"),
+ ))
+ }
+ };
let tx = Transaction {
nonce: payment_settings.nonce.clone(),
@@ -48,17 +59,26 @@ pub fn withdraw(path: Path<(Address, u64)>) -> Box<dyn Future<Item = HttpRespons
let transaction_status = web3.eth_send_raw_transaction(transaction_bytes);
- Box::new(transaction_status.then(|result| {
- match result {
+ Box::new(transaction_status.then(move |result| match result {
Ok(tx_id) => Box::new(future::ok({
SETTING.get_payment_mut().nonce += 1u64.into();
HttpResponse::Ok().json(format!("txid:{:#066x}", tx_id))
})),
- Err(e) => Box::new(future::ok(
+ Err(e) => {
+ update_nonce(our_address, &web3);
+ if e.to_string().contains("nonce") {
+ Box::new(future::ok(
+ HttpResponse::new(StatusCode::from_u16(504u16).unwrap())
+ .into_builder()
+ .json(format!("The nonce was not updated, try again {:?}", e)),
+ ))
+ } else {
+ Box::new(future::ok(
HttpResponse::new(StatusCode::from_u16(504u16).unwrap())
.into_builder()
.json(format!("Full node failed to send transaction! {:?}", e)),
- )),
+ ))
+ }
}
}))
}
