Skip to content

Commit

Permalink
Add multiple deposit feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Oct 30, 2024
1 parent 7b15c73 commit ee556c1
Show file tree
Hide file tree
Showing 21 changed files with 1,262 additions and 253 deletions.
2 changes: 1 addition & 1 deletion clients/apps/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async fn main() -> Result<()> {

let force_send = force_send.unwrap_or(false);

mercuryrustlib::transfer_sender::execute(&client_config, &to_address, &wallet_name, &statechain_id, force_send, batch_id).await?;
mercuryrustlib::transfer_sender::execute(&client_config, &to_address, &wallet_name, &statechain_id, None, force_send, batch_id).await?;

let obj = json!({"Transfer": "sent"});

Expand Down
26 changes: 25 additions & 1 deletion clients/libs/rust/src/coin_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ async fn check_deposit(client_config: &ClientConfig, coin: &mut Coin, wallet_net
let utxo_txid = utxo.tx_hash.to_string();
let utxo_vout = utxo.tx_pos as u32;

let backup_tx = create_tx1(client_config, coin, wallet_netwotk, &utxo_txid, utxo_vout).await?;
if coin.status != CoinStatus::INITIALISED {
return Err(anyhow!("The coin with the public key {} is not in the INITIALISED state", coin.user_pubkey.to_string()));
}

coin.utxo_txid = Some(utxo_txid.to_string());
coin.utxo_vout = Some(utxo_vout);

coin.status = CoinStatus::IN_MEMPOOL;

let backup_tx = create_tx1(client_config, coin, wallet_netwotk, 1u32).await?;

let activity_utxo = format!("{}:{}", utxo.tx_hash.to_string(), utxo.tx_pos);

Expand Down Expand Up @@ -257,6 +266,21 @@ pub async fn update_coins(client_config: &ClientConfig, wallet_name: &str) -> Re

wallet.coins.extend(duplicated_coins);

// invalidate duplicated coins that were not transferred
for i in 0..wallet.coins.len() {
if wallet.coins[i].status == CoinStatus::DUPLICATED {
let is_transferred = (0..wallet.coins.len()).any(|j|
i != j && // Skip comparing with self
wallet.coins[j].statechain_id == wallet.coins[i].statechain_id &&
wallet.coins[j].locktime == wallet.coins[i].locktime &&
wallet.coins[j].status == CoinStatus::TRANSFERRED
);
if is_transferred {
wallet.coins[i].status = CoinStatus::INVALIDATED;
}
}
}

update_wallet(&client_config.pool, &wallet).await?;

Ok(())
Expand Down
19 changes: 4 additions & 15 deletions clients/libs/rust/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result, Ok};
use mercurylib::{deposit::{create_deposit_msg1, create_aggregated_address}, wallet::{Wallet, BackupTx, CoinStatus, Coin}, transaction:: get_user_backup_address, utils::get_blockheight};
use mercurylib::{deposit::{create_deposit_msg1, create_aggregated_address}, wallet::{Wallet, BackupTx, Coin}, transaction:: get_user_backup_address, utils::get_blockheight};

use crate::{client_config::ClientConfig, sqlite_manager::{get_wallet, update_wallet}, transaction::new_transaction, utils::info_config};

Expand All @@ -23,19 +23,8 @@ pub async fn get_deposit_bitcoin_address(client_config: &ClientConfig, wallet_na
Ok(aggregated_public_key.aggregate_address)
}

pub async fn create_tx1(client_config: &ClientConfig, coin: &mut Coin, wallet_netwotk: &str, tx0_hash: &str, tx0_vout: u32) -> Result<BackupTx> {

if coin.status != CoinStatus::INITIALISED {
return Err(anyhow!("The coin with the public key {} is not in the INITIALISED state", coin.user_pubkey.to_string()));
}

if coin.utxo_txid.is_some() && coin.utxo_vout.is_some() {
return Err(anyhow!("The coin with the public key {} has already been deposited", coin.user_pubkey.to_string()));
}
coin.utxo_txid = Some(tx0_hash.to_string());
coin.utxo_vout = Some(tx0_vout);

coin.status = CoinStatus::IN_MEMPOOL;
// When sending duplicated coins, the tx_n of the backup_tx must be different
pub async fn create_tx1(client_config: &ClientConfig, coin: &mut Coin, wallet_netwotk: &str, tx_n: u32) -> Result<BackupTx> {

let to_address = get_user_backup_address(&coin, wallet_netwotk.to_string())?;

Expand Down Expand Up @@ -73,7 +62,7 @@ pub async fn create_tx1(client_config: &ClientConfig, coin: &mut Coin, wallet_ne
}

let backup_tx = BackupTx {
tx_n: 1,
tx_n,
tx: signed_tx,
client_public_nonce: coin.public_nonce.as_ref().unwrap().to_string(),
server_public_nonce: coin.server_public_nonce.as_ref().unwrap().to_string(),
Expand Down
1 change: 1 addition & 0 deletions clients/libs/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use mercurylib::wallet::CoinStatus;
pub use mercurylib::wallet::Coin;
pub use mercurylib::wallet::BackupTx;
pub use mercurylib::wallet::Activity;
pub use mercurylib::wallet::get_previous_outpoint;

pub use mercurylib::transfer::sender::{TransferSenderRequestPayload, TransferSenderResponsePayload, create_transfer_signature, create_transfer_update_msg};
pub use mercurylib::transaction::{SignFirstRequestPayload, SignFirstResponsePayload, create_and_commit_nonces};
Expand Down
Loading

0 comments on commit ee556c1

Please sign in to comment.