Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Oct 24, 2023
1 parent 6683634 commit a387ee1
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 54 deletions.
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct Chain {
pub priority_fee: f64,
pub max_fee_per_gas: f64,
pub gas_left_warning_limit: u64,
pub token: Option<Token>,
pub token: Token,
pub multi_contract: Option<MultiContractSettings>,
pub transaction_timeout: u64,
pub confirmation_blocks: u64,
Expand Down
23 changes: 6 additions & 17 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,7 @@ impl PaymentRuntime {
chain_name
))?;

let token_address = chain_cfg
.token
.as_ref()
.ok_or(err_custom_create!(
"Chain {} doesn't define a token",
chain_name
))?
.address;
let token_address = chain_cfg.token.address;

let web3 = self.setup.get_provider(chain_cfg.chain_id)?;

Expand Down Expand Up @@ -534,14 +527,7 @@ impl PaymentRuntime {

let token_addr = match tx_type {
TransferType::Token => {
let address = chain_cfg
.token
.as_ref()
.ok_or(err_custom_create!(
"Chain {} doesn't define its token",
chain_name
))?
.address;
let address = chain_cfg.token.address;
Some(address)
}
TransferType::Gas => None,
Expand Down Expand Up @@ -582,12 +568,15 @@ impl PaymentRuntime {
sender: Address,
receiver: Address,
amount: U256,
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 glm_address = self
.get_chain(chain_id)
.ok_or(err_custom_create!("Chain {} not found", chain_id))?
.glm_address;
let prov = self.get_web3_provider(network_name).await?;
verify_transaction(
&prov,
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ pub async fn faucet(data: Data<Box<ServerData>>, req: HttpRequest) -> impl Respo
}
}

let glm_address = return_on_error!(chain.glm_address.ok_or("GLM address not set on chain"));
let glm_address = chain.glm_address;

let from_secret = return_on_error!(data
.payment_setup
Expand Down
10 changes: 2 additions & 8 deletions crates/erc20_payment_lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::error::{ErrorBag, PaymentError};
use crate::transaction::find_receipt_extended;
use crate::utils::{ConversionError, U256ConvExt};

use crate::err_from;
use crate::setup::{ChainSetup, PaymentSetup};
use crate::{err_custom_create, err_from};

use crate::contracts::encode_erc20_balance_of;
use crate::runtime::SharedState;
Expand Down Expand Up @@ -59,13 +59,7 @@ pub async fn add_glm_request(
from_addr: format!("{payer_addr:#x}"),
receiver_addr: format!("{receiver_addr:#x}"),
chain_id: chain_setup.chain_id,
token_addr: Some(format!(
"{:#x}",
chain_setup.glm_address.ok_or(err_custom_create!(
"GLM address not set for chain {}",
chain_setup.chain_id
))?
)),
token_addr: Some(format!("{:#x}", chain_setup.glm_address)),
token_amount: token_amount.to_string(),
tx_hash: None,
requested_date: chrono::Utc::now(),
Expand Down
11 changes: 3 additions & 8 deletions crates/erc20_payment_lib/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct ChainSetup {
pub max_fee_per_gas: U256,
pub gas_left_warning_limit: u64,
pub priority_fee: U256,
pub glm_address: Option<Address>,
pub glm_address: Address,
pub multi_contract_address: Option<Address>,
pub multi_contract_max_at_once: usize,
pub transaction_timeout: u64,
Expand Down Expand Up @@ -131,13 +131,8 @@ impl PaymentSetup {
max_fee_per_gas: gwei_to_u256(chain_config.1.max_fee_per_gas)
.map_err(err_from!())?,
priority_fee: gwei_to_u256(chain_config.1.priority_fee).map_err(err_from!())?,
glm_address: chain_config.1.token.clone().map(|t| t.address),
currency_glm_symbol: chain_config
.1
.token
.clone()
.map(|t| t.symbol)
.unwrap_or_else(|| "GLM".to_string()),
glm_address: chain_config.1.token.address,
currency_glm_symbol: chain_config.1.token.symbol.clone(),
multi_contract_address: chain_config
.1
.multi_contract
Expand Down
4 changes: 1 addition & 3 deletions crates/erc20_payment_lib_extra/src/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ pub async fn account_balance(
Some(
chain_cfg
.token
.clone()
.ok_or(err_custom_create!("Token not found in config"))?
.address,
)
} else {
Expand Down Expand Up @@ -139,7 +137,7 @@ pub async fn account_balance(
format!(
"{:.03} {}",
(f64::from_str(&v).unwrap_or(0.0) * 1000.0).floor() / 1000.0,
&chain_cfg.token.clone().unwrap().symbol
&chain_cfg.token.symbol
)
});
result_map.borrow_mut().insert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub async fn generate_test_payments(
Rc::new(RefCell::new(rng)),
chain_cfg.chain_id,
&from_addrs,
Some(chain_cfg.token.clone().unwrap().address),
Some(chain_cfg.token.address),
&addr_pool,
generate_options.random_receivers,
&amount_pool,
Expand Down
4 changes: 2 additions & 2 deletions crates/erc20_payment_lib_test/src/config_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub async fn create_default_config_setup(proxy_url_base: &str, proxy_key: &str)
priority_fee: 1.1,
max_fee_per_gas: 500.0,
gas_left_warning_limit: 1000000,
token: Some(Token {
token: Token {
symbol: "tGLM".to_string(),
address: Address::from_str("0xfff17584d526aba263025eE7fEF517E4A31D4246").unwrap(),
faucet: None,
}),
},
multi_contract: Some(MultiContractSettings {
address: Address::from_str("0xF9861F83766CD507E0d2749B60d4fD6C68E5B96C").unwrap(),
max_at_once: 10,
Expand Down
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async fn main_internal() -> Result<(), PaymentError> {

#[allow(clippy::if_same_then_else)]
let token = if single_transfer_options.token == "glm" {
Some(format!("{:#x}", chain_cfg.token.clone().unwrap().address))
Some(format!("{:#x}", chain_cfg.token.address))
} else if single_transfer_options.token == "eth" {
None
} else if single_transfer_options.token == "matic" {
Expand Down Expand Up @@ -376,7 +376,7 @@ async fn main_internal() -> Result<(), PaymentError> {

let txs = import_erc20_txs(
web3,
chain_cfg.token.clone().unwrap().address,
chain_cfg.token.address,
chain_cfg.chain_id,
Some(&[sender]),
None,
Expand All @@ -394,7 +394,7 @@ async fn main_internal() -> Result<(), PaymentError> {
&conn,
chain_cfg.chain_id,
&format!("{tx:#x}"),
chain_cfg.token.clone().unwrap().address,
chain_cfg.token.address,
)
.await
{
Expand Down Expand Up @@ -452,17 +452,15 @@ async fn main_internal() -> Result<(), PaymentError> {
token_transfer.chain_id
))?;

if let (Some(token_chain_cfg), Some(token_addr)) =
(&chain_cfg.token, &token_transfer.token_addr)
if let Some(token_addr) = &token_transfer.token_addr
{
if format!("{:#x}", token_chain_cfg.address)
!= token_addr.to_lowercase()
if format!("{:#x}", chain_cfg.token.address) != token_addr.to_lowercase()
{
return Err(err_custom_create!(
"Token address in line {} is different from default token address {} != {:#x}",
line_no,
token_addr.to_lowercase(),
token_chain_cfg.address
chain_cfg.token.address
));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub async fn run_stats(
let token_transferred = stats
.all
.erc20_token_transferred
.get(&chain_cfg.token.clone().unwrap().address)
.get(&chain_cfg.token.address)
.copied();

metrics += &format!(
Expand Down Expand Up @@ -269,7 +269,7 @@ pub async fn run_stats(
.1
.all
.erc20_token_transferred
.get(&chain_cfg.token.clone().unwrap().address)
.get(&chain_cfg.token.address)
.copied();
println!(
"Erc20 token sent: {}",
Expand Down
2 changes: 1 addition & 1 deletion tests/docker_01_basic/single_erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn test_erc20_transfer() -> Result<(), anyhow::Error> {
(fee_paid, tx_dao_return)
});
let config = create_default_config_setup(&proxy_url_base, proxy_key).await;
let token_address = config.chain.get("dev").unwrap().token.clone().unwrap().address;
let token_address = config.chain.get("dev").unwrap().token.address;
let web3 = {
//load private key for account 0xbfb29b133aa51c4b45b49468f9a22958eafea6fa
let private_keys = load_private_keys("0228396638e32d52db01056c00e19bc7bd9bb489e2970a3a7a314d67e55ee963")?;
Expand Down
4 changes: 2 additions & 2 deletions tests/docker_04_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool
("0x0000000000000000000000000000000000000009", 600000000000000678_u128)];

let config = create_default_config_setup(&proxy_url_base, proxy_key).await;
let token_address = config.chain.get("dev").unwrap().token.clone().unwrap().address;
let token_address = config.chain.get("dev").unwrap().token.address;
let web3 = {
//config.chain.get_mut("dev").unwrap().confirmation_blocks = 0;

Expand All @@ -116,7 +116,7 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool
Address::from_str(addr).unwrap(),
config.chain.get("dev").unwrap().chain_id,
Some("test_payment"),
Some(config.chain.get("dev").unwrap().token.clone().unwrap().address),
Some(config.chain.get("dev").unwrap().token.address),
U256::from(*val),
)
).await?;
Expand Down

0 comments on commit a387ee1

Please sign in to comment.