Skip to content

Commit

Permalink
Projected NFT indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
gostkin committed Oct 31, 2023
1 parent c050fd5 commit 8f22a32
Show file tree
Hide file tree
Showing 22 changed files with 1,267 additions and 652 deletions.
371 changes: 257 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ anyhow = { version = "1.0.69" }
async-trait = { version = "0.1.64" }
base64 = { version = "0.21.0" }
cardano-multiplatform-lib = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", branch = "metadata-and-addr" }
projected-nft-sdk = { git = "https://github.com/dcSpark/projected-nft-whirlpool.git", rev = "8dc510c53fb86cb5c9eae746a05bb64c9b9f7f4b" }
clap = { version = "3.1", features = ["derive"] }
ctrlc = { version = "3.2.4", features = ["termination"] }
dotenv = { version = "0.15.0" }
Expand Down
2 changes: 1 addition & 1 deletion indexer/entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub mod dex_swap;
pub mod native_asset;
pub mod plutus_data;
pub mod plutus_data_hash;
pub mod transaction_metadata;
pub mod projected_nft;
pub mod transaction_metadata;
5 changes: 5 additions & 0 deletions indexer/entity/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub use super::plutus_data_hash::{
Entity as PlutusDataHash, Model as PlutusDataHashModel, PrimaryKey as PlutusDataHashPrimaryKey,
Relation as PlutusDataHashRelation,
};
pub use super::projected_nft::{
ActiveModel as ProjectedNftActiveModel, Column as ProjectedNftColumn, Entity as ProjectedNft,
Model as ProjectedNftModel, PrimaryKey as ProjectedNftPrimaryKey,
Relation as ProjectedNftRelation,
};
pub use super::stake_credential::{
ActiveModel as StakeCredentialActiveModel, Column as StakeCredentialColumn,
Entity as StakeCredential, Model as StakeCredentialModel,
Expand Down
36 changes: 11 additions & 25 deletions indexer/entity/src/projected_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,31 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, column_type = "BigInteger")]
pub id: i64,
#[sea_orm(column_type = "BigInteger")]
pub utxo_id: i64,
#[sea_orm(column_type = "BigInteger", nullable)]
pub utxo_id: Option<i64>,
#[sea_orm(column_type = "BigInteger")]
pub tx_id: i64,
pub asset: String,
#[sea_orm(primary_key, column_type = "BigInteger")]
pub amount: i64,
pub operation: i32, // lock / unlock / claim
// address_id here is useful for fast pagination without joining w/ txoutput table
#[sea_orm(column_type = "BigInteger", nullable)]
pub asset_id: Option<i64>,
pub amount: u64,
pub plutus_datum: Vec<u8>,
}

#[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"
belongs_to = "super::transaction_output::Entity",
from = "Column::UtxoId",
to = "super::transaction_output::Column::Id"
)]
TransactionOutput,
#[sea_orm(
belongs_to = "super::transaction::Entity",
from = "Column::TxId",
to = "super::transaction::Column::Id"
belongs_to = "super::transaction::Entity",
from = "Column::TxId",
to = "super::transaction::Column::Id"
)]
Transaction,
#[sea_orm(
belongs_to = "super::native_asset::Entity",
from = "Column::AssetId",
to = "super::native_asset::Column::Id"
)]
Asset,
}

// TODO: figure out why this isn't automatically handle by the macros above
impl Related<super::native_asset::Entity> for Entity {
fn to() -> RelationDef {
Relation::Asset.def()
}
}

// TODO: figure out why this isn't automatically handle by the macros above
Expand Down
2 changes: 2 additions & 0 deletions indexer/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod m20220528_000012_create_plutus_data_table;
mod m20220808_000013_create_transaction_reference_input_table;
mod m20221031_000014_create_dex_table;
mod m20230223_000015_modify_block_table;
mod m20231025_000016_projected_nft;

pub struct Migrator;

Expand All @@ -41,6 +42,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220808_000013_create_transaction_reference_input_table::Migration),
Box::new(m20221031_000014_create_dex_table::Migration),
Box::new(m20230223_000015_modify_block_table::Migration),
Box::new(m20231025_000016_projected_nft::Migration),
]
}
}
69 changes: 69 additions & 0 deletions indexer/migration/src/m20231025_000016_projected_nft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use sea_schema::migration::prelude::*;

use entity::projected_nft::*;

pub struct Migration;

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

#[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::UtxoId).big_integer())
.col(ColumnDef::new(Column::TxId).big_integer().not_null())
.col(ColumnDef::new(Column::Asset).text().not_null())
.col(ColumnDef::new(Column::Amount).big_integer().not_null())
.col(ColumnDef::new(Column::Operation).integer())
.col(ColumnDef::new(Column::PlutusDatum).binary())
.foreign_key(
ForeignKey::create()
.name("fk-projected_nft-tx_id")
.from(Entity, Column::TxId)
.to(
entity::prelude::Transaction,
entity::prelude::TransactionColumn::Id,
)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-projected_nft-utxo_id")
.from(Entity, Column::UtxoId)
.to(
entity::prelude::TransactionOutput,
entity::prelude::TransactionOutputColumn::Id,
)
.on_delete(ForeignKeyAction::Cascade),
)
.primary_key(
Index::create()
.table(Entity)
.name("projected_nft-pk")
.col(Column::Id),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Entity).to_owned())
.await
}
}
3 changes: 3 additions & 0 deletions indexer/tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ entity = { path = "../entity" }
# [tasks]
anyhow = { version = "1.0.69" }
cardano-multiplatform-lib = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", branch = "metadata-and-addr" }
cml-chain = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", rev = "acca172633d0570a7432058aa5b0717ad2f0c6d7" }
cml-core = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", rev = "acca172633d0570a7432058aa5b0717ad2f0c6d7" }
projected-nft-sdk = { git = "https://github.com/dcSpark/projected-nft-whirlpool.git", rev = "a350ccfcbdef22b73c93561c6816bd42b13f00ff" }
cfg-if = { version = "0.1.10" }
cryptoxide = { version = "0.4.2" }
hex = { version = "0.4.3" }
Expand Down
4 changes: 2 additions & 2 deletions indexer/tasks/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[allow(non_snake_case)]
pub mod AddressConfig;
#[allow(non_snake_case)]
pub mod EmptyConfig;
#[allow(non_snake_case)]
pub mod PayloadAndReadonlyConfig;
#[allow(non_snake_case)]
pub mod PayloadConfig;
#[allow(non_snake_case)]
pub mod ReadonlyConfig;
#[allow(non_snake_case)]
pub mod AddressConfig;
4 changes: 2 additions & 2 deletions indexer/tasks/src/multiera/dex/minswap_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Dex for MinSwapV1 {
&[POOL_SCRIPT_HASH1, POOL_SCRIPT_HASH2],
&tx.plutus_data(),
)
.get(0)
.first()
{
let datum = datum.to_json();

Expand Down Expand Up @@ -76,7 +76,7 @@ impl Dex for MinSwapV1 {
&[POOL_SCRIPT_HASH1, POOL_SCRIPT_HASH2],
&tx.plutus_data(),
)
.get(0)
.first()
{
let main_datum = main_datum.to_json();
let mut free_utxos: Vec<MultiEraOutput> = tx.outputs();
Expand Down
4 changes: 2 additions & 2 deletions indexer/tasks/src/multiera/dex/sundaeswap_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Dex for SundaeSwapV1 {
// Note: there should be at most one pool output
if let Some((output, datum)) =
filter_outputs_and_datums_by_hash(&tx.outputs(), &[POOL_SCRIPT_HASH], &tx.plutus_data())
.get(0)
.first()
{
let datum = datum.to_json();

Expand Down Expand Up @@ -68,7 +68,7 @@ impl Dex for SundaeSwapV1 {
// Note: there should be at most one pool output
if let Some((main_output, main_datum)) =
filter_outputs_and_datums_by_hash(&tx.outputs(), &[POOL_SCRIPT_HASH], &tx.plutus_data())
.get(0)
.first()
{
let main_datum = main_datum.to_json();
let mut free_utxos: Vec<MultiEraOutput> = tx.outputs();
Expand Down
6 changes: 3 additions & 3 deletions indexer/tasks/src/multiera/dex/wingriders_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Dex for WingRidersV1 {
// Note: there should be at most one pool output
if let Some((output, datum)) =
filter_outputs_and_datums_by_hash(&tx.outputs(), &[POOL_SCRIPT_HASH], &tx.plutus_data())
.get(0)
.first()
{
let datum = datum.to_json();

Expand Down Expand Up @@ -81,12 +81,12 @@ impl Dex for WingRidersV1 {
// Note: there should be at most one pool output
if let Some((pool_output, _)) =
filter_outputs_and_datums_by_hash(&tx.outputs(), &[POOL_SCRIPT_HASH], &tx.plutus_data())
.get(0)
.first()
{
let redeemers = tx.redeemers().ok_or("No redeemers")?;

// Get pool input from redemeers
let pool_input_redeemer = redeemers.get(0).ok_or("No redeemers")?;
let pool_input_redeemer = redeemers.first().ok_or("No redeemers")?;
let pool_input = pool_input_redeemer.data.to_json()["fields"][0]["int"]
.as_i64()
.ok_or("Failed to parse pool input index")?;
Expand Down
2 changes: 1 addition & 1 deletion indexer/tasks/src/multiera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod multiera_executor;
pub mod multiera_metadata;
pub mod multiera_minswap_v1_mean_price;
pub mod multiera_minswap_v1_swap;
pub mod multiera_projected_nft;
pub mod multiera_reference_inputs;
pub mod multiera_stake_credentials;
pub mod multiera_sundaeswap_v1_mean_price;
Expand All @@ -22,4 +23,3 @@ pub mod multiera_wingriders_v1_mean_price;
pub mod multiera_wingriders_v1_swap;
pub mod relation_map;
pub mod utils;
pub mod multiera_projected_nft;
Loading

0 comments on commit 8f22a32

Please sign in to comment.