citycoins/ui

๐Ÿž[BUG] mining claim state wrong if API call fails

Opened this issue ยท 0 comments

Describe the bug

In the logic for claiming mining rewards, the UI checks both is-block-winner and can-claim-mining-reward to determine if the user can claim or not.

// verify user is winner at block height
const winner = await isBlockWinner(version, currentCity.data, block, stxAddress.data);
if (winner) {
setFormMsg({
type: 'success',
hidden: false,
text: 'Winner at block height, checking if reward claimed.',
});
} else {
setFormMsg({
type: 'danger',
hidden: false,
text: 'Cannot claim, did not win at the selected block height.',
});
setLoading(false);
return;
}
// verify user can claim mining reward
const canClaim = await canClaimMiningReward(version, currentCity.data, block, stxAddress.data);
if (canClaim) {
setFormMsg({
type: 'success',
hidden: false,
text: 'Can claim reward, sending claim transaction.',
});
} else {
setFormMsg({
type: 'danger',
hidden: false,
text: 'Cannot claim, reward already claimed.',
});
setLoading(false);
return;
}

The related functions used in the citycoins lib for those checks returns undefined if the fetch is unsuccessful. The check for this incorrectly interprets false and undefined as the same state and informs the user they cannot claim when there is an error fetching the data.

ui/src/lib/citycoins.js

Lines 91 to 111 in 4b05c79

export const isBlockWinner = async (version, city, block, address) => {
const url = `${CC_API_BASE}/${version}/${city}/mining-claims/is-block-winner/${block}/${address}`;
try {
const result = await fetchJson(url);
return result.value;
} catch (err) {
debugLog(`isBlockWinner: ${err}`);
return undefined;
}
};
export const canClaimMiningReward = async (version, city, block, address) => {
const url = `${CC_API_BASE}/${version}/${city}/mining-claims/can-claim-mining-reward/${block}/${address}`;
try {
const result = await fetchJson(url);
return result.value;
} catch (err) {
debugLog(`canClaimMiningReward: ${err}`);
return undefined;
}
};

To Reproduce

The incorrect state is intermittent depending on if the fetch to the API is successful or not, although it has been reported twice recently through Discord DMs.

Expected behavior

The state should indicate if there is a) a failure in the query or b) if the user is unable to claim.

This could likely be remedied by checking if the value is undefined or false and showing the appropriate state.

Additional context

This may apply to other parts of the logic in this component, and possibly other components.