Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞[BUG] mining claim state wrong if API call fails #157

Open
whoabuddy opened this issue Oct 26, 2022 · 0 comments
Open

🐞[BUG] mining claim state wrong if API call fails #157

whoabuddy opened this issue Oct 26, 2022 · 0 comments
Labels
Bug Something isn't working

Comments

@whoabuddy
Copy link

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.

@whoabuddy whoabuddy added the Bug Something isn't working label Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant