Skip to content

Commit

Permalink
feat: geofence transak in uk (#2598)
Browse files Browse the repository at this point in the history
* feat: geofence transak in uk

Co-authored-by: Jean Ribeiro <[email protected]>

* fix: updateCountryCode condition

---------

Co-authored-by: Jean Ribeiro <[email protected]>
  • Loading branch information
nicole-obrien and jeeanribeiro authored Jun 5, 2024
1 parent ec3e96b commit 4789d91
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/desktop/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import { _ } from '@core/i18n'
import { getAndUpdateShimmerEvmTokensMetadata } from '@core/market/actions'
import { initializeWalletConnect } from '@auxiliary/wallet-connect/actions'
import { getAndUpdateCountryCode } from '@auxiliary/country/actions'
$: $activeProfile, saveActiveProfile()
Expand All @@ -50,6 +51,7 @@
let splash = true
void setupI18n({ fallbackLocale: 'en', initialLocale: $appSettings.language })
void getAndUpdateCountryCode()
onMount(async () => {
if (features.analytics.appStart.enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
} from '@core/router'
import { isDashboardSideBarExpanded } from '@core/ui'
import { IDashboardSidebarTab } from '@desktop/routers'
import features from '@features/features'
import { Logo } from '@ui'
import { campaignsRouter } from '../campaigns'
import LedgerStatusTile from './LedgerStatusTile.svelte'
import StrongholdStatusTile from './StrongholdStatusTile.svelte'
import { BackupToast, VersionToast } from './toasts'
import { isFeatureEnabled, isFeatureNotGeoFenced } from '@lib/features/utils'
let expanded = true
function toggleExpand(): void {
Expand All @@ -38,7 +38,7 @@
route: DashboardRoute.Wallet,
onClick: openWallet,
},
...(features?.collectibles?.enabled && profileFeatures?.collectibles
...(isFeatureEnabled(DashboardRoute.Collectibles) && profileFeatures?.collectibles
? [
{
icon: IconName.Image,
Expand All @@ -48,7 +48,7 @@
},
]
: []),
...(features?.governance?.enabled && profileFeatures?.governance
...(isFeatureEnabled(DashboardRoute.Governance) && profileFeatures?.governance
? [
{
icon: IconName.Bank,
Expand All @@ -58,7 +58,7 @@
},
]
: []),
...(features?.campaigns?.enabled && profileFeatures?.campaigns
...(isFeatureEnabled(DashboardRoute.Campaigns) && profileFeatures?.campaigns
? [
{
icon: IconName.Trophy,
Expand All @@ -68,7 +68,9 @@
},
]
: []),
...(features?.buySell?.enabled && profileFeatures?.buySell
...(isFeatureEnabled(DashboardRoute.BuySell) &&
isFeatureNotGeoFenced(DashboardRoute.BuySell) &&
profileFeatures?.buySell
? [
{
icon: IconName.ArrowDownUp,
Expand All @@ -83,7 +85,7 @@
},
]
: []),
...(features?.developerTools?.enabled && profileFeatures?.developer
...(isFeatureEnabled('developerTools') && profileFeatures?.developer
? [
{
icon: IconName.Developer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { activeProfile, updateActiveProfile } from '@core/profile/stores'
import { DashboardRoute } from '@core/router'
import SettingsSection from '../SettingsSection.svelte'
import { isFeatureNotGeoFenced, isFeatureNotGeoFenced } from '@lib/features/utils'
const features = $activeProfile?.features ?? {
[DashboardRoute.Wallet]: true,
Expand Down Expand Up @@ -47,12 +48,14 @@
label={localize('tabs.campaigns')}
bind:checked={features[DashboardRoute.Campaigns]}
/>
<Checkbox
size="md"
textType="base"
label={localize('tabs.buySell')}
bind:checked={features[DashboardRoute.BuySell]}
/>
{#if isFeatureNotGeoFenced(DashboardRoute.BuySell)}
<Checkbox
size="md"
textType="base"
label={localize('tabs.buySell')}
bind:checked={features[DashboardRoute.BuySell]}
/>
{/if}
<div class="flex flex-row space-x-2 items-center">
<Checkbox
size="md"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CountryApi } from '../api'
import { updateCountryCode } from '../stores'

export async function getAndUpdateCountryCode(): Promise<void> {
const api = new CountryApi()
const countryCode = await api.getCountryCode()
updateCountryCode(countryCode)
}
1 change: 1 addition & 0 deletions packages/shared/src/lib/auxiliary/country/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getAndUpdateCountryCode'
42 changes: 42 additions & 0 deletions packages/shared/src/lib/auxiliary/country/api/country.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { BaseApi } from '@core/utils'

interface IIpApiResponse {
ip: string
network: string
version: string
city: string
region: string
region_code: string
country: string
country_name: string
country_code: string
country_code_iso3: string
country_capital: string
country_tld: string
continent_code: string
in_eu: boolean
postal: string
latitude: number
longitude: number
timezone: string
utc_offset: string
country_calling_code: string
currency: string
currency_name: string
languages: string
country_area: number
country_population: number
asn: string
org: string
}

export class CountryApi extends BaseApi {
constructor() {
super('https://ipapi.co')
}

async getCountryCode(): Promise<string | undefined> {
const ipData = await this.get<IIpApiResponse>('json')
return ipData?.country_code
}
}
1 change: 1 addition & 0 deletions packages/shared/src/lib/auxiliary/country/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './country.api'
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { writable } from 'svelte/store'

export const countryCode = writable<string | undefined>(undefined)

export function updateCountryCode(_countryCode: string | undefined): void {
if (_countryCode) {
countryCode?.set(_countryCode)
}
}
1 change: 1 addition & 0 deletions packages/shared/src/lib/auxiliary/country/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './country-code.store'
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export interface IAppParameters {
allowlists: {
urls: string[]
}
geoFence: {
buySell: string[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"allowlists": {
"urls": ["https://tideprotocol.infura-ipfs.io"]
},
"geoFence": {
"buySell": ["GB"]
}
}
1 change: 1 addition & 0 deletions packages/shared/src/lib/features/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './isFeatureEnabled'
export * from './isFeatureGeoFenced'
21 changes: 21 additions & 0 deletions packages/shared/src/lib/features/utils/isFeatureGeoFenced.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { countryCode } from '@auxiliary/country/stores'
import { appParameters } from '@core/app/stores'
import { get } from 'svelte/store'

export function isFeatureNotGeoFenced(featureString: string): boolean {
const currentCountryCode = get(countryCode)
const featurePathToCheck = featureString.split('.')
const geoFence = get(appParameters)?.geoFence
let previousFeatures = geoFence
for (let i = 0; i < featurePathToCheck.length; i++) {
const currentFeature = previousFeatures?.[featurePathToCheck[i]]
if (!currentFeature) {
return true
}
if (i === featurePathToCheck.length - 1) {
return !currentFeature?.some((blockedCountry) => blockedCountry === currentCountryCode)
}
previousFeatures = currentFeature
}
return true
}

0 comments on commit 4789d91

Please sign in to comment.