-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
150 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use std::str::FromStr; | ||
use rust_decimal::Decimal; | ||
use sqlx::SqlitePool; | ||
use web3::types::Address; | ||
use erc20_payment_lib::config::Config; | ||
use erc20_payment_lib::runtime::get_token_balance; | ||
use erc20_payment_lib::setup::PaymentSetup; | ||
use erc20_payment_lib_common::{err_custom_create, err_from}; | ||
use erc20_payment_lib_common::error::PaymentError; | ||
use erc20_payment_lib_common::model::TokenTransferDbObj; | ||
use erc20_payment_lib_common::ops::{insert_token_transfer_with_deposit_check, update_token_transfer}; | ||
use erc20_payment_lib_common::utils::{DecimalConvExt, StringConvExt}; | ||
use crate::actions::check_address_name; | ||
use crate::options::TransferOptions; | ||
use erc20_payment_lib_common::error::ErrorBag; | ||
|
||
pub async fn transfer_local( | ||
conn: SqlitePool, | ||
single_transfer_options: TransferOptions, | ||
config: Config, | ||
public_addrs: &[Address], | ||
) -> Result<(), PaymentError> { | ||
log::info!("Adding single transfer..."); | ||
let chain_cfg = config | ||
.chain | ||
.get(&single_transfer_options.chain_name) | ||
.ok_or(err_custom_create!( | ||
"Chain {} not found in config file", | ||
single_transfer_options.chain_name | ||
))?; | ||
|
||
#[allow(clippy::if_same_then_else)] | ||
let token = if single_transfer_options.token == "glm" { | ||
Some(format!("{:#x}", chain_cfg.token.address)) | ||
} else if single_transfer_options.token == "eth" { | ||
None | ||
} else if single_transfer_options.token == "matic" { | ||
//matic is the same as eth | ||
None | ||
} else { | ||
return Err(err_custom_create!( | ||
"Unknown token: {}", | ||
single_transfer_options.token | ||
)); | ||
}; | ||
|
||
let recipient = check_address_name(&single_transfer_options.recipient).unwrap(); | ||
|
||
let public_addr = if let Some(address) = single_transfer_options.address { | ||
address | ||
} else if let Some(account_no) = single_transfer_options.account_no { | ||
*public_addrs | ||
.get(account_no) | ||
.expect("No public adss found with specified account_no") | ||
} else { | ||
*public_addrs.first().expect("No public address found") | ||
}; | ||
//let mut db_transaction = conn.clone().unwrap().begin().await.unwrap(); | ||
|
||
let amount_str = if let Some(amount) = single_transfer_options.amount { | ||
amount.to_u256_from_eth().unwrap().to_string() | ||
} else if single_transfer_options.all { | ||
let payment_setup = PaymentSetup::new_empty(&config)?; | ||
{ | ||
#[allow(clippy::if_same_then_else)] | ||
if single_transfer_options.token == "glm" { | ||
get_token_balance( | ||
payment_setup.get_provider(chain_cfg.chain_id)?, | ||
chain_cfg.token.address, | ||
public_addr, | ||
None, | ||
) | ||
.await? | ||
.to_string() | ||
} else if single_transfer_options.token == "eth" | ||
|| single_transfer_options.token == "matic" | ||
{ | ||
let val = payment_setup | ||
.get_provider(chain_cfg.chain_id)? | ||
.eth_balance(public_addr, None) | ||
.await | ||
.map_err(err_from!())?; | ||
let gas_val = Decimal::from_str(&chain_cfg.max_fee_per_gas.to_string()) | ||
.map_err(|e| err_custom_create!("Failed to convert {e}"))? | ||
* Decimal::from(21500); //leave some room for rounding error | ||
let gas_val = gas_val.to_u256_from_gwei().map_err(err_from!())?; | ||
if gas_val > val { | ||
return Err(err_custom_create!( | ||
"Not enough eth to pay for gas, required: {}, available: {}", | ||
gas_val, | ||
val | ||
)); | ||
} | ||
(val - gas_val).to_string() | ||
} else { | ||
return Err(err_custom_create!( | ||
"Unknown token: {}", | ||
single_transfer_options.token | ||
)); | ||
} | ||
} | ||
} else { | ||
return Err(err_custom_create!("No amount specified")); | ||
}; | ||
let amount_decimal = amount_str.to_eth().unwrap(); | ||
|
||
let mut tt = insert_token_transfer_with_deposit_check( | ||
&conn.clone(), | ||
&TokenTransferDbObj { | ||
id: 0, | ||
payment_id: None, | ||
from_addr: format!("{:#x}", public_addr), | ||
receiver_addr: format!("{:#x}", recipient), | ||
chain_id: chain_cfg.chain_id, | ||
token_addr: token, | ||
token_amount: amount_str, | ||
deposit_id: single_transfer_options.deposit_id, | ||
deposit_finish: 0, | ||
create_date: Default::default(), | ||
tx_id: None, | ||
paid_date: None, | ||
fee_paid: None, | ||
error: None, | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let payment_id = format!("{}_transfer_{}", single_transfer_options.token, tt.id); | ||
tt.payment_id = Some(payment_id.clone()); | ||
update_token_transfer(&conn.clone(), &tt) | ||
.await | ||
.unwrap(); | ||
|
||
log::info!( | ||
"Transfer added to db amount: {}, payment id: {}", | ||
amount_decimal, | ||
payment_id | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters