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

feat: ensure appData is always correct #3310

Merged
merged 1 commit into from
Nov 1, 2023
Merged
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
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
Loading