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 .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DB=lending-indexer
POSTGRES_PORT=5432
DATABASE_URL=postgresql://postgres:password@postgres:5432/lending-indexer
DATABASE_URL=postgres://postgres:password@postgres:5432/lending-indexer
WEB_PORT=80

# Build-time variables for web image
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
runs-on: ubuntu-latest
env:
SQLX_OFFLINE: true
SIMPLEX_VERSION: v0.0.3

steps:
- name: Checkout
Expand All @@ -40,7 +41,7 @@ jobs:
BASE_DIR="${XDG_CONFIG_HOME:-$HOME}"
SIMPLEX_DIR="${SIMPLEX_DIR:-$BASE_DIR/.simplex}"
SIMPLEX_BIN_DIR="$SIMPLEX_DIR/bin"
"$SIMPLEX_BIN_DIR/simplexup"
"$SIMPLEX_BIN_DIR/simplexup" --install "$SIMPLEX_VERSION"
echo "$SIMPLEX_BIN_DIR" >> "$GITHUB_PATH"
"$SIMPLEX_BIN_DIR/simplex" --version

Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
SQLX_VERSION: 0.8.0
SQLX_FEATURES: "rustls,postgres"
SQLX_FEATURES_KEY: "rustls-postgres"
SIMPLEX_VERSION: v0.0.3
APP_USER: app
APP_USER_PWD: secret
APP_DB_NAME: lending-indexer
Expand Down Expand Up @@ -79,7 +80,7 @@ jobs:
BASE_DIR="${XDG_CONFIG_HOME:-$HOME}"
SIMPLEX_DIR="${SIMPLEX_DIR:-$BASE_DIR/.simplex}"
SIMPLEX_BIN_DIR="$SIMPLEX_DIR/bin"
"$SIMPLEX_BIN_DIR/simplexup"
"$SIMPLEX_BIN_DIR/simplexup" --install "$SIMPLEX_VERSION"
echo "$SIMPLEX_BIN_DIR" >> "$GITHUB_PATH"
"$SIMPLEX_BIN_DIR/simplex" --version

Expand Down Expand Up @@ -107,6 +108,11 @@ jobs:
cd crates/indexer
SKIP_DOCKER=true ./scripts/init_db.sh

- name: Run indexer tests
env:
DATABASE_URL: postgres://${{ env.APP_USER }}:${{ env.APP_USER_PWD }}@localhost:5432/${{ env.APP_DB_NAME }}
run: cargo test -p lending-indexer

- name: Run simplex tests
shell: bash
run: |
Expand Down

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

43 changes: 43 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ features = [
[dev-dependencies]
anyhow = "1"
cargo-husky = { workspace = true }
dotenvy = "0.15"
serial_test = "3"
2 changes: 1 addition & 1 deletion crates/indexer/src/api/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn fetch_offers_full_info_filtered(
) -> Result<Vec<OfferListItemFull>, sqlx::Error> {
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
r#"
SELECT id, current_status, borrower_pubkey, collateral_asset_id, principal_asset_id,
SELECT id, current_status, borrower_pubkey, borrower_output_script_hash, collateral_asset_id, principal_asset_id,
first_parameters_nft_asset_id, second_parameters_nft_asset_id, borrower_nft_asset_id,
lender_nft_asset_id, collateral_amount, principal_amount, interest_rate,
loan_expiration_time, created_at_height, created_at_txid FROM offers WHERE 1=1
Expand Down
52 changes: 52 additions & 0 deletions crates/indexer/src/api/dto/offer_participants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,55 @@ impl From<OfferParticipantModel> for ParticipantDto {
pub struct ScriptQuery {
pub script_pubkey: String,
}

#[cfg(test)]
mod tests {
use super::ParticipantDto;
use crate::models::{OfferParticipantModel, ParticipantType};
use uuid::Uuid;

#[test]
fn participant_dto_from_model_maps_hex_and_spent_fields() {
let offer_id = Uuid::new_v4();
let model = OfferParticipantModel {
offer_id,
participant_type: ParticipantType::Borrower,
script_pubkey: vec![0x51, 0xac],
txid: vec![0x01, 0x02, 0x03],
vout: 4,
created_at_height: 500,
spent_txid: Some(vec![0xaa, 0xbb]),
spent_at_height: Some(777),
};

let dto = ParticipantDto::from(model);

assert_eq!(dto.offer_id, offer_id);
assert_eq!(dto.participant_type, ParticipantType::Borrower);
assert_eq!(dto.script_pubkey, "51ac");
assert_eq!(dto.txid, "030201");
assert_eq!(dto.vout, 4);
assert_eq!(dto.created_at_height, 500);
assert_eq!(dto.spent_txid, Some("bbaa".to_string()));
assert_eq!(dto.spent_at_height, Some(777));
}

#[test]
fn participant_dto_from_model_handles_unspent_participant_utxo() {
let model = OfferParticipantModel {
offer_id: Uuid::new_v4(),
participant_type: ParticipantType::Lender,
script_pubkey: vec![0x00],
txid: vec![0x10],
vout: 0,
created_at_height: 1,
spent_txid: None,
spent_at_height: None,
};

let dto = ParticipantDto::from(model);

assert_eq!(dto.spent_txid, None);
assert_eq!(dto.spent_at_height, None);
}
}
49 changes: 49 additions & 0 deletions crates/indexer/src/api/dto/offer_utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,52 @@ impl From<OfferUtxoModel> for OfferUtxoDto {
}
}
}

#[cfg(test)]
mod tests {
use super::OfferUtxoDto;
use crate::models::{OfferUtxoModel, UtxoType};
use uuid::Uuid;

#[test]
fn offer_utxo_dto_from_model_maps_optional_spent_fields() {
let offer_id = Uuid::new_v4();
let model = OfferUtxoModel {
offer_id,
txid: vec![0x01, 0x02, 0x03],
vout: 7,
utxo_type: UtxoType::Repayment,
created_at_height: 123,
spent_txid: Some(vec![0xaa, 0xbb]),
spent_at_height: Some(456),
};

let dto = OfferUtxoDto::from(model);

assert_eq!(dto.offer_id, offer_id);
assert_eq!(dto.txid, "030201");
assert_eq!(dto.vout, 7);
assert_eq!(dto.utxo_type, UtxoType::Repayment);
assert_eq!(dto.created_at_height, 123);
assert_eq!(dto.spent_txid, Some("bbaa".to_string()));
assert_eq!(dto.spent_at_height, Some(456));
}

#[test]
fn offer_utxo_dto_from_model_handles_unspent_utxo() {
let model = OfferUtxoModel {
offer_id: Uuid::new_v4(),
txid: vec![0x11],
vout: 0,
utxo_type: UtxoType::Lending,
created_at_height: 1,
spent_txid: None,
spent_at_height: None,
};

let dto = OfferUtxoDto::from(model);

assert_eq!(dto.spent_txid, None);
assert_eq!(dto.spent_at_height, None);
}
}
74 changes: 74 additions & 0 deletions crates/indexer/src/api/dto/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,77 @@ pub struct OfferDetailsResponse {
pub info: OfferListItemFull,
pub participants: Vec<ParticipantDto>,
}

#[cfg(test)]
mod tests {
use super::{OfferListItemFull, OfferListItemShort};
use crate::models::{OfferModel, OfferModelShort, OfferStatus};
use uuid::Uuid;

#[test]
fn offer_list_item_short_from_model_short_maps_and_formats_fields() {
let id = Uuid::new_v4();
let model = OfferModelShort {
id,
collateral_asset_id: vec![0x01, 0x02, 0x03],
principal_asset_id: vec![0x04, 0x05, 0x06],
collateral_amount: 1000,
principal_amount: 500,
interest_rate: 250,
loan_expiration_time: 123,
current_status: OfferStatus::Active,
created_at_height: 456,
created_at_txid: vec![0xaa, 0xbb, 0xcc],
};

let dto = OfferListItemShort::from(model);

assert_eq!(dto.id, id);
assert_eq!(dto.status, OfferStatus::Active);
assert_eq!(dto.collateral_asset, "030201");
assert_eq!(dto.principal_asset, "060504");
assert_eq!(dto.collateral_amount, 1000);
assert_eq!(dto.principal_amount, 500);
assert_eq!(dto.interest_rate, 250);
assert_eq!(dto.loan_expiration_time, 123);
assert_eq!(dto.created_at_height, 456);
assert_eq!(dto.created_at_txid, "ccbbaa");
}

#[test]
fn offer_list_item_full_from_model_maps_nested_and_extra_fields() {
let id = Uuid::new_v4();
let model = OfferModel {
id,
borrower_pubkey: vec![0x11, 0x22],
borrower_output_script_hash: vec![0x33, 0x44],
collateral_asset_id: vec![0x01, 0x02],
principal_asset_id: vec![0x03, 0x04],
first_parameters_nft_asset_id: vec![0x05, 0x06],
second_parameters_nft_asset_id: vec![0x07, 0x08],
borrower_nft_asset_id: vec![0x09, 0x0a],
lender_nft_asset_id: vec![0x0b, 0x0c],
collateral_amount: 99,
principal_amount: 77,
interest_rate: 12,
loan_expiration_time: 321,
current_status: OfferStatus::Pending,
created_at_height: 55,
created_at_txid: vec![0xde, 0xad],
};

let dto = OfferListItemFull::from(model);

assert_eq!(dto.base.id, id);
assert_eq!(dto.base.status, OfferStatus::Pending);
assert_eq!(dto.base.collateral_asset, "0201");
assert_eq!(dto.base.principal_asset, "0403");
assert_eq!(dto.base.created_at_txid, "adde");
assert_eq!(dto.borrower_pubkey, "1122");
assert_eq!(dto.borrower_output_script_hash, "3344");
assert_eq!(dto.first_parameters_nft_asset, "0605");
assert_eq!(dto.second_parameters_nft_asset, "0807");
assert_eq!(dto.borrower_nft_asset, "0a09");
assert_eq!(dto.lender_nft_asset, "0c0b");
}
}
Loading
Loading