-
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: Support rewards mana on delegation output (#1017)
* feat: Add mana details to metadata section of Output page (stored with decay, potential, total) for spent and unspent outputs * feat: Include output mana rewards in Output page nova * feat: Cleanup IRewardsRequest interface * chore: Ignore more eslint import unresolved (nova CI) * fix: comments --------- Co-authored-by: Branko Bosnic <[email protected]>
- Loading branch information
Showing
11 changed files
with
186 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 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,16 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
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,30 @@ | ||
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 { NovaApiService } from "../../../../services/nova/novaApiService"; | ||
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 {}; | ||
} | ||
|
||
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`); | ||
return novaApiService.getRewards(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 mana rewards, 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 }) | ||
.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, | ||
delegationRewards: delegationRewards !== null ? delegationRewards?.toString() : undefined, | ||
totalMana: totalMana.toString(), | ||
}; | ||
} |
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,16 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
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