Use currency enum instead of WALLET_CURRENCY_CODE constant

This commit is contained in:
silverpill 2022-04-27 18:14:15 +00:00
parent b384dcd4d2
commit a1af35997c
6 changed files with 50 additions and 15 deletions

View file

@ -9,6 +9,7 @@ use url::Url;
use crate::activitypub::views::get_instance_actor_url;
use crate::errors::ConversionError;
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
use crate::models::profiles::currencies::Currency;
use crate::utils::crypto::deserialize_private_key;
#[derive(Clone, Debug)]
@ -168,6 +169,10 @@ impl Config {
pub fn media_dir(&self) -> PathBuf {
self.storage_dir.join("media")
}
pub fn default_currency(&self) -> Currency {
Currency::Ethereum
}
}
pub struct Instance {

View file

@ -13,6 +13,7 @@ use web3::{
use crate::config::BlockchainConfig;
use crate::database::{Pool, get_database_client};
use crate::errors::{ConversionError, DatabaseError};
use crate::models::profiles::currencies::Currency;
use crate::models::profiles::queries::search_profile_by_wallet_address;
use crate::models::relationships::queries::unsubscribe;
use crate::models::subscriptions::queries::{
@ -22,7 +23,6 @@ use crate::models::subscriptions::queries::{
get_subscription_by_addresses,
};
use crate::models::users::queries::get_user_by_wallet_address;
use crate::models::users::types::WALLET_CURRENCY_CODE;
use super::errors::EthereumError;
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
use super::utils::{address_to_string, parse_address};
@ -35,6 +35,8 @@ fn u256_to_date(value: U256) -> Result<DateTime<Utc>, ConversionError> {
Ok(datetime)
}
const ETHEREUM: Currency = Currency::Ethereum;
/// Search for subscription update events
pub async fn check_subscriptions(
web3: &Web3<Http>,
@ -103,7 +105,7 @@ pub async fn check_subscriptions(
// New subscription
let profiles = search_profile_by_wallet_address(
db_client,
WALLET_CURRENCY_CODE,
&ETHEREUM,
&sender_address,
).await?;
let sender = match &profiles[..] {

View file

@ -18,7 +18,6 @@ use crate::models::profiles::queries::{
use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::{
validate_wallet_address,
WALLET_CURRENCY_CODE,
User,
};
use super::types::SearchResults;
@ -139,10 +138,11 @@ pub async fn search(
};
},
SearchQuery::WalletAddress(address) => {
// Search is case insensitive
// Search by wallet address, assuming default currency (ethereum)
// TODO: support other currencies
profiles = search_profile_by_wallet_address(
db_client,
WALLET_CURRENCY_CODE,
&config.default_currency(),
&address,
).await?;
},

View file

@ -1,7 +1,19 @@
use crate::ethereum::identity::ETHEREUM_EIP191_PROOF;
pub fn get_currency_field_name(currency_code: &str) -> String {
format!("${}", currency_code.to_uppercase())
pub enum Currency {
Ethereum,
}
impl Currency {
fn code(&self) -> String {
match self {
Self::Ethereum => "ETH",
}.to_string()
}
}
pub fn get_currency_field_name(currency: &Currency) -> String {
format!("${}", currency.code())
}
pub fn get_identity_proof_field_name(proof_type: &str) -> Option<String> {
@ -11,3 +23,17 @@ pub fn get_identity_proof_field_name(proof_type: &str) -> Option<String> {
};
Some(field_name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_currency_field_name() {
let ethereum = Currency::Ethereum;
assert_eq!(
get_currency_field_name(&ethereum),
"$ETH",
);
}
}

View file

@ -10,7 +10,7 @@ use crate::models::cleanup::{
};
use crate::models::relationships::types::RelationshipType;
use crate::utils::id::new_uuid;
use super::currencies::get_currency_field_name;
use super::currencies::{get_currency_field_name, Currency};
use super::types::{
DbActorProfile,
ExtraFields,
@ -370,16 +370,18 @@ pub async fn search_profile(
pub async fn search_profile_by_wallet_address(
db_client: &impl GenericClient,
currency_code: &str,
currency: &Currency,
wallet_address: &str,
) -> Result<Vec<DbActorProfile>, DatabaseError> {
let field_name = get_currency_field_name(currency_code);
let field_name = get_currency_field_name(currency);
// If currency is Ethereum,
// search over extra fields must be case insensitive
let rows = db_client.query(
"
SELECT actor_profile
FROM actor_profile LEFT JOIN user_account USING (id)
WHERE
user_account.wallet_address ILIKE $2
user_account.wallet_address = $2
OR EXISTS (
SELECT 1
FROM jsonb_array_elements(actor_profile.extra_fields) AS field
@ -534,6 +536,8 @@ mod tests {
assert_eq!(deletion_queue.ipfs_objects.len(), 0);
}
const ETHEREUM: Currency = Currency::Ethereum;
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_local() {
@ -545,7 +549,7 @@ mod tests {
};
let user = create_user(db_client, user_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
db_client, "ETH", wallet_address).await.unwrap();
db_client, &ETHEREUM, wallet_address).await.unwrap();
assert_eq!(profiles.len(), 1);
assert_eq!(profiles[0].id, user.profile.id);
@ -566,7 +570,7 @@ mod tests {
};
let profile = create_profile(db_client, profile_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
db_client, "ETH", "0x1234abcd").await.unwrap();
db_client, &ETHEREUM, "0x1234abcd").await.unwrap();
assert_eq!(profiles.len(), 1);
assert_eq!(profiles[0].id, profile.id);

View file

@ -62,8 +62,6 @@ pub fn validate_local_username(username: &str) -> Result<(), ValidationError> {
Ok(())
}
pub const WALLET_CURRENCY_CODE: &str = "ETH";
/// Verifies that wallet address is valid ethereum address
pub fn validate_wallet_address(wallet_address: &str) -> Result<(), ValidationError> {
// Address should be lowercase