forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Indexer] added nft_points custom contract parsing (aptos-labs#7018)
* added nft_points custom contract parsing * Add more fields
- Loading branch information
1 parent
354ed8b
commit 3fc72cc
Showing
8 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
crates/indexer/migrations/2023-03-08-205402_nft_points/down.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- This file should undo anything in `up.sql`\ | ||
DROP TABLE IF EXISTS nft_points; | ||
DROP INDEX IF EXISTS np_oa_idx; | ||
DROP INDEX IF EXISTS np_tt_oa_idx; | ||
DROP INDEX IF EXISTS np_insat_idx; |
13 changes: 13 additions & 0 deletions
13
crates/indexer/migrations/2023-03-08-205402_nft_points/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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE nft_points ( | ||
transaction_version BIGINT UNIQUE PRIMARY KEY NOT NULL, | ||
owner_address VARCHAR(66) NOT NULL, | ||
token_name TEXT NOT NULL, | ||
point_type TEXT NOT NULL, | ||
amount NUMERIC NOT NULL, | ||
transaction_timestamp TIMESTAMP NOT NULL, | ||
inserted_at TIMESTAMP NOT NULL DEFAULT NOW() | ||
); | ||
CREATE INDEX np_oa_idx ON nft_points (owner_address); | ||
CREATE INDEX np_tt_oa_idx ON nft_points (transaction_timestamp, owner_address); | ||
CREATE INDEX np_insat_idx ON nft_points (inserted_at); |
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
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,75 @@ | ||
// 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 crate::{ | ||
schema::nft_points, | ||
util::{parse_timestamp, standardize_address}, | ||
}; | ||
use aptos_api_types::{Transaction as APITransaction, TransactionPayload}; | ||
use bigdecimal::BigDecimal; | ||
use diesel::prelude::*; | ||
use field_count::FieldCount; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] | ||
#[diesel(primary_key(transaction_version))] | ||
#[diesel(table_name = nft_points)] | ||
pub struct NftPoints { | ||
pub transaction_version: i64, | ||
pub owner_address: String, | ||
pub token_name: String, | ||
pub point_type: String, | ||
pub amount: BigDecimal, | ||
pub transaction_timestamp: chrono::NaiveDateTime, | ||
} | ||
|
||
impl NftPoints { | ||
pub fn from_transaction( | ||
transaction: &APITransaction, | ||
nft_points_contract: Option<String>, | ||
) -> Option<Self> { | ||
if let Some(contract) = nft_points_contract { | ||
if let APITransaction::UserTransaction(user_txn) = transaction { | ||
let payload = &user_txn.request.payload; | ||
// If failed transaction, end | ||
if !user_txn.info.success { | ||
return None; | ||
} | ||
if let TransactionPayload::EntryFunctionPayload(entry_function_payload) = payload { | ||
if entry_function_payload.function.to_string() == contract { | ||
let transaction_version = user_txn.info.version.0 as i64; | ||
let owner_address = standardize_address( | ||
entry_function_payload.arguments[0].as_str().unwrap(), | ||
); | ||
let amount = entry_function_payload.arguments[2] | ||
.as_str() | ||
.unwrap() | ||
.parse() | ||
.unwrap(); | ||
let transaction_timestamp = | ||
parse_timestamp(user_txn.timestamp.0, transaction_version); | ||
return Some(Self { | ||
transaction_version, | ||
owner_address, | ||
token_name: entry_function_payload.arguments[1] | ||
.as_str() | ||
.unwrap() | ||
.to_string(), | ||
point_type: entry_function_payload.arguments[3] | ||
.as_str() | ||
.unwrap() | ||
.to_string(), | ||
amount, | ||
transaction_timestamp, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
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
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
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