Skip to content

Commit

Permalink
fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Oct 24, 2023
1 parent d85fadb commit 6683634
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
20 changes: 15 additions & 5 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::signer::Signer;
use crate::transaction::{create_token_transfer, find_receipt_extended};
use crate::{err_custom_create, err_from};
use std::collections::BTreeMap;
use std::ops::{DerefMut};
use std::ops::DerefMut;
use std::path::Path;
use std::str::FromStr;

Expand Down Expand Up @@ -582,14 +582,23 @@ impl PaymentRuntime {
sender: Address,
receiver: Address,
amount: U256,
glm_address: Address
glm_address: Address,
) -> Result<VerifyTransactionResult, PaymentError> {
let network_name = self.network_name(chain_id).ok_or(err_custom_create!(
"Chain {} not found in config file",
chain_id
))?;
let prov = self.get_web3_provider(network_name).await?;
verify_transaction(&prov, chain_id, tx_hash, sender, receiver, amount, glm_address).await
verify_transaction(
&prov,
chain_id,
tx_hash,
sender,
receiver,
amount,
glm_address,
)
.await
}

pub fn chains(&self) -> Vec<i64> {
Expand All @@ -610,9 +619,10 @@ pub async fn verify_transaction(
sender: Address,
receiver: Address,
amount: U256,
glm_address: Address
glm_address: Address,
) -> Result<VerifyTransactionResult, PaymentError> {
let (chain_tx_dao, transfers) = find_receipt_extended(web3, tx_hash, chain_id, glm_address).await?;
let (chain_tx_dao, transfers) =
find_receipt_extended(web3, tx_hash, chain_id, glm_address).await?;
if chain_tx_dao.chain_status == 1 {
//one transaction can contain multiple transfers. Search for ours.
for transfer in transfers {
Expand Down
27 changes: 16 additions & 11 deletions crates/erc20_payment_lib/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -13,12 +12,12 @@ use crate::utils::{ConversionError, U256ConvExt};
use crate::setup::{ChainSetup, PaymentSetup};
use crate::{err_custom_create, err_from};

use crate::contracts::encode_erc20_balance_of;
use crate::runtime::SharedState;
use sqlx::SqlitePool;
use web3::transports::Http;
use web3::types::{Address, BlockNumber, CallRequest, U256};
use web3::Web3;
use crate::contracts::encode_erc20_balance_of;

pub async fn add_payment_request_2(
conn: &SqlitePool,
Expand Down Expand Up @@ -82,7 +81,7 @@ pub async fn transaction_from_chain_and_into_db(
conn: &SqlitePool,
chain_id: i64,
tx_hash: &str,
glm_address: Address
glm_address: Address,
) -> Result<Option<ChainTxDao>, PaymentError> {
println!("tx_hash: {tx_hash}");
let tx_hash = web3::types::H256::from_str(tx_hash)
Expand All @@ -97,7 +96,8 @@ pub async fn transaction_from_chain_and_into_db(
return Ok(Some(chain_tx));
}

let (mut chain_tx_dao, transfers) = find_receipt_extended(web3, tx_hash, chain_id, glm_address).await?;
let (mut chain_tx_dao, transfers) =
find_receipt_extended(web3, tx_hash, chain_id, glm_address).await?;

if chain_tx_dao.chain_status != 1 {
return Ok(None);
Expand Down Expand Up @@ -149,13 +149,17 @@ pub async fn transaction_from_chain_and_into_db(
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
},
Some(web3::types::BlockId::Number(BlockNumber::Number(chain_tx_dao.block_number.into()))),
Some(web3::types::BlockId::Number(BlockNumber::Number(
chain_tx_dao.block_number.into(),
))),
)
.await {
.await
{
Ok(v) => {
if v.0.len() == 32 {
break Some(U256::from_big_endian(&v.0))
} }
break Some(U256::from_big_endian(&v.0));
}
}
Err(e) => {
log::debug!("Error getting token balance: {}", e);
}
Expand All @@ -168,11 +172,13 @@ pub async fn transaction_from_chain_and_into_db(

log::info!(
"Token balance: {:.5} for block {}",
token_balance.map(|v| v.to_eth().unwrap()).unwrap_or_default(),
token_balance
.map(|v| v.to_eth().unwrap())
.unwrap_or_default(),
chain_tx_dao.block_number
);

chain_tx_dao.balance_eth = balance.map(|b|b.to_string());
chain_tx_dao.balance_eth = balance.map(|b| b.to_string());
chain_tx_dao.balance_glm = token_balance.map(|v| v.to_string());

let mut db_transaction = conn.begin().await.map_err(err_from!())?;
Expand Down Expand Up @@ -218,7 +224,6 @@ pub async fn transaction_from_chain_and_into_db(
}
}


db_transaction.commit().await.map_err(err_from!())?;
log::info!("Transaction found and parsed successfully: {}", tx.id);
Ok(Some(tx))
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub async fn find_receipt_extended(
web3: &Web3<Http>,
tx_hash: H256,
chain_id: i64,
glm_address: Address
glm_address: Address,
) -> Result<(ChainTxDao, Vec<ChainTransferDao>), PaymentError> {
let mut chain_tx_dao = ChainTxDao {
id: -1,
Expand Down

0 comments on commit 6683634

Please sign in to comment.