Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Removed api/index (#131)
Browse files Browse the repository at this point in the history
Moved all initializers to respective API's index file
Updated all apps to use the new initializers

Co-authored-by: Leandro Boscariol <[email protected]>
  • Loading branch information
alfetopito and Leandro Boscariol authored Feb 10, 2021
1 parent 7ed0004 commit 43af8ff
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 201 deletions.
18 changes: 18 additions & 0 deletions src/api/deposit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Erc20Api } from 'api/erc20/Erc20Api'

import { DepositApi, DepositApiDependencies } from './DepositApi'
import { DepositApiMock } from './DepositApiMock'
import { DepositApiProxy } from './DepositApiProxy'

import { exchangeBalanceStates } from '../../../test/data'

export function createDepositApi(erc20Api: Erc20Api, injectedDependencies: DepositApiDependencies): DepositApi {
let depositApi
if (process.env.MOCK_DEPOSIT === 'true') {
depositApi = new DepositApiMock(exchangeBalanceStates, erc20Api)
} else {
depositApi = new DepositApiProxy(injectedDependencies)
}
window['depositApi'] = depositApi // register for convenience
return depositApi
}
18 changes: 18 additions & 0 deletions src/api/dexPriceEstimator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DexPriceEstimatorApi } from './DexPriceEstimatorApi'
import { DexPriceEstimatorApiProxy } from './DexPriceEstimatorApiProxy'

export function createDexPriceEstimatorApi(): DexPriceEstimatorApi {
const { type, config } = CONFIG.dexPriceEstimator
let dexPriceEstimatorApi: DexPriceEstimatorApi
switch (type) {
case 'dex-price-estimator':
dexPriceEstimatorApi = new DexPriceEstimatorApiProxy(config)
break

default:
throw new Error('Unknown implementation for DexPriceEstimatorApi: ' + type)
}

window['dexPriceEstimatorApi'] = dexPriceEstimatorApi
return dexPriceEstimatorApi
}
16 changes: 16 additions & 0 deletions src/api/erc20/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Erc20Api, Erc20ApiDependencies } from './Erc20Api'
import Erc20ApiMock from './Erc20ApiMock'
import { Erc20ApiProxy } from './Erc20ApiProxy'

import { erc20Balances, erc20Allowances, unregisteredTokens } from '../../../test/data'

export function createErc20Api(injectedDependencies: Erc20ApiDependencies): Erc20Api {
let erc20Api
if (process.env.MOCK_ERC20 === 'true') {
erc20Api = new Erc20ApiMock({ balances: erc20Balances, allowances: erc20Allowances, tokens: unregisteredTokens })
} else {
erc20Api = new Erc20ApiProxy(injectedDependencies)
}
window['erc20Api'] = erc20Api // register for convenience
return erc20Api
}
28 changes: 28 additions & 0 deletions src/api/exchange/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DepositApiDependencies } from 'api/deposit/DepositApi'
import { Erc20Api } from 'api/erc20/Erc20Api'

import { ExchangeApi } from './ExchangeApi'
import ExchangeApiMock from './ExchangeApiMock'
import { ExchangeApiProxy } from './ExchangeApiProxy'

import { tokenList, exchangeBalanceStates, FEE_TOKEN, exchangeOrders, TOKEN_8 } from '../../../test/data'

export function createExchangeApi(erc20Api: Erc20Api, injectedDependencies: DepositApiDependencies): ExchangeApi {
let exchangeApi
if (process.env.MOCK_EXCHANGE === 'true') {
const tokens = [FEE_TOKEN, ...tokenList.map((token) => token.address), TOKEN_8]
exchangeApi = new ExchangeApiMock({
balanceStates: exchangeBalanceStates,
erc20Api,
registeredTokens: tokens,
ordersByUser: exchangeOrders,
})
} else {
exchangeApi = new ExchangeApiProxy({
...injectedDependencies,
contractsDeploymentBlocks: CONFIG.exchangeContractConfig.config,
})
}
window['exchangeApi'] = exchangeApi
return exchangeApi
}
185 changes: 0 additions & 185 deletions src/api/index.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/api/tcr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Web3 from 'web3'

import { MultiTcrApiProxy } from './MultiTcrApiProxy'
import { TcrApi } from './TcrApi'

export function createTcrApi(web3: Web3): TcrApi | undefined {
const { type } = CONFIG.tcr
let tcrApi: TcrApi | undefined
switch (CONFIG.tcr.type) {
case 'none':
tcrApi = undefined
break
case 'multi-tcr': {
const multiTcrApiConfig = CONFIG.tcr
tcrApi = new MultiTcrApiProxy({ web3, ...multiTcrApiConfig.config })
break
}

default:
throw new Error('Unknown implementation for DexPriceEstimatorApi: ' + type)
}

window['tcrApi'] = tcrApi
return tcrApi
}
18 changes: 18 additions & 0 deletions src/api/thegraph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TheGraphApi } from './TheGraphApi'
import { TheGraphApiProxy } from './TheGraphApiProxy'

export function createTheGraphApi(): TheGraphApi {
const { type, config } = CONFIG.theGraphApi
let theGraphApi: TheGraphApi
switch (type) {
case 'the-graph':
theGraphApi = new TheGraphApiProxy(config)
break

default:
throw new Error('Unknown implementation for TheGraphApi: ' + type)
}

window['theGraphApi'] = theGraphApi
return theGraphApi
}
20 changes: 20 additions & 0 deletions src/api/tokenList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Network } from 'types'

import TokenListApiImpl, { TokenList } from './TokenListApi'
import TokenListApiMock from './TokenListApiMock'

import { tokenList } from '../../../test/data'

export function createTokenListApi(): TokenList {
const networkIds = [Network.Mainnet, Network.Rinkeby, Network.xDAI]

let tokenListApi: TokenList
if (process.env.MOCK_TOKEN_LIST === 'true') {
tokenListApi = new TokenListApiMock(tokenList)
} else {
tokenListApi = new TokenListApiImpl({ networkIds, initialTokenList: CONFIG.initialTokenList })
}

window['tokenListApi'] = tokenListApi // register for convenience
return tokenListApi
}
2 changes: 1 addition & 1 deletion src/api/wallet/WalletApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Network, Command } from 'types'
import BN from 'bn.js'
import assert from 'assert'
import { getDefaultProvider } from '..'
import { getDefaultProvider } from 'api/web3'
import { toBN } from '@gnosis.pm/dex-js'

import Web3Modal, { getProviderInfo, IProviderOptions, IProviderInfo, isMobile } from 'web3modal'
Expand Down
15 changes: 15 additions & 0 deletions src/api/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Web3 from 'web3'

import WalletApiImpl, { WalletApi } from './WalletApi'
import WalletApiMock from './WalletApiMock'

export function createWalletApi(web3: Web3): WalletApi {
let walletApi
if (process.env.MOCK_WALLET === 'true') {
walletApi = new WalletApiMock()
} else {
walletApi = new WalletApiImpl(web3)
}
window['walletApi'] = walletApi // register for convenience
return walletApi
}
20 changes: 20 additions & 0 deletions src/api/web3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Web3 from 'web3'

import { ETH_NODE_URL } from 'const'

// TODO connect to mainnet if we need AUTOCONNECT at all
export const getDefaultProvider = (): string | null => (process.env.NODE_ENV === 'test' ? null : ETH_NODE_URL)

export function createWeb3Api(): Web3 {
// TODO: Create an `EthereumApi` https://github.com/gnosis/gp-v1-ui/issues/331
const web3 = new Web3(getDefaultProvider())
// `handleRevert = true` makes `require` failures to throw
// For more details see https://github.com/gnosis/gp-v1-ui/issues/511
web3.eth['handleRevert'] = true

if (process.env.MOCK_WEB3 === 'true') {
// Only function that needs to be mocked so far. We can add more and add extra logic as needed
web3.eth.getCode = async (address: string): Promise<string> => address
}
return web3
}
14 changes: 14 additions & 0 deletions src/api/weth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { WethApi, WethApiDependencies, WethApiImpl } from './WethApi'
import { WethApiMock } from './WethApiMock'

export function createWethApi(injectedDependencies: WethApiDependencies): WethApi {
let wethApi
if (process.env.MOCK_WETH === 'true') {
wethApi = new WethApiMock()
} else {
wethApi = new WethApiImpl(injectedDependencies)
}
window['wethApi'] = wethApi // register for convenience

return wethApi
}
Loading

0 comments on commit 43af8ff

Please sign in to comment.