Skip to content

Commit

Permalink
add index for utxos by native asset
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Dec 21, 2023
1 parent 8378f15 commit ab58a2f
Show file tree
Hide file tree
Showing 17 changed files with 726 additions and 1 deletion.
141 changes: 141 additions & 0 deletions docs/bin/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,88 @@
],
"type": "object"
},
"AssetUtxosResponse": {
"items": {
"properties": {
"txId": {
"type": "string"
},
"slot": {
"type": "number",
"format": "double"
},
"paymentCred": {
"type": "string"
},
"utxo": {
"properties": {
"index": {
"type": "number",
"format": "double"
},
"tx": {
"type": "string"
}
},
"required": [
"index",
"tx"
],
"type": "object"
},
"amount": {
"type": "number",
"format": "double",
"description": "If the utxo is created, this has the amount. It's undefined if the utxo\nis spent.",
"example": 1031423725351
}
},
"required": [
"txId",
"slot",
"paymentCred",
"utxo"
],
"type": "object"
},
"type": "array"
},
"Cip14Fingerprint": {
"type": "string",
"example": "asset1c43p68zwjezc7f6w4w9qkhkwv9ppwz0f7c3amw"
},
"AssetUtxosRequest": {
"properties": {
"assets": {
"items": {
"$ref": "#/components/schemas/Cip14Fingerprint"
},
"type": "array"
},
"range": {
"properties": {
"maxSlot": {
"type": "number",
"format": "double"
},
"minSlot": {
"type": "number",
"format": "double"
}
},
"required": [
"maxSlot",
"minSlot"
],
"type": "object"
}
},
"required": [
"assets",
"range"
],
"type": "object"
},
"BlockSubset": {
"properties": {
"slot": {
Expand Down Expand Up @@ -1146,6 +1228,65 @@
}
}
},
"/asset/utxos": {
"post": {
"operationId": "AssetUtxos",
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AssetUtxosResponse"
}
}
}
},
"400": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorShape"
}
}
}
},
"409": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorShape"
}
}
}
},
"422": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorShape"
}
}
}
}
},
"security": [],
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AssetUtxosRequest"
}
}
}
}
}
},
"/block/latest": {
"post": {
"operationId": "BlockLatest",
Expand Down
59 changes: 59 additions & 0 deletions indexer/entity/src/asset_utxos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "AssetUtxo")]
pub struct Model {
#[sea_orm(primary_key, column_type = "BigInteger")]
pub id: i64,
#[sea_orm(column_type = "BigInteger")]
pub asset_id: i64,
#[sea_orm(column_type = "BigInteger")]
pub utxo_id: i64,
pub amount: Option<i64>,
pub tx_id: i64,
}

#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::transaction_output::Entity",
from = "Column::UtxoId",
to = "super::transaction_output::Column::Id"
)]
TransactionOutput,

#[sea_orm(
belongs_to = "super::native_asset::Entity",
from = "Column::AssetId",
to = "super::native_asset::Column::Id"
)]
Asset,

#[sea_orm(
belongs_to = "super::transaction::Entity",
from = "Column::TxId",
to = "super::transaction::Column::Id"
)]
Transaction,
}

impl Related<super::transaction_output::Entity> for Entity {
fn to() -> RelationDef {
Relation::TransactionOutput.def()
}
}

impl Related<super::native_asset::Entity> for Entity {
fn to() -> RelationDef {
Relation::Asset.def()
}
}

impl Related<super::transaction::Entity> for Entity {
fn to() -> RelationDef {
Relation::Transaction.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions indexer/entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod transaction_reference_input;
pub mod tx_credential;
pub use sea_orm;
pub mod asset_mint;
pub mod asset_utxos;
pub mod cip25_entry;
pub mod dex_swap;
pub mod native_asset;
Expand Down
4 changes: 4 additions & 0 deletions indexer/entity/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub use super::asset_mint::{
ActiveModel as AssetMintActiveModel, Column as AssetMintColumn, Entity as AssetMint,
Model as AssetMintModel, PrimaryKey as AssetMintPrimaryKey, Relation as AssetMintRelation,
};
pub use super::asset_utxos::{
ActiveModel as AssetUtxoActiveModel, Column as AssetUtxoCredentialColumn, Entity as AssetUtxo,
Model as AssetUtxoModel, PrimaryKey as AssetUtxoPrimaryKey, Relation as AssetUtxoRelation,
};
pub use super::block::{
ActiveModel as BlockActiveModel, Column as BlockColumn, Entity as Block, Model as BlockModel,
PrimaryKey as BlockPrimaryKey, Relation as BlockRelation,
Expand Down
2 changes: 2 additions & 0 deletions indexer/execution_plans/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ readonly=false
[MultieraCip25EntryTask]

[MultieraAddressDelegationTask]

[MultieraAssetUtxos]
2 changes: 2 additions & 0 deletions indexer/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod m20221031_000014_create_dex_table;
mod m20230223_000015_modify_block_table;
mod m20230927_000016_create_stake_delegation_table;
mod m20231025_000017_projected_nft;
mod m20231220_000018_asset_utxo_table;

pub struct Migrator;

Expand All @@ -45,6 +46,7 @@ impl MigratorTrait for Migrator {
Box::new(m20230223_000015_modify_block_table::Migration),
Box::new(m20230927_000016_create_stake_delegation_table::Migration),
Box::new(m20231025_000017_projected_nft::Migration),
Box::new(m20231220_000018_asset_utxo_table::Migration),
]
}
}
80 changes: 80 additions & 0 deletions indexer/migration/src/m20231220_000018_asset_utxo_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use entity::{asset_utxos::*, prelude};
use sea_schema::migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20231220_000018_asset_utxos.rs"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Entity)
.if_not_exists()
.col(
ColumnDef::new(Column::Id)
.big_integer()
.not_null()
.auto_increment(),
)
.col(ColumnDef::new(Column::AssetId).big_integer().not_null())
.col(ColumnDef::new(Column::UtxoId).big_integer().not_null())
.col(ColumnDef::new(Column::TxId).big_integer().not_null())
.col(ColumnDef::new(Column::Amount).big_integer())
.foreign_key(
ForeignKey::create()
.name("fk-asset_utxo-asset_id")
.from(Entity, Column::AssetId)
.to(prelude::NativeAsset, prelude::NativeAssetColumn::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-asset_utxo-tx_id")
.from(Entity, Column::TxId)
.to(prelude::Transaction, prelude::TransactionColumn::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-asset_utxo-utxo_id")
.from(Entity, Column::UtxoId)
.to(
prelude::TransactionOutput,
prelude::TransactionOutputColumn::Id,
)
.on_delete(ForeignKeyAction::Cascade),
)
.primary_key(
Index::create()
.table(Entity)
.name("asset_utxo-pk")
.col(Column::Id),
)
.to_owned(),
)
.await?;

manager
.create_index(
Index::create()
.table(Entity)
.name("index-asset_utxo-transaction")
.col(Column::TxId)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Entity).to_owned())
.await
}
}
1 change: 1 addition & 0 deletions indexer/tasks/src/multiera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod multiera_address;
pub mod multiera_address_credential_relations;
pub mod multiera_address_delegation;
pub mod multiera_asset_mint;
pub mod multiera_asset_utxo;
pub mod multiera_block;
pub mod multiera_cip25entry;
pub mod multiera_datum;
Expand Down
Loading

0 comments on commit ab58a2f

Please sign in to comment.