-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from makerdao/develop
v0.6.0 - Peanut Wandering Whistling-Duck - (Dendrocygna arcuata)
- Loading branch information
Showing
142 changed files
with
2,076 additions
and
909 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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Unit Tests | ||
|
||
on: [push] | ||
|
||
jobs: | ||
unit: | ||
runs-on: ubuntu-latest | ||
container: makerdaodux/cypress-dapptools-node-14:latest | ||
env: | ||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
ETHERSCAN_KEY: ${{ secrets.ETHERSCAN_KEY }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
USE_CACHE: false | ||
REDIS_URL: ${{ secrets.REDIS_URL }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install node packages | ||
run: yarn | ||
|
||
- name: Cypress install | ||
uses: cypress-io/github-action@v2 | ||
with: | ||
# Disable running of tests within install job | ||
runTests: false | ||
build: yarn build | ||
install: false | ||
|
||
- run: yarn lint | ||
|
||
- run: yarn test:ci | ||
|
||
- name: Codecov | ||
run: | | ||
yarn add --dev codecov | ||
./node_modules/.bin/codecov | ||
# Store built next data | ||
# - name: Save build folder | ||
# uses: actions/upload-artifact@v2 | ||
# with: | ||
# name: build | ||
# if-no-files-found: error | ||
# path: | | ||
# .next | ||
# public |
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { parseUnits } from 'ethers/lib/utils'; | ||
import { formatValue } from 'lib/string'; | ||
|
||
describe('Format value for numbers', () => { | ||
it('Should round down numbers', () => { | ||
const result = formatValue(parseUnits('0.00132'), undefined, undefined, true, true); | ||
|
||
expect(result).toEqual('≈0.00'); | ||
}); | ||
|
||
it('should not round down if disabled', () => { | ||
const result = formatValue(parseUnits('0.00132'), undefined, 3); | ||
|
||
expect(result).toEqual('0.001'); | ||
}); | ||
|
||
it('should not display commas if not stated', () => { | ||
const result = formatValue(parseUnits('10222.0132'), undefined, undefined, false, true); | ||
|
||
expect(result).toEqual('10222'); | ||
}); | ||
|
||
it('should not show decimals for a number bigger than 999', () => { | ||
const result = formatValue(parseUnits('10222.013222')); | ||
|
||
expect(result).toEqual('10,222'); | ||
}); | ||
|
||
it('should show 2 decimals for number less than 999', () => { | ||
const result = formatValue(parseUnits('222.013222')); | ||
|
||
expect(result).toEqual('222.01'); | ||
}); | ||
|
||
it('should show 5 decimals for number less than 999 if specified to 5', () => { | ||
const result = formatValue(parseUnits('222.013222'), undefined, 5); | ||
|
||
expect(result).toEqual('222.01322'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import { DEFAULT_NETWORK, SupportedNetworks } from 'modules/web3/constants/networks'; | ||
import { config } from 'lib/config'; | ||
import Redis from 'ioredis'; | ||
import packageJSON from '../package.json'; | ||
import logger from './logger'; | ||
|
||
const redis = config.REDIS_URL | ||
? new Redis(config.REDIS_URL, { | ||
connectTimeout: 10000 | ||
}) | ||
: null; | ||
|
||
const oneHourInMS = 60 * 60 * 1000; | ||
|
||
// Mem cache does not work on local instances of nextjs because nextjs creates clean memory states each time. | ||
const memoryCache = {}; | ||
|
||
function getFilePath(name: string, network: string): string { | ||
const date = new Date().toISOString().substring(0, 10); | ||
|
||
return `${os.tmpdir()}/gov-portal-version-${packageJSON.version}-${network}-${name}-${date}`; | ||
} | ||
|
||
export const cacheDel = (path: string): void => { | ||
const isRedisCache = !!config.REDIS_URL; | ||
|
||
if (isRedisCache && redis) { | ||
redis?.del(path); | ||
} else { | ||
try { | ||
logger.debug('cacheDel: ', path); | ||
memoryCache[path] = null; | ||
fs.unlinkSync(path); | ||
} catch (e) { | ||
logger.error(`cacheDel: ${e.message}`); | ||
} | ||
} | ||
}; | ||
|
||
export const cacheGet = async ( | ||
name: string, | ||
network?: SupportedNetworks, | ||
expiryMs?: number | ||
): Promise<any> => { | ||
if (!config.USE_CACHE || config.USE_CACHE === 'false') { | ||
return Promise.resolve(null); | ||
} | ||
|
||
const isRedisCache = !!config.REDIS_URL; | ||
|
||
try { | ||
const currentNetwork = network || DEFAULT_NETWORK.network; | ||
const path = getFilePath(name, currentNetwork); | ||
|
||
if (isRedisCache && redis) { | ||
// Get redis data if it exists | ||
const cachedData = await redis.get(path); | ||
logger.debug(`Redis cache get for ${path}`); | ||
return cachedData; | ||
} else { | ||
// If fs does not exist as a module, return null (TODO: This shouldn't happen, consider removing this check) | ||
if (Object.keys(fs).length === 0) return null; | ||
const memCached = memoryCache[path]; | ||
|
||
if (memCached) { | ||
logger.debug(`mem cache hit: ${path}`); | ||
|
||
if (memCached.expiry && memCached.expiry < Date.now()) { | ||
logger.debug('mem cache expired'); | ||
cacheDel(path); | ||
return null; | ||
} | ||
|
||
return memoryCache[path].data; | ||
} | ||
|
||
if (fs.existsSync(path)) { | ||
// In nextjs serverless instances of API functions sometimes reset their in memory cache (they are different instances) | ||
// In order to have an expiry date we can also check the last time this file was accessed or it was created. This conditions having to pass the expiryMs on the cacheGet too | ||
const { birthtime } = fs.statSync(path); | ||
|
||
if (expiryMs && birthtime && birthtime.getTime() < Date.now() + expiryMs) { | ||
cacheDel(path); | ||
return null; | ||
} | ||
|
||
logger.debug(`fs cache hit: ${path}`); | ||
return fs.readFileSync(path).toString(); | ||
} | ||
} | ||
} catch (e) { | ||
logger.error(`CacheGet: Error getting cached data, ${name} - ${network}`, e.message); | ||
return null; | ||
} | ||
}; | ||
|
||
export const cacheSet = ( | ||
name: string, | ||
data: string, | ||
network?: SupportedNetworks, | ||
expiryMs = oneHourInMS | ||
): void => { | ||
if (!config.USE_CACHE || config.USE_CACHE === 'false') { | ||
return; | ||
} | ||
|
||
const isRedisCache = !!config.REDIS_URL; | ||
const currentNetwork = network || DEFAULT_NETWORK.network; | ||
|
||
const path = getFilePath(name, currentNetwork); | ||
|
||
try { | ||
if (isRedisCache && redis) { | ||
// If redis cache is enabled, store in redis, with a TTL in seconds | ||
const expirySeconds = Math.round(expiryMs / 1000); | ||
logger.debug(`Redis cache set for ${path}, with TTL ${expirySeconds} seconds`); | ||
|
||
redis.set(path, data, 'EX', expirySeconds); | ||
} else { | ||
// File cache | ||
if (Object.keys(fs).length === 0) return; | ||
|
||
fs.writeFileSync(path, data); | ||
|
||
memoryCache[path] = { | ||
expiry: expiryMs ? Date.now() + expiryMs : null, | ||
data | ||
}; | ||
} | ||
} catch (e) { | ||
logger.error(`CacheSet: Error storing data in cache, ${name} - ${network}`, e.message); | ||
} | ||
}; |
Oops, something went wrong.
2e85e2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
governance-portal-v2 – ./
legacy-vote.vercel.app
governance-portal-v2.vercel.app
governance-portal-v2-dux-core-unit.vercel.app
governance-portal-v2-git-master-dux-core-unit.vercel.app
v2.vote.makerdao.com
vote.makerdao.com