-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protocol-fees): arb1 protocol fee (#5055)
* feat: feature flags can also be numbers * feat: add arb1 stable coins * feat: apply a/b testing fee to arb1 * chore: remove exports * fix: fix getDoNotQueryStatusEndpoint only accepting a boolean * feat: return fee even if not connected when percentage set to 100% * feat: add token logos for arb1 stablecoins * chore: added todo to remove gnosis chain feature flag * refactor: use a set for quicker lookup instead of array * chore: fix set usage --------- Co-authored-by: Alexandr Kazachenko <[email protected]>
- Loading branch information
1 parent
0fbb9b5
commit ed176c3
Showing
4 changed files
with
129 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { atom } from 'jotai' | ||
|
||
export const featureFlagsAtom = atom<Record<string, boolean>>({}) | ||
export const featureFlagsAtom = atom<Record<string, boolean | number>>({}) |
42 changes: 35 additions & 7 deletions
42
apps/cowswap-frontend/src/modules/volumeFee/state/cowswapFeeAtom.ts
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,41 +1,69 @@ | ||
import { atom } from 'jotai' | ||
|
||
import { GNOSIS_CHAIN_STABLECOINS } from '@cowprotocol/common-const' | ||
import { STABLECOINS } from '@cowprotocol/common-const' | ||
import { getCurrencyAddress, isInjectedWidget } from '@cowprotocol/common-utils' | ||
import { SupportedChainId } from '@cowprotocol/cow-sdk' | ||
import { walletInfoAtom } from '@cowprotocol/wallet' | ||
|
||
import { derivedTradeStateAtom } from 'modules/trade' | ||
|
||
import { featureFlagsAtom } from 'common/state/featureFlagsState' | ||
|
||
import { VolumeFee } from '../types' | ||
|
||
const COWSWAP_VOLUME_FEES: Record<SupportedChainId, VolumeFee | null> = { | ||
[SupportedChainId.MAINNET]: null, | ||
[SupportedChainId.SEPOLIA]: null, | ||
[SupportedChainId.ARBITRUM_ONE]: null, | ||
// Only Gnosis chain | ||
[SupportedChainId.ARBITRUM_ONE]: { | ||
bps: 10, // 0.1% | ||
recipient: '0x451100Ffc88884bde4ce87adC8bB6c7Df7fACccd', // Arb1 Protocol fee safe | ||
}, | ||
[SupportedChainId.GNOSIS_CHAIN]: { | ||
bps: 10, // 0.1% | ||
recipient: '0x6b3214fD11dc91De14718DeE98Ef59bCbFcfB432', // Gnosis Chain Protocol fee safe | ||
}, | ||
} | ||
|
||
export const cowSwapFeeAtom = atom((get) => { | ||
const { chainId } = get(walletInfoAtom) | ||
const { chainId, account } = get(walletInfoAtom) | ||
const tradeState = get(derivedTradeStateAtom) | ||
const featureFlags = get(featureFlagsAtom) | ||
|
||
const { inputCurrency, outputCurrency } = tradeState || {} | ||
|
||
// No widget mode | ||
// Don't use it in the widget | ||
if (isInjectedWidget()) return null | ||
|
||
// Don't user it when the currencies are not set | ||
if (!inputCurrency || !outputCurrency) return null | ||
|
||
const isInputTokenStable = GNOSIS_CHAIN_STABLECOINS.includes(getCurrencyAddress(inputCurrency).toLowerCase()) | ||
const isOutputTokenStable = GNOSIS_CHAIN_STABLECOINS.includes(getCurrencyAddress(outputCurrency).toLowerCase()) | ||
// TODO: remove this feature flag in another PR | ||
// Don't use it when isCowSwapFeeEnabled is not enabled | ||
if (!featureFlags.isCowSwapFeeEnabled) return null | ||
|
||
// Don't use it when on arb1 and shouldn't apply fee based on percentage | ||
if (chainId === SupportedChainId.ARBITRUM_ONE && !shouldApplyFee(account, featureFlags.arb1CowSwapFeePercentage)) | ||
return null | ||
|
||
const isInputTokenStable = STABLECOINS[chainId].has(getCurrencyAddress(inputCurrency).toLowerCase()) | ||
const isOutputTokenStable = STABLECOINS[chainId].has(getCurrencyAddress(outputCurrency).toLowerCase()) | ||
|
||
// No stable-stable trades | ||
if (isInputTokenStable && isOutputTokenStable) return null | ||
|
||
return COWSWAP_VOLUME_FEES[chainId] | ||
}) | ||
|
||
function shouldApplyFee(account: string | undefined, percentage: number | boolean | undefined): boolean { | ||
// Early exit for 100%, meaning should be enabled for everyone | ||
if (percentage === 100) { | ||
return true | ||
} | ||
|
||
// Falsy conditions | ||
if (typeof percentage !== 'number' || !account || percentage < 0 || percentage > 100) { | ||
return false | ||
} | ||
|
||
return BigInt(account) % 100n < percentage | ||
} |
Oops, something went wrong.