use alloy_primitives::Address;
use serde::{Deserialize, Serialize};
use super::Moves;
use crate::{
Result,
model::{Move, MoveEndpoint, MoveQuote},
transport::{Call, ops},
types::{Amount, Bps, ClientId, MoveId},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MoveRecipient {
Address(Address),
LighterAccount {
index: u64,
owner: Address,
},
}
impl MoveRecipient {
pub(crate) fn wire(&self) -> String {
match self {
Self::Address(address) => format!("{address:#x}"),
Self::LighterAccount { index, .. } => index.to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MoveQuery {
from: MoveEndpoint,
to: MoveEndpoint,
amount_in: Amount,
min_out: Amount,
recipient: MoveRecipient,
payer: Option<Address>,
max_fee_bps: Option<Bps>,
}
impl MoveQuery {
pub fn new(
from: MoveEndpoint,
to: MoveEndpoint,
amount_in: Amount,
min_out: Amount,
recipient: MoveRecipient,
) -> Self {
Self { from, to, amount_in, min_out, recipient, payer: None, max_fee_bps: None }
}
#[must_use]
pub fn payer(mut self, payer: Address) -> Self {
self.payer = Some(payer);
self
}
#[must_use]
pub fn max_fee_bps(mut self, bps: Bps) -> Self {
self.max_fee_bps = Some(bps);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MoveRequest {
pub(crate) client_move_id: ClientId,
pub(crate) from: MoveEndpoint,
pub(crate) to: MoveEndpoint,
pub(crate) amount_in: Amount,
pub(crate) min_out: Amount,
pub(crate) payer: Address,
pub(crate) recipient: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) max_fee_bps: Option<Bps>,
}
impl MoveRequest {
pub fn new(
id: ClientId,
from: MoveEndpoint,
to: MoveEndpoint,
amount_in: Amount,
min_out: Amount,
payer: Address,
recipient: &MoveRecipient,
) -> Self {
Self {
client_move_id: id,
from,
to,
amount_in,
min_out,
payer,
recipient: recipient.wire(),
max_fee_bps: None,
}
}
#[must_use]
pub fn max_fee_bps(mut self, bps: Bps) -> Self {
self.max_fee_bps = Some(bps);
self
}
}
#[derive(Serialize)]
struct CloseBody<'a> {
unallocated: Amount,
#[serde(skip_serializing_if = "Option::is_none")]
unallocated_reason: Option<&'a str>,
}
impl Moves<'_> {
pub async fn quote(&self, query: &MoveQuery) -> Result<MoveQuote> {
let mut call = Call::new(&ops::QUOTE_MOVE);
for (venue, chain, token, endpoint) in [
("from", "from_chain_id", "from_token", &query.from),
("to", "to_chain_id", "to_token", &query.to),
] {
call = call.query(venue, &endpoint.venue);
if let Some(id) = endpoint.chain_id {
call = call.query(chain, id);
}
if let Some(address) = endpoint.token {
call = call.query(token, format_args!("{address:#x}"));
}
}
call = call
.query("amount_in", query.amount_in)
.query("min_out", query.min_out)
.query("recipient", query.recipient.wire());
if let Some(payer) = query.payer {
call = call.query("payer", format_args!("{payer:#x}"));
}
if let Some(bps) = query.max_fee_bps {
call = call.query("max_fee_bps", bps.get());
}
self.inner.send(call, self.auth).await
}
pub async fn create(&self, request: &MoveRequest) -> Result<Move> {
self.inner.send(Call::new(&ops::CREATE_MOVE).json(request)?, self.auth).await
}
pub async fn get(&self, id: MoveId) -> Result<Move> {
self.inner.send(Call::new(&ops::GET_MOVE).segment(id), self.auth).await
}
pub async fn close(
&self,
id: MoveId,
unallocated: Amount,
reason: Option<&str>,
) -> Result<Move> {
let body = CloseBody { unallocated, unallocated_reason: reason };
let call = Call::new(&ops::CLOSE_MOVE).segment(id).json(&body)?;
self.inner.send(call, self.auth).await
}
}
#[cfg(test)]
mod tests;