This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Claim investment flow 2 #2163
Merged
Merged
Claim investment flow 2 #2163
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
345c482
Moved invest data redux
nenadV91 1346e0b
Updated invest percentage logic
nenadV91 46410cc
PR updates
nenadV91 d90b526
Merge branch 'claim' into claim-investment-flow-2
fairlighteth 704c508
Update for initInvestFlowData
nenadV91 9f448bf
Merge branch 'claim-investment-flow-2' of github.com:gnosis/cowswap i…
nenadV91 fd056b1
Merge branch 'claim' into claim-investment-flow-2
nenadV91 95fa842
Invest amount calculation update
nenadV91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
import { useCallback, useMemo, useState } from 'react' | ||
import styled from 'styled-components/macro' | ||
import CowProtocolLogo from 'components/CowProtocolLogo' | ||
import { formatUnits, parseUnits } from '@ethersproject/units' | ||
import { CurrencyAmount } from '@uniswap/sdk-core' | ||
import { formatUnits } from '@ethersproject/units' | ||
import { Currency, CurrencyAmount, Fraction } from '@uniswap/sdk-core' | ||
|
||
import { InvestTokenGroup, TokenLogo, InvestSummary, InvestInput } from '../styled' | ||
import { InvestTokenGroup, TokenLogo, InvestSummary, InvestInput, InvestAvailableBar } from '../styled' | ||
import { formatSmart } from 'utils/format' | ||
import Row from 'components/Row' | ||
import { CheckCircle } from 'react-feather' | ||
import { InvestOptionProps } from '.' | ||
import { ApprovalState } from 'hooks/useApproveCallback' | ||
import { useCurrencyBalance } from 'state/wallet/hooks' | ||
import { useActiveWeb3React } from 'hooks/web3' | ||
import { useClaimDispatchers, useClaimState } from 'state/claim/hooks' | ||
|
||
import { ButtonConfirmed } from 'components/Button' | ||
import { ButtonSize } from 'theme' | ||
|
@@ -29,40 +30,61 @@ const RangeStep = styled.button` | |
border: none; | ||
font-size: 0.8rem; | ||
cursor: pointer; | ||
color: blue; | ||
color: ${({ theme }) => theme.primary1}; | ||
padding: 0; | ||
` | ||
|
||
const INVESTMENT_STEPS = [0, 25, 50, 75, 100] | ||
const INVESTMENT_STEPS = ['0', '25', '50', '75', '100'] | ||
|
||
export default function InvestOption({ approveData, updateInvestAmount, claim }: InvestOptionProps) { | ||
const { currencyAmount, price, cost: maxCost, investedAmount } = claim | ||
function _scaleValue(maxValue: CurrencyAmount<Currency>, value: string) { | ||
// parse percent to string, example 25% -> 4 or 50% -> 2 | ||
const parsedValue = new Fraction(value, '100') | ||
|
||
// divide maxValue with parsed value to get invest amount | ||
return maxValue.multiply(parsedValue).asFraction | ||
} | ||
|
||
export default function InvestOption({ approveData, claim, optionIndex }: InvestOptionProps) { | ||
const { currencyAmount, price, cost: maxCost } = claim | ||
const { updateInvestAmount } = useClaimDispatchers() | ||
const { investFlowData } = useClaimState() | ||
|
||
const investedAmount = useMemo(() => investFlowData[optionIndex].investedAmount, [investFlowData, optionIndex]) | ||
|
||
const [percentage, setPercentage] = useState<string>(INVESTMENT_STEPS[0]) | ||
|
||
const { account } = useActiveWeb3React() | ||
|
||
const token = currencyAmount?.currency | ||
|
||
const balance = useCurrencyBalance(account || undefined, token) | ||
|
||
const handlePercentChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
console.log(event.target.value) | ||
} | ||
const decimals = balance?.currency?.decimals | ||
|
||
const handleStepChange = (value: number) => { | ||
console.log(value) | ||
} | ||
const handleStepChange = useCallback( | ||
(value: string) => { | ||
if (!maxCost || !balance) { | ||
return | ||
} | ||
|
||
const scaledCurrencyAmount = _scaleValue(maxCost, value) | ||
|
||
updateInvestAmount({ index: optionIndex, amount: scaledCurrencyAmount.quotient.toString() }) | ||
setPercentage(value) | ||
}, | ||
[balance, maxCost, optionIndex, updateInvestAmount] | ||
) | ||
|
||
const onMaxClick = useCallback(() => { | ||
if (!maxCost || !balance) { | ||
return | ||
} | ||
|
||
const amount = maxCost.greaterThan(balance) ? balance : maxCost | ||
// store the value as a string to prevent unnecessary re-renders | ||
const investAmount = formatUnits(amount.quotient.toString(), balance.currency.decimals) | ||
|
||
updateInvestAmount(claim.index, investAmount) | ||
}, [balance, claim.index, maxCost, updateInvestAmount]) | ||
updateInvestAmount({ index: optionIndex, amount: amount.quotient.toString() }) | ||
setPercentage(INVESTMENT_STEPS[INVESTMENT_STEPS.length - 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what a elaborate way to just set the percentage to 100% There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehe that's my bad :P we did this together on the call |
||
}, [balance, maxCost, optionIndex, updateInvestAmount]) | ||
|
||
// Cache approveData methods | ||
const approveCallback = approveData?.approveCallback | ||
|
@@ -84,11 +106,11 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }: | |
}, [approveCallback, token?.symbol]) | ||
|
||
const vCowAmount = useMemo(() => { | ||
if (!token || !price) { | ||
if (!token || !price || !investedAmount) { | ||
return | ||
} | ||
|
||
const investA = CurrencyAmount.fromRawAmount(token, parseUnits(investedAmount, token.decimals).toString()) | ||
const investA = CurrencyAmount.fromRawAmount(token, investedAmount) | ||
return investA.multiply(price) | ||
}, [investedAmount, price, token]) | ||
|
||
|
@@ -162,21 +184,14 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }: | |
|
||
<div> | ||
<RangeSteps> | ||
{INVESTMENT_STEPS.map((step: number) => ( | ||
{INVESTMENT_STEPS.map((step: string) => ( | ||
<RangeStep onClick={() => handleStepChange(step)} key={step}> | ||
{step}% | ||
</RangeStep> | ||
))} | ||
</RangeSteps> | ||
|
||
<input | ||
style={{ width: '100%' }} | ||
onChange={handlePercentChange} | ||
type="range" | ||
min="0" | ||
max="100" | ||
value={0} | ||
/> | ||
<InvestAvailableBar percentage={Number(percentage)} /> | ||
</div> | ||
</span> | ||
</InvestSummary> | ||
|
@@ -194,7 +209,7 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }: | |
<input | ||
// disabled | ||
placeholder="0" | ||
value={investedAmount} | ||
value={investedAmount ? formatUnits(investedAmount, decimals) : '0'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this field be handled if it changes? |
||
max={formatSmart(currencyAmount)} | ||
/> | ||
<b>{currencyAmount?.currency?.symbol}</b> | ||
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as we discussed yesterday, we won't need this (unles u want it for set max amount), which i guess would be overkill