Skip to content

Commit

Permalink
Release/0.11 (#213)
Browse files Browse the repository at this point in the history
* Reconnect on connection not open on send() (#210)

* update web3

* setup reconnect

* extend error condition

* delay reconnect attempt

* reconnect on interval

* Add xDAI compatibility (#212)

* Add xDAI contract in env

* Upgrade version of dex-js

* Go back to dex-js version 0.3

* Allow to not provide the TCR

* Add TOKEN_OVERRIDES for xDAI

* Simplify type

* Remove unused import

Co-authored-by: Velenir <Velenir@users.noreply.github.com>
  • Loading branch information
anxolin and Velenir authored Oct 8, 2020
1 parent d7d35c2 commit 8e0b90a
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 306 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ NODE_URL=wss://mainnet.infura.io/ws/v3/<your-api-key>
# https://github.com/gnosis/dex-contracts/blob/master/networks.json
STABLE_COIN_CONTRACT_ADDRESS=0x6F400810b62df8E13fded51bE75fF5393eaa841F

# xDAO Gnosis Protocol
# https://github.com/gnosis/dex-contracts/blob/master/networks.json
STABLE_COIN_CONTRACT_ADDRESS=0x25B06305CC4ec6AfCF3E7c0b673da1EF8ae26313

# Rinkeby Gnosis Protocol
# https://github.com/gnosis/dex-contracts/blob/master/networks.json
#STABLE_COIN_CONTRACT_ADDRESS=0xC576eA7bd102F7E476368a5E98FA455d1Ea34dE2
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"node-cache": "^5.1.0",
"node-telegram-bot-api": "^0.40.0",
"rxjs": "7.0.0-beta.0",
"web3": "^1.2.7",
"web3-core": "^1.2.7"
"web3": "^1.2.11",
"web3-core": "^1.2.11"
},
"devDependencies": {
"@types/debug": "^4.1.5",
Expand Down Expand Up @@ -69,4 +69,4 @@
"typescript": "^3.7.2",
"typescript-transform-paths": "^1.1.13"
}
}
}
2 changes: 0 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const WEB_BASE_URL = process.env.WEB_BASE_URL
assert(WEB_BASE_URL, 'WEB_BASE_URL is required')
const port = parseInt(process.env.API_PORT || '3000')

assert(process.env.TCR_CONTRACT_ADDRESS, 'TCR_CONTRACT_ADDRESS env var is required')

moment.tz.setDefault('Etc/GMT')

logUnhandledErrors()
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const TOKEN_OVERRIDES = {
forceAddressDisplay: true, // name and symbol are ignored when `forceAddressDisplay` is set
},
},
100: {},
}
2 changes: 1 addition & 1 deletion src/helpers/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import { TCR_CONTRACT_ADDRESS } from 'config'

export const batchExchangeContract = createBatchExchangeContract(web3)
export const erc20Contract = createErc20Contract(web3)
export const tcrContract = createTcrContract(web3, TCR_CONTRACT_ADDRESS)
export const tcrContract = TCR_CONTRACT_ADDRESS ? createTcrContract(web3, TCR_CONTRACT_ADDRESS) : undefined
45 changes: 40 additions & 5 deletions src/services/DfusionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ import { version as dexJsVersion } from '@gnosis.pm/dex-js/package.json'
import { version as contractsVersion } from '@gnosis.pm/dex-contracts/package.json'
import { TCR_LIST_ID, TCR_CACHE_TIME, TOKEN_OVERRIDES } from 'config'

// declaration merging
// to allow for error callback
declare module 'web3-core' {
interface WebsocketProvider {
on(type: string, callback: () => void): void;
on(type: 'error', callback: (error: Error) => void): void
}
interface IpcProvider {
on(type: string, callback: () => void): void;
on(type: 'error', callback: (error: Error) => void): void
}

}

const PEER_COUNT_WARN_THRESHOLD = 3 // Warning if the node has less than X peers
const BLOCK_TIME_ERR_THRESHOLD_MINUTES = 2 // Error if there's no a new block in X min

Expand All @@ -25,7 +39,7 @@ const log = new Logger('service:dfusion')
export interface Params {
batchExchangeContract: BatchExchangeContract
erc20Contract: Erc20Contract
tcrContract: TcrContract
tcrContract?: TcrContract
tokenIdsFilter?: string[]
web3: Web3
}
Expand Down Expand Up @@ -82,7 +96,7 @@ export interface AboutDto {
contractsVersion: string
dexJsVersion: string
batchExchangeAddress: string
tcrContractAddress: string
tcrContractAddress?: string
tcrListId: number
}

Expand All @@ -106,7 +120,7 @@ export class DfusionRepoImpl implements DfusionService {
private _erc20Contract: Erc20Contract
private _tokenIdsFilter?: string[]

private _tcrContract: TcrContract
private _tcrContract?: TcrContract
private _networkId: number
private _batchTime: BigNumber
private _cache: NodeCache
Expand All @@ -121,6 +135,26 @@ export class DfusionRepoImpl implements DfusionService {
this._tokenIdsFilter = tokenIdsFilter
this._web3 = web3

const provider = web3.currentProvider
if (provider && typeof provider === 'object' && 'on' in provider) {
provider.on('error', (error: Error): void => {
log.error('Web3 Provider error: ', error)
// for now triggers `connection not open on send()`
// but we can aniticipate `on request()`
if (error.message.includes('connection not open on ') || error.message.includes('connection got closed')) {
const intervalId = setInterval(() => {
log.error('Connection failure. Reconnecting...')
provider.reconnect()

provider.once('connect', () => {
log.info('Reconnection successfull')
clearInterval(intervalId)
})
}, 5000)
}
})
}

this._cache = new NodeCache({ useClones: false })
}

Expand Down Expand Up @@ -174,6 +208,7 @@ export class DfusionRepoImpl implements DfusionService {
public watchOrderPlacement(params: WatchOrderPlacementParams) {
const OrderPlacement = this._contract.events.OrderPlacement
const subscriptions: Map<string, ContractEventEmitter<OrderPlacement>> = new Map()

if (this._tokenIdsFilter) {
const tokenListDescription = this._tokenIdsFilter.join(', ')
subscriptions.set(
Expand Down Expand Up @@ -281,7 +316,7 @@ export class DfusionRepoImpl implements DfusionService {
dexJsVersion,
version: packageJson.version,
batchExchangeAddress: this._contract.options.address,
tcrContractAddress: this._tcrContract.options.address,
tcrContractAddress: this._tcrContract?.options.address,
tcrListId: TCR_LIST_ID,
}
}
Expand Down Expand Up @@ -369,7 +404,7 @@ export class DfusionRepoImpl implements DfusionService {
return cachedAddresses
}

const tcrList = await this._tcrContract.methods
const tcrList = await this._tcrContract?.methods
.getTokens(TCR_LIST_ID)
.call()
.catch(() => [])
Expand Down
Loading

0 comments on commit 8e0b90a

Please sign in to comment.