Skip to content

Commit

Permalink
Using lock payment contract address from arguments instead of config (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 authored Apr 24, 2024
1 parent 0e693a3 commit 9c9224d
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 143 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/distribute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ jobs:
- name: Distribute ETH
run: |
erc20_processor distribute --amounts "0.0001;0.0001;0.0001;0.0001;0.0001" --recipients "$ETH_ADDRESS_0;$ETH_ADDRESS_1;$ETH_ADDRESS_2;$ETH_ADDRESS_3;$ETH_ADDRESS_4"
erc20_processor distribute --amounts "0.0002;0.0002;0.0002;0.0003;0.0004" --recipients "$ETH_ADDRESS_0;$ETH_ADDRESS_1;$ETH_ADDRESS_2;$ETH_ADDRESS_3;$ETH_ADDRESS_4"
erc20_processor run
env:
ETH_PRIVATE_KEYS: ${{ secrets.HOLESKY_FUND_ENV }}

- name: Wait for blockchain propagate over all RPC nodes (we are talking with multiple RPC nodes)
run: |
sleep 30
- name: Transfer all left ETH tokens
run: |
set -x
erc20_processor show-config > config.toml.tmp
sed 's/^max-fee-per-gas = "20"$/max-fee-per-gas = "1.1"/' config.toml.tmp > config-payments.toml
sed 's/^max-fee-per-gas = "20"$/max-fee-per-gas = "5.1"/' config.toml.tmp > config-payments.toml
erc20_processor transfer --account-no 0 --recipient 0x0079dce233830c7b0cd41116214e17b93c64e030 --token eth --all
erc20_processor transfer --account-no 1 --recipient 0x0079dce233830c7b0cd41116214e17b93c64e030 --token eth --all
erc20_processor transfer --account-no 2 --recipient 0x0079dce233830c7b0cd41116214e17b93c64e030 --token eth --all
Expand Down
80 changes: 41 additions & 39 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ use sqlx::SqlitePool;
use crate::account_balance::{test_balance_loop, BalanceOptions2};
use crate::config::AdditionalOptions;
use crate::contracts::CreateDepositArgs;
use crate::eth::{
get_eth_addr_from_secret, get_latest_block_info, nonce_from_deposit_id, DepositDetails,
};
use crate::eth::{get_eth_addr_from_secret, get_latest_block_info, DepositDetails};
use crate::sender::service_loop;
use crate::utils::{DecimalConvExt, StringConvExt, U256ConvExt};
use chrono::{DateTime, Utc};
use erc20_payment_lib_common::model::TokenTransferDbObj;
use erc20_payment_lib_common::model::{DepositId, TokenTransferDbObj};
use erc20_payment_lib_common::{
DriverEvent, DriverEventContent, FaucetData, SharedInfoTx, StatusProperty,
TransactionStuckReason, Web3RpcPoolContent,
Expand Down Expand Up @@ -385,7 +383,7 @@ pub struct TransferArgs {
pub amount: U256,
pub payment_id: String,
pub deadline: Option<DateTime<Utc>>,
pub deposit_id: Option<String>,
pub deposit_id: Option<DepositId>,
}

impl PaymentRuntime {
Expand Down Expand Up @@ -679,8 +677,7 @@ impl PaymentRuntime {
pub async fn deposit_details(
&self,
chain_name: String,
deposit_id: U256,
lock_contract_address: Address,
deposit_id: DepositId,
) -> Result<DepositDetails, PaymentError> {
let chain_cfg = self
.config
Expand All @@ -693,7 +690,7 @@ impl PaymentRuntime {

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

deposit_details(web3, deposit_id, lock_contract_address).await
deposit_details(web3, deposit_id).await
}

pub async fn get_token_balance(
Expand Down Expand Up @@ -1048,8 +1045,7 @@ impl PaymentRuntime {
&self,
chain_name: &str,
from: Address,
deposit_contract: Address,
deposit_id: U256,
deposit_id: DepositId,
) -> Result<(), PaymentError> {
let chain_cfg = self.config.chain.get(chain_name).ok_or(err_custom_create!(
"Chain {} not found in config file",
Expand All @@ -1063,7 +1059,6 @@ impl PaymentRuntime {
chain_cfg.chain_id as u64,
from,
CloseDepositOptionsInt {
lock_contract_address: deposit_contract,
skip_deposit_check: true,
deposit_id,
token_address: chain_cfg.token.address,
Expand Down Expand Up @@ -1247,15 +1242,14 @@ pub async fn mint_golem_token(

pub async fn deposit_details(
web3: Arc<Web3RpcPool>,
deposit_id: U256,
lock_contract_address: Address,
deposit_id: DepositId,
) -> Result<DepositDetails, PaymentError> {
let block_info = get_latest_block_info(web3.clone()).await?;

let mut result = crate::eth::get_deposit_details(
web3.clone(),
deposit_id,
lock_contract_address,
deposit_id.deposit_id,
deposit_id.lock_address,
Some(block_info.block_number),
)
.await?;
Expand All @@ -1268,16 +1262,14 @@ pub async fn deposit_details(
}

pub struct CloseDepositOptionsInt {
pub lock_contract_address: Address,
pub skip_deposit_check: bool,
pub deposit_id: U256,
pub deposit_id: DepositId,
pub token_address: Address,
}

pub struct TerminateDepositOptionsInt {
pub lock_contract_address: Address,
pub skip_deposit_check: bool,
pub deposit_id: U256,
pub deposit_id: DepositId,
}

pub async fn close_deposit(
Expand All @@ -1289,18 +1281,23 @@ pub async fn close_deposit(
) -> Result<(), PaymentError> {
//let mut block_info: Option<Web3BlockInfo> = None;
if !opt.skip_deposit_check {
let deposit_details =
deposit_details(web3.clone(), opt.deposit_id, opt.lock_contract_address).await?;
let deposit_details = deposit_details(web3.clone(), opt.deposit_id).await?;
if deposit_details.amount_decimal.is_zero() {
log::error!("Deposit {} not found", opt.deposit_id);
log::error!("Deposit {} not found", opt.deposit_id.deposit_id);

return Err(err_custom_create!("Deposit {} not found", opt.deposit_id));
return Err(err_custom_create!(
"Deposit {} not found",
opt.deposit_id.deposit_id
));
}
if deposit_details.spender != from {
log::error!("You are not the spender of deposit {}", opt.deposit_id);
log::error!(
"You are not the spender of deposit {}",
opt.deposit_id.deposit_id
);
return Err(err_custom_create!(
"You are not the spender of deposit {}",
opt.deposit_id
opt.deposit_id.deposit_id
));
}
}
Expand All @@ -1310,7 +1307,7 @@ pub async fn close_deposit(
let current_token_transfers = get_token_transfers_by_deposit_id(
&mut *db_transaction,
chain_id as i64,
&format!("{:#x}", opt.deposit_id),
&opt.deposit_id.to_db_string(),
)
.await
.map_err(err_from!())?;
Expand All @@ -1319,7 +1316,7 @@ pub async fn close_deposit(
if tt.deposit_finish > 0 {
return Err(err_custom_create!(
"Deposit {} already being closed or closed",
opt.deposit_id
opt.deposit_id.deposit_id
));
}
}
Expand Down Expand Up @@ -1348,13 +1345,13 @@ pub async fn close_deposit(
//empty transfer is just a marker that we need deposit to be closed
let new_tt = TokenTransferDbObj {
id: 0,
payment_id: Some(format!("close_deposit_{:#x}", opt.deposit_id)),
payment_id: Some(format!("close_deposit_{:#x}", opt.deposit_id.deposit_id)),
from_addr: format!("{:#x}", from),
receiver_addr: format!("{:#x}", Address::zero()),
chain_id: chain_id as i64,
token_addr: Some(format!("{:#x}", opt.token_address)),
token_amount: "0".to_string(),
deposit_id: Some(format!("{:#x}", opt.deposit_id)),
deposit_id: Some(opt.deposit_id.to_db_string()),
deposit_finish: 1,
create_date: chrono::Utc::now(),
tx_id: None,
Expand Down Expand Up @@ -1387,41 +1384,46 @@ pub async fn terminate_deposit(
//let mut block_info: Option<Web3BlockInfo> = None;
if !opt.skip_deposit_check {
let deposit_id = opt.deposit_id;
let deposit_details =
deposit_details(web3.clone(), deposit_id, opt.lock_contract_address).await?;
let deposit_details = deposit_details(web3.clone(), deposit_id).await?;
if deposit_details.amount_decimal.is_zero() {
log::error!("Deposit {} not found", deposit_id);
log::error!("Deposit {} not found", deposit_id.deposit_id);

return Err(err_custom_create!("Deposit {} not found", deposit_id));
return Err(err_custom_create!(
"Deposit {} not found",
deposit_id.deposit_id
));
}
if deposit_details.funder != from {
log::error!("You are not the funder of deposit {}", opt.deposit_id);
log::error!(
"You are not the funder of deposit {}",
opt.deposit_id.deposit_id
);
return Err(err_custom_create!(
"You are not the funder of deposit {}",
opt.deposit_id
opt.deposit_id.deposit_id
));
}
let est_time_left = (deposit_details.valid_to - Utc::now()).num_seconds();

if est_time_left > 10 {
log::error!(
"Deposit {} is not ready to be terminated. Estimated time left: {}",
deposit_id,
deposit_id.deposit_id,
est_time_left
);
return Err(err_custom_create!(
"Deposit {} is not ready to be terminated. Estimated time left: {}",
deposit_id,
deposit_id.deposit_id,
est_time_left
));
}
}
let free_deposit_tx_id = create_terminate_deposit(
from,
opt.lock_contract_address,
opt.deposit_id.lock_address,
chain_id,
None,
nonce_from_deposit_id(opt.deposit_id),
opt.deposit_id.nonce(),
)?;

let mut db_transaction = conn.begin().await.map_err(err_from!())?;
Expand Down
60 changes: 16 additions & 44 deletions crates/erc20_payment_lib/src/sender/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use sqlx::SqlitePool;
use tokio::sync::mpsc;

use crate::signer::SignerAccount;
use erc20_payment_lib_common::model::TokenTransferDbObj;
use erc20_payment_lib_common::model::{DepositId, TokenTransferDbObj};
use erc20_payment_lib_common::DriverEvent;
use web3::types::{Address, U256};

Expand Down Expand Up @@ -51,7 +51,7 @@ pub async fn gather_transactions_pre(
account: &SignerAccount,
chain_id: i64,
conn: &SqlitePool,
payment_setup: &PaymentSetup,
_payment_setup: &PaymentSetup,
process_tx_needed: &mut bool,
) -> Result<TokenTransferMap, PaymentError> {
let mut transfer_map = TokenTransferMap::new();
Expand Down Expand Up @@ -92,30 +92,17 @@ pub async fn gather_transactions_pre(
deposit_id
);

let deposit_id_u256 = U256::from_str(deposit_id)
let deposit_id_obj = DepositId::from_db_string(deposit_id)
.map_err(|_| err_custom_create!("Invalid deposit id: {}", deposit_id))?;

let lock_contract_address = payment_setup
.chain_setup
.get(&f.chain_id)
.ok_or(err_custom_create!(
"No setup found for chain id: {}",
f.chain_id
))?
.lock_contract_address
.ok_or(err_custom_create!(
"Lock contract address not set for chain id: {}",
f.chain_id
))?;

let from_addr = Address::from_str(&f.from_addr).map_err(err_from!())?;

let close_deposit_tx_id = create_close_deposit(
from_addr,
lock_contract_address,
deposit_id_obj.lock_address,
f.chain_id as u64,
None,
deposit_id_u256,
deposit_id_obj.deposit_id,
)?;

let mut transaction = conn.begin().await.map_err(err_from!())?;
Expand Down Expand Up @@ -283,21 +270,16 @@ pub async fn gather_transactions_batch_multi(
);

if let Some(deposit_id) = token_transfer.deposit_id.as_ref() {
let lock_contract_address =
chain_setup.lock_contract_address.ok_or(err_custom_create!(
"Lock contract address not set for chain id: {}",
token_transfer.chain_id
))?;
let deposit_id = U256::from_str(deposit_id)
let deposit_id_obj = DepositId::from_db_string(deposit_id)
.map_err(|err| err_custom_create!("Invalid deposit id: {}", err))?;
create_erc20_deposit_transfer(SingleTransferDepositArgs {
from: Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
lock_contract: lock_contract_address,
lock_contract: deposit_id_obj.lock_address,
erc20_to: erc20_to[0],
erc20_amount: erc20_amounts[0],
chain_id: token_transfer.chain_id as u64,
gas_limit: None,
deposit_id,
deposit_id: deposit_id_obj.deposit_id,
deposit_finish: is_deposit_finish,
})?
} else {
Expand All @@ -311,27 +293,22 @@ pub async fn gather_transactions_batch_multi(
)?
}
} else if let Some(deposit_id) = token_transfer.deposit_id.as_ref() {
let lock_contract_address =
chain_setup.lock_contract_address.ok_or(err_custom_create!(
"Lock contract address not set for chain id: {}",
token_transfer.chain_id
))?;
let deposit_id = U256::from_str(deposit_id)
let deposit_id_obj = DepositId::from_db_string(deposit_id)
.map_err(|err| err_custom_create!("Invalid deposit id: {}", err))?;
log::info!(
"Inserting transaction stub for ERC20 multi payment: {:?} for {} distinct transfers",
lock_contract_address,
deposit_id_obj.lock_address,
erc20_to.len(),
);

create_erc20_transfer_multi_deposit(MultiTransferDepositArgs {
from: Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
lock_contract: lock_contract_address,
lock_contract: deposit_id_obj.lock_address,
erc20_to,
erc20_amount: erc20_amounts,
chain_id: token_transfer.chain_id as u64,
gas_limit: None,
deposit_id,
deposit_id: deposit_id_obj.deposit_id,
deposit_finish: is_deposit_finish,
})?
} else if let Some(multi_contract_address) = chain_setup.multi_contract_address {
Expand Down Expand Up @@ -393,7 +370,7 @@ pub async fn gather_transactions_batch(
sum += U256::from_dec_str(&token_transfer.token_amount).map_err(err_from!())?;
}

let Some(chain_setup) = payment_setup.chain_setup.get(&token_transfer.chain_id) else {
let Some(_chain_setup) = payment_setup.chain_setup.get(&token_transfer.chain_id) else {
return Err(err_custom_create!(
"No setup found for chain id: {}",
token_transfer.chain_id
Expand All @@ -404,23 +381,18 @@ pub async fn gather_transactions_batch(

let web3tx = if let Some(token_addr) = token_transfer.token_addr.as_ref() {
if let Some(deposit_id) = token_transfer.deposit_id.as_ref() {
let lock_contract_address =
chain_setup.lock_contract_address.ok_or(err_custom_create!(
"Lock contract address not set for chain id: {}",
token_transfer.chain_id
))?;
let deposit_id = U256::from_str(deposit_id)
let deposit_id_obj = DepositId::from_db_string(deposit_id)
.map_err(|err| err_custom_create!("Invalid deposit id: {}", err))?;

let is_deposit_finish = token_transfers.iter().any(|f| f.deposit_finish > 0);
create_erc20_deposit_transfer(SingleTransferDepositArgs {
from: Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
lock_contract: lock_contract_address,
lock_contract: deposit_id_obj.lock_address,
erc20_to: Address::from_str(&token_transfer.receiver_addr).map_err(err_from!())?,
erc20_amount: sum,
chain_id: token_transfer.chain_id as u64,
gas_limit: None,
deposit_id,
deposit_id: deposit_id_obj.deposit_id,
deposit_finish: is_deposit_finish,
})?
} else {
Expand Down
Loading

0 comments on commit 9c9224d

Please sign in to comment.