-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate swapVCow code from claim (#365)
* Separate swapVCow code from claim * Updated names
- Loading branch information
Showing
8 changed files
with
231 additions
and
12 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createAction } from '@reduxjs/toolkit' | ||
|
||
export enum SwapVCowStatus { | ||
INITIAL = 'INITIAL', | ||
ATTEMPTING = 'ATTEMPTING', | ||
SUBMITTED = 'SUBMITTED', | ||
} | ||
|
||
export type CowTokenActions = { | ||
setSwapVCowStatus: (payload: SwapVCowStatus) => void | ||
} | ||
|
||
export const setSwapVCowStatus = createAction<SwapVCowStatus>('cowToken/setSwapVCowStatus') |
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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import { Currency, CurrencyAmount } from '@uniswap/sdk-core' | ||
import { TransactionResponse } from '@ethersproject/providers' | ||
|
||
import { useVCowContract } from 'hooks/useContract' | ||
import { useActiveWeb3React } from 'hooks/web3' | ||
import { useSingleCallResult, Result } from 'state/multicall/hooks' | ||
import { useTransactionAdder } from 'state/enhancedTransactions/hooks' | ||
import { V_COW } from 'constants/tokens' | ||
import { AppState } from 'state' | ||
import { useAppDispatch, useAppSelector } from 'state/hooks' | ||
import { setSwapVCowStatus, SwapVCowStatus } from './actions' | ||
import { OperationType } from 'components/TransactionConfirmationModal' | ||
import { APPROVE_GAS_LIMIT_DEFAULT } from 'hooks/useApproveCallback/useApproveCallbackMod' | ||
|
||
export type SetSwapVCowStatusCallback = (payload: SwapVCowStatus) => void | ||
|
||
type VCowData = { | ||
isLoading: boolean | ||
total: CurrencyAmount<Currency> | undefined | null | ||
unvested: CurrencyAmount<Currency> | undefined | null | ||
vested: CurrencyAmount<Currency> | undefined | null | ||
} | ||
|
||
interface SwapVCowCallbackParams { | ||
openModal: (message: string, operationType: OperationType) => void | ||
closeModal: () => void | ||
} | ||
|
||
/** | ||
* Hook that parses the result input with BigNumber value to CurrencyAmount | ||
*/ | ||
function useParseVCowResult(result: Result | undefined) { | ||
const { chainId } = useActiveWeb3React() | ||
|
||
const vCowToken = chainId ? V_COW[chainId] : undefined | ||
|
||
return useMemo(() => { | ||
if (!chainId || !vCowToken || !result) { | ||
return | ||
} | ||
|
||
return CurrencyAmount.fromRawAmount(vCowToken, result[0].toString()) | ||
}, [chainId, result, vCowToken]) | ||
} | ||
|
||
/** | ||
* Hook that fetches the needed vCow data and returns it in VCowData type | ||
*/ | ||
export function useVCowData(): VCowData { | ||
const vCowContract = useVCowContract() | ||
const { account } = useActiveWeb3React() | ||
|
||
const { loading: isVestedLoading, result: vestedResult } = useSingleCallResult(vCowContract, 'swappableBalanceOf', [ | ||
account ?? undefined, | ||
]) | ||
const { loading: isTotalLoading, result: totalResult } = useSingleCallResult(vCowContract, 'balanceOf', [ | ||
account ?? undefined, | ||
]) | ||
|
||
const vested = useParseVCowResult(vestedResult) | ||
const total = useParseVCowResult(totalResult) | ||
|
||
const unvested = useMemo(() => { | ||
if (!total || !vested) { | ||
return null | ||
} | ||
|
||
// Check if total < vested, if it is something is probably wrong and we return null | ||
if (total.lessThan(vested)) { | ||
return null | ||
} | ||
|
||
return total.subtract(vested) | ||
}, [total, vested]) | ||
|
||
const isLoading = isVestedLoading || isTotalLoading | ||
|
||
return { isLoading, vested, unvested, total } | ||
} | ||
|
||
/** | ||
* Hook used to swap vCow to Cow token | ||
*/ | ||
export function useSwapVCowCallback({ openModal, closeModal }: SwapVCowCallbackParams) { | ||
const { chainId, account } = useActiveWeb3React() | ||
const vCowContract = useVCowContract() | ||
|
||
const addTransaction = useTransactionAdder() | ||
const vCowToken = chainId ? V_COW[chainId] : undefined | ||
|
||
const swapCallback = useCallback(async () => { | ||
if (!account) { | ||
throw new Error('Not connected') | ||
} | ||
if (!chainId) { | ||
throw new Error('No chainId') | ||
} | ||
if (!vCowContract) { | ||
throw new Error('vCOW contract not present') | ||
} | ||
if (!vCowToken) { | ||
throw new Error('vCOW token not present') | ||
} | ||
|
||
const estimatedGas = await vCowContract.estimateGas.swapAll({ from: account }).catch(() => { | ||
// general fallback for tokens who restrict approval amounts | ||
return vCowContract.estimateGas.swapAll().catch((error) => { | ||
console.log( | ||
'[useSwapVCowCallback] Error estimating gas for swapAll. Using default gas limit ' + | ||
APPROVE_GAS_LIMIT_DEFAULT.toString(), | ||
error | ||
) | ||
return APPROVE_GAS_LIMIT_DEFAULT | ||
}) | ||
}) | ||
|
||
const summary = `Convert vCOW to COW` | ||
openModal(summary, OperationType.CONVERT_VCOW) | ||
|
||
return vCowContract | ||
.swapAll({ from: account, gasLimit: estimatedGas }) | ||
.then((tx: TransactionResponse) => { | ||
addTransaction({ | ||
swapVCow: true, | ||
hash: tx.hash, | ||
summary, | ||
}) | ||
}) | ||
.finally(closeModal) | ||
}, [account, addTransaction, chainId, closeModal, openModal, vCowContract, vCowToken]) | ||
|
||
return { | ||
swapCallback, | ||
} | ||
} | ||
|
||
/** | ||
* Hook that sets the swap vCow->Cow status | ||
*/ | ||
export function useSetSwapVCowStatus(): SetSwapVCowStatusCallback { | ||
const dispatch = useAppDispatch() | ||
return useCallback((payload: SwapVCowStatus) => dispatch(setSwapVCowStatus(payload)), [dispatch]) | ||
} | ||
|
||
/** | ||
* Hook that gets swap vCow->Cow status | ||
*/ | ||
export function useSwapVCowStatus() { | ||
return useAppSelector((state: AppState) => state.cowToken.swapVCowStatus) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isAnyOf, Middleware } from '@reduxjs/toolkit' | ||
import { AppState } from 'state' | ||
import { finalizeTransaction } from '../enhancedTransactions/actions' | ||
import { setSwapVCowStatus, SwapVCowStatus } from './actions' | ||
import { getCowSoundSuccess } from 'utils/sound' | ||
|
||
const isFinalizeTransaction = isAnyOf(finalizeTransaction) | ||
|
||
// Watch for swapVCow tx being finalized and triggers a change of status | ||
export const cowTokenMiddleware: Middleware<Record<string, unknown>, AppState> = (store) => (next) => (action) => { | ||
const result = next(action) | ||
|
||
let cowSound | ||
|
||
if (isFinalizeTransaction(action)) { | ||
const { chainId, hash } = action.payload | ||
const transaction = store.getState().transactions[chainId][hash] | ||
|
||
if (transaction.swapVCow) { | ||
const status = transaction.receipt?.status | ||
|
||
console.debug( | ||
`[stat:swapVCow:middleware] Convert vCOW to COW transaction finalized with status ${status}`, | ||
transaction.hash | ||
) | ||
|
||
store.dispatch(setSwapVCowStatus(SwapVCowStatus.INITIAL)) | ||
cowSound = getCowSoundSuccess() | ||
} | ||
} | ||
|
||
if (cowSound) { | ||
cowSound.play().catch((e) => { | ||
console.error('🐮 [middleware::swapVCow] Moooooo cannot be played', e) | ||
}) | ||
} | ||
|
||
return result | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createReducer } from '@reduxjs/toolkit' | ||
import { SwapVCowStatus, setSwapVCowStatus } from './actions' | ||
|
||
export type CowTokenState = { | ||
swapVCowStatus: SwapVCowStatus | ||
} | ||
|
||
export const initialState: CowTokenState = { | ||
swapVCowStatus: SwapVCowStatus.INITIAL, | ||
} | ||
|
||
export default createReducer(initialState, (builder) => | ||
builder.addCase(setSwapVCowStatus, (state, { payload }) => { | ||
state.swapVCowStatus = payload | ||
}) | ||
) |
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