Skip to content

Commit

Permalink
refactor: event keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kespinola committed Jun 16, 2023
1 parent 4cf4892 commit 77edb92
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 54 deletions.
7 changes: 3 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
DATABASE_URL=postgres://postgres:holaplex@localhost:5438/treasuries
DATABASE_URL=postgres://postgres:holaplex@localhost:5443/treasuries
POSTGRES_DB=treasuries
POSTGRES_PASSWORD=holaplex
KAFKA_BROKERS=127.0.0.1:9092
KAFKA_SSL=false
SOLANA_ENDPOINT=https://api.devnet.solana.com
SECRET_PATH=fireblocks_secret.key
FIREBLOCKS_SECRET_PATH=fireblocks_secret.key
FIREBLOCKS_ENDPOINT=https://api.fireblocks.io
CREDIT_SHEET=credits-test.toml
FIREBLOCKS_API_KEY=""
FIREBLOCKS_SUPPORTED_ASSET_IDS=SOL,MATIC
CREDIT_SHEET=./credits.toml
6 changes: 3 additions & 3 deletions api/proto.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sha512 = "d75800df0d4744c6b0f4d9a9952d3bfd0bb6b24a8babd19104cc11b54a525f85551b3c
[[schemas]]
subject = "nfts"
version = 1
sha512 = "10338b46a4d2caa2e1a22d1a7c62eb9d9b3465dd457cdbc12837cc2b2d0ca25099a4723ae3cc26e4eb41871866ee9957deffdf90f4fa2b3e9dfe667aa3fe09c3"
sha512 = "15565dbbf5fbed49cdf4dac7c44911eafa6836589f3ffbe06ad9debca60e7267f5461c3dcdd43aef4ab11e0ebb657dc0d2a06c299463df1f9579e0bc72e9bd8c"

[[schemas]]
subject = "organization"
Expand All @@ -16,9 +16,9 @@ sha512 = "db2ea24b06b84c84efdf833ea5b878d908c794073e1f943659c4ac947a921466309494
[[schemas]]
subject = "solana_nfts"
version = 1
sha512 = "2d6a5208c2e76bc07b180b9a0909062aad2ff82c9661c96dd483f2c50870e89a5111ecf6bc99147a24837618b2d9794712581838c01829ffd1181614ee32a790"
sha512 = "c2f0ca52d5606e8ff1836ee48ff5d524190ea38fbb27a7b3e65b55990b63eafbd188a8d81d2e413455d3b881b5c9e40a79df88f573f2df505cf4250d6481572c"

[[schemas]]
subject = "treasury"
version = 1
sha512 = "e35c2e7766d4cd718c6827c5c57b2fcb6a92ad84b0d56e2afca2bf1a132831b0d2bb39ec4a0bf486a7559ac77b8e2de6a346c9452c5882d0d9a02ec7892f95a1"
sha512 = "ad39716a72fa04a5a6c20c5a31e1804eb4b4dba5518e6189e7bb8a3cdef263e4c6a1f848a6e2a3519524e500c2593a4ddfbc12257bc3b8006b35eb2b9623b599"
3 changes: 2 additions & 1 deletion api/src/events/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ impl CustomerEventHandler for Processor {
event: Some(treasury_events::Event::CustomerTreasuryCreated(
CustomerTreasury {
customer_id: key.id.clone(),
project_id: customer.project_id,
project_id: project_id.to_string(),
},
)),
};

let key = TreasuryEventKey {
id: treasury.id.to_string(),
user_id: key.id,
project_id: Some(project_id.to_string()),
};

self.producer.send(Some(&event), Some(&key)).await?;
Expand Down
8 changes: 3 additions & 5 deletions api/src/events/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Emitter {
key: TreasuryEventKey,
signed_transaction: SignedTransaction,
) -> Result<()> {
info!("{:?} create_drop_signed", signed_transaction);
let event = TreasuryEvents {
event: Some(Event::MessageSigned(SignedTransactionEvent {
event: Some(signed_transaction_event::Event::CreateDrop(
Expand Down Expand Up @@ -125,10 +126,7 @@ impl Emitter {
}

impl From<SolanaNftEventKey> for TreasuryEventKey {
fn from(key: SolanaNftEventKey) -> Self {
Self {
id: key.project_id,
user_id: key.user_id,
}
fn from(SolanaNftEventKey { id, user_id, project_id }: SolanaNftEventKey) -> Self {
Self { id, user_id, project_id: Some(project_id) }
}
}
4 changes: 3 additions & 1 deletion api/src/events/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ impl OrganizationEventHandler for Processor {
active_model.insert(conn).await?;

let proto_blockchain_enum: proto::Blockchain = asset_type.into();
let project_id = project.id.to_string();

let event = treasury_events::Event::ProjectWalletCreated(ProjectWallet {
project_id: project.id.to_string(),
project_id: project_id.clone(),
wallet_address: vault_asset.address,
blockchain: proto_blockchain_enum as i32,
});
Expand All @@ -128,6 +129,7 @@ impl OrganizationEventHandler for Processor {
let key = TreasuryEventKey {
id: treasury.id.to_string(),
user_id: user_id.to_string(),
project_id: Some(project_id)
};

self.producer.send(Some(&event), Some(&key)).await?;
Expand Down
2 changes: 1 addition & 1 deletion api/src/events/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Processor {

Ok(())
},
None => Ok(()),
_ => Ok(()),
}
},
}
Expand Down
38 changes: 35 additions & 3 deletions api/src/events/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ use sea_orm::{

use crate::{
db::Connection,
entities::{treasuries, wallets},
proto::treasury_events::SignedTransaction,
entities::{sea_orm_active_enums::TxType, treasuries, wallets},
proto::{
treasury_events::{
signed_transaction::Transaction, SignedTransaction, SolanaSignedTransaction,
},
SolanaNftEventKey, SolanaTransaction,
},
};

#[async_trait]
Expand Down Expand Up @@ -52,7 +57,7 @@ impl Signer {
.context("wallet not found")
}

pub async fn sign_message(
async fn sign_message(
&self,
note: String,
serialized_message: Vec<u8>,
Expand Down Expand Up @@ -91,4 +96,31 @@ impl Signer {

Ok(signature_decoded)
}

pub async fn sign_transaction(
&self,
tx_type: TxType,
key: SolanaNftEventKey,
mut payload: SolanaTransaction,
) -> Result<SignedTransaction> {
let note = format!(
"{:?} by {:?} for project {:?}",
tx_type, key.user_id, key.project_id
);

let signature = self
.sign_message(note, payload.serialized_message.clone())
.await?;

payload
.signed_message_signatures
.push(bs58::encode(signature).into_string());

Ok(SignedTransaction {
transaction: Some(Transaction::Solana(SolanaSignedTransaction {
serialized_message: payload.serialized_message,
signed_message_signatures: payload.signed_message_signatures,
})),
})
}
}
41 changes: 7 additions & 34 deletions api/src/events/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
entities::sea_orm_active_enums::TxType,
proto::{
treasury_events::{
signed_transaction::Transaction, SignedTransaction, SolanaSignedTransaction,
SignedTransaction,
},
SolanaNftEventKey, SolanaTransaction, TreasuryEvents,
},
Expand Down Expand Up @@ -52,74 +52,47 @@ impl TransactionSigner<SolanaNftEventKey, SolanaTransaction> for Signer {
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::CreateDrop, key, payload).await
self.sign_transaction(TxType::CreateDrop, key, payload).await
}

async fn update_drop(
&self,
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::UpdateMetadata, key, payload).await
self.sign_transaction(TxType::UpdateMetadata, key, payload).await
}

async fn mint_drop(
&self,
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::MintEdition, key, payload).await
self.sign_transaction(TxType::MintEdition, key, payload).await
}

async fn transfer_asset(
&self,
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::TransferMint, key, payload).await
self.sign_transaction(TxType::TransferMint, key, payload).await
}

async fn retry_create_drop(
&self,
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::CreateDrop, key, payload).await
self.sign_transaction(TxType::CreateDrop, key, payload).await
}

async fn retry_mint_drop(
&self,
key: SolanaNftEventKey,
payload: SolanaTransaction,
) -> Result<SignedTransaction> {
sign_transaction(self, TxType::MintEdition, key, payload).await
self.sign_transaction(TxType::MintEdition, key, payload).await
}
}

async fn sign_transaction(
signer: &Signer,
tx_type: TxType,
key: SolanaNftEventKey,
mut payload: SolanaTransaction,
) -> Result<SignedTransaction> {
let note = format!(
"{:?} by {:?} for project {:?}",
tx_type, key.user_id, key.project_id
);

let signature = signer
.sign_message(note, payload.serialized_message.clone())
.await?;

payload
.signed_message_signatures
.push(bs58::encode(signature).into_string());

Ok(SignedTransaction {
transaction: Some(Transaction::Solana(SolanaSignedTransaction {
serialized_message: payload.serialized_message,
signed_message_signatures: payload.signed_message_signatures,
project_id: key.project_id,
})),
})
}
4 changes: 3 additions & 1 deletion api/src/mutations/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ impl Mutation {
.await?;

let wallet = update_wallet_address(db, vault_asset.address, deduction_id).await?;
let project_id = customer_treasury.project_id.to_string();

let event = TreasuryEvents {
event: Some(treasury_events::Event::CustomerWalletCreated(
treasury_events::CustomerWallet {
project_id: customer_treasury.project_id.to_string(),
project_id: project_id.clone(),
customer_id: customer_treasury.customer_id.to_string(),
blockchain: asset_type.into(),
},
Expand All @@ -111,6 +112,7 @@ impl Mutation {
let key = TreasuryEventKey {
id: treasury.id.to_string(),
user_id: user_id.to_string(),
project_id: Some(project_id),
};

producer.send(Some(&event), Some(&key)).await?;
Expand Down
23 changes: 23 additions & 0 deletions credits.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[MintEdition]
solana = 5
polygon = 10

[CreateDrop]
solana = 5
polygon = 15

[RetryMint]
solana = 5
polygon = 10

[TransferAsset]
solana = 5
polygon = 10

[RetryDrop]
solana = 5
polygon = 10

[CreateWallet]
solana = 1
polygon = 1
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
env_file:
- .env
ports:
- 5438:5432
- 5443:5432
volumes:
- hub_treasuries_db:/var/lib/postgresql/data
redpanda:
Expand Down

0 comments on commit 77edb92

Please sign in to comment.