-
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: Include output mana rewards in Output page nova
- Loading branch information
Showing
11 changed files
with
195 additions
and
5 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 IRewardsRequest { | ||
/** | ||
* The network to search on. | ||
*/ | ||
network: string; | ||
|
||
/** | ||
* The output id to get the rewards for. | ||
*/ | ||
outputId: 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,14 @@ | ||
import { ManaRewardsResponse } from "@iota/sdk-nova"; | ||
import { IResponse } from "./IResponse"; | ||
|
||
export interface IRewardsResponse extends IResponse { | ||
/** | ||
* The output Id. | ||
*/ | ||
outputId?: string; | ||
|
||
/** | ||
* The output mana rewards. | ||
*/ | ||
manaRewards?: ManaRewardsResponse; | ||
} |
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 { IRewardsRequest } from "../../../../models/api/nova/IRewardsRequest"; | ||
import { IRewardsResponse } from "../../../../models/api/nova/IRewardsResponse"; | ||
import { IConfiguration } from "../../../../models/configuration/IConfiguration"; | ||
import { NOVA } from "../../../../models/db/protocolVersion"; | ||
import { NetworkService } from "../../../../services/networkService"; | ||
import { NovaApi } from "../../../../services/nova/novaApi"; | ||
import { ValidationHelper } from "../../../../utils/validationHelper"; | ||
|
||
/** | ||
* Get the output rewards. | ||
* @param config The configuration. | ||
* @param request The request. | ||
* @returns The response. | ||
*/ | ||
export async function get(config: IConfiguration, request: IRewardsRequest): Promise<IRewardsResponse> { | ||
const networkService = ServiceFactory.get<NetworkService>("network"); | ||
const networks = networkService.networkNames(); | ||
ValidationHelper.oneOf(request.network, networks, "network"); | ||
ValidationHelper.string(request.outputId, "outputId"); | ||
|
||
const networkConfig = networkService.get(request.network); | ||
|
||
if (networkConfig.protocolVersion !== NOVA) { | ||
return {}; | ||
} | ||
|
||
return NovaApi.getRewards(networkConfig, request.outputId); | ||
} |
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,57 @@ | ||
import { ManaRewardsResponse } from "@iota/sdk-wasm-nova/web"; | ||
import { useEffect, useState } from "react"; | ||
import { ServiceFactory } from "~/factories/serviceFactory"; | ||
import { useIsMounted } from "~/helpers/hooks/useIsMounted"; | ||
import { NOVA } from "~/models/config/protocolVersion"; | ||
import { NovaApiClient } from "~/services/nova/novaApiClient"; | ||
|
||
/** | ||
* Fetch output mana rewards for a given output. | ||
* @param network The Network in context | ||
* @param outputId The output id | ||
* @param slotIndex The slot index | ||
* @returns The output, metadata, loading bool and error message. | ||
**/ | ||
export function useOutputManaRewards( | ||
network: string, | ||
outputId: string, | ||
slotIndex?: number, | ||
): { | ||
manaRewards: ManaRewardsResponse | null; | ||
isLoading: boolean; | ||
error: string | null; | ||
} { | ||
const isMounted = useIsMounted(); | ||
const [apiClient] = useState(ServiceFactory.get<NovaApiClient>(`api-client-${NOVA}`)); | ||
const [manaRewards, setManaRewards] = useState<ManaRewardsResponse | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
setManaRewards(null); | ||
setError(null); | ||
|
||
if (outputId) { | ||
// eslint-disable-next-line no-void | ||
void (async () => { | ||
apiClient | ||
.getRewards({ network, outputId, slotIndex }) | ||
.then((response) => { | ||
if (isMounted) { | ||
const manaRewards = response.manaRewards; | ||
setError(response.error ?? null); | ||
setManaRewards(manaRewards ?? null); | ||
} | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
})(); | ||
} else { | ||
setIsLoading(false); | ||
} | ||
}, [network, outputId, slotIndex]); | ||
|
||
return { manaRewards, isLoading, error }; | ||
} |
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 |
---|---|---|
@@ -1,27 +1,35 @@ | ||
import { BasicOutput, Output, ProtocolParameters, Utils } from "@iota/sdk-wasm-nova/web"; | ||
import { BasicOutput, ManaRewardsResponse, Output, ProtocolParameters, Utils } from "@iota/sdk-wasm-nova/web"; | ||
|
||
export interface OutputManaDetails { | ||
storedMana: string; | ||
storedManaDecayed: string; | ||
potentialMana: string; | ||
totalMana: string; | ||
delegationRewards?: string | null; | ||
} | ||
|
||
export function buildManaDetailsForOutput( | ||
output: Output, | ||
createdSlotIndex: number, | ||
spentOrLatestSlotIndex: number, | ||
protocolParameters: ProtocolParameters, | ||
outputManaRewards: ManaRewardsResponse | null, | ||
): OutputManaDetails { | ||
const decayedMana = Utils.outputManaWithDecay(output, createdSlotIndex, spentOrLatestSlotIndex, protocolParameters); | ||
const storedManaDecayed = BigInt(decayedMana.stored).toString(); | ||
const potentialMana = BigInt(decayedMana.potential).toString(); | ||
const totalMana = BigInt(decayedMana.stored) + BigInt(decayedMana.potential); | ||
const delegationRewards = outputManaRewards && BigInt(outputManaRewards?.rewards) > 0 ? BigInt(outputManaRewards?.rewards) : null; | ||
let totalMana = BigInt(decayedMana.stored) + BigInt(decayedMana.potential); | ||
|
||
if (delegationRewards !== null) { | ||
totalMana += delegationRewards; | ||
} | ||
|
||
return { | ||
storedMana: (output as BasicOutput).mana?.toString(), | ||
storedManaDecayed, | ||
potentialMana, | ||
totalMana: totalMana.toString(), | ||
delegationRewards: delegationRewards !== null ? delegationRewards?.toString() : undefined, | ||
}; | ||
} |
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,16 @@ | ||
export interface IRewardsRequest { | ||
/** | ||
* The network to search on. | ||
*/ | ||
network: string; | ||
|
||
/** | ||
* The output id to get the rewards for. | ||
*/ | ||
outputId: string; | ||
|
||
/** | ||
* The slot index to use. | ||
*/ | ||
slotIndex?: number; | ||
} |
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,14 @@ | ||
import { ManaRewardsResponse } from "@iota/sdk-wasm-nova/web"; | ||
import { IResponse } from "./IResponse"; | ||
|
||
export interface IRewardsResponse extends IResponse { | ||
/** | ||
* The output Id. | ||
*/ | ||
outputId: string; | ||
|
||
/** | ||
* The output mana rewards. | ||
*/ | ||
manaRewards?: ManaRewardsResponse; | ||
} |
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