Skip to content
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
2 changes: 1 addition & 1 deletion apps/daemon/src/pusher/pusher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Pusher {
let message = format!("Closed perpetual position for {value} at {to_address}");
Ok(Message { title, message: Some(message) })
}
TransactionType::AssetActivation | TransactionType::PerpetualModifyPosition => todo!(),
TransactionType::AssetActivation | TransactionType::PerpetualModifyPosition | TransactionType::EarnDeposit | TransactionType::EarnWithdraw => todo!(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using todo!() in a daemon service is risky as it will cause a panic if an EarnDeposit or EarnWithdraw transaction is encountered. It's better to handle these types gracefully, for example by returning a generic message or a specific error, to ensure service stability.

TransactionType::StakeFreeze => Ok(Message {
title: localizer.notification_freeze_title(self.get_value(amount, asset.symbol.clone()).as_str()),
message: None,
Expand Down
9 changes: 1 addition & 8 deletions crates/gem_aptos/src/provider/staking_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ pub fn map_validators(validator_set: ValidatorSet, apy: f64, pool_address: &str,
}

pub fn map_validator(validator: &ValidatorInfo, apy: f64, commission: f64, is_active: bool) -> DelegationValidator {
DelegationValidator {
chain: Chain::Aptos,
id: validator.addr.clone(),
name: "".to_string(),
is_active,
commission,
apr: apy,
}
DelegationValidator::stake(Chain::Aptos, validator.addr.clone(), "".to_string(), is_active, commission, apy)
}

fn map_delegation(asset_id: &primitives::AssetId, state: DelegationState, balance: BigUint, validator_id: &str, completion_date: Option<DateTime<Utc>>) -> DelegationBase {
Expand Down
2 changes: 1 addition & 1 deletion crates/gem_aptos/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<C: Client> AptosClient<C> {
}
}
TransactionInputType::Swap(_, _, _) | TransactionInputType::Stake(_, _) | TransactionInputType::TokenApprove(_, _) | TransactionInputType::Generic(_, _, _) => Ok(1500),
TransactionInputType::Perpetual(_, _) => unimplemented!(),
TransactionInputType::Perpetual(_, _) | TransactionInputType::Earn(_, _, _) => unimplemented!(),
}
}

Expand Down
18 changes: 12 additions & 6 deletions crates/gem_cosmos/src/provider/preload_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ fn get_fee(chain: CosmosChain, input_type: &TransactionInputType) -> BigInt {
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => BigInt::from(3_000u64),
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => BigInt::from(3_000u64),
TransactionInputType::Swap(_, _, _) => BigInt::from(3_000u64),
TransactionInputType::Stake(_, _) => BigInt::from(25_000u64),
},
Expand All @@ -22,7 +23,8 @@ fn get_fee(chain: CosmosChain, input_type: &TransactionInputType) -> BigInt {
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => BigInt::from(10_000u64),
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => BigInt::from(10_000u64),
TransactionInputType::Swap(_, _, _) => BigInt::from(10_000u64),
TransactionInputType::Stake(_, _) => BigInt::from(100_000u64),
},
Expand All @@ -33,7 +35,8 @@ fn get_fee(chain: CosmosChain, input_type: &TransactionInputType) -> BigInt {
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => BigInt::from(3_000u64),
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => BigInt::from(3_000u64),
TransactionInputType::Swap(_, _, _) => BigInt::from(3_000u64),
TransactionInputType::Stake(_, _) => BigInt::from(10_000u64),
},
Expand All @@ -44,7 +47,8 @@ fn get_fee(chain: CosmosChain, input_type: &TransactionInputType) -> BigInt {
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => BigInt::from(100_000u64),
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => BigInt::from(100_000u64),
TransactionInputType::Swap(_, _, _) => BigInt::from(100_000u64),
TransactionInputType::Stake(_, _) => BigInt::from(200_000u64),
},
Expand All @@ -55,7 +59,8 @@ fn get_fee(chain: CosmosChain, input_type: &TransactionInputType) -> BigInt {
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => BigInt::from(100_000_000_000_000u64),
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => BigInt::from(100_000_000_000_000u64),
TransactionInputType::Swap(_, _, _) => BigInt::from(100_000_000_000_000u64),
TransactionInputType::Stake(_, _) => BigInt::from(1_000_000_000_000_000u64),
},
Expand All @@ -71,7 +76,8 @@ fn get_gas_limit(input_type: &TransactionInputType, _chain: CosmosChain) -> u64
| TransactionInputType::Account(_, _)
| TransactionInputType::TokenApprove(_, _)
| TransactionInputType::Generic(_, _, _)
| TransactionInputType::Perpetual(_, _) => 200_000,
| TransactionInputType::Perpetual(_, _)
| TransactionInputType::Earn(_, _, _) => 200_000,
TransactionInputType::Swap(_, _, _) => 200_000,
TransactionInputType::Stake(_, operation) => match operation {
StakeType::Stake(_) | StakeType::Unstake(_) => 1_000_000,
Expand Down
9 changes: 1 addition & 8 deletions crates/gem_cosmos/src/provider/staking_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,7 @@ pub fn map_staking_validators(validators: Vec<Validator>, chain: CosmosChain, ap
let is_active = !validator.jailed && validator.status == BOND_STATUS_BONDED;
let validator_apr = if is_active { apy.map(|apr| apr - (apr * commission_rate)).unwrap_or(0.0) } else { 0.0 };

DelegationValidator {
chain: chain.as_chain(),
id: validator.operator_address,
name: validator.description.moniker,
is_active,
commission: commission_rate * 100.0,
apr: validator_apr,
}
DelegationValidator::stake(chain.as_chain(), validator.operator_address, validator.description.moniker, is_active, commission_rate * 100.0, validator_apr)
})
.collect()
}
Expand Down
10 changes: 6 additions & 4 deletions crates/gem_evm/src/provider/preload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use gem_client::Client;
use num_bigint::BigInt;
use primitives::GasPriceType;
#[cfg(feature = "rpc")]
use primitives::stake_type::StakeData;
use primitives::ContractCallData;
#[cfg(feature = "rpc")]
use primitives::{FeeRate, TransactionFee, TransactionInputType, TransactionLoadData, TransactionLoadInput, TransactionLoadMetadata, TransactionPreloadInput};
#[cfg(feature = "rpc")]
Expand Down Expand Up @@ -68,9 +68,11 @@ impl<C: Client + Clone> EthereumClient<C> {
TransactionLoadMetadata::Evm { nonce, chain_id, .. } => TransactionLoadMetadata::Evm {
nonce,
chain_id,
stake_data: Some(StakeData {
data: if params.data.is_empty() { None } else { Some(hex::encode(&params.data)) },
to: Some(params.to),
contract_call: Some(ContractCallData {
contract_address: params.to,
call_data: hex::encode(&params.data),
approval: None,
gas_limit: None,
}),
},
_ => input.metadata,
Expand Down
87 changes: 31 additions & 56 deletions crates/gem_evm/src/provider/preload_mapper.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::error::Error;
use std::str::FromStr;

use alloy_primitives::{Address, U256};
use alloy_primitives::{Address, U256, hex};
use alloy_sol_types::SolCall;
use gem_bsc::stake_hub::STAKE_HUB_ADDRESS;
use num_bigint::BigInt;
use num_traits::Num;
use primitives::swap::SwapQuoteDataType;
use primitives::{
AssetSubtype, Chain, EVMChain, FeeRate, NFTType, StakeType, TransactionInputType, TransactionLoadInput, TransactionLoadMetadata, fee::FeePriority, fee::GasPriceType,
AssetSubtype, Chain, EVMChain, FeeRate, NFTType, StakeType, TransactionInputType, TransactionLoadInput, TransactionLoadMetadata, decode_hex, fee::FeePriority, fee::GasPriceType,
};

use crate::contracts::{IERC20, IERC721, IERC1155};
Expand All @@ -30,22 +30,26 @@ impl TransactionParams {
pub fn new(to: String, data: Vec<u8>, value: BigInt) -> Self {
Self { to, data, value }
}

pub fn new_approval(to: String, data: Vec<u8>) -> Self {
Self { to, data, value: BigInt::from(0) }
}
}

pub fn bigint_to_hex_string(value: &BigInt) -> String {
format!("0x{:x}", value)
}

pub fn bytes_to_hex_string(data: &[u8]) -> String {
format!("0x{}", alloy_primitives::hex::encode(data))
format!("0x{}", hex::encode(data))
}

pub fn map_transaction_preload(nonce_hex: String, chain_id: String) -> Result<TransactionLoadMetadata, Box<dyn std::error::Error + Send + Sync>> {
let nonce = u64::from_str_radix(nonce_hex.trim_start_matches("0x"), 16)?;
Ok(TransactionLoadMetadata::Evm {
nonce,
chain_id: chain_id.parse::<u64>()?,
stake_data: None,
contract_call: None,
})
}

Expand Down Expand Up @@ -92,13 +96,13 @@ pub fn get_transaction_params(chain: EVMChain, input: &TransactionLoadInput) ->
match from_asset.id.token_subtype() {
AssetSubtype::NATIVE => Ok(TransactionParams::new(
swap_data.data.to.clone(),
alloy_primitives::hex::decode(swap_data.data.data.clone())?,
hex::decode(swap_data.data.data.clone())?,
BigInt::from_str_radix(&swap_data.data.value, 10)?,
)),
AssetSubtype::TOKEN => match swap_data.data.data_type {
SwapQuoteDataType::Contract => Ok(TransactionParams::new(
swap_data.data.to.clone(),
alloy_primitives::hex::decode(swap_data.data.data.clone())?,
hex::decode(swap_data.data.data.clone())?,
BigInt::ZERO,
)),
SwapQuoteDataType::Transfer => {
Expand Down Expand Up @@ -145,6 +149,13 @@ pub fn get_transaction_params(chain: EVMChain, input: &TransactionLoadInput) ->
}
_ => Err("Unsupported chain for staking".into()),
},
TransactionInputType::Earn(_, _, earn_data) => {
if let Some(approval) = &earn_data.approval {
Ok(TransactionParams::new_approval(approval.token.clone(), encode_erc20_approve(&approval.spender)?))
} else {
Ok(TransactionParams::new(earn_data.contract_address.clone(), decode_hex(&earn_data.call_data)?, BigInt::from(0)))
}
}
_ => Err("Unsupported transfer type".into()),
}
}
Expand Down Expand Up @@ -183,6 +194,12 @@ pub fn get_extra_fee_gas_limit(input: &TransactionLoadInput) -> Result<BigInt, B
Ok(BigInt::from(0))
}
}
TransactionInputType::Earn(_, _, earn_data) => {
if earn_data.approval.is_some() && let Some(gas_limit) = &earn_data.gas_limit {
return Ok(BigInt::from_str_radix(gas_limit, 10)?);
}
Ok(BigInt::from(0))
}
_ => Ok(BigInt::from(0)),
}
}
Expand Down Expand Up @@ -287,14 +304,7 @@ mod tests {
use primitives::{Delegation, DelegationBase, DelegationState, DelegationValidator, RedelegateData};

fn everstake_validator() -> DelegationValidator {
DelegationValidator {
chain: Chain::Ethereum,
id: EVERSTAKE_POOL_ADDRESS.to_string(),
name: "Everstake Pool".to_string(),
is_active: true,
commission: 10.0,
apr: 4.2,
}
DelegationValidator::stake(Chain::Ethereum, EVERSTAKE_POOL_ADDRESS.to_string(), "Everstake".to_string(), true, 10.0, 4.2)
}

#[test]
Expand All @@ -305,10 +315,10 @@ mod tests {
let result = map_transaction_preload(nonce_hex, chain_id)?;

match result {
TransactionLoadMetadata::Evm { nonce, chain_id, stake_data } => {
TransactionLoadMetadata::Evm { nonce, chain_id, contract_call } => {
assert_eq!(nonce, 10);
assert_eq!(chain_id, 1);
assert!(stake_data.is_none());
assert!(contract_call.is_none());
}
_ => panic!("Expected Evm variant"),
}
Expand Down Expand Up @@ -421,14 +431,7 @@ mod tests {

#[test]
fn test_encode_stake_hub_delegate() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let validator = DelegationValidator {
chain: Chain::SmartChain,
id: "0x773760b0708a5Cc369c346993a0c225D8e4043B1".to_string(),
name: "Test Validator".to_string(),
is_active: true,
commission: 5.0,
apr: 10.0,
};
let validator = DelegationValidator::stake(Chain::SmartChain, "0x773760b0708a5Cc369c346993a0c225D8e4043B1".to_string(), "Test Validator".to_string(), true, 5.0, 10.0);

let stake_type = StakeType::Stake(validator);
let amount = BigInt::from(1_000_000_000_000_000_000u64); // 1 BNB
Expand Down Expand Up @@ -457,14 +460,7 @@ mod tests {
delegation_id: "test".to_string(),
validator_id: "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(),
},
validator: DelegationValidator {
chain: Chain::SmartChain,
id: "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(),
name: "Test Validator".to_string(),
is_active: true,
commission: 5.0,
apr: 10.0,
},
validator: DelegationValidator::stake(Chain::SmartChain, "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(), "Test Validator".to_string(), true, 5.0, 10.0),
price: None,
};

Expand Down Expand Up @@ -494,25 +490,11 @@ mod tests {
delegation_id: "test".to_string(),
validator_id: "0x773760b0708a5Cc369c346993a0c225D8e4043B1".to_string(),
},
validator: DelegationValidator {
chain: Chain::SmartChain,
id: "0x773760b0708a5Cc369c346993a0c225D8e4043B1".to_string(),
name: "Source Validator".to_string(),
is_active: true,
commission: 5.0,
apr: 10.0,
},
validator: DelegationValidator::stake(Chain::SmartChain, "0x773760b0708a5Cc369c346993a0c225D8e4043B1".to_string(), "Source Validator".to_string(), true, 5.0, 10.0),
price: None,
};

let to_validator = DelegationValidator {
chain: Chain::SmartChain,
id: "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(),
name: "Target Validator".to_string(),
is_active: true,
commission: 3.0,
apr: 12.0,
};
let to_validator = DelegationValidator::stake(Chain::SmartChain, "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(), "Target Validator".to_string(), true, 3.0, 12.0);

let redelegate_data = RedelegateData { delegation, to_validator };

Expand Down Expand Up @@ -542,14 +524,7 @@ mod tests {
delegation_id: "test".to_string(),
validator_id: "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(),
},
validator: DelegationValidator {
chain: Chain::SmartChain,
id: "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(),
name: "Test Validator".to_string(),
is_active: true,
commission: 5.0,
apr: 10.0,
},
validator: DelegationValidator::stake(Chain::SmartChain, "0x343dA7Ff0446247ca47AA41e2A25c5Bbb230ED0A".to_string(), "Test Validator".to_string(), true, 5.0, 10.0),
price: None,
};

Expand Down
9 changes: 1 addition & 8 deletions crates/gem_evm/src/provider/staking_ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ impl<C: Client + Clone> EthereumClient<C> {
}

pub async fn get_ethereum_validators(&self, apy: f64) -> Result<Vec<DelegationValidator>, Box<dyn Error + Sync + Send>> {
Ok(vec![DelegationValidator {
id: EVERSTAKE_POOL_ADDRESS.to_string(),
chain: Chain::Ethereum,
name: "Everstake".to_string(),
is_active: true,
commission: 0.1,
apr: apy,
}])
Ok(vec![DelegationValidator::stake(Chain::Ethereum, EVERSTAKE_POOL_ADDRESS.to_string(), "Everstake".to_string(), true, 0.1, apy)])
}

pub async fn get_ethereum_delegations(&self, address: &str) -> Result<Vec<DelegationBase>, Box<dyn Error + Sync + Send>> {
Expand Down
16 changes: 8 additions & 8 deletions crates/gem_evm/src/provider/staking_monad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ impl<C: Client + Clone> EthereumClient<C> {
.map(|name| (*name).to_string())
.unwrap_or_else(|| validator.validator_id.to_string());

DelegationValidator {
id: validator.validator_id.to_string(),
chain: Chain::Monad,
name: validator_name,
is_active: validator.is_active,
commission: Self::lens_commission_rate(&validator.commission),
apr: if validator.apy_bps > 0 { validator.apy_bps as f64 / 100.0 } else { network_apy },
}
DelegationValidator::stake(
Chain::Monad,
validator.validator_id.to_string(),
validator_name,
validator.is_active,
Self::lens_commission_rate(&validator.commission),
if validator.apy_bps > 0 { validator.apy_bps as f64 / 100.0 } else { network_apy },
)
}

fn map_lens_state(position: &MonadLensDelegation) -> DelegationState {
Expand Down
9 changes: 1 addition & 8 deletions crates/gem_evm/src/provider/staking_smartchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ impl<C: Client + Clone> EthereumClient<C> {

Ok(validators
.into_iter()
.map(|v| DelegationValidator {
id: v.operator_address.clone(),
chain: Chain::SmartChain,
name: v.moniker,
is_active: !v.jailed,
commission: v.commission as f64 / 10000.0,
apr: v.apy as f64 / 100.0,
})
.map(|v| DelegationValidator::stake(Chain::SmartChain, v.operator_address.clone(), v.moniker, !v.jailed, v.commission as f64 / 10000.0, v.apy as f64 / 100.0))
.collect())
}

Expand Down
9 changes: 1 addition & 8 deletions crates/gem_hypercore/src/provider/staking_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ pub fn map_staking_validators(validators: Vec<Validator>, chain: Chain, apy: Opt
let calculated_apy = apy.unwrap_or_else(|| Validator::max_apr(validators.clone()));
validators
.into_iter()
.map(|x| DelegationValidator {
chain,
id: x.validator_address(),
name: x.name,
is_active: x.is_active,
commission: x.commission,
apr: calculated_apy,
})
.map(|x| DelegationValidator::stake(chain, x.validator_address(), x.name, x.is_active, x.commission, calculated_apy))
.collect()
}

Expand Down
Loading
Loading