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

View file

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

View file

@ -18,7 +18,6 @@ use crate::models::profiles::queries::{
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::{ use crate::models::users::types::{
validate_wallet_address, validate_wallet_address,
WALLET_CURRENCY_CODE,
User, User,
}; };
use super::types::SearchResults; use super::types::SearchResults;
@ -139,10 +138,11 @@ pub async fn search(
}; };
}, },
SearchQuery::WalletAddress(address) => { 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( profiles = search_profile_by_wallet_address(
db_client, db_client,
WALLET_CURRENCY_CODE, &config.default_currency(),
&address, &address,
).await?; ).await?;
}, },

View file

@ -1,7 +1,19 @@
use crate::ethereum::identity::ETHEREUM_EIP191_PROOF; use crate::ethereum::identity::ETHEREUM_EIP191_PROOF;
pub fn get_currency_field_name(currency_code: &str) -> String { pub enum Currency {
format!("${}", currency_code.to_uppercase()) 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> { 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) 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::models::relationships::types::RelationshipType;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
use super::currencies::get_currency_field_name; use super::currencies::{get_currency_field_name, Currency};
use super::types::{ use super::types::{
DbActorProfile, DbActorProfile,
ExtraFields, ExtraFields,
@ -370,16 +370,18 @@ pub async fn search_profile(
pub async fn search_profile_by_wallet_address( pub async fn search_profile_by_wallet_address(
db_client: &impl GenericClient, db_client: &impl GenericClient,
currency_code: &str, currency: &Currency,
wallet_address: &str, wallet_address: &str,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> 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( let rows = db_client.query(
" "
SELECT actor_profile SELECT actor_profile
FROM actor_profile LEFT JOIN user_account USING (id) FROM actor_profile LEFT JOIN user_account USING (id)
WHERE WHERE
user_account.wallet_address ILIKE $2 user_account.wallet_address = $2
OR EXISTS ( OR EXISTS (
SELECT 1 SELECT 1
FROM jsonb_array_elements(actor_profile.extra_fields) AS field FROM jsonb_array_elements(actor_profile.extra_fields) AS field
@ -534,6 +536,8 @@ mod tests {
assert_eq!(deletion_queue.ipfs_objects.len(), 0); assert_eq!(deletion_queue.ipfs_objects.len(), 0);
} }
const ETHEREUM: Currency = Currency::Ethereum;
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_search_profile_by_wallet_address_local() { 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 user = create_user(db_client, user_data).await.unwrap();
let profiles = search_profile_by_wallet_address( 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.len(), 1);
assert_eq!(profiles[0].id, user.profile.id); 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 profile = create_profile(db_client, profile_data).await.unwrap();
let profiles = search_profile_by_wallet_address( 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.len(), 1);
assert_eq!(profiles[0].id, profile.id); assert_eq!(profiles[0].id, profile.id);

View file

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