-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
24 changes: 14 additions & 10 deletions
24
rust/processor/migrations/2024-05-21-221101_add_royalty_v1/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE token_royalty ( | ||
transaction_version BIGINT NOT NULL, | ||
token_data_id VARCHAR(64) NOT NULL, | ||
payee_address VARCHAR(64) NOT NULL, | ||
royalty_points_numerator_v1 INT NOT NULL, | ||
royalty_points_denominator_v1 INT NOT NULL, | ||
token_data_id VARCHAR(66) NOT NULL, | ||
payee_address VARCHAR(66) NOT NULL, | ||
royalty_points_numerator NUMERIC NOT NULL, | ||
royalty_points_denominator NUMERIC NOT NULL, | ||
token_standard TEXT NOT NULL, | ||
(transaction_version, token_data_id) PRIMARY KEY | ||
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (transaction_version, token_data_id) | ||
); | ||
|
||
CREATE TABLE current_token_royalty ( | ||
token_data_id VARCHAR(64) NOT NULL, | ||
payee_address VARCHAR(64) NOT NULL, | ||
royalty_points_numerator_v1 INT NOT NULL, | ||
royalty_points_denominator_v1 INT NOT NULL, | ||
token_data_id VARCHAR(66) NOT NULL, | ||
payee_address VARCHAR(66) NOT NULL, | ||
royalty_points_numerator NUMERIC NOT NULL, | ||
royalty_points_denominator NUMERIC NOT NULL, | ||
token_standard TEXT NOT NULL, | ||
token_data_id PRIMARY KEY | ||
last_transaction_version BIGINT NOT NULL, | ||
last_transaction_timestamp TIMESTAMP NOT NULL, | ||
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (token_data_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
rust/processor/src/models/token_v2_models/v2_token_royalty.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This is required because a diesel macro makes clippy sad | ||
#![allow(clippy::extra_unused_lifetimes)] | ||
#![allow(clippy::unused_unit)] | ||
|
||
use super::v2_token_utils::TokenStandard; | ||
use crate::{ | ||
models::token_models::token_utils::TokenWriteSet, | ||
schema::{current_token_royalty, token_royalty}, | ||
}; | ||
use aptos_protos::transaction::v1::WriteTableItem; | ||
use bigdecimal::BigDecimal; | ||
use field_count::FieldCount; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] | ||
#[diesel(primary_key(token_data_id, transaction_version))] | ||
#[diesel(table_name = token_royalty)] | ||
pub struct TokenRoyalty { | ||
pub transaction_version: i64, | ||
pub token_data_id: String, | ||
pub payee_address: String, | ||
pub royalty_points_numerator: BigDecimal, | ||
pub royalty_points_denominator: BigDecimal, | ||
pub token_standard: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] | ||
#[diesel(primary_key(token_data_id))] | ||
#[diesel(table_name = current_token_royalty)] | ||
pub struct CurrentTokenRoyalty { | ||
pub token_data_id: String, | ||
pub payee_address: String, | ||
pub royalty_points_numerator: BigDecimal, | ||
pub royalty_points_denominator: BigDecimal, | ||
pub token_standard: String, | ||
pub last_transaction_version: i64, | ||
pub last_transaction_timestamp: chrono::NaiveDateTime, | ||
} | ||
|
||
impl TokenRoyalty { | ||
// Royalty for v2 token is more complicated and not supported yet. For token v2, royalty can be on the collection (default) or on | ||
// the token (override). | ||
pub fn get_v1_from_write_table_item( | ||
write_table_item: &WriteTableItem, | ||
transaction_version: i64, | ||
transaction_timestamp: chrono::NaiveDateTime, | ||
) -> anyhow::Result<Option<(Self, CurrentTokenRoyalty)>> { | ||
let table_item_data = write_table_item.data.as_ref().unwrap(); | ||
|
||
let maybe_token_data = match TokenWriteSet::from_table_item_type( | ||
table_item_data.value_type.as_str(), | ||
&table_item_data.value, | ||
transaction_version, | ||
)? { | ||
Some(TokenWriteSet::TokenData(inner)) => Some(inner), | ||
_ => None, | ||
}; | ||
|
||
if let Some(token_data) = maybe_token_data { | ||
let maybe_token_data_id = match TokenWriteSet::from_table_item_type( | ||
table_item_data.key_type.as_str(), | ||
&table_item_data.key, | ||
transaction_version, | ||
)? { | ||
Some(TokenWriteSet::TokenDataId(inner)) => Some(inner), | ||
_ => None, | ||
}; | ||
if let Some(token_data_id_struct) = maybe_token_data_id { | ||
let token_data_id = token_data_id_struct.to_hash(); | ||
let payee_address = token_data.royalty.get_payee_address(); | ||
let royalty_points_numerator = token_data.royalty.royalty_points_numerator.clone(); | ||
let royalty_points_denominator = | ||
token_data.royalty.royalty_points_denominator.clone(); | ||
|
||
return Ok(Some(( | ||
TokenRoyalty { | ||
transaction_version, | ||
token_data_id: token_data_id.clone(), | ||
payee_address: payee_address.clone(), | ||
royalty_points_numerator: royalty_points_numerator.clone(), | ||
royalty_points_denominator: royalty_points_denominator.clone(), | ||
token_standard: TokenStandard::V1.to_string(), | ||
}, | ||
CurrentTokenRoyalty { | ||
token_data_id, | ||
payee_address, | ||
royalty_points_numerator, | ||
royalty_points_denominator, | ||
token_standard: TokenStandard::V1.to_string(), | ||
last_transaction_version: transaction_version, | ||
last_transaction_timestamp: transaction_timestamp, | ||
}, | ||
))); | ||
} else { | ||
tracing::warn!( | ||
transaction_version, | ||
key_type = table_item_data.key_type, | ||
key = table_item_data.key, | ||
"Expecting token_data_id as key for value = token_data" | ||
); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.