set provider fee from provider config

This commit is contained in:
0xfirefist 2024-05-06 12:29:51 +05:30
parent ff6b11023c
commit a93aedbddf
6 changed files with 30 additions and 26 deletions

View File

@ -1488,7 +1488,7 @@ dependencies = [
[[package]]
name = "fortuna"
version = "5.2.1"
version = "6.0.0"
dependencies = [
"anyhow",
"axum",

View File

@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "5.2.1"
version = "6.0.0"
edition = "2021"
[dependencies]

View File

@ -122,25 +122,15 @@ pub async fn run_keeper(
pub async fn run(opts: &RunOptions) -> Result<()> {
let config = Config::load(&opts.config.config)?;
let provider_config = opts
.provider_config
.provider_config
.as_ref()
.map(|path| ProviderConfig::load(&path).expect("Failed to load provider config"));
let provider_config = ProviderConfig::load(&opts.provider_config.provider_config)?;
let secret = opts.randomness.load_secret()?;
let (tx_exit, rx_exit) = watch::channel(false);
let mut chains: HashMap<ChainId, BlockchainState> = HashMap::new();
for (chain_id, chain_config) in &config.chains {
let contract = Arc::new(PythContract::from_config(&chain_config)?);
let provider_chain_config = provider_config
.as_ref()
.and_then(|c| c.get_chain_config(chain_id));
let mut provider_commitments = provider_chain_config
.as_ref()
.map(|c| c.get_sorted_commitments())
.unwrap_or_else(|| Vec::new());
let provider_chain_config = provider_config.get_chain_config(chain_id)?;
let mut provider_commitments = provider_chain_config.get_sorted_commitments();
let provider_info = contract.get_provider_info(opts.provider).call().await?;
let latest_metadata =
bincode::deserialize::<CommitmentMetadata>(&provider_info.commitment_metadata)

View File

@ -8,6 +8,7 @@ use {
},
config::{
Config,
ProviderConfig,
RegisterProviderOptions,
SetupProviderOptions,
},
@ -39,11 +40,13 @@ use {
/// 5. Update provider uri if there is a mismatch with the uri set on contract.
pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
let config = Config::load(&opts.config.config)?;
let provider_config = ProviderConfig::load(&opts.provider_config.provider_config)?;
let private_key = opts.load_private_key()?;
let secret = opts.randomness.load_secret()?;
let provider_address = private_key.clone().parse::<LocalWallet>()?.address();
for (chain_id, chain_config) in &config.chains {
let provider_fee = provider_config.get_chain_config(chain_id)?.fee;
// Initialize a Provider to interface with the EVM contract.
let contract =
Arc::new(SignablePythContract::from_config(&chain_config, &private_key).await?);
@ -112,16 +115,21 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
chain_id: chain_id.clone(),
private_key: private_key.clone(),
randomness: opts.randomness.clone(),
fee: opts.fee,
fee: provider_fee,
uri,
})
.await
.map_err(|e| anyhow!("Chain: {} - Failed to register provider: {}", &chain_id, e))?;
tracing::info!("{}: registered", &chain_id);
} else {
if provider_info.fee_in_wei != opts.fee {
if provider_info.fee_in_wei != provider_fee {
tracing::info!("{}: updating provider fee", chain_id);
if let Some(r) = contract.set_provider_fee(opts.fee).send().await?.await? {
if let Some(r) = contract
.set_provider_fee(provider_fee)
.send()
.await?
.await?
{
tracing::info!("{0}: updated provider fee: {1:?}", chain_id, r);
}
}

View File

@ -165,7 +165,7 @@ pub struct EthereumConfig {
pub struct ProviderConfigOptions {
#[arg(long = "provider-config")]
#[arg(env = "FORTUNA_PROVIDER_CONFIG")]
pub provider_config: Option<String>,
pub provider_config: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
@ -183,14 +183,21 @@ impl ProviderConfig {
/// Get the provider chain config. The method returns an Option for ProviderChainConfig.
/// We may not have past any commitments for a chain. For example, for a new chain
pub fn get_chain_config(&self, chain_id: &ChainId) -> Option<ProviderChainConfig> {
self.chains.get(chain_id).map(|x| x.clone())
pub fn get_chain_config(&self, chain_id: &ChainId) -> Result<ProviderChainConfig> {
self.chains.get(chain_id).map(|x| x.clone()).ok_or(
anyhow!(
"Could not find chain id {} in provider configuration",
&chain_id
)
.into(),
)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ProviderChainConfig {
commitments: Vec<Commitment>,
pub fee: u128,
}
impl ProviderChainConfig {

View File

@ -7,6 +7,7 @@ use {
clap::Args,
std::fs,
};
use crate::config::ProviderConfigOptions;
#[derive(Args, Clone, Debug)]
#[command(next_help_heading = "Setup Provider Options")]
@ -15,6 +16,9 @@ pub struct SetupProviderOptions {
#[command(flatten)]
pub config: ConfigOptions,
#[command(flatten)]
pub provider_config: ProviderConfigOptions,
/// Path to a file containing a 20-byte (40 char) hex encoded Ethereum private key.
/// This key is required to submit transactions (such as registering with the contract).
#[arg(long = "private-key")]
@ -24,11 +28,6 @@ pub struct SetupProviderOptions {
#[command(flatten)]
pub randomness: RandomnessOptions,
/// The fee to charge (in wei) for each requested random number
#[arg(long = "pyth-contract-fee")]
#[arg(default_value = "100")]
pub fee: u128,
/// The base URI for fortuna.
/// e.g., https://fortuna-staging.dourolabs.app
#[arg(long = "uri")]