Skip to content

Commit

Permalink
style: fix validations response from pallas and better ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mikarasv committed May 10, 2024
1 parent 8da804d commit 73b4110
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 166 deletions.
2 changes: 1 addition & 1 deletion napi-pallas/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface SectionValidation {
validations: Validations
}
export function safeParseTx(raw: string, context: ValidationContext): SectionValidation
export function getLatestParams(network: string): ProtocolParams
export function getLatestParameters(network: string): ProtocolParams
export interface Validation {
name: string
value: boolean
Expand Down
4 changes: 2 additions & 2 deletions napi-pallas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { parseAddress, safeParseBlock, safeParseTx, getLatestParams } = nativeBinding
const { parseAddress, safeParseBlock, safeParseTx, getLatestParameters } = nativeBinding

module.exports.parseAddress = parseAddress
module.exports.safeParseBlock = safeParseBlock
module.exports.safeParseTx = safeParseTx
module.exports.getLatestParams = getLatestParams
module.exports.getLatestParameters = getLatestParameters
2 changes: 1 addition & 1 deletion napi-pallas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn safe_parse_tx(raw: String, context: ValidationContext) -> SectionValidati

#[tokio::main]
#[napi]
pub async fn get_latest_params(network: String) -> ProtocolParams {
pub async fn get_latest_parameters(network: String) -> ProtocolParams {
match tx::get_epochs_latest_parameters(network).await {
Ok(params) => params,
Err(_) => ProtocolParams::new(),
Expand Down
9 changes: 5 additions & 4 deletions napi-pallas/src/validations/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn validate_alonzo_output_val_size(mtx_a: &MintedTx, prot_pps: &AlonzoProtParams
"The size of the value in each of the transaction outputs (regular outputs and collateral outputs) is not greater than the maximum allowed".to_string(),
);
return Validation::new()
.with_name("Minimum lovelace".to_string())
.with_name("Outputs value size".to_string())
.with_value(res.is_ok())
.with_description(description);
}
Expand All @@ -127,10 +127,11 @@ fn validate_alonzo_tx_ex_units(mtx_a: &MintedTx, prot_pps: &AlonzoProtParams) ->
let res = check_tx_ex_units(mtx_a, &prot_pps);
let description = set_description(
&res,
"The size of the value in each of the transaction outputs (regular outputs and collateral outputs) is not greater than the maximum allowed".to_string(),
"The number of execution units of the transaction should not exceed the maximum allowed"
.to_string(),
);
return Validation::new()
.with_name("Minimum lovelace".to_string())
.with_name("Execution units".to_string())
.with_value(res.is_ok())
.with_description(description);
}
Expand All @@ -142,7 +143,7 @@ fn validate_alonzo_languages(mtx_a: &MintedTx, prot_pps: &AlonzoProtParams) -> V
"The required script languages are included in the protocol parameters.".to_string(),
);
return Validation::new()
.with_name("Minimum lovelace".to_string())
.with_name("Languages".to_string())
.with_value(res.is_ok())
.with_description(description);
}
Expand Down
42 changes: 41 additions & 1 deletion napi-pallas/src/validations/babbage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{Validation, ValidationContext, Validations};
use blockfrost::{BlockFrostSettings, BlockfrostAPI};
use blockfrost_openapi::models::tx_content_utxo_outputs_inner::TxContentUtxoOutputsInner;
use dotenv::dotenv;
use pallas::{
applying::{
babbage::{
Expand Down Expand Up @@ -253,7 +256,7 @@ fn validate_babbage_script_data_hash(
"The Plutus scripts and native scripts of the transaction are valid.".to_string(),
);
return Validation::new()
.with_name("Languages".to_string())
.with_name("Script data hash".to_string())
.with_value(res.is_ok())
.with_description(description);
}
Expand Down Expand Up @@ -287,6 +290,40 @@ fn validate_babbage_network_id(mtx: &BabbageMintedTx, network_id: u8) -> Validat
.with_description(description);
}

use std::env;

pub async fn get_input_from_index(
hash: String,
network: String,
index: i32,
) -> Option<TxContentUtxoOutputsInner> {
let settings = BlockFrostSettings::new();
dotenv().ok();
let mut project_id = env::var("MAINNET_PROJECT_ID").expect("MAINNET_PROJECT_ID must be set.");
if network == "Preprod" {
project_id = env::var("PREPROD_PROJECT_ID").expect("PREPROD_PROJECT_ID must be set.");
} else if network == "Preview" {
project_id = env::var("PREVIEW_PROJECT_ID").expect("PREVIEW_PROJECT_ID must be set.");
}

let api = BlockfrostAPI::new(&project_id, settings);
let tx = api.transactions_utxos(&hash).await;
print!("{:?}", tx);
match tx {
Ok(tx_) => {
let outputs = tx_.outputs;
outputs.iter().find_map(|output| {
if output.output_index == index {
Some(output.clone())
} else {
None
}
})
}
Err(_) => None,
}
}

pub fn validate_babbage(mtx_b: &BabbageMintedTx, context: ValidationContext) -> Validations {
let tx_body: &MintedTransactionBody = &mtx_b.transaction_body.clone();
let ppt_params = context.protocol_params;
Expand Down Expand Up @@ -373,6 +410,9 @@ pub fn validate_babbage(mtx_b: &BabbageMintedTx, context: ValidationContext) ->
network_id: net_id,
};

let inputs = mtx_b.transaction_body.inputs.clone();
let mut utxos: UTxOs = UTxOs::new();

let out = Validations::new()
.with_era(context.era.to_string())
.add_new_validation(validate_babbage_ins_not_empty(&mtx_b))
Expand Down
4 changes: 1 addition & 3 deletions napi-pallas/src/validations/conway.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use pallas::ledger::primitives::conway::MintedTx;

use crate::Validations;
pub fn validate_conway(mtx_c: &MintedTx) -> Validations {
pub fn validate_conway() -> Validations {
let out = Validations::new()
.with_era("Conway".to_string())
.add_new_validation(crate::Validation {
Expand Down
2 changes: 1 addition & 1 deletion napi-pallas/src/validations/shelley_ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn validate_shelley_ma_witnesses(
let res = check_witnesses(&tx_body, &tx_wits, &utxos);
let description = set_description(
&res,
" The owner of each transaction input signed the transaction.".to_string(),
"The owner of each transaction input signed the transaction.".to_string(),
);
return Validation::new()
.with_name("Witnesses".to_string())
Expand Down
2 changes: 1 addition & 1 deletion napi-pallas/src/validations/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn validate(mtx: &MultiEraTx<'_>, context: ValidationContext) -> Validations
}
MultiEraTx::AlonzoCompatible(mtx_a, Era::Alonzo) => validate_alonzo(&mtx_a, context),
MultiEraTx::Babbage(mtx_b) => validate_babbage(&mtx_b, context),
MultiEraTx::Conway(mtx_c) => validate_conway(&mtx_c),
MultiEraTx::Conway(_) => validate_conway(),
// This case is impossible. TODO: Handle error
_ => Validations::new(),
}
Expand Down
6 changes: 3 additions & 3 deletions web/app/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
color?: "blue" | "pink";
}

export const Button = ({
export function Button({
type = "button",
children,
className: customClassName,
color = "blue",
onClick: onClickFn,
}: ButtonProps) => {
}: ButtonProps) {
return (
<button
onClick={onClickFn}
Expand All @@ -30,4 +30,4 @@ export const Button = ({
{children}
</button>
);
};
}
6 changes: 3 additions & 3 deletions web/app/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
isCheckbox?: boolean;
}

export const Input = ({
export function Input({
name,
disabled,
id,
Expand All @@ -21,7 +21,7 @@ export const Input = ({
label,
isCheckbox = false,
checked,
}: InputProps) => {
}: InputProps) {
if (isCheckbox) {
return (
<div className="w-full text-left flex justify-between p-2">
Expand Down Expand Up @@ -69,4 +69,4 @@ export const Input = ({
}`}
/>
);
};
}
45 changes: 21 additions & 24 deletions web/app/components/RootSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import { getTopicMeta } from "~/utils";
import { HexBlock, PropBlock, TopicMeta } from "./constructors";
import { DataSection, ValidationInformation } from "./index";

export function RootSection(props: {
export function RootSection({
data,
topics,
era,
uiConfigs,
}: {
data: Section;
topics: Record<string, TopicMeta>;
era: EraType;
uiConfigs: IUiConfigs;
era?: EraType;
uiConfigs?: IUiConfigs;
}) {
const goesBeginning = props.uiConfigs.beginning;
const topic = getTopicMeta(props.data.topic, props.topics);
const goesBeginning = uiConfigs?.beginning;
const topic = getTopicMeta(data.topic, topics);

if (props.data.error)
if (data.error)
return (
<>
<div className="block mt-8 p-4 border-2 bg-red-200 border-red-700 shadow shadow-black rounded-lg text-2xl">
<h4 className="text-3xl">{topic.description}</h4>
{props.data.error}
{data.error}
<br />
Try other network or try checking your cbor.
</div>
Expand All @@ -32,27 +37,19 @@ export function RootSection(props: {

return (
<div className="flex flex-col">
{goesBeginning && (
<ValidationInformation
era={props.era}
initialOpen={props.uiConfigs.alwaysOpen}
/>
{goesBeginning && era && uiConfigs && (
<ValidationInformation era={era} initialOpen={uiConfigs.alwaysOpen} />
)}
<h4 className="text-3xl">{topic.title}</h4>
{!!props.data.bytes && (
<HexBlock name="bytes (hex)" value={props.data.bytes} />
)}
{props.data.attributes?.map((c) => (
<PropBlock key={c.topic} data={c} topics={props.topics} />
{!!data.bytes && <HexBlock name="bytes (hex)" value={data.bytes} />}
{data.attributes?.map((c) => (
<PropBlock key={c.topic} data={c} topics={topics} />
))}
{props.data.children?.map((c) => (
<DataSection key={c.identity} data={c} topics={props.topics} />
{data.children?.map((c) => (
<DataSection key={c.identity} data={c} topics={topics} />
))}
{!goesBeginning && (
<ValidationInformation
era={props.era}
initialOpen={props.uiConfigs.alwaysOpen}
/>
{!goesBeginning && era && uiConfigs && (
<ValidationInformation era={era} initialOpen={uiConfigs.alwaysOpen} />
)}
</div>
);
Expand Down
41 changes: 36 additions & 5 deletions web/app/components/Validations/Configurations/Configurations.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { useLoaderData } from "@remix-run/react";
import {
Dispatch,
SetStateAction,
useContext,
useEffect,
useState,
} from "react";
import { Button } from "~/components/Button";
import { IProtocolParam, IUiConfigs, TabNames, TabType } from "~/interfaces";
import { ValidationsContext } from "~/contexts/validations.context";
import {
IProtocolParam,
IUiConfigs,
Networks,
TabNames,
TabType,
} from "~/interfaces";
import { loader } from "~/routes/tx";
import { paramsParser } from "~/utils";
import { ContextTab } from "./ContextTab";
import { UITab } from "./UITab";

interface ConfigsModalProps {
closeModal: () => void;
latestParams: IProtocolParam[] | undefined;
uiConfigs: IUiConfigs;
setUiConfigs: Dispatch<SetStateAction<IUiConfigs>>;
}

export function ConfigsModal({
closeModal,
latestParams,
uiConfigs,
setUiConfigs,
}: ConfigsModalProps) {
const { context } = useContext(ValidationsContext);

const tabs: TabType[] = [TabNames.Context, TabNames.UI_Options];
const [selected, setSelected] = useState<TabType>(TabNames.Context);
const latestParams = useLoaderData<typeof loader>();
const [params, setParams] = useState<IProtocolParam[] | undefined>(undefined);

useEffect(() => {
if (latestParams) {
const parsedParams = JSON.parse(JSON.stringify(latestParams));
if (context.selectedNetwork === Networks.Mainnet)
paramsParser(parsedParams.mainnetParams, setParams);
if (context.selectedNetwork === Networks.Preprod)
paramsParser(parsedParams.preprodParams, setParams);
if (context.selectedNetwork === Networks.Preview)
paramsParser(parsedParams.previewParams, setParams);
}
}, [context.selectedNetwork]);

const changeSelected = (tab: TabType) => () => setSelected(tab);

Expand All @@ -45,6 +75,7 @@ export function ConfigsModal({
<button
className="absolute right-5 top-3 text-4xl cursor-pointer rotate-45 box-border"
onClick={closeModal}
type="button"
>
+
</button>
Expand All @@ -70,7 +101,7 @@ export function ConfigsModal({
selected == TabNames.Context ? "block" : "hidden"
}`}
>
<ContextTab latestParams={latestParams} />
<ContextTab latestParams={params} />
</div>

<div
Expand Down
Loading

0 comments on commit 73b4110

Please sign in to comment.