Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Mar 15, 2024
1 parent d1cadd1 commit d82a363
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/erc20_payment_lib/config-payments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ token = { address = "0x8888888815bf4DB87e57B609A50f938311EEd068", symbol = "tGLM
multi-contract = { address = "0xAaAAAaA00E1841A63342db7188abA84BDeE236c7", max-at-once = 10 }
mint-contract = { address = "0xFACe100969FF47EB58d2CF603321B581A84bcEaC", max-glm-allowed = 400 }
lock-contract = { address = "0xfe1B27Bac0e3Ad39d55C9459ae59894De847dcbf" }
distributor-contract = { address = "0xb7Fb99e86f93dc3047A12932052236d853065173" }
faucet-client = { max-eth-allowed = 0.009, faucet-srv = "_holesky-faucet._tcp", faucet-host = "faucet.testnet.golem.network", faucet-lookup-domain = "dev.golem.network", faucet-srv-port = 4002 }
confirmation-blocks = 0
block-explorer-url = "https://holesky.etherscan.io"
Expand Down
1 change: 1 addition & 0 deletions crates/erc20_payment_lib/contracts/distributor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"bytes","name":"addrs","type":"bytes"},{"internalType":"uint256[]","name":"values","type":"uint256[]"}],"name":"distribute","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"addrs","type":"bytes"}],"name":"distributeEqual","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"addrs","type":"bytes"},{"internalType":"uint32[]","name":"values","type":"uint32[]"}],"name":"distributeEther","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"addrs","type":"bytes"},{"internalType":"uint64[]","name":"values","type":"uint64[]"}],"name":"distributeGwei","outputs":[],"stateMutability":"payable","type":"function"}]
7 changes: 7 additions & 0 deletions crates/erc20_payment_lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ pub struct LockContractSettings {
pub address: Address,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DistributorContractSettings {
pub address: Address,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct FaucetClientSettings {
Expand Down Expand Up @@ -128,6 +134,7 @@ pub struct Chain {
pub multi_contract: Option<MultiContractSettings>,
pub mint_contract: Option<MintContractSettings>,
pub lock_contract: Option<LockContractSettings>,
pub distributor_contract: Option<DistributorContractSettings>,
pub faucet_client: Option<FaucetClientSettings>,
pub transaction_timeout: u64,
pub confirmation_blocks: u64,
Expand Down
2 changes: 2 additions & 0 deletions crates/erc20_payment_lib/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ lazy_static! {
};
pub static ref LOCK_CONTRACT_TEMPLATE: Contract<Http> =
prepare_contract_template(include_bytes!("../contracts/lock_payments.json")).unwrap();
pub static ref DISTRIBUTOR_CONTRACT_TEMPLATE: Contract<Http> =
prepare_contract_template(include_bytes!("../contracts/distributor.json")).unwrap();
}

pub fn prepare_contract_template(json_abi: &[u8]) -> Result<Contract<Http>, PaymentError> {
Expand Down
47 changes: 47 additions & 0 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,33 @@ impl PaymentRuntime {
Ok(())
}

pub async fn distribute_gas(
&self,
chain_name: &str,
from: Address,
) -> Result<(), PaymentError> {
let chain_cfg = self.config.chain.get(chain_name).ok_or(err_custom_create!(
"Chain {} not found in config file",
chain_name
))?;
let golem_address = chain_cfg.token.address;
let web3 = self.setup.get_provider(chain_cfg.chain_id)?;

let res = mint_golem_token(
web3,
&self.conn,
chain_cfg.chain_id as u64,
from,
golem_address,
chain_cfg.mint_contract.clone().map(|c| c.address),
false,
)
.await;
self.wake.notify_one();
res
}


pub async fn mint_golem_token(
&self,
chain_name: &str,
Expand Down Expand Up @@ -1029,6 +1056,25 @@ impl VerifyTransactionResult {
}
}

pub async fn distribute_gas(
web3: Arc<Web3RpcPool>,
conn: &SqlitePool,
chain_id: u64,
from: Address,
faucet_contract_address: Option<Address>,
skip_balance_check: bool,
) -> Result<(), PaymentError> {
let faucet_contract_address = if let Some(faucet_contract_address) = faucet_contract_address {
faucet_contract_address
} else {
return Err(err_custom_create!(
"Faucet/mint contract address unknown. If not sure try on holesky network"
));
};


}

pub async fn mint_golem_token(
web3: Arc<Web3RpcPool>,
conn: &SqlitePool,
Expand Down Expand Up @@ -1366,6 +1412,7 @@ pub async fn make_deposit(
Ok(())
}


pub async fn get_token_balance(
web3: Arc<Web3RpcPool>,
token_address: Address,
Expand Down
6 changes: 6 additions & 0 deletions crates/erc20_payment_lib/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct ChainSetup {
pub glm_address: Address,
pub multi_contract_address: Option<Address>,
pub lock_contract_address: Option<Address>,
pub distribute_contract_address: Option<Address>,
pub faucet_setup: FaucetSetup,
pub multi_contract_max_at_once: usize,
pub transaction_timeout: u64,
Expand Down Expand Up @@ -301,6 +302,11 @@ impl PaymentSetup {
.map(|m| m.max_at_once)
.unwrap_or(1),
lock_contract_address: chain_config.1.lock_contract.clone().map(|m| m.address),
distribute_contract_address: chain_config
.1
.distributor_contract
.clone()
.map(|m| m.address),
faucet_setup,

transaction_timeout: chain_config.1.transaction_timeout,
Expand Down
1 change: 1 addition & 0 deletions crates/erc20_payment_lib_test/src/config_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub async fn create_default_config_setup(proxy_url_base: &str, proxy_key: &str)
}),
mint_contract: None,
lock_contract: None,
distributor_contract: None,
faucet_client: None,
transaction_timeout: 25,
confirmation_blocks: 1,
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_rpc_pool/src/rpc_pool/eth_generic_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Web3RpcPool {
}
return Err(web3::Error::Rpc(e));
} else {
log::warn!("Unknown RPC error: {}", e);
log::warn!("Unknown RPC error when calling {} from endpoint {}: {}", EthMethodCall::METHOD, self.get_name(idx),e);
self.mark_rpc_error(
idx,
EthMethodCall::METHOD.to_string(),
Expand Down
17 changes: 13 additions & 4 deletions prometheus/metrics.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# HELP senders_count Number of distinct receivers
# TYPE senders_count counter
senders_count{chain_id="137", sender="0x09e4f0ae44d5e60d44a8928af7531e6a862290bc"} 19
# HELP receivers_count Number of distinct receivers
# TYPE receivers_count counter
receivers_count{chain_id="17000", receiver="0x001111a27323e8fba0176393d03714c0f7467e2b"} 30
# HELP erc20_transferred Number of distinct receivers
# TYPE erc20_transferred counter
erc20_transferred{chain_id="137", sender="0x09e4f0ae44d5e60d44a8928af7531e6a862290bc"} 0.000000029482970684
erc20_transferred{chain_id="17000", sender="0x001111a27323e8fba0176393d03714c0f7467e2b"} 0
# HELP payment_count Number of distinct payments
# TYPE payment_count counter
payment_count{chain_id="17000", sender="0x001111a27323e8fba0176393d03714c0f7467e2b"} 0
# HELP transaction_count Number of web3 transactions
# TYPE transaction_count counter
transaction_count{chain_id="17000", sender="0x001111a27323e8fba0176393d03714c0f7467e2b"} 4
# HELP fee_paid Total fee paid
# TYPE fee_paid counter
fee_paid{chain_id="17000", sender="0x001111a27323e8fba0176393d03714c0f7467e2b"} 0
35 changes: 35 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async fn main_internal() -> Result<(), PaymentError> {
PaymentCommands::MintTestTokens { .. } => {}
PaymentCommands::Deposit { .. } => {}
PaymentCommands::Transfer { .. } => {}
PaymentCommands::Distribute { .. } => {}
PaymentCommands::Balance { .. } => {}
PaymentCommands::ImportPayments { .. } => {}
PaymentCommands::ScanBlockchain { .. } => {}
Expand Down Expand Up @@ -276,6 +277,40 @@ async fn main_internal() -> Result<(), PaymentError> {
} => {
check_rpc_local(check_web3_rpc_options, config).await?;
}
PaymentCommands::Distribute {
distribute_options
} => {
let public_addr = if let Some(address) = distribute_options.address {
address
} else if let Some(account_no) = distribute_options.account_no {
*public_addrs
.get(account_no)
.expect("No public adss found with specified account_no")
} else {
*public_addrs.first().expect("No public adss found")
};
let chain_cfg = config
.chain
.get(&distribute_options.chain_name)
.ok_or(err_custom_create!(
"Chain {} not found in config file",
distribute_options.chain_name
))?;

let payment_setup = PaymentSetup::new_empty(&config)?;
let web3 = payment_setup.get_provider(chain_cfg.chain_id)?;

distribute_gas(
web3,
&conn.clone().unwrap(),
chain_cfg.chain_id as u64,
public_addr,
chain_cfg.token.address,
chain_cfg.mint_contract.clone().map(|c| c.address),
true,
)
.await?;
}
PaymentCommands::GetDevEth {
get_dev_eth_options,
} => {
Expand Down
27 changes: 27 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ pub struct WithdrawTokensOptions {
pub skip_balance_check: bool,
}

#[derive(StructOpt)]
#[structopt(about = "Distribute token (gas) options")]
pub struct DistributeOptions {
#[structopt(short = "c", long = "chain-name", default_value = "holesky")]
pub chain_name: String,

#[structopt(long = "address", help = "Address (has to have private key)")]
pub address: Option<Address>,

#[structopt(long = "account-no", help = "Address by index (for convenience)")]
pub account_no: Option<usize>,

#[structopt(short = "r", long = "recipients", help = "Recipient")]
pub recipients: String,

#[structopt(
short = "a",
long = "amount",
help = "Amount (decimal, full precision, i.e. 0.01)"
)]
pub amounts: Vec<rust_decimal::Decimal>,
}

#[derive(StructOpt)]
#[structopt(about = "Single transfer options")]
pub struct TransferOptions {
Expand Down Expand Up @@ -412,6 +435,10 @@ pub enum PaymentCommands {
#[structopt(flatten)]
single_transfer_options: TransferOptions,
},
Distribute {
#[structopt(flatten)]
distribute_options: DistributeOptions,
},
Balance {
#[structopt(flatten)]
account_balance_options: BalanceOptions,
Expand Down

0 comments on commit d82a363

Please sign in to comment.