Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3 #83

Merged
merged 11 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "src/main.rs"
name = "erc20_processor"
description = "Payment processor for ERC20 tokens"
authors = ["Sieciech Czajka <[email protected]>"]
version = "0.2.2"
version = "0.3.0"
edition = "2021"
license = "MIT"

Expand Down Expand Up @@ -99,8 +99,8 @@ eth-keystore = { workspace = true }
stream-rate-limiter = { workspace = true }
fastrand = { workspace = true }

erc20_payment_lib = { path = "crates/erc20_payment_lib", version = "0.2.1" }
erc20_payment_lib_extra = { path = "crates/erc20_payment_lib_extra", version = "0.2.1" }
erc20_payment_lib = { path = "crates/erc20_payment_lib", version = "0.3.0" }
erc20_payment_lib_extra = { path = "crates/erc20_payment_lib_extra", version = "0.3.0" }

[dev-dependencies]
bollard = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions config-payments.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[engine]
service-sleep = 10
process-sleep = 10
# service sleep is for internal runtime checks
service-sleep = 5
# process sleep is to set how often payments are gathered
gather-sleep = 600
# gather payments on payment driver start (otherwise wait for first gather-sleep)
gather-at-start = true
automatic-recover = false

[chain.rinkeby]
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "erc20_payment_lib"
description = "Payment processor for ERC20 tokens"
authors = ["Sieciech Czajka <[email protected]>"]
version = "0.2.2"
version = "0.3.0"
edition = "2021"
license = "MIT"

Expand Down
3 changes: 2 additions & 1 deletion crates/erc20_payment_lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl AdditionalOptions {
#[serde(rename_all = "kebab-case")]
pub struct Engine {
pub service_sleep: u64,
pub process_sleep: u64,
pub gather_sleep: u64,
pub gather_at_start: bool,
pub automatic_recover: bool,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/erc20_payment_lib/src/db/model/tx_dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct TxDao {
pub to_addr: String,
pub chain_id: i64,
pub gas_limit: Option<i64>,
pub max_fee_per_gas: String,
pub priority_fee: String,
pub max_fee_per_gas: Option<String>,
pub priority_fee: Option<String>,
pub val: String,
pub nonce: Option<i64>,
pub processing: i64,
Expand Down
13 changes: 4 additions & 9 deletions crates/erc20_payment_lib/src/db/ops/token_transfer_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,10 @@ pub async fn get_transfer_count(
.fetch_one(conn)
.await?
} else if let Some(receiver) = receiver {
sqlx::query_scalar::<_, i64>(
format!(
r"SELECT COUNT(*) FROM token_transfer WHERE {transfer_filter} AND receiver_addr = $1"
)
.as_str(),
)
.bind(receiver)
.fetch_one(conn)
.await?
sqlx::query_scalar::<_, i64>(format!(r"SELECT COUNT(*) FROM token_transfer WHERE {transfer_filter} AND receiver_addr = $1").as_str())
.bind(receiver)
.fetch_one(conn)
.await?
} else {
sqlx::query_scalar::<_, i64>(
format!(r"SELECT COUNT(*) FROM token_transfer WHERE {transfer_filter}").as_str(),
Expand Down
22 changes: 11 additions & 11 deletions crates/erc20_payment_lib/src/db/ops/transfer_in_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub async fn insert_transfer_in(
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
",
)
.bind(&token_transfer.payment_id)
.bind(&token_transfer.from_addr)
.bind(&token_transfer.receiver_addr)
.bind(token_transfer.chain_id)
.bind(&token_transfer.token_addr)
.bind(&token_transfer.token_amount)
.bind(&token_transfer.tx_hash)
.bind(token_transfer.requested_date)
.bind(token_transfer.received_date)
.fetch_one(conn)
.await?;
.bind(&token_transfer.payment_id)
.bind(&token_transfer.from_addr)
.bind(&token_transfer.receiver_addr)
.bind(token_transfer.chain_id)
.bind(&token_transfer.token_addr)
.bind(&token_transfer.token_amount)
.bind(&token_transfer.tx_hash)
.bind(token_transfer.requested_date)
.bind(token_transfer.received_date)
.fetch_one(conn)
.await?;
Ok(res)
}

Expand Down
10 changes: 6 additions & 4 deletions crates/erc20_payment_lib/src/db/ops/tx_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ async fn tx_test() -> sqlx::Result<()> {

let mut tx_to_insert = TxDao {
id: -1,
tx_hash: Some("0x13d8a54dec1c0a30f1cd5129f690c3e27b9aadd59504957bad4d247966dadae7".to_string()),
tx_hash: Some(
"0x13d8a54dec1c0a30f1cd5129f690c3e27b9aadd59504957bad4d247966dadae7".to_string(),
),
signed_raw_data: None,
signed_date: Some(chrono::Utc::now()),
broadcast_date: Some(chrono::Utc::now()),
Expand All @@ -263,8 +265,8 @@ async fn tx_test() -> sqlx::Result<()> {
to_addr: "0xbcfe9736a4f5bf2e43620061ff3001ea0d003c0f".to_string(),
chain_id: 987789,
gas_limit: Some(100000),
max_fee_per_gas: "110000000000".to_string(),
priority_fee: "5110000000000".to_string(),
max_fee_per_gas: Some("110000000000".to_string()),
priority_fee: Some("5110000000000".to_string()),
val: "0".to_string(),
nonce: Some(1),
processing: 0,
Expand All @@ -278,7 +280,7 @@ async fn tx_test() -> sqlx::Result<()> {
engine_message: None,
engine_error: None,
first_processed: None,
confirm_date: None
confirm_date: None,
};

let tx_from_insert = insert_tx(&conn, &tx_to_insert).await?;
Expand Down
38 changes: 2 additions & 36 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,41 +168,6 @@ impl SharedState {
}
}

#[derive(Clone)]
pub struct ValidatedOptions {
pub receivers: Vec<Address>,
pub amounts: Vec<U256>,
pub chain_id: i64,
pub token_addr: Option<Address>,
pub keep_running: bool,
pub generate_tx_only: bool,
pub skip_multi_contract_check: bool,
pub service_sleep: u64,
pub process_sleep: u64,
pub http_threads: u64,
pub http_port: u16,
pub http_addr: String,
}

impl Default for ValidatedOptions {
fn default() -> Self {
ValidatedOptions {
receivers: vec![],
amounts: vec![],
chain_id: 80001,
token_addr: None,
keep_running: true,
generate_tx_only: false,
skip_multi_contract_check: false,
service_sleep: 10,
process_sleep: 10,
http_threads: 2,
http_port: 8080,
http_addr: "127.0.0.1".to_string(),
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum StatusProperty {
InvalidChainId {
Expand Down Expand Up @@ -398,7 +363,8 @@ impl PaymentRuntime {
options.generate_tx_only,
options.skip_multi_contract_check,
config.engine.service_sleep,
config.engine.process_sleep,
config.engine.gather_sleep,
config.engine.gather_at_start,
config.engine.automatic_recover,
)?;
payment_setup.use_transfer_for_single_payment = options.use_transfer_for_single_payment;
Expand Down
5 changes: 0 additions & 5 deletions crates/erc20_payment_lib/src/sender/allowance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ pub async fn process_allowance(
signer: &impl Signer,
) -> Result<u32, PaymentError> {
let minimum_allowance: U256 = U256::max_value() / U256::from(2);
let chain_setup = payment_setup.get_chain_setup(allowance_request.chain_id)?;
let web3 = payment_setup.get_provider(allowance_request.chain_id)?;
let max_fee_per_gas = chain_setup.max_fee_per_gas;
let priority_fee = chain_setup.priority_fee;

let mut db_allowance = find_allowance(
conn,
Expand Down Expand Up @@ -129,8 +126,6 @@ pub async fn process_allowance(
Address::from_str(&allowance_request.spender_addr).map_err(err_from!())?,
allowance_request.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
)?;
let mut db_transaction = conn.begin().await.map_err(err_from!())?;
let web3_tx_dao = insert_tx(&mut *db_transaction, &approve_tx)
Expand Down
34 changes: 17 additions & 17 deletions crates/erc20_payment_lib/src/sender/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ pub async fn gather_transactions_batch_multi(
multi_order_vector: &mut [TokenTransferMultiOrder],
token_transfer: &TokenTransferMultiKey,
) -> Result<u32, PaymentError> {
let chain_setup = payment_setup.get_chain_setup(token_transfer.chain_id)?;
let chain_setup = payment_setup
.chain_setup
.get(&token_transfer.chain_id)
.ok_or(err_custom_create!(
"No setup found for chain id: {}",
token_transfer.chain_id
))?;

let use_direct_method = payment_setup.contract_use_direct_method;
let use_unpacked_method = payment_setup.contract_use_unpacked_method;

let max_fee_per_gas = chain_setup.max_fee_per_gas;
let priority_fee = chain_setup.priority_fee;

let max_per_batch = chain_setup.multi_contract_max_at_once;
log::debug!("Processing token transfer {:?}", token_transfer);
if let Some(token_addr) = token_transfer.token_addr.as_ref() {
Expand Down Expand Up @@ -212,11 +215,13 @@ pub async fn gather_transactions_batch_multi(
erc20_amounts[0],
token_transfer.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
)?
} else if let Some(multi_contract_address) = chain_setup.multi_contract_address {
log::info!("Inserting transaction stub for ERC20 multi transfer contract: {:?} for {} distinct transfers", multi_contract_address, erc20_to.len());
log::info!(
"Inserting transaction stub for ERC20 multi transfer contract: {:?} for {} distinct transfers",
multi_contract_address,
erc20_to.len()
);

create_erc20_transfer_multi(
Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
Expand All @@ -225,8 +230,6 @@ pub async fn gather_transactions_batch_multi(
erc20_amounts,
token_transfer.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
use_direct_method,
use_unpacked_method,
)?
Expand Down Expand Up @@ -272,7 +275,11 @@ pub async fn gather_transactions_batch(
sum += U256::from_dec_str(&token_transfer.token_amount).map_err(err_from!())?;
}

let Ok(chain_setup) = payment_setup.get_chain_setup(token_transfer.chain_id) else {
if payment_setup
.chain_setup
.get(&token_transfer.chain_id)
.is_none()
{
send_driver_event(
&event_sender,
DriverEventContent::TransactionFailed(TransactionFailedReason::InvalidChainId(
Expand All @@ -286,9 +293,6 @@ pub async fn gather_transactions_batch(
));
};

let max_fee_per_gas = chain_setup.max_fee_per_gas;
let priority_fee = chain_setup.priority_fee;

log::debug!("Processing token transfer {:?}", token_transfer);
let web3tx = if let Some(token_addr) = token_transfer.token_addr.as_ref() {
create_erc20_transfer(
Expand All @@ -298,17 +302,13 @@ pub async fn gather_transactions_batch(
sum,
token_transfer.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
)?
} else {
create_eth_transfer(
Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
Address::from_str(&token_transfer.receiver_addr).map_err(err_from!())?,
token_transfer.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
sum,
)
};
Expand Down
Loading
Loading