Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schema to the data contracts #58

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/api/src/controllers/DataContractsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DataContractsController {
const {identifier} = request.params

const rows = await this.knex('data_contracts')
.select('data_contracts.identifier as identifier')
.select('data_contracts.identifier as identifier', 'data_contracts.schema as schema')
.where('data_contracts.identifier', identifier);

const [row] = rows
Expand All @@ -28,7 +28,7 @@ class DataContractsController {
response.status(404).send({message: 'not found'})
}

response.send({identifier: row.identifier});
response.send({identifier: row.identifier, schema: row.schema});
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import TransactionRoute, {
loader as transactionLoader,
} from "./routes/transaction/transaction.route";
import DataContractRoute, {loader as dataContractLoader} from "./routes/dataContract/data.contract.route";
import TransactionsRoute from "./routes/transactions/tranactions.route";

const router = createBrowserRouter([
{
Expand Down Expand Up @@ -50,10 +49,6 @@ const router = createBrowserRouter([
element: <DataContractRoute/>,
loader: dataContractLoader,
},
{
path: "transactions",
element: <TransactionsRoute/>,
},
]
}
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import {useLoaderData} from "react-router-dom";
import * as Api from "../../util/Api";
import './data_contract.css'

export async function loader({params}) {
const {identifier} = params
Expand All @@ -12,9 +13,12 @@ function DataContractRoute() {

return (
<div className="container">
<div className={""}>
<div className={"data_contract_identifier"}>
Identifier: {dataContract.identifier}
</div>
<div className={"data_contract_schema"}>
{JSON.stringify(dataContract.schema, null, 2)}
</div>
</div>
);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/frontend/src/routes/dataContract/data_contract.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.data_contract_identifier {
margin-top: 20px;
background-color: #9a3a3a;
padding: 10px;
}

.data_contract_schema {
margin-top: 20px;
background-color: #9a5c3a;
padding: 10px;
white-space: break-spaces;
height: 80%;
}

13 changes: 0 additions & 13 deletions packages/frontend/src/routes/dataContracts/data.contracts.route.js

This file was deleted.

13 changes: 0 additions & 13 deletions packages/frontend/src/routes/transactions/tranactions.route.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/indexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ refinery = { version = "0.8.10", features = ["postgres"] }
sha256 = "1.4.0"
dotenv = "0.15.0"
chrono = { version = "0.4.31", features = ["serde", "rustc-serialize"] }
tokio-postgres = { version="0.7.10", features=["with-chrono-0_4"] }
tokio-postgres = { version="0.7.10", features=["with-chrono-0_4", "with-serde_json-1"] }
time = { version="0.3.29", features=["serde"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE data_contracts
ADD COLUMN "schema" jsonb not null;
12 changes: 9 additions & 3 deletions packages/indexer/src/processor/psql/dao/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::env;
use std::time::SystemTime;
use deadpool_postgres::{Config, Manager, ManagerConfig, Pool, PoolError, RecyclingMethod, Runtime, tokio_postgres, Transaction};
Expand All @@ -10,6 +11,9 @@ use crate::models::{TDBlock, TDBlockHeader};
use sha256::{digest, try_digest};
use base64::{Engine as _, engine::{general_purpose}};
use chrono::{DateTime, Utc};
use dpp::platform_value::Value;
use tokio_postgres::types::{Format, IsNull, ToSql, Type};
use tokio_postgres::types::private::BytesMut;
use crate::entities::block_header::BlockHeader;

pub struct PostgresDAO {
Expand Down Expand Up @@ -52,11 +56,14 @@ impl PostgresDAO {
let id = state_transition.data_contract().id();
let id_str = id.to_string(Encoding::Base58);

let query = "INSERT INTO data_contracts(identifier) VALUES ($1);";
let schema = state_transition.data_contract().document_schemas().clone();
let schema_decoded = serde_json::to_value(schema).unwrap();

let query = "INSERT INTO data_contracts(identifier, schema) VALUES ($1, $2);";

let client = self.connection_pool.get().await.unwrap();
let stmt = client.prepare_cached(query).await.unwrap();
client.query(&stmt, &[&id_str]).await.unwrap();
client.query(&stmt, &[&id_str, &schema_decoded]).await.unwrap();
}

pub async fn get_latest_block(&self) -> i32 {
Expand Down Expand Up @@ -94,4 +101,3 @@ impl PostgresDAO {
return block_id;
}
}