Skip to content

Commit

Permalink
Add shared lib and handle native currency
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Aug 22, 2024
1 parent c8e0f5c commit e43c52c
Show file tree
Hide file tree
Showing 34 changed files with 176 additions and 23 deletions.
2 changes: 1 addition & 1 deletion libs/repositories/src/Erc20Repository/Erc20Repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';

export const erc20RepositorySymbol = Symbol.for('Erc20Repository');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Erc20RepositoryCache } from './Erc20RepositoryCache';
import { Erc20, Erc20Repository } from './Erc20Repository';
import { CacheRepository } from '../CacheRepository/CacheRepository';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';

describe('Erc20RepositoryCache', () => {
let erc20RepositoryCache: Erc20RepositoryCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inject, injectable } from 'inversify';
import NodeCache from 'node-cache';
import { Erc20, Erc20Repository } from './Erc20Repository';
import { CacheRepository } from '../CacheRepository/CacheRepository';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { getCacheKey, PartialCacheKey } from '../utils/cache';

const NULL_VALUE = 'null';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Erc20RepositoryViem } from './Erc20RepositoryViem';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { PublicClient } from 'viem';
import { erc20Abi } from 'viem';
import { Erc20 } from './Erc20Repository';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { injectable } from 'inversify';
import { Erc20, Erc20Repository } from './Erc20Repository';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { erc20Abi, getAddress, PublicClient } from 'viem';

@injectable()
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/UsdRepository/UsdRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';

export const usdRepositorySymbol = Symbol.for('UsdRepository');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UsdRepositoryCache } from './UsdRepositoryCache';
import IORedis from 'ioredis';
import { UsdRepository } from './UsdRepository';
import { CacheRepositoryRedis } from '../CacheRepository/CacheRepositoryRedis';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { WETH } from '../../test/mock';
import type { PricePoint } from './UsdRepository';

Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/UsdRepository/UsdRepositoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
deserializePricePoints,
serializePricePoints,
} from './UsdRepository';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import ms from 'ms';
import { CacheRepository } from '../CacheRepository/CacheRepository';
import { getCacheKey, PartialCacheKey } from '../utils/cache';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from 'inversify';
import { UsdRepositoryCoingecko } from './UsdRepositoryCoingecko';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { WETH, DEFINITELY_NOT_A_TOKEN } from '../../test/mock';
import ms from 'ms';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SimplePriceResponse,
coingeckoProClient,
} from '../datasources/coingecko';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { throwIfUnsuccessful } from '../utils/throwIfUnsuccessful';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { UsdRepositoryCow } from './UsdRepositoryCow';

import {
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/UsdRepository/UsdRepositoryCow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { injectable } from 'inversify';
import { UsdRepositoryNoop } from './UsdRepository';
import { OneBigNumber, TenBigNumber, USDC, ZeroBigNumber } from '../const';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { BigNumber } from 'bignumber.js';
import { throwIfUnsuccessful } from '../utils/throwIfUnsuccessful';
import { Erc20Repository } from '../Erc20Repository/Erc20Repository';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';
import { PricePoint, UsdRepository } from './UsdRepository';
import { UsdRepositoryFallback } from './UsdRepositoryFallback';
import { WETH } from '../../test/mock';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { injectable } from 'inversify';
import { PricePoint, PriceStrategy, UsdRepository } from './UsdRepository';
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';

@injectable()
export class UsdRepositoryFallback implements UsdRepository {
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from 'bignumber.js';
import { SupportedChainId } from './types';
import { SupportedChainId } from '../../shared/src/types';

interface TokenAddressAndDecimals {
address: string;
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/datasources/coingecko.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedChainId } from '../types';
import { SupportedChainId } from '@cowprotocol/shared';

import createClient from 'openapi-fetch';

Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/datasources/cowApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import createClient from 'openapi-fetch';
const COW_API_BASE_URL = process.env.COW_API_BASE_URL || 'https://api.cow.fi';

import type { paths } from '../gen/cow/cow-api-types';
import { ALL_CHAIN_IDS, SupportedChainId } from '../types';
import { ALL_CHAIN_IDS, SupportedChainId } from '@cowprotocol/shared';

export type CowApiClient = ReturnType<typeof createClient<paths>>;

Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/datasources/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
webSocket,
} from 'viem';
import { arbitrum, gnosis, mainnet, sepolia } from 'viem/chains';
import { ALL_CHAIN_IDS, SupportedChainId } from '../types';
import { ALL_CHAIN_IDS, SupportedChainId } from '@cowprotocol/shared';

const NETWORKS: Record<SupportedChainId, Chain> = {
[SupportedChainId.MAINNET]: mainnet,
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './types';
export * from '../../shared/src/types';

// Utils
export * from './utils/cache';
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts", "../shared/src/types.ts"]
}
2 changes: 2 additions & 0 deletions libs/services/src/SlippageService/SlippageService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PricePoint, SupportedChainId } from '@cowprotocol/repositories';
import { Address } from 'viem';

/**
* BPS (Basis Points)
Expand All @@ -23,6 +24,7 @@ export interface OrderForSlippageCalculation {
}

export interface VolatilityDetails {
tokenAddress: string;
usdPrice: number;
prices: PricePoint[] | null;
volatilityInUsd: number;
Expand Down
7 changes: 5 additions & 2 deletions libs/services/src/SlippageService/SlippageServiceMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
VolatilityDetails,
} from './SlippageService';
import ms from 'ms';
import { toTokenAddress } from '@cowprotocol/shared';

export const MIN_SLIPPAGE_BPS = 50;
export const MAX_SLIPPAGE_BPS = 200;
Expand Down Expand Up @@ -56,15 +57,16 @@ export class SlippageServiceMain implements SlippageService {
* Get the volatility of the asset in some time (enough for a solver to execute a solvable order)
*
* @param chainId
* @param tokenAddress
* @param tokenAddressString
*
* @returns volatility in decimal format
*/
async getVolatilityDetails(
chainId: SupportedChainId,
tokenAddress: string,
tokenAddressString: string,
order?: OrderForSlippageCalculation
): Promise<VolatilityDetails | null> {
const tokenAddress = toTokenAddress(tokenAddressString, chainId);
const prices = await this.usdRepository.getUsdPrices(
chainId,
tokenAddress,
Expand Down Expand Up @@ -122,6 +124,7 @@ export class SlippageServiceMain implements SlippageService {
const normalizedVolatility = volatilityForFairSettlement / usdPrice;

return {
tokenAddress,
prices,
usdPrice,
volatilityInUsd: volatilityForFairSettlement,
Expand Down
18 changes: 18 additions & 0 deletions libs/shared/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# shared

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test shared` to execute the unit tests via [Jest](https://jestjs.io).
11 changes: 11 additions & 0 deletions libs/shared/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'shared',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/shared',
};
30 changes: 30 additions & 0 deletions libs/shared/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "shared",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/shared/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/shared/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/shared/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
18 changes: 18 additions & 0 deletions libs/shared/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Address } from 'viem';
import { SupportedChainId } from './types';

/**
* Native currency address. For example, represents Ether in Mainnet and Arbitrum, and xDAI in Gnosis chain.
*/
export const NativeCurrencyAddress =
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';

/**
* Wrapped native token address. For example, represents WETH in Mainnet and Arbitrum, and wxDAI in Gnosis chain.
*/
export const WrappedNativeTokenAddress: Record<SupportedChainId, Address> = {
[SupportedChainId.MAINNET]: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
[SupportedChainId.GNOSIS_CHAIN]: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d',
[SupportedChainId.ARBITRUM_ONE]: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
[SupportedChainId.SEPOLIA]: '0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14',
};
3 changes: 3 additions & 0 deletions libs/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './types';
export * from './const';
export * from './utils';
File renamed without changes.
19 changes: 19 additions & 0 deletions libs/shared/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Address, getAddress } from 'viem';
import { NativeCurrencyAddress, WrappedNativeTokenAddress } from './const';
import { SupportedChainId } from './types';

/**
* Returns the token address. This function will throw if the address passed is not an Ethereum address.
* It will also convert the address representing the native currency (0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) into
* its wrapped version.
*/
export function toTokenAddress(
address: string,
chainId: SupportedChainId
): Address {
if (address.toLocaleLowerCase() === NativeCurrencyAddress) {
return WrappedNativeTokenAddress[chainId];
}

return getAddress(address.toLowerCase());
}
16 changes: 16 additions & 0 deletions libs/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/shared/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/shared/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"@cowprotocol/cms-api": ["libs/cms-api/src/index.ts"],
"@cowprotocol/notifications": ["libs/notifications/src/index.ts"],
"@cowprotocol/repositories": ["libs/repositories/src/index.ts"],
"@cowprotocol/services": ["libs/services/src/index.ts"]
"@cowprotocol/services": ["libs/services/src/index.ts"],
"@cowprotocol/shared": ["libs/shared/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit e43c52c

Please sign in to comment.