Skip to content

Commit

Permalink
Move Ethereum dest validation into module
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Sep 29, 2024
1 parent ddcd5ee commit b5cf936
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
23 changes: 13 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,24 +405,21 @@ impl InnerApp {
} => {
self.ethereum
.network(*network)?
.connection((*connection).into())?;
if *address == [0; 20] {
return Err(Error::App("Invalid Ethereum address".to_string()));
}
.connection((*connection).into())?
.validate_transfer((*address).into(), amount.into())?;
}
#[cfg(feature = "ethereum")]
Dest::EthCall {
network,
connection,
fallback_address,
max_gas,
..
} => {
self.ethereum
.network(*network)?
.connection((*connection).into())?;
if *fallback_address == [0; 20] {
return Err(Error::App("Invalid Ethereum address".to_string()));
}
.connection((*connection).into())?
.validate_contract_call(*max_gas, (*fallback_address).into(), amount.into())?;
}
Dest::Bitcoin { data } => self.bitcoin.validate_withdrawal(data, amount)?,
Dest::Stake {
Expand Down Expand Up @@ -450,12 +447,18 @@ impl InnerApp {
}
}
Dest::Unstake { index } => {
self.babylon
// TODO: move into babylon
let owner_dels = self
.babylon
.delegations
.get(sender)?
.ok_or_else(|| Error::App("No delegations found for owner".to_string()))?
.ok_or_else(|| Error::App("No delegations found for owner".to_string()))?;
let del = owner_dels
.get(*index)?
.ok_or_else(|| Error::App("Delegation not found".to_string()))?;
if del.status() == babylon::DelegationStatus::Withdrawn {
return Err(Error::App("Delegation already withdrawn".to_string()));
}
}
Dest::AdjustEmergencyDisbursalBalance { data, difference } => {
// TODO
Expand Down
2 changes: 1 addition & 1 deletion src/babylon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Babylon {
.ok_or_else(|| Error::Orga(orga::Error::App("Delegation not found".to_string())))?
.get_mut(index)?
.ok_or_else(|| Error::Orga(orga::Error::App("Delegation not found".to_string())))?
.unbond(frost, btc, &self.params)
.request_unbond(frost, btc, &self.params)
}
}

Expand Down
43 changes: 35 additions & 8 deletions src/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,24 @@ impl Connection {
Ok(())
}

pub fn transfer(&mut self, dest: Address, coins: Coin<Nbtc>) -> Result<()> {
// TODO: validation (min amount, etc)

pub fn validate_transfer(&self, dest: Address, amount: u64) -> Result<()> {
let fee_amount = APPROX_TRANSFER_GAS * GAS_PRICE;
if coins.amount < fee_amount {
return Err(Error::App("insufficient funds for fee".to_string()).into());
if amount < fee_amount {
return Err(Error::App("Insufficient funds for fee".to_string()).into());
}

if dest == Address::NULL {
return Err(Error::App("Invalid Ethereum address".to_string()).into());
}

Ok(())
}

pub fn transfer(&mut self, dest: Address, coins: Coin<Nbtc>) -> Result<()> {
let amount: u64 = coins.amount.into();
self.validate_transfer(dest, amount)?;

let fee_amount = APPROX_TRANSFER_GAS * GAS_PRICE;
let amount = amount - fee_amount;

// TODO: batch transfers
Expand All @@ -447,6 +457,24 @@ impl Connection {
Ok(())
}

pub fn validate_contract_call(
&self,
max_gas: u64,
fallback_address: Address,
amount: u64,
) -> Result<()> {
if fallback_address == Address::NULL {
return Err(Error::App("Invalid Ethereum address".to_string()).into());
}

let fee_amount = (APPROX_CALL_GAS + max_gas) * GAS_PRICE;
if amount < fee_amount {
return Err(Error::App("Insufficient funds for fee".to_string()).into());
}

Ok(())
}

pub fn call_contract(
&mut self,
// TODO: ethaddress type
Expand All @@ -458,10 +486,9 @@ impl Connection {
coins: Coin<Nbtc>,
) -> Result<()> {
let transfer_amount: u64 = coins.amount.into();
self.validate_contract_call(max_gas, fallback_address.into(), transfer_amount)?;

let fee_amount = (APPROX_CALL_GAS + max_gas) * GAS_PRICE;
if transfer_amount < fee_amount {
return Err(Error::App("insufficient funds for fee".to_string()).into());
}
let transfer_amount = transfer_amount - fee_amount;

self.coins.give(coins)?;
Expand Down

0 comments on commit b5cf936

Please sign in to comment.