Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiderson committed Nov 13, 2024
1 parent ba817af commit 8edf71d
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 130 deletions.
66 changes: 33 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ substrate-crypto-light = { git = "https://github.com/Alzymologist/substrate-cryp
# Don't forget to update me in `[dependencies]`!
shadow-rs = { version = "0.35", default-features = false }

[patch."https://github.com/Alzymologist/substrate-crypto-light"]
substrate-crypto-light = { path = "../substrate-crypto-light" }

[profile.release]
strip = true
lto = true
Expand Down
109 changes: 58 additions & 51 deletions src/chain/payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! priority, optimized for lazy and very delayed process, and in some cases might be disabeled
//! altogether (TODO)
use super::definitions::ChainTrackerRequest;
use crate::{
chain::{
definitions::Invoice,
Expand Down Expand Up @@ -58,63 +57,69 @@ pub async fn payout(
let order_amount = Balance::parse(order.amount, order.currency.decimals);

// Payout operation logic
let transactions = if balance.0.abs_diff(order_amount.0) <= loss_tolerance
let (transactions, final_amount) = if balance.0.abs_diff(order_amount.0) <= loss_tolerance
// modulus(balance-order.amount) <= loss_tolerance
{
tracing::info!("Regular withdrawal");
match currency.kind {
TokenKind::Native => {
let balance_transfer_constructor = BalanceTransferConstructor {
amount: order_amount.0,
to_account: &order.recipient,
is_clearing: true,
};
vec![construct_single_balance_transfer_call(
&chain.metadata,
&balance_transfer_constructor,
)?]
}
TokenKind::Asset => {
let asset_transfer_constructor = AssetTransferConstructor {
asset_id: currency.asset_id.ok_or(ChainError::AssetId)?,
amount: order_amount.0,
to_account: &order.recipient,
};
vec![construct_single_asset_transfer_call(
&chain.metadata,
&asset_transfer_constructor,
)?]
}
}
(
match currency.kind {
TokenKind::Native => {
let balance_transfer_constructor = BalanceTransferConstructor {
amount: order_amount.0,
to_account: &order.recipient,
is_clearing: true,
};
vec![construct_single_balance_transfer_call(
&chain.metadata,
&balance_transfer_constructor,
)?]
}
TokenKind::Asset => {
let asset_transfer_constructor = AssetTransferConstructor {
asset_id: currency.asset_id.ok_or(ChainError::AssetId)?,
amount: order_amount.0,
to_account: &order.recipient,
};
vec![construct_single_asset_transfer_call(
&chain.metadata,
&asset_transfer_constructor,
)?]
}
},
order_amount.0,
)
} else {
tracing::info!("Overpayment or forced");
// We will transfer all the available balance
// TODO smarter handling and returns probably

match currency.kind {
TokenKind::Native => {
let balance_transfer_constructor = BalanceTransferConstructor {
amount: balance.0,
to_account: &order.recipient,
is_clearing: true,
};
vec![construct_single_balance_transfer_call(
&chain.metadata,
&balance_transfer_constructor,
)?]
}
TokenKind::Asset => {
let asset_transfer_constructor = AssetTransferConstructor {
asset_id: currency.asset_id.ok_or(ChainError::AssetId)?,
amount: balance.0,
to_account: &order.recipient,
};
vec![construct_single_asset_transfer_call(
&chain.metadata,
&asset_transfer_constructor,
)?]
}
}
(
match currency.kind {
TokenKind::Native => {
let balance_transfer_constructor = BalanceTransferConstructor {
amount: balance.0,
to_account: &order.recipient,
is_clearing: true,
};
vec![construct_single_balance_transfer_call(
&chain.metadata,
&balance_transfer_constructor,
)?]
}
TokenKind::Asset => {
let asset_transfer_constructor = AssetTransferConstructor {
asset_id: currency.asset_id.ok_or(ChainError::AssetId)?,
amount: balance.0,
to_account: &order.recipient,
};
vec![construct_single_asset_transfer_call(
&chain.metadata,
&asset_transfer_constructor,
)?]
}
},
balance.0,
)
};

let mut batch_transaction = construct_batch_transaction(
Expand Down Expand Up @@ -157,7 +162,9 @@ pub async fn payout(
finalized_tx: None,
sender: signer.public(order.id.clone(), 42).await?,
recipient: order.recipient.to_base58_string(42),
amount: Amount::Exact(order.amount),
amount: Amount::Exact(
Balance(final_amount).format(order.currency.decimals),
),
currency: order.currency,
status: TxStatus::Pending,
kind: TxKind::Withdrawal,
Expand Down
2 changes: 1 addition & 1 deletion src/chain/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn deserialize_block_number<'d, D: Deserializer<'d>>(d: D) -> Result<BlockNumber
let n = U256::deserialize(d)?;

n.try_into()
.map_err(|_| de::Error::custom("Try from failed"))
.map_err(|_| de::Error::custom("failed to convert `U256` to a block number"))
}

#[derive(Deserialize)]
Expand Down
Loading

0 comments on commit 8edf71d

Please sign in to comment.