0.5.6
This commit is contained in:
@@ -62,6 +62,133 @@ LIMIT 1
|
||||
error
|
||||
))),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists normalized pool token rows by pool id.
|
||||
pub async fn list_pool_tokens_by_pool_id(
|
||||
database: &crate::KbDatabase,
|
||||
pool_id: i64,
|
||||
) -> Result<std::vec::Vec<crate::KbPoolTokenDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPoolTokenEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
pool_id,
|
||||
token_id,
|
||||
role,
|
||||
vault_address,
|
||||
token_order,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_pool_tokens
|
||||
WHERE pool_id = ?
|
||||
ORDER BY token_order ASC, id ASC
|
||||
"#,
|
||||
)
|
||||
.bind(pool_id)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_pool_tokens for pool_id '{}' on sqlite: {}",
|
||||
pool_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbPoolTokenDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn pool_token_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("pool_token_roundtrip.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dex_id = crate::upsert_dex(
|
||||
&database,
|
||||
&crate::KbDexDto::new(
|
||||
"raydium".to_string(),
|
||||
"Raydium".to_string(),
|
||||
None,
|
||||
None,
|
||||
true,
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("dex upsert must succeed");
|
||||
let token_id = crate::upsert_token(
|
||||
&database,
|
||||
&crate::KbTokenDto::new(
|
||||
"Base111111111111111111111111111111111111111".to_string(),
|
||||
Some("BASE".to_string()),
|
||||
Some("Base Token".to_string()),
|
||||
Some(6),
|
||||
crate::SPL_TOKEN_PROGRAM_ID.to_string(),
|
||||
false,
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("token upsert must succeed");
|
||||
let pool_id = crate::upsert_pool(
|
||||
&database,
|
||||
&crate::KbPoolDto::new(
|
||||
dex_id,
|
||||
"Pool111111111111111111111111111111111111111".to_string(),
|
||||
crate::KbPoolKind::Amm,
|
||||
crate::KbPoolStatus::Active,
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("pool upsert must succeed");
|
||||
let pool_token_id = crate::upsert_pool_token(
|
||||
&database,
|
||||
&crate::KbPoolTokenDto::new(
|
||||
pool_id,
|
||||
token_id,
|
||||
crate::KbPoolTokenRole::Base,
|
||||
Some("Vault111111111111111111111111111111111111".to_string()),
|
||||
Some(0),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("pool token upsert must succeed");
|
||||
assert!(pool_token_id > 0);
|
||||
let pool_tokens = crate::list_pool_tokens_by_pool_id(&database, pool_id)
|
||||
.await
|
||||
.expect("list pool tokens must succeed");
|
||||
assert_eq!(pool_tokens.len(), 1);
|
||||
assert_eq!(pool_tokens[0].role, crate::KbPoolTokenRole::Base);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user