Skip to content

Commit

Permalink
chore: add same conditions for outputs.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Jan 29, 2024
1 parent 4dc9464 commit 12a93e8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TransactionPayload extends AsyncComponent<TransactionPayloadProps, Transac
*/
public render(): ReactNode {
const { network, inputs, unlocks, outputs, header, isLinksDisabled } = this.props;
// console.log('--- !!!!!!inputs', inputs[1].unlockConditionOpenedIndexes);
console.log('--- outputs', outputs);
return (
<div className="transaction-payload">
{header && (
Expand Down Expand Up @@ -92,7 +92,7 @@ class TransactionPayload extends AsyncComponent<TransactionPayloadProps, Transac
showCopyAmount={true}
isPreExpanded={true}
isLinksDisabled={isLinksDisabled}
unlockConditionOpenedIndexes={[]}
unlockConditionOpenedIndexes={output.unlockConditionOpenedIndexes}
/>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface TransactionPayloadProps {
/**
* The outputs.
*/
outputs: IOutput[];
outputs: (IOutput & { unlockConditionOpenedIndexes?: number[] })[];

/**
* The header title of this section.
Expand Down
50 changes: 42 additions & 8 deletions client/src/app/routes/stardust/TransactionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { IInput } from "~models/api/stardust/IInput";
import { OutputResponse, ExpirationUnlockCondition } from "@iota/sdk-wasm/web";
import { DateHelper } from "~helpers/dateHelper";
import { HexHelper } from "~helpers/stardust/hexHelper";
import { IOutput } from "~models/api/stardust/IOutput";

enum TRANSACTION_PAGE_TABS {
Payload = "Payload",
Expand All @@ -61,6 +62,19 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
const [isFormattedBalance, setIsFormattedBalance] = useState(true);
const [inputsExtraInfo, setInputsExtraInfo] = useState<{ [outputId: string]: { unlockConditionOpenedIndexes?: number[] } }>({});

const outputsWithExtraInfo = React.useMemo(() => {
if (!outputs) return null;
return outputs.map((output, idx) => {
const extraInfo = inputsExtraInfo[output.id];
console.log('--- extraInfo', extraInfo);
return {
...output,
unlockConditionOpenedIndexes: extraInfo?.unlockConditionOpenedIndexes ?? [],
};
});
}, [outputs, inputsExtraInfo]);


const inputsWithExtraInfo = React.useMemo(() => {
if (!inputs) return null;
return inputs.map((input, idx) => {
Expand All @@ -86,12 +100,13 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({

const expirationUnlockCondition = getUnlockCondition(input, UnlockConditionType.Expiration) as ExpirationUnlockCondition;
const expirationConditionTimestamp = expirationUnlockCondition.unixTime * 1000;
const commonOutput = input.output?.output as CommonOutput;

if (!isOutputSpent(input)) {
const isExpired = DateHelper.isExpired(expirationConditionTimestamp);
const indexes: number[] = isExpired ?
getUnlockConditionIndexes(input, UnlockConditionType.Expiration) :
getUnlockConditionIndexes(input, UnlockConditionType.Address);
getUnlockConditionIndexes(commonOutput, UnlockConditionType.Expiration) :
getUnlockConditionIndexes(commonOutput, UnlockConditionType.Address);

setInputsExtraInfo((prev) => ({
...prev,
Expand All @@ -111,8 +126,8 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
const isSpentAfterUnlock = DateHelper.isExpired(expirationConditionTimestamp, transactionTimestamp);

const indexes = isSpentAfterUnlock ?
getUnlockConditionIndexes(input, UnlockConditionType.Expiration) :
getUnlockConditionIndexes(input, UnlockConditionType.Address);
getUnlockConditionIndexes(commonOutput, UnlockConditionType.Expiration) :
getUnlockConditionIndexes(commonOutput, UnlockConditionType.Address);

setInputsExtraInfo((prev) => ({
...prev,
Expand All @@ -122,6 +137,17 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
}));
};

const updateOutputExtraInfo = async (output: IOutput) => {
const indexes = getUnlockConditionIndexes(output?.output as CommonOutput, UnlockConditionType.Address);
console.log('--- indexes', indexes);
setInputsExtraInfo((prev) => ({
...prev,
[output.id]: {
unlockConditionOpenedIndexes: indexes,
},
}));
};

useEffect(() => {
if (block?.payload?.type === PayloadType.Transaction) {
const transactionPayload = block.payload as ITransactionPayload;
Expand All @@ -143,6 +169,15 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
);
})();
}, [inputs, setInputsExtraInfo]);
useEffect(() => {
(async () => {
if (!outputs) return;

outputs.map((output) => {
updateOutputExtraInfo(output);
});
})();
}, [outputs, setInputsExtraInfo]);

const { metadata, metadataError, conflictReason, blockTangleStatus } = blockMetadata;
const isLinksDisabled = metadata?.ledgerInclusionState === "conflicting";
Expand Down Expand Up @@ -243,9 +278,9 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
},
}}
>
{inputsWithExtraInfo && unlocks && outputs ? (
{inputsWithExtraInfo && unlocks && outputsWithExtraInfo ? (
<div className="section">
<TransactionPayload network={network} inputs={inputsWithExtraInfo} unlocks={unlocks} outputs={outputs} />
<TransactionPayload network={network} inputs={inputsWithExtraInfo} unlocks={unlocks} outputs={outputsWithExtraInfo} />
</div>
) : (
<></>
Expand Down Expand Up @@ -342,8 +377,7 @@ function getUnlockCondition(input: IInput, type: UnlockConditionType) {
return output.unlockConditions.find((i) => i.type === type);
}

function getUnlockConditionIndexes(input: IInput, type: UnlockConditionType) {
const output = input.output?.output as CommonOutput;
function getUnlockConditionIndexes(output: CommonOutput, type: UnlockConditionType) {
if (!output?.unlockConditions) return [];

return output.unlockConditions
Expand Down

0 comments on commit 12a93e8

Please sign in to comment.