96 lines
2.9 KiB
Rust
Raw Normal View History

use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse};
2025-10-03 15:59:27 -03:00
use nssa::{
AccountId, PublicTransaction,
2025-10-03 15:59:27 -03:00
program::Program,
public_transaction::{Message, WitnessSet},
};
2025-09-22 16:38:25 +03:00
use super::NativeTokenTransfer;
2025-09-22 16:38:25 +03:00
impl NativeTokenTransfer<'_> {
pub async fn send_public_transfer(
2025-09-22 16:38:25 +03:00
&self,
from: AccountId,
to: AccountId,
2025-09-22 16:38:25 +03:00
balance_to_move: u128,
) -> Result<SendTxResponse, ExecutionFailureKind> {
2026-03-04 18:42:33 +03:00
let balance = self
.0
.get_account_balance(from)
.await
.map_err(ExecutionFailureKind::SequencerError)?;
2025-09-22 16:38:25 +03:00
if balance >= balance_to_move {
2026-03-04 18:42:33 +03:00
let nonces = self
.0
.get_accounts_nonces(vec![from])
.await
.map_err(ExecutionFailureKind::SequencerError)?;
2025-09-22 16:38:25 +03:00
let account_ids = vec![from, to];
2025-10-03 15:59:27 -03:00
let program_id = Program::authenticated_transfer_program().id();
2026-03-18 10:28:52 -04:00
let message = Message::try_new(
program_id,
account_ids,
nonces
.iter()
.map(|x| nssa_core::account::Nonce(*x))
.collect(),
balance_to_move,
)
.unwrap();
2025-09-22 16:38:25 +03:00
let signing_key = self.0.storage.user_data.get_pub_account_signing_key(from);
2025-09-22 16:38:25 +03:00
let Some(signing_key) = signing_key else {
return Err(ExecutionFailureKind::KeyNotFoundError);
};
2025-10-03 15:59:27 -03:00
let witness_set = WitnessSet::for_message(&message, &[signing_key]);
2025-09-22 16:38:25 +03:00
2025-10-03 15:59:27 -03:00
let tx = PublicTransaction::new(message, witness_set);
2025-09-22 16:38:25 +03:00
Ok(self.0.sequencer_client.send_tx_public(tx).await?)
2025-09-22 16:38:25 +03:00
} else {
Err(ExecutionFailureKind::InsufficientFundsError)
}
}
2025-10-10 17:47:23 -03:00
pub async fn register_account(
2025-10-10 17:47:23 -03:00
&self,
from: AccountId,
2025-10-10 17:47:23 -03:00
) -> Result<SendTxResponse, ExecutionFailureKind> {
2026-03-04 18:42:33 +03:00
let nonces = self
.0
.get_accounts_nonces(vec![from])
.await
.map_err(ExecutionFailureKind::SequencerError)?;
2025-10-10 17:47:23 -03:00
let instruction: u128 = 0;
let account_ids = vec![from];
2025-10-10 17:47:23 -03:00
let program_id = Program::authenticated_transfer_program().id();
2026-03-18 10:28:52 -04:00
let message = Message::try_new(
program_id,
account_ids,
nonces
.iter()
.map(|x| nssa_core::account::Nonce(*x))
.collect(),
instruction,
)
.unwrap();
2025-10-10 17:47:23 -03:00
let signing_key = self.0.storage.user_data.get_pub_account_signing_key(from);
2025-10-10 17:47:23 -03:00
let Some(signing_key) = signing_key else {
return Err(ExecutionFailureKind::KeyNotFoundError);
};
let witness_set = WitnessSet::for_message(&message, &[signing_key]);
let tx = PublicTransaction::new(message, witness_set);
Ok(self.0.sequencer_client.send_tx_public(tx).await?)
2025-10-10 17:47:23 -03:00
}
2025-09-22 16:38:25 +03:00
}