-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Basic support for Anchor address page (#1093)
* chore: Extract AddressNotFound to own component * feat: Make AddressPage.tsx an entrypoint for address pages. Refactor useAddressPageState to be Account address specific. Add AccountAddressView. * feat: Add support for Ed25519 address page (state hook and tsx component) * fix: Fix OutputPage for Nft and Account outputs * feat: Support Nft address page (basic). Add custom hooks for nft address page and load nft output details. * feat: Support Anchor address page (basic). Add custom hooks for anchor address page and load anchor output details. * feat: Add Anchor address support in bech32AddressHelper
- Loading branch information
Showing
13 changed files
with
277 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface IAnchorDetailsRequest { | ||
/** | ||
* The network to search on. | ||
*/ | ||
network: string; | ||
|
||
/** | ||
* The anchor id to get the anchor output details for. | ||
*/ | ||
anchorId: string; | ||
} |
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,11 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
import { OutputResponse } from "@iota/sdk-nova"; | ||
import { IResponse } from "./IResponse"; | ||
|
||
export interface IAnchorDetailsResponse extends IResponse { | ||
/** | ||
* The anchor details response. | ||
*/ | ||
anchorOutputDetails?: OutputResponse; | ||
} |
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,29 @@ | ||
import { ServiceFactory } from "../../../factories/serviceFactory"; | ||
import { IAnchorDetailsRequest } from "../../../models/api/nova/IAnchorDetailsRequest"; | ||
import { IAnchorDetailsResponse } from "../../../models/api/nova/IAnchorDetailsResponse"; | ||
import { IConfiguration } from "../../../models/configuration/IConfiguration"; | ||
import { NOVA } from "../../../models/db/protocolVersion"; | ||
import { NetworkService } from "../../../services/networkService"; | ||
import { NovaApiService } from "../../../services/nova/novaApiService"; | ||
import { ValidationHelper } from "../../../utils/validationHelper"; | ||
|
||
/** | ||
* Get anchor output details by Anchor id | ||
* @param config The configuration. | ||
* @param request The request. | ||
* @returns The response. | ||
*/ | ||
export async function get(config: IConfiguration, request: IAnchorDetailsRequest): Promise<IAnchorDetailsResponse> { | ||
const networkService = ServiceFactory.get<NetworkService>("network"); | ||
const networks = networkService.networkNames(); | ||
ValidationHelper.oneOf(request.network, networks, "network"); | ||
ValidationHelper.string(request.anchorId, "anchorId"); | ||
|
||
const networkConfig = networkService.get(request.network); | ||
|
||
if (networkConfig.protocolVersion !== NOVA) { | ||
return {}; | ||
} | ||
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`); | ||
return novaApiService.anchorDetails(request.anchorId); | ||
} |
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
52 changes: 52 additions & 0 deletions
52
client/src/app/components/nova/address/AnchorAddressView.tsx
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,52 @@ | ||
import { AnchorAddress } from "@iota/sdk-wasm-nova/web"; | ||
import React from "react"; | ||
import { useAnchorAddressState } from "~/helpers/nova/hooks/useAnchorAddressState"; | ||
import Spinner from "../../Spinner"; | ||
import Bech32Address from "../../stardust/address/Bech32Address"; | ||
import AssociatedOutputs from "./section/association/AssociatedOutputs"; | ||
|
||
interface AnchorAddressViewProps { | ||
anchorAddress: AnchorAddress; | ||
} | ||
|
||
const AnchorAddressView: React.FC<AnchorAddressViewProps> = ({ anchorAddress }) => { | ||
const { anchorAddressDetails, isAnchorDetailsLoading } = useAnchorAddressState(anchorAddress); | ||
const isPageLoading = isAnchorDetailsLoading; | ||
|
||
return ( | ||
<div className="address-page"> | ||
<div className="wrapper"> | ||
{anchorAddressDetails && ( | ||
<div className="inner"> | ||
<div className="addr--header"> | ||
<div className="row middle"> | ||
<h1>{anchorAddressDetails.typeLabel?.replace("Ed25519", "Address")}</h1> | ||
</div> | ||
{isPageLoading && <Spinner />} | ||
</div> | ||
<div className="section no-border-bottom padding-b-0"> | ||
<div className="section--header"> | ||
<div className="row middle"> | ||
<h2>General</h2> | ||
</div> | ||
</div> | ||
<div className="general-content"> | ||
<div className="section--data"> | ||
<Bech32Address addressDetails={anchorAddressDetails} advancedMode={true} /> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="section no-border-bottom padding-b-0"> | ||
<div className="row middle"> | ||
<h2>Associated Outputs</h2> | ||
</div> | ||
<AssociatedOutputs addressDetails={anchorAddressDetails} /> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AnchorAddressView; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Reducer, useEffect, useReducer } from "react"; | ||
import { AnchorAddress, AnchorOutput } from "@iota/sdk-wasm-nova/web"; | ||
import { IBech32AddressDetails } from "~/models/api/IBech32AddressDetails"; | ||
import { useAnchorDetails } from "./useAnchorDetails"; | ||
import { useLocation, useParams } from "react-router-dom"; | ||
import { AddressRouteProps } from "~/app/routes/AddressRouteProps"; | ||
import { useNetworkInfoNova } from "../networkInfo"; | ||
import { Bech32AddressHelper } from "~/helpers/nova/bech32AddressHelper"; | ||
|
||
export interface IAnchorAddressState { | ||
anchorAddressDetails: IBech32AddressDetails | null; | ||
anchorOutput: AnchorOutput | null; | ||
isAnchorDetailsLoading: boolean; | ||
} | ||
|
||
const initialState = { | ||
anchorAddressDetails: null, | ||
anchorOutput: null, | ||
isAnchorDetailsLoading: true, | ||
}; | ||
|
||
/** | ||
* Route Location Props | ||
*/ | ||
interface IAddressPageLocationProps { | ||
addressDetails: IBech32AddressDetails; | ||
} | ||
|
||
export const useAnchorAddressState = (address: AnchorAddress): IAnchorAddressState => { | ||
const location = useLocation(); | ||
const { network } = useParams<AddressRouteProps>(); | ||
const { bech32Hrp } = useNetworkInfoNova((s) => s.networkInfo); | ||
const [state, setState] = useReducer<Reducer<IAnchorAddressState, Partial<IAnchorAddressState>>>( | ||
(currentState, newState) => ({ ...currentState, ...newState }), | ||
initialState, | ||
); | ||
|
||
const { anchorOutput, isLoading: isAnchorDetailsLoading } = useAnchorDetails(network, address.anchorId); | ||
|
||
useEffect(() => { | ||
const locationState = location.state as IAddressPageLocationProps; | ||
const { addressDetails } = locationState?.addressDetails | ||
? locationState | ||
: { addressDetails: Bech32AddressHelper.buildAddress(bech32Hrp, address) }; | ||
|
||
setState({ | ||
...initialState, | ||
anchorAddressDetails: addressDetails, | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setState({ | ||
anchorOutput, | ||
isAnchorDetailsLoading, | ||
}); | ||
}, [anchorOutput, isAnchorDetailsLoading]); | ||
|
||
return { | ||
anchorAddressDetails: state.anchorAddressDetails, | ||
anchorOutput: state.anchorOutput, | ||
isAnchorDetailsLoading: state.isAnchorDetailsLoading, | ||
}; | ||
}; |
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,48 @@ | ||
import { AnchorOutput } from "@iota/sdk-wasm-nova/web"; | ||
import { useEffect, useState } from "react"; | ||
import { ServiceFactory } from "~/factories/serviceFactory"; | ||
import { useIsMounted } from "~/helpers/hooks/useIsMounted"; | ||
import { HexHelper } from "~/helpers/stardust/hexHelper"; | ||
import { NOVA } from "~/models/config/protocolVersion"; | ||
import { NovaApiClient } from "~/services/nova/novaApiClient"; | ||
|
||
/** | ||
* Fetch anchor output details | ||
* @param network The Network in context | ||
* @param anchorID The anchor id | ||
* @returns The output response and loading bool. | ||
*/ | ||
export function useAnchorDetails(network: string, anchorId: string | null): { anchorOutput: AnchorOutput | null; isLoading: boolean } { | ||
const isMounted = useIsMounted(); | ||
const [apiClient] = useState(ServiceFactory.get<NovaApiClient>(`api-client-${NOVA}`)); | ||
const [anchorOutput, setAnchorOutput] = useState<AnchorOutput | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
if (anchorId) { | ||
// eslint-disable-next-line no-void | ||
void (async () => { | ||
apiClient | ||
.anchorDetails({ | ||
network, | ||
anchorId: HexHelper.addPrefix(anchorId), | ||
}) | ||
.then((response) => { | ||
if (!response?.error && isMounted) { | ||
const output = response.anchorOutputDetails?.output as AnchorOutput; | ||
|
||
setAnchorOutput(output); | ||
} | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
})(); | ||
} else { | ||
setIsLoading(false); | ||
} | ||
}, [network, anchorId]); | ||
|
||
return { anchorOutput, isLoading }; | ||
} |
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,11 @@ | ||
export interface IAnchorDetailsRequest { | ||
/** | ||
* The network to search on. | ||
*/ | ||
network: string; | ||
|
||
/** | ||
* The anchor id to get the anchor output details for. | ||
*/ | ||
anchorId: string; | ||
} |
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,9 @@ | ||
import { OutputResponse } from "@iota/sdk-wasm-nova/web"; | ||
import { IResponse } from "../IResponse"; | ||
|
||
export interface IAnchorDetailsResponse extends IResponse { | ||
/** | ||
* The anchor details response. | ||
*/ | ||
anchorOutputDetails?: OutputResponse; | ||
} |
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