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

Allow selecting fee currency #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/components/TransactionSettings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState, useRef, useContext } from 'react'
import { CeloContract } from '@celo/contractkit'
import Toggle from 'components/Toggle'
import { darken } from 'polished'
import React, { useContext, useRef, useState } from 'react'
import { useUserFeeCurrency } from 'state/user/hooks'
import styled, { ThemeContext } from 'styled-components'

import QuestionHelper from '../QuestionHelper'
import { TYPE } from '../../theme'
import { AutoColumn } from '../Column'
import QuestionHelper from '../QuestionHelper'
import { RowBetween, RowFixed } from '../Row'

import { darken } from 'polished'

enum SlippageError {
InvalidInput = 'InvalidInput',
RiskyLow = 'RiskyLow',
Expand Down Expand Up @@ -99,6 +100,7 @@ export default function SlippageTabs({ rawSlippage, setRawSlippage, deadline, se

const [slippageInput, setSlippageInput] = useState('')
const [deadlineInput, setDeadlineInput] = useState('')
const [feeCurrency, setFeeCurrency] = useUserFeeCurrency()

const slippageInputIsValid =
slippageInput === '' || (rawSlippage / 100).toFixed(2) === Number.parseFloat(slippageInput).toFixed(2)
Expand Down Expand Up @@ -247,6 +249,24 @@ export default function SlippageTabs({ rawSlippage, setRawSlippage, deadline, se
</TYPE.body>
</RowFixed>
</AutoColumn>

<AutoColumn gap="sm">
<RowFixed>
<TYPE.black fontSize={14} fontWeight={400} color={theme.text2}>
Use Celo as fee currency
</TYPE.black>
<QuestionHelper text="If enabled, Celo will be used as the fee currency instead of cUSD." />
</RowFixed>
<Toggle
id="toggle-fee-currency"
isActive={feeCurrency === CeloContract.StableToken}
toggle={
feeCurrency === CeloContract.StableToken
? () => setFeeCurrency(CeloContract.GoldToken)
: () => setFeeCurrency(CeloContract.StableToken)
}
/>
</AutoColumn>
</AutoColumn>
)
}
2 changes: 2 additions & 0 deletions src/state/user/actions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CeloToken } from '@celo/contractkit'
import { createAction } from '@reduxjs/toolkit'

export interface SerializedToken {
Expand All @@ -17,6 +18,7 @@ export const updateMatchesDarkMode = createAction<{ matchesDarkMode: boolean }>(
export const updateUserDarkMode = createAction<{ userDarkMode: boolean }>('user/updateUserDarkMode')
export const updateUserExpertMode = createAction<{ userExpertMode: boolean }>('user/updateUserExpertMode')
export const updateUserSingleHopOnly = createAction<{ userSingleHopOnly: boolean }>('user/updateUserSingleHopOnly')
export const updateUserFeeCurrency = createAction<{ userFeeCurrency: CeloToken }>('user/updateUserFeeCurrency')
export const updateUserSlippageTolerance = createAction<{ userSlippageTolerance: number }>(
'user/updateUserSlippageTolerance'
)
Expand Down
21 changes: 21 additions & 0 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CeloToken } from '@celo/contractkit'
import { Pair, Token } from '@ubeswap/sdk'
import { IValoraAccount } from 'connectors/valora/valoraUtils'
import flatMap from 'lodash.flatmap'
Expand All @@ -20,6 +21,7 @@ import {
updateUserDarkMode,
updateUserDeadline,
updateUserExpertMode,
updateUserFeeCurrency,
updateUserSingleHopOnly,
updateUserSlippageTolerance,
} from './actions'
Expand Down Expand Up @@ -129,6 +131,25 @@ export function useUserSingleHopOnly(): [boolean, (newSingleHopOnly: boolean) =>
return [singleHopOnly, setSingleHopOnly]
}

export function useUserFeeCurrency(): [CeloToken, (newFeeCurrency: CeloToken) => void] {
const dispatch = useDispatch<AppDispatch>()

const feeCurrency = useSelector<AppState, AppState['user']['userFeeCurrency']>((state) => state.user.userFeeCurrency)

const setFeeCurrency = useCallback(
(newFeeCurrency: CeloToken) => {
ReactGA.event({
category: 'Routing',
action: `fees in ${newFeeCurrency}`,
})
dispatch(updateUserFeeCurrency({ userFeeCurrency: newFeeCurrency }))
},
[dispatch]
)

return [feeCurrency, setFeeCurrency]
}

export function useUserSlippageTolerance(): [number, (slippage: number) => void] {
const dispatch = useDispatch<AppDispatch>()
const userSlippageTolerance = useSelector<AppState, AppState['user']['userSlippageTolerance']>((state) => {
Expand Down
8 changes: 8 additions & 0 deletions src/state/user/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CeloContract, CeloToken } from '@celo/contractkit'
import { createReducer } from '@reduxjs/toolkit'
import { DEFAULT_DEADLINE_FROM_NOW, INITIAL_ALLOWED_SLIPPAGE } from '../../constants'
import { updateVersion } from '../global/actions'
Expand All @@ -15,6 +16,7 @@ import {
updateUserDarkMode,
updateUserDeadline,
updateUserExpertMode,
updateUserFeeCurrency,
updateUserSingleHopOnly,
updateUserSlippageTolerance,
} from './actions'
Expand All @@ -32,6 +34,8 @@ export interface UserState {

userSingleHopOnly: boolean // only allow swaps on direct pairs

userFeeCurrency: CeloToken

// user defined slippage tolerance in bips, used in all txns
userSlippageTolerance: number

Expand Down Expand Up @@ -70,6 +74,7 @@ export const initialState: UserState = {
matchesDarkMode: false,
userExpertMode: false,
userSingleHopOnly: false,
userFeeCurrency: CeloContract.StableToken,
userSlippageTolerance: INITIAL_ALLOWED_SLIPPAGE,
userDeadline: DEFAULT_DEADLINE_FROM_NOW,
tokens: {},
Expand Down Expand Up @@ -119,6 +124,9 @@ export default createReducer(initialState, (builder) =>
.addCase(updateUserSingleHopOnly, (state, action) => {
state.userSingleHopOnly = action.payload.userSingleHopOnly
})
.addCase(updateUserFeeCurrency, (state, action) => {
state.userFeeCurrency = action.payload.userFeeCurrency
})
.addCase(addSerializedToken, (state, { payload: { serializedToken } }) => {
state.tokens[serializedToken.chainId] = state.tokens[serializedToken.chainId] || {}
state.tokens[serializedToken.chainId][serializedToken.address] = serializedToken
Expand Down