sui_indexer_alt_schema/
transactions.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use diesel::backend::Backend;
5use diesel::deserialize::FromSqlRow;
6use diesel::deserialize::{self};
7use diesel::expression::AsExpression;
8use diesel::prelude::*;
9use diesel::serialize;
10use diesel::sql_types::SmallInt;
11use serde::Deserialize;
12use serde::Serialize;
13use sui_field_count::FieldCount;
14use sui_types::object::Owner;
15
16use crate::schema::kv_transactions;
17use crate::schema::tx_affected_addresses;
18use crate::schema::tx_affected_objects;
19use crate::schema::tx_balance_changes;
20use crate::schema::tx_calls;
21use crate::schema::tx_digests;
22use crate::schema::tx_kinds;
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub enum BalanceChange {
26    V1 {
27        /// Owner whose balance changed
28        owner: Owner,
29
30        /// Type of the Coin (just the one-time witness type).
31        coin_type: String,
32
33        /// The amount the balance changed by. A negative amount means the net flow of value is
34        /// from the owner, and a positive amount means the net flow of value is to the owner.
35        amount: i128,
36    },
37}
38
39#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
40#[diesel(table_name = kv_transactions)]
41pub struct StoredTransaction {
42    pub tx_digest: Vec<u8>,
43    pub cp_sequence_number: i64,
44    pub timestamp_ms: i64,
45    pub raw_transaction: Vec<u8>,
46    pub raw_effects: Vec<u8>,
47    pub events: Vec<u8>,
48    pub user_signatures: Vec<u8>,
49}
50
51#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
52#[diesel(table_name = tx_affected_addresses)]
53pub struct StoredTxAffectedAddress {
54    pub tx_sequence_number: i64,
55    /// Address affected by the transaction, including the sender, the gas payer
56    /// and any recipients of objects.
57    pub affected: Vec<u8>,
58    pub sender: Vec<u8>,
59}
60
61#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
62#[diesel(table_name = tx_affected_objects)]
63pub struct StoredTxAffectedObject {
64    pub tx_sequence_number: i64,
65    /// Object affected by the transaction, including deleted, wrapped, mutated,
66    /// and created objects.
67    pub affected: Vec<u8>,
68    pub sender: Vec<u8>,
69}
70
71#[derive(Insertable, Selectable, Debug, Clone, FieldCount, Queryable)]
72#[diesel(table_name = tx_balance_changes)]
73pub struct StoredTxBalanceChange {
74    pub tx_sequence_number: i64,
75    pub balance_changes: Vec<u8>,
76}
77
78#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
79#[diesel(table_name = tx_calls)]
80pub struct StoredTxCalls {
81    pub package: Vec<u8>,
82    pub module: String,
83    pub function: String,
84    pub tx_sequence_number: i64,
85    pub sender: Vec<u8>,
86}
87
88#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
89#[diesel(table_name = tx_digests)]
90pub struct StoredTxDigest {
91    pub tx_sequence_number: i64,
92    pub tx_digest: Vec<u8>,
93}
94
95#[derive(AsExpression, FromSqlRow, Copy, Clone, Debug)]
96#[diesel(sql_type = SmallInt)]
97#[repr(i16)]
98pub enum StoredKind {
99    SystemTransaction = 0,
100    ProgrammableTransaction = 1,
101}
102
103#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
104#[diesel(table_name = tx_kinds)]
105pub struct StoredTxKind {
106    pub tx_sequence_number: i64,
107    pub tx_kind: StoredKind,
108}
109
110impl<DB: Backend> serialize::ToSql<SmallInt, DB> for StoredKind
111where
112    i16: serialize::ToSql<SmallInt, DB>,
113{
114    fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, DB>) -> serialize::Result {
115        match self {
116            StoredKind::SystemTransaction => 0.to_sql(out),
117            StoredKind::ProgrammableTransaction => 1.to_sql(out),
118        }
119    }
120}
121
122impl<DB: Backend> deserialize::FromSql<SmallInt, DB> for StoredKind
123where
124    i16: deserialize::FromSql<SmallInt, DB>,
125{
126    fn from_sql(raw: DB::RawValue<'_>) -> deserialize::Result<Self> {
127        Ok(match i16::from_sql(raw)? {
128            0 => StoredKind::SystemTransaction,
129            1 => StoredKind::ProgrammableTransaction,
130            k => return Err(format!("Unexpected StoredTxKind: {k}").into()),
131        })
132    }
133}