Skip to content

Commit

Permalink
Reference (#195)
Browse files Browse the repository at this point in the history
* added reference

* remove logging of devices

* fixed accounts

* fixed imports

* fixed version
  • Loading branch information
lassemand authored Aug 26, 2024
1 parent 14ed387 commit f3c03eb
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 35 deletions.
2 changes: 1 addition & 1 deletion notification-server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion notification-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Concordium AG [email protected]"]
edition = "2021"
name = "notification-server"
version = "0.1.9"
version = "0.1.10"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
6 changes: 1 addition & 5 deletions notification-server/src/bin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,12 @@ async fn process_device_subscription(
Ok("Subscribed accounts to device".to_string())
}

#[tracing::instrument(skip(state))]
async fn upsert_account_device(
Path(device): Path<String>,
State(state): State<Arc<AppState>>,
Json(subscription): Json<DeviceSubscription>,
) -> impl IntoResponse {
info!(
"Subscribing accounts {:?} to device {}",
subscription, device
);
info!("Subscribing accounts {:?} to device a device", subscription);
let response: Result<String, (StatusCode, String)> =
process_device_subscription(device, subscription, state).await;
match response {
Expand Down
30 changes: 25 additions & 5 deletions notification-server/src/google_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
use async_trait::async_trait;
use backoff::ExponentialBackoff;
use concordium_rust_sdk::{
base::smart_contracts::OwnedContractName,
base::{hashes::TransactionHash, smart_contracts::OwnedContractName},
cis2::{MetadataUrl, TokenId},
id::types::AccountAddress,
types::{hashes::Hash, ContractAddress},
Expand Down Expand Up @@ -336,7 +336,8 @@ mod tests {
"data": {
"recipient": "4FmiTW2L2AccyR9VjzsnpWFSAcohXWf7Vf797i36y526mqiEcp",
"amount": "100",
"type": "ccd-tx"
"type": "ccd-tx",
"reference": "3d1c2f4fb9a0eb468bfe39e75c59897c1a375082a6440f4a5da77102182ba055",
}
}
});
Expand Down Expand Up @@ -368,6 +369,10 @@ mod tests {
AccountAddress::from_str("4FmiTW2L2AccyR9VjzsnpWFSAcohXWf7Vf797i36y526mqiEcp")
.unwrap(),
"100".to_string(),
TransactionHash::from_str(
"3d1c2f4fb9a0eb468bfe39e75c59897c1a375082a6440f4a5da77102182ba055",
)
.unwrap(),
));
assert!(gc
.send_push_notification("valid_device_token", &notification_information)
Expand All @@ -393,7 +398,8 @@ mod tests {
"type": "cis2-tx",
"token_id": "ffffff",
"contract_address": {"index": 3, "subindex": 0},
"contract_name": "init_contract"
"contract_name": "init_contract",
"reference": "6a6d250ecefb518253db4c0d7759b2f4ff2862217ed2c8343879a77e0c2c97a2",
}
}
});
Expand Down Expand Up @@ -429,6 +435,10 @@ mod tests {
ContractAddress::new(3, 0),
OwnedContractName::new("init_contract".to_string()).unwrap(),
None,
TransactionHash::from_str(
"6a6d250ecefb518253db4c0d7759b2f4ff2862217ed2c8343879a77e0c2c97a2",
)
.unwrap(),
));
assert!(gc
.send_push_notification("test_token", &notification_information)
Expand All @@ -455,7 +465,8 @@ mod tests {
"contract_address": {"index": 112, "subindex": 2},
"contract_name": "init_contract",
"token_id": "ffffff",
"token_metadata_url": {"hash": None::<Hash>, "url": "https://example.com"}
"token_metadata": {"hash": None::<Hash>, "url": "https://example.com"},
"reference": "494d7848e389d44a2c2fe81eeee6dc427ce33ab1d0c92cba23be321d495be110",
}
}
});
Expand Down Expand Up @@ -490,6 +501,10 @@ mod tests {
ContractAddress::new(112, 2),
OwnedContractName::new("init_contract".to_string()).unwrap(),
Some(MetadataUrl::new("https://example.com".to_string(), None).unwrap()),
TransactionHash::from_str(
"494d7848e389d44a2c2fe81eeee6dc427ce33ab1d0c92cba23be321d495be110",
)
.unwrap(),
));
assert!(gc
.send_push_notification("test_token", &notification_information)
Expand All @@ -516,7 +531,8 @@ mod tests {
"contract_address": {"index": 111, "subindex": 1},
"contract_name": "init_contract",
"token_id": "ffffff",
"token_metadata_url": {"hash": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "url": "https://example.com"}
"token_metadata": {"hash": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "url": "https://example.com"},
"reference": "8a3a09bffa6ead269f79be4192fcb7773cc4e10a2e90c0dec3eb9ca5200c06bc"
}
}
});
Expand Down Expand Up @@ -562,6 +578,10 @@ mod tests {
)
.unwrap(),
),
TransactionHash::from_str(
"8a3a09bffa6ead269f79be4192fcb7773cc4e10a2e90c0dec3eb9ca5200c06bc",
)
.unwrap(),
));
assert!(gc
.send_push_notification("test_token", &notification_information)
Expand Down
1 change: 0 additions & 1 deletion notification-server/src/models/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::collections::HashSet;
#[derive(Debug, Deserialize)]
pub struct DeviceSubscription {
pub preferences: Vec<Preference>,
// TODO: Investigate if this should be a AccountAddress instead of a String
pub accounts: Vec<String>,
}

Expand Down
29 changes: 19 additions & 10 deletions notification-server/src/models/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use concordium_rust_sdk::{
base::{contracts_common::AccountAddress, smart_contracts::OwnedContractName},
cis2::{Cis2QueryError, Cis2Type, MetadataUrl, TokenId},
contract_client::ContractClient,
types::ContractAddress,
types::{hashes::TransactionHash, ContractAddress},
v2::{Client, IntoBlockIdentifier},
};
use serde::{Deserialize, Serialize, Serializer};
Expand Down Expand Up @@ -54,6 +54,7 @@ impl NotificationInformationBasic {
.token_metadata_single(block_identifier, info.token_id)
.await
.ok(),
info.transaction_hash,
),
))
}
Expand All @@ -68,30 +69,33 @@ pub struct CCDTransactionNotificationInformation {
pub amount: String,
#[serde(rename = "type")]
pub transaction_type: Preference,
pub reference: TransactionHash,
}

impl CCDTransactionNotificationInformation {
pub fn new(address: AccountAddress, amount: String) -> Self {
pub fn new(address: AccountAddress, amount: String, reference: TransactionHash) -> Self {
Self {
address,
amount,
transaction_type: Preference::CCDTransaction,
reference,
}
}
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct CIS2EventNotificationInformation {
#[serde(rename = "recipient")]
pub address: AccountAddress,
pub amount: String,
pub address: AccountAddress,
pub amount: String,
#[serde(rename = "type")]
pub transaction_type: Preference,
pub token_id: TokenId,
pub contract_address: ContractAddress,
pub contract_name: OwnedContractName,
pub transaction_type: Preference,
pub token_id: TokenId,
pub contract_address: ContractAddress,
pub contract_name: OwnedContractName,
#[serde(skip_serializing_if = "Option::is_none")]
pub token_metadata_url: Option<MetadataUrl>,
pub token_metadata: Option<MetadataUrl>,
pub reference: TransactionHash,
}

impl CIS2EventNotificationInformation {
Expand All @@ -102,6 +106,7 @@ impl CIS2EventNotificationInformation {
contract_address: ContractAddress,
contract_name: OwnedContractName,
token_metadata_url: Option<MetadataUrl>,
reference: TransactionHash,
) -> Self {
Self {
address,
Expand All @@ -110,7 +115,8 @@ impl CIS2EventNotificationInformation {
token_id,
contract_address,
contract_name,
token_metadata_url,
token_metadata: token_metadata_url,
reference,
}
}
}
Expand Down Expand Up @@ -145,6 +151,7 @@ pub struct CIS2EventNotificationInformationBasic {
transaction_type: Preference,
token_id: TokenId,
contract_address: ContractAddress,
transaction_hash: TransactionHash,
}

impl CIS2EventNotificationInformationBasic {
Expand All @@ -153,13 +160,15 @@ impl CIS2EventNotificationInformationBasic {
amount: String,
token_id: TokenId,
contract_address: ContractAddress,
transaction_hash: TransactionHash,
) -> Self {
Self {
address,
amount,
transaction_type: Preference::CIS2Transaction,
token_id,
contract_address,
transaction_hash,
}
}
}
48 changes: 36 additions & 12 deletions notification-server/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::models::notification::{
NotificationInformationBasic,
};
use concordium_rust_sdk::{
base::hashes::TransactionHash,
cis2,
cis2::Event,
types,
Expand All @@ -20,6 +21,7 @@ fn convert<T: Into<BigInt>>(
is_positive: bool,
token_id: cis2::TokenId,
contract_address: ContractAddress,
reference: TransactionHash,
) -> Option<NotificationInformationBasic> {
let mut amount: BigInt = amount.into();
if !is_positive {
Expand All @@ -33,6 +35,7 @@ fn convert<T: Into<BigInt>>(
amount.to_string(),
token_id,
contract_address,
reference,
),
)),
_ => None,
Expand All @@ -41,16 +44,25 @@ fn convert<T: Into<BigInt>>(

fn map_transaction_to_notification_information(
effects: &AccountTransactionEffects,
transaction_hash: TransactionHash,
) -> Vec<NotificationInformationBasic> {
match &effects {
AccountTransactionEffects::AccountTransfer { to, amount } => {
vec![NotificationInformationBasic::CCD(
CCDTransactionNotificationInformation::new(*to, amount.micro_ccd.to_string()),
CCDTransactionNotificationInformation::new(
*to,
amount.micro_ccd.to_string(),
transaction_hash,
),
)]
}
AccountTransactionEffects::AccountTransferWithMemo { to, amount, .. } => {
vec![NotificationInformationBasic::CCD(
CCDTransactionNotificationInformation::new(*to, amount.micro_ccd.to_string()),
CCDTransactionNotificationInformation::new(
*to,
amount.micro_ccd.to_string(),
transaction_hash,
),
)]
}
AccountTransactionEffects::ContractUpdateIssued { effects } => effects
Expand All @@ -61,6 +73,7 @@ fn map_transaction_to_notification_information(
CCDTransactionNotificationInformation::new(
*to,
amount.micro_ccd.to_string(),
transaction_hash,
),
)]
}
Expand All @@ -74,17 +87,21 @@ fn map_transaction_to_notification_information(
to,
amount,
..
}) => convert(to, amount.0, true, token_id, address),
}) => convert(to, amount.0, true, token_id, address, transaction_hash),
Ok(Event::Mint {
token_id,
owner,
amount,
}) => convert(owner, amount.0, true, token_id, address),
}) => {
convert(owner, amount.0, true, token_id, address, transaction_hash)
}
Ok(Event::Burn {
token_id,
owner,
amount,
}) => convert(owner, amount.0, false, token_id, address),
}) => {
convert(owner, amount.0, false, token_id, address, transaction_hash)
}
_ => None,
})
.collect()
Expand All @@ -102,6 +119,7 @@ fn map_transaction_to_notification_information(
acc + BigInt::from(item.micro_ccd)
})
.to_string(),
transaction_hash,
),
)]
}
Expand All @@ -115,6 +133,7 @@ fn map_transaction_to_notification_information(
acc + BigInt::from(item.micro_ccd)
})
.to_string(),
transaction_hash,
),
)]
}
Expand All @@ -130,7 +149,10 @@ pub async fn process(
.flat_map(|t| {
futures::stream::iter(match t.details {
AccountTransaction(ref account_transaction) => {
map_transaction_to_notification_information(&account_transaction.effects)
map_transaction_to_notification_information(
&account_transaction.effects,
t.hash,
)
}
_ => vec![],
})
Expand All @@ -157,10 +179,11 @@ mod tests {
constants::EncryptedAmountsCurve,
encrypted_transfers::types::EncryptedAmount,
types::{
hashes, AccountCreationDetails, AccountTransactionDetails, AccountTransactionEffects,
BlockItemSummary, BlockItemSummaryDetails, CredentialRegistrationID, CredentialType,
EncryptedSelfAmountAddedEvent, Energy, ExchangeRate, Memo, RejectReason,
TransactionIndex, TransactionType, UpdateDetails, UpdatePayload,
hashes, hashes::TransactionHash, AccountCreationDetails, AccountTransactionDetails,
AccountTransactionEffects, BlockItemSummary, BlockItemSummaryDetails,
CredentialRegistrationID, CredentialType, EncryptedSelfAmountAddedEvent, Energy,
ExchangeRate, Memo, RejectReason, TransactionIndex, TransactionType, UpdateDetails,
UpdatePayload,
},
};
use futures::stream;
Expand Down Expand Up @@ -297,18 +320,19 @@ mod tests {
];

let effect = g.choose(&effects).unwrap().clone();

let hash = fixed_hash();
let summary = BlockItemSummary {
index: random_transaction_index(),
energy_cost: ArbitraryEnergy::arbitrary(g).0,
hash: fixed_hash(),
hash: hash.clone(),
details: BlockItemSummaryDetails::AccountTransaction(details(effect.clone())),
};

let notification =
NotificationInformationBasic::CCD(CCDTransactionNotificationInformation::new(
receiver_address,
BigInt::from(amount.micro_ccd).to_string(),
hash,
));
EmittingBlockItemSummaryPair(summary, notification)
}
Expand Down

0 comments on commit f3c03eb

Please sign in to comment.