Skip to content

Commit

Permalink
Fix: mana calculation in output and address pages (#1464)
Browse files Browse the repository at this point in the history
* fix: Fix mana calculation in OutputPage and AddressPage (don't use included.slot number!)

* fix: Pass the slotIndex (spent) to getManaRewards

---------

Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
msarcev and begonaalvarezd authored May 8, 2024
1 parent e3d196e commit d3b4008
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions api/src/models/api/nova/IRewardsRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ export interface IRewardsRequest {
* The output id to get the rewards for.
*/
outputId: string;

/**
* The slot index to use as 'up to' to get the rewards for (like when the output was spent).
*/
slotIndex?: string;
}
6 changes: 3 additions & 3 deletions api/src/routes/nova/output/rewards/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { ValidationHelper } from "../../../../utils/validationHelper";

/**
* Get the output rewards.
* @param config The configuration.
* @param _ The configuration.
* @param request The request.
* @returns The response.
*/
export async function get(config: IConfiguration, request: IRewardsRequest): Promise<IRewardsResponse> {
export async function get(_: IConfiguration, request: IRewardsRequest): Promise<IRewardsResponse> {
const networkService = ServiceFactory.get<NetworkService>("network");
const networks = networkService.networkNames();
ValidationHelper.oneOf(request.network, networks, "network");
Expand All @@ -26,5 +26,5 @@ export async function get(config: IConfiguration, request: IRewardsRequest): Pro
}

const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`);
return novaApiService.getRewards(request.outputId);
return novaApiService.getRewards(request.outputId, request.slotIndex);
}
6 changes: 4 additions & 2 deletions api/src/services/nova/novaApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,12 @@ export class NovaApiService {
/**
* Get the output mana rewards.
* @param outputId The outputId to get the rewards for.
* @param slotIndex The slotIndex to get the rewards for.
* @returns The mana rewards.
*/
public async getRewards(outputId: string): Promise<IRewardsResponse> {
const manaRewardsResponse = await this.client.getOutputManaRewards(outputId);
public async getRewards(outputId: string, slotIndex?: string): Promise<IRewardsResponse> {
const parsedSlotIndex = slotIndex === undefined ? undefined : Number.parseInt(slotIndex, 10);
const manaRewardsResponse = await this.client.getOutputManaRewards(outputId, parsedSlotIndex);

return manaRewardsResponse ? { outputId, manaRewards: manaRewardsResponse } : { outputId, message: "Rewards data not found" };
}
Expand Down
5 changes: 3 additions & 2 deletions client/src/app/routes/nova/OutputPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useNetworkInfoNova } from "~/helpers/nova/networkInfo";
import { buildManaDetailsForOutput, OutputManaDetails } from "~/helpers/nova/manaUtils";
import { Converter } from "~/helpers/stardust/convertUtils";
import { useOutputManaRewards } from "~/helpers/nova/hooks/useOutputManaRewards";
import { Utils } from "@iota/sdk-wasm-nova/web";
import "./OutputPage.scss";

interface OutputPageProps {
Expand All @@ -31,7 +32,7 @@ const OutputPage: React.FC<RouteComponentProps<OutputPageProps>> = ({
},
}) => {
const { output, outputMetadataResponse, error } = useOutputDetails(network, outputId);
const { manaRewards } = useOutputManaRewards(network, outputId);
const { manaRewards } = useOutputManaRewards(network, outputId, outputMetadataResponse?.spent?.slot);
const { protocolInfo, latestConfirmedSlot } = useNetworkInfoNova((s) => s.networkInfo);

if (error) {
Expand All @@ -54,7 +55,7 @@ const OutputPage: React.FC<RouteComponentProps<OutputPageProps>> = ({
const { blockId, included, spent } = outputMetadataResponse ?? {};

const transactionId = included?.transactionId ?? null;
const createdSlotIndex = (included?.slot as number) ?? null;
const createdSlotIndex = transactionId ? Utils.computeSlotIndex(transactionId) : null;
const spentSlotIndex = (spent?.slot as number) ?? null;
const isSpent = spentSlotIndex !== null;
const transactionIdSpent = spent?.transactionId ?? null;
Expand Down
6 changes: 5 additions & 1 deletion client/src/helpers/nova/hooks/useAccountAddressState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ export const useAccountAddressState = (address: AccountAddress): [IAccountAddres
);

const { accountOutput, accountOutputMetadata, isLoading: isAccountDetailsLoading } = useAccountDetails(network, address.accountId);
const { manaRewards: outputManaRewards } = useOutputManaRewards(network, accountOutputMetadata?.outputId ?? "");
const { manaRewards: outputManaRewards } = useOutputManaRewards(
network,
accountOutputMetadata?.outputId ?? "",
accountOutputMetadata?.spent?.slot,
);

const [addressBasicOutputs, isBasicOutputsLoading] = useAddressBasicOutputs(network, state.addressDetails?.bech32 ?? null);
const [addressNftOutputs, isNftOutputsLoading] = useAddressNftOutputs(network, state.addressDetails?.bech32 ?? null);
Expand Down
4 changes: 2 additions & 2 deletions client/src/helpers/nova/hooks/useAddressBalance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType, NftOutput, AccountOutput, AnchorOutput, OutputMetadataResponse } from "@iota/sdk-wasm-nova/web";
import { AddressType, NftOutput, AccountOutput, AnchorOutput, OutputMetadataResponse, Utils } from "@iota/sdk-wasm-nova/web";
import { useEffect, useState } from "react";
import { IManaBalance } from "~/models/api/nova/address/IAddressBalanceResponse";
import { IAddressDetails } from "~/models/api/nova/IAddressDetails";
Expand Down Expand Up @@ -64,7 +64,7 @@ export function useAddressBalance(

// Output mana
const { included, spent } = outputMetadata ?? {};
const createdSlotIndex = (included?.slot as number) ?? null;
const createdSlotIndex = included?.transactionId ? Utils.computeSlotIndex(included.transactionId) : null;
const spentSlotIndex = (spent?.slot as number) ?? null;

if (output && createdSlotIndex !== null && protocolInfo) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/helpers/nova/hooks/useOutputManaRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useOutputManaRewards(
// eslint-disable-next-line no-void
void (async () => {
apiClient
.getRewards({ network, outputId })
.getRewards({ network, outputId, slotIndex })
.then((response) => {
if (isMounted) {
const manaRewards = response.manaRewards;
Expand Down
5 changes: 5 additions & 0 deletions client/src/models/api/nova/IRewardsRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ export interface IRewardsRequest {
* The output id to get the rewards for.
*/
outputId: string;

/**
* The slot index to use as 'up to' to get the rewards for (like when the output was spent).
*/
slotIndex?: number;
}
3 changes: 2 additions & 1 deletion client/src/services/nova/novaApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ export class NovaApiClient extends ApiClient {
* @returns The response from the request.
*/
public async getRewards(request: IRewardsRequest): Promise<IRewardsResponse> {
return this.callApi<unknown, IRewardsResponse>(`nova/output/rewards/${request.network}/${request.outputId}`, "get");
const params = FetchHelper.urlParams({ slotIndex: request.slotIndex });
return this.callApi<unknown, IRewardsResponse>(`nova/output/rewards/${request.network}/${request.outputId}${params}`, "get");
}

/**
Expand Down

0 comments on commit d3b4008

Please sign in to comment.