Skip to content

Commit

Permalink
fix(Swap): close short approvals fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdt committed Mar 22, 2021
1 parent bea33d7 commit c8efd34
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 82 deletions.
127 changes: 47 additions & 80 deletions src/components/Market/OrderCard/components/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ const Swap: React.FC = () => {
)
}, [orderType, entity.isPut, item.market, scaleUp, scaleDown])
useEffect(() => {
console.log(getSpotPremiums())
setPrem(getSpotPremiums())
}, [orderType])

Expand Down Expand Up @@ -427,7 +426,10 @@ const Swap: React.FC = () => {
}, [impact, slippage])

const isApproved = useCallback(() => {
if (item.entity.isWethCall && orderType !== Operation.CLOSE_LONG) {
if (
item.entity.isWethCall &&
(orderType === Operation.LONG || orderType === Operation.SHORT)
) {
return true
} else if (item.entity.isCall) {
return approved[0]
Expand All @@ -451,6 +453,17 @@ const Swap: React.FC = () => {
''
)
})
} else if (orderType === Operation.CLOSE_SHORT) {
onApprove(tokenAddress, spender, amount)
.then()
.catch((error) => {
addNotif(
0,
`Approving ${item.asset.toUpperCase()}`,
error.message,
''
)
})
} else if (item.entity.isWethCall) {
// no approval
return
Expand Down Expand Up @@ -827,86 +840,40 @@ const Swap: React.FC = () => {
</div>
) : (
<>
{orderType === Operation.SHORT ? (
<>
{isApproved() ? (
<Button
disabled={
!isApproved() ||
!parsedAmount?.gt(0) ||
error ||
!hasLiquidity ||
!isBelowSlippage() ||
!getHasEnoughForTrade()
}
full
size="sm"
onClick={handleSubmitClick}
isLoading={loading}
text={
!isBelowSlippage() && typedValue !== ''
? 'Price Impact Too High'
: getHasEnoughForTrade()
? 'Confirm Trade'
: 'Insufficient Balance'
}
/>
) : (
<>
<Button
disabled={parsedAmount.isZero()}
full
size="sm"
onClick={() =>
handleApproval(formatEther(getScaledAmount()))
}
isLoading={loading}
text="Approve Tokens"
/>
</>
)}
</>
{isApproved() ? (
<Button
disabled={
!isApproved() ||
!parsedAmount?.gt(0) ||
error ||
!hasLiquidity ||
!isBelowSlippage() ||
!getHasEnoughForTrade()
}
full
size="sm"
onClick={handleSubmitClick}
isLoading={loading}
text={
!isBelowSlippage() && typedValue !== ''
? 'Price Impact Too High'
: getHasEnoughForTrade()
? 'Confirm Trade'
: 'Insufficient Balance'
}
/>
) : (
<>
{isApproved() ? (
<>
{' '}
<Button
disabled={
!isApproved() ||
!parsedAmount?.gt(0) ||
error ||
!hasLiquidity ||
!isBelowSlippage() ||
!getHasEnoughForTrade()
}
full
size="sm"
onClick={handleSubmitClick}
isLoading={loading}
text={
!isBelowSlippage() && typedValue !== ''
? 'Price Impact Too High'
: getHasEnoughForTrade()
? 'Confirm Trade'
: 'Insufficient Balance'
}
/>
</>
) : (
<>
<Button
disabled={parsedAmount.isZero()}
full
size="sm"
onClick={() =>
handleApproval(formatEther(getScaledAmount()))
}
isLoading={loading}
text="Approve Tokens"
/>
</>
)}
<Button
disabled={parsedAmount.isZero()}
full
size="sm"
onClick={() =>
handleApproval(formatEther(getScaledAmount()))
}
isLoading={loading}
text="Approve Tokens"
/>
</>
)}
</>
Expand Down
5 changes: 3 additions & 2 deletions src/state/order/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export const useUpdateItem = (): ((
const isUniswap = item.venue === Venue.UNISWAP ? true : false
const spender =
orderType === Operation.CLOSE_SHORT || orderType === Operation.SHORT
? isUniswap
? !isUniswap
? SUSHI_ROUTER_ADDRESS[chainId]
: PRIMITIVE_ROUTER[chainId].address
: isUniswap
Expand Down Expand Up @@ -344,7 +344,8 @@ export const useUpdateItem = (): ((
secondaryAddress,
account
)

console.log('SPENDER CHECK -> ', spender)
console.log(tokenAllowance.toString(), primaryBal.toString())
dispatch(
updateItem({
item,
Expand Down

0 comments on commit c8efd34

Please sign in to comment.