Repository navigation
Conversation
…in in Add/Remove mar) Automated fix and test assertions. Signed-off-by: Ollie <olliemcgrath@icloud.com>
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #19
/claim #19
Solution for [github/issues_19]: [capofficial/client] Show available margin in Add/Remove margin modal
I will address the bounty by modifying the
AddRemoveMarginModalcomponent to display the user's wallet balance when adding margin and the available margin to remove when removing margin.Root Cause Analysis
The current
AddRemoveMarginModalcomponent, as described in the issue, does not display the relevant balance information to the user. When a user intends to add margin, they need to know their available wallet balance to make an informed decision on how much to add. Similarly, when removing margin, they need to know the current margin allocated to the position to understand the maximum amount they can withdraw.The missing pieces are:
Architecture Design
The solution will involve:
useBalanceor similar hook exists or can be easily integrated.positionobject passed to the modal.AddRemoveMarginModalcomponent:isAddingMarginandpositionprops.position.collateralToken).position.margin.ptag ordiv) will be added within the modal, conditionally rendered based onisAddingMargin.formatUnits).Assumptions:
useBalancehook or similar mechanism available in the project to fetch a user's token balance given a token address. If not, I'll provide a placeholder and note where it needs to be integrated. For this solution, I'll assume auseBalancehook that takes a token address and returns aBigNumberbalance and adecimalsvalue.positionobject passed to the modal containscollateralToken(which has anaddressandsymbol) andmargin(aBigNumber).formatUnitsutility function is available for displayingBigNumbervalues.Production-Ready Code/Patch Implementation
I will modify the
AddRemoveMarginModal.tsxfile.File: src/components/AddRemoveMarginModal.tsx
Explanation of Changes:
useAccount,useBalancefromwagmi: These hooks are used to get the connected user's address and their token balance.formatUnitsfromethers/lib/utils: This utility is crucial for convertingBigNumbervalues (like those returned bywagmior stored inposition) into human-readable decimal strings.walletBalanceDataandwalletBalance:useBalancehook is called with the user's address and thecollateralToken.addressfrom theposition.watch: isOpenensures the balance is updated if the modal is open and the balance changes.enabled: isOpen && isAddingMarginoptimizes fetching, only requesting the balance when the modal is open and the user is in "Add Margin" mode.walletBalanceFormattedusesformatUnitsto display the balance correctly.availableMarginToRemove: This is directly taken fromposition.margin.availableMarginToRemoveFormattedusesformatUnitsfor display.if (isAddingMargin)block now renders the "Available in Wallet" text with the fetched balance.elseblock renders the "Available to Remove" text with the position's current margin.PositionInterface: I've added a placeholderPositioninterface. It's crucial to replace this with the actualPositiontype defined in the project. EnsurecollateralTokenhasaddress,symbol, anddecimals, andmarginis aBigNumber.Testing Verification Steps
To verify the implementation, follow these steps:
capofficial/clientproject is running locally and connected to a testnet (e.g., Sepolia, Goerli) where you have some test tokens (e.g., USDC).watch: trueis effective, the displayed balance in the open modal should update (though this might require a re-render or a slight delay depending onwagmi's polling interval).onConfirmcallback with the correct parsed amount.type="number"input helps here).Pull Request Summary
Payout & Settlement Details
0x66545035486C8B3b7966c45dc035560F4066aBb5