Skip to content

Commit

Permalink
feat(EX-2709): affiliate
Browse files Browse the repository at this point in the history
* feat: affiliate

* feat: affiliate

* fix: refresh + token by address

* fix: login popup trigger multiple times

* feat: handle refcode

* feat: using config duration from BE

* pre-release aggregator
  • Loading branch information
viet-nv authored Feb 13, 2025
1 parent ad94259 commit d4839c8
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ VITE_REFERRAL_URL=https://referral.kyberswap.com/api

VITE_TOKEN_API_URL=https://pre-token-api.kyberengineering.io/api
VITE_ZAP_EARN_URL=https://zap-earn-service.kyberengineering.io/api

VITE_AFFILIATE_SERVICE=https://pre-affiliate-service.kyberengineering.io/api
VITE_OAUTH_INTERCEPTOR_API=https://pre-oauth-interceptor-api.kyberengineering.io/api
3 changes: 3 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ VITE_REFERRAL_URL=https://referral.kyberswap.com/api

VITE_TOKEN_API_URL=https://pre-token-api.kyberengineering.io/api
VITE_ZAP_EARN_URL=https://zap-earn-service.kyberengineering.io/api

VITE_AFFILIATE_SERVICE=https://pre-affiliate-service.kyberengineering.io/api
VITE_OAUTH_INTERCEPTOR_API=https://pre-oauth-interceptor-api.kyberengineering.io/api
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ VITE_REFERRAL_URL=https://referral.kyberswap.com/api

VITE_TOKEN_API_URL=https://token-api.kyberengineering.io/api
VITE_ZAP_EARN_URL=https://zap-earn-service.kyberengineering.io/api

VITE_AFFILIATE_SERVICE=https://affiliate-service.kyberengineering.io/api
3 changes: 3 additions & 0 deletions .env.stg
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ VITE_REFERRAL_URL=https://referral.kyberswap.com/api

VITE_TOKEN_API_URL=https://pre-token-api.kyberengineering.io/api
VITE_ZAP_EARN_URL=https://zap-earn-service.kyberengineering.io/api

VITE_AFFILIATE_SERVICE=https://pre-affiliate-service.kyberengineering.io/api
VITE_OAUTH_INTERCEPTOR_API=https://pre-oauth-interceptor-api.kyberengineering.io/api
4 changes: 4 additions & 0 deletions src/components/SwapForm/hooks/useBuildRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { NETWORKS_INFO } from 'constants/networks'
import { useActiveWeb3React } from 'hooks'
import useENS from 'hooks/useENS'
import { useKyberswapGlobalConfig } from 'hooks/useKyberSwapConfig'
import { getCookieValue } from 'utils'

export type BuildRouteResult =
| {
Expand Down Expand Up @@ -58,6 +59,8 @@ const useBuildRoute = (args: Args) => {
}
}

const refCode = getCookieValue('refCode')

const payload: BuildRoutePayload = {
routeSummary,
deadline: Math.floor(Date.now() / 1000) + transactionTimeout,
Expand All @@ -68,6 +71,7 @@ const useBuildRoute = (args: Args) => {
skipSimulateTx: false,
enableGasEstimation: true,
permit,
referral: refCode,
// for calculating price impact only
chainId,
tokenInDecimals: currencyIn?.decimals,
Expand Down
5 changes: 4 additions & 1 deletion src/components/swapv2/LimitOrder/LimitOrderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { tryParseAmount } from 'state/swap/hooks'
import { useCurrencyBalance } from 'state/wallet/hooks'
import { MEDIA_WIDTHS } from 'theme'
import { TransactionFlowState } from 'types/TransactionFlowState'
import { getCookieValue } from 'utils'
import { subscribeNotificationOrderCancelled, subscribeNotificationOrderExpired } from 'utils/firebase'
import { maxAmountSpend } from 'utils/maxAmountSpend'
import { formatTimeDuration } from 'utils/time'
Expand Down Expand Up @@ -520,10 +521,12 @@ const LimitOrderForm = forwardRef<LimitOrderFormHandle, Props>(function LimitOrd
throw new Error('wrong input')
}

const refCode = getCookieValue('refCode')

const { signature, salt } = await signOrder(params)
const payload = getPayloadCreateOrder(params)
setFlowState(state => ({ ...state, pendingText: t`Placing order` }))
const response = await submitOrder({ ...payload, salt, signature }).unwrap()
const response = await submitOrder({ ...payload, salt, signature, referral: refCode }).unwrap()
setFlowState(state => ({ ...state, showConfirm: false }))

notify(
Expand Down
1 change: 1 addition & 0 deletions src/constants/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const WALLETCONNECT_PROJECT_ID = required('WALLETCONNECT_PROJECT_ID')
export const CAMPAIGN_URL = required('CAMPAIGN_URL')
export const REFERRAL_URL = required('REFERRAL_URL')
export const TOKEN_API_URL = required('TOKEN_API_URL')
export const AFFILIATE_SERVICE_URL = required('AFFILIATE_SERVICE')

type FirebaseConfig = {
apiKey: string
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/useAffiliate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect } from 'react'
import { useSearchParams } from 'react-router-dom'

import { AFFILIATE_SERVICE_URL } from 'constants/env'

export const useAffiliate = () => {
const [searchParams, setSearchParams] = useSearchParams()
const refCode = searchParams.get('refCode')

useEffect(() => {
if (!refCode) return

fetch(`${AFFILIATE_SERVICE_URL}/v1/public/affiliates/${refCode}/info`)
.then(res => res.json())
.then(res => {
const duration = res?.info?.sessionDurationSecond
if (!duration) return

const expireTime = new Date().getTime() + 1000 * duration
const d = new Date()
d.setTime(expireTime)
document.cookie = `refCode=${refCode};expires=${d.toUTCString()};path=/`
searchParams.delete('refCode')
setSearchParams(searchParams)
})
}, [refCode, searchParams, setSearchParams])
}
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'swiper/swiper.min.css'
import Web3Provider from 'components/Web3Provider'
import { ENV_LEVEL, GTM_ID, MIXPANEL_PROJECT_TOKEN, SENTRY_DNS, TAG } from 'constants/env'
import { ENV_TYPE } from 'constants/type'
import { useAffiliate } from 'hooks/useAffiliate'

import { sentryRequestId } from './constants'
import { LanguageProvider } from './i18n'
Expand Down Expand Up @@ -91,6 +92,7 @@ if (window.ethereum) {
}

function Updaters() {
useAffiliate()
return (
<>
<ListsUpdater />
Expand Down
11 changes: 9 additions & 2 deletions src/pages/Earns/useLiquidityWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { NETWORKS_INFO } from 'constants/networks'
import { useActiveWeb3React, useWeb3React } from 'hooks'
import { useChangeNetwork } from 'hooks/web3/useChangeNetwork'
import { useNotify, useWalletModalToggle } from 'state/application/hooks'
import { getCookieValue } from 'utils'

import useFilter from './PoolExplorer/useFilter'

Expand Down Expand Up @@ -179,12 +180,15 @@ const useLiquidityWidget = () => {
const { changeNetwork } = useChangeNetwork()
const navigate = useNavigate()

const refCode = getCookieValue('refCode')

const addLiquidityParams: AddLiquidityParams | null = useMemo(
() =>
addLiquidityPureParams
? {
...addLiquidityPureParams,
source: 'KyberSwap-Earn',
referral: refCode,
onViewPosition: () => {
setAddLiquidityPureParams(null)
navigate(`/earns/positions`)
Expand Down Expand Up @@ -216,6 +220,7 @@ const useLiquidityWidget = () => {
[
addLiquidityPureParams,
account,
refCode,
chainId,
toggleWalletModal,
handleOpenZapMigrationWidget,
Expand All @@ -231,6 +236,7 @@ const useLiquidityWidget = () => {
? {
...migrateLiquidityPureParams,
client: 'KyberSwap-Earn',
referral: refCode,
connectedAccount: {
address: account,
chainId: chainId as unknown as MigrateChainId,
Expand Down Expand Up @@ -260,7 +266,7 @@ const useLiquidityWidget = () => {
},
}
: null,
[account, chainId, library, migrateLiquidityPureParams, changeNetwork, toggleWalletModal, navigate],
[account, chainId, library, migrateLiquidityPureParams, changeNetwork, toggleWalletModal, navigate, refCode],
)

const [zapOutPureParams, setZapOutPureParams] = useState<{
Expand All @@ -275,6 +281,7 @@ const useLiquidityWidget = () => {
? {
...zapOutPureParams,
source: 'KyberSwap-Earn',
referral: refCode,
connectedAccount: {
address: account,
chainId: chainId as unknown as MigrateChainId,
Expand All @@ -295,7 +302,7 @@ const useLiquidityWidget = () => {
},
}
: null,
[account, chainId, changeNetwork, library, toggleWalletModal, zapOutPureParams],
[account, chainId, changeNetwork, library, toggleWalletModal, zapOutPureParams, refCode],
)

const handleOpenZapOut = (position: { dex: string; chainId: number; poolAddress: string; id: string }) => {
Expand Down
1 change: 1 addition & 0 deletions src/services/route/types/buildRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type BuildRoutePayload = {
skipSimulateTx: boolean
enableGasEstimation?: boolean
permit?: string
referral?: string
// for calculating price impact only
chainId?: number
tokenInDecimals?: number
Expand Down
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,6 @@ export function buildFlagsForFarmV21({
if (isClaimReward) flags = flags | (1 << 1)
return flags
}

export const getCookieValue = (name: string) =>
document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''

0 comments on commit d4839c8

Please sign in to comment.