Skip to content

Commit

Permalink
feat: ensure appData is always correct (#3310)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin authored Nov 1, 2023
1 parent 65e2ad7 commit a13a254
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
14 changes: 7 additions & 7 deletions apps/cowswap-frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@
# To set your own `AppData`, change `REACT_APP_FULL_APP_DATA_<environment>`

# AppData, build yours at https://explorer.cow.fi/appdata
REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"0.7.0","appCode":"CoW Swap","environment":"production","metadata":{}}'
REACT_APP_FULL_APP_DATA_ENS='{"version":"0.7.0","appCode":"CoW Swap","environment":"ens","metadata":{}}'
REACT_APP_FULL_APP_DATA_BARN='{"version":"0.7.0","appCode":"CoW Swap","environment":"barn","metadata":{}}'
REACT_APP_FULL_APP_DATA_STAGING='{"version":"0.7.0","appCode":"CoW Swap","environment":"staging","metadata":{}}'
REACT_APP_FULL_APP_DATA_PR='{"version":"0.7.0","appCode":"CoW Swap","environment":"pr","metadata":{}}'
REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"0.7.0","appCode":"CoW Swap","environment":"development","metadata":{}}'
REACT_APP_FULL_APP_DATA_LOCAL='{"version":"0.7.0","appCode":"CoW Swap","environment":"local","metadata":{}}'
REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"0.10.0","appCode":"CoW Swap","environment":"production","metadata":{}}'
REACT_APP_FULL_APP_DATA_ENS='{"version":"0.10.0","appCode":"CoW Swap","environment":"ens","metadata":{}}'
REACT_APP_FULL_APP_DATA_BARN='{"version":"0.10.0","appCode":"CoW Swap","environment":"barn","metadata":{}}'
REACT_APP_FULL_APP_DATA_STAGING='{"version":"0.10.0","appCode":"CoW Swap","environment":"staging","metadata":{}}'
REACT_APP_FULL_APP_DATA_PR='{"version":"0.10.0","appCode":"CoW Swap","environment":"pr","metadata":{}}'
REACT_APP_FULL_APP_DATA_DEVELOPMENT='{"version":"0.10.0","appCode":"CoW Swap","environment":"development","metadata":{}}'
REACT_APP_FULL_APP_DATA_LOCAL='{"version":"0.10.0","appCode":"CoW Swap","environment":"local","metadata":{}}'



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { LATEST_APP_DATA_VERSION } from '@cowprotocol/app-data'
import { MetadataApi } from '@cowprotocol/app-data'
import { DEFAULT_APP_CODE } from '@cowprotocol/common-const'
import { ALL_ENVIRONMENTS, EnvironmentName } from '@cowprotocol/common-utils'

import { getFullAppDataByEnv } from './fullAppData'

const getAppData = (env: EnvironmentName | undefined) => JSON.parse(getFullAppDataByEnv(env))
const metadataApi = new MetadataApi()

describe('getFullAppDataByEnv', () => {
ALL_ENVIRONMENTS.forEach((env) => {
it(`[${env}] AppData is on its last version and has the right content`, () => {
const appData = getAppData(env)
expect(appData).toEqual({
version: LATEST_APP_DATA_VERSION,
appCode: DEFAULT_APP_CODE,
environment: env,
metadata: {},
})
})

it(`[${env}] AppData is valid`, async () => {
const appData = getAppData(env)
const result = await metadataApi.validateAppDataDoc(appData)
expect(result).toEqual({ success: true, errors: undefined })
})
})

it(`[DEFAULT_FULL_APP_DATA] AppData is on its last version and has the right content`, () => {
const appData = getAppData(undefined)
expect(appData).toEqual({ version: LATEST_APP_DATA_VERSION, appCode: DEFAULT_APP_CODE, metadata: {} })
})

it(`[DEFAULT_FULL_APP_DATA] AppData is valid`, async () => {
const appData = getAppData(undefined)

const result = await metadataApi.validateAppDataDoc(appData)
expect(result).toEqual({ success: true, errors: undefined })
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { environmentName } from '@cowprotocol/common-utils'
import { EnvironmentName, environmentName } from '@cowprotocol/common-utils'

import { AppDataInfo } from '../types'
import { toKeccak256 } from '../utils/buildAppData'

const DEFAULT_FULL_APP_DATA = '{"version":"0.7.0","appCode":"CoW Swap","metadata":{}}'
const DEFAULT_FULL_APP_DATA = '{"version":"0.10.0","appCode":"CoW Swap","metadata":{}}'

let appData: AppDataInfo = (() => {
const fullAppData = _getFullAppData()
const fullAppData = getFullAppDataByEnv(environmentName)
return _fromFullAppData(fullAppData)
})()

Expand All @@ -20,7 +20,7 @@ export function updateFullAppData(fullAppData: string | undefined) {
}
}

function _getFullAppData(): string {
export function getFullAppDataByEnv(environmentName: EnvironmentName | undefined): string {
switch (environmentName) {
case 'production':
return process.env.REACT_APP_FULL_APP_DATA_PRODUCTION || DEFAULT_FULL_APP_DATA
Expand Down
9 changes: 9 additions & 0 deletions libs/common-utils/src/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ const { isLocal, isDev, isPr, isStaging, isProd, isEns, isBarn } = checkEnvironm
window.location.pathname
)

export const ALL_ENVIRONMENTS: EnvironmentName[] = [
'local',
'development',
'pr',
'staging',
'production',
'barn',
'ens',
]
export type EnvironmentName = 'local' | 'development' | 'pr' | 'staging' | 'production' | 'barn' | 'ens'

export const environmentName: EnvironmentName | undefined = (function () {
Expand Down

0 comments on commit a13a254

Please sign in to comment.