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

fix(api): improve error handler #59

Merged
merged 1 commit into from
Aug 19, 2021
Merged
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
58 changes: 29 additions & 29 deletions packages/api/src/web3Middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ export class Web3Middleware {
drTxHash: toHex(drTxHash).slice(2)
}
} catch (err) {
console.log('[ERROR]', err)
return {
lastPrice: null,
lastTimestamp: null,
lastRequestId: null,
drTxHash: null
}
throw new Error(
`Error reading contract state`
)
}
}

Expand All @@ -144,28 +140,32 @@ export class Web3Middleware {
address: string
}
) {
const {
drTxHash,
lastPrice,
lastRequestId,
lastTimestamp
}: ContractsState = await this.readContractsState(contracts)
const address = feed.address
const lastStoredResult = this.lastStoredResult[address]

const isAlreadyStored = lastStoredResult?.timestamp === lastTimestamp
const isDrSolved = drTxHash !== '0'
if (!isAlreadyStored && isDrSolved) {
const result = await this.repositories.resultRequestRepository.insert({
feedId: feed.id.toString(),
result: lastPrice,
timestamp: lastTimestamp,
requestId: lastRequestId,
address: feed.address,
drTxHash: drTxHash,
label: feed.label
})
this.lastStoredResult[feed.address] = result
try {
const {
drTxHash,
lastPrice,
lastRequestId,
lastTimestamp
}: ContractsState = await this.readContractsState(contracts)
const address = feed.address
const lastStoredResult = this.lastStoredResult[address]

const isAlreadyStored = lastStoredResult?.timestamp === lastTimestamp
const isDrSolved = drTxHash !== '0'
if (!isAlreadyStored && isDrSolved) {
const result = await this.repositories.resultRequestRepository.insert({
feedId: feed.id.toString(),
result: lastPrice,
timestamp: lastTimestamp,
requestId: lastRequestId,
address: feed.address,
drTxHash: drTxHash,
label: feed.label
})
this.lastStoredResult[feed.address] = result
}
} catch (error) {
console.error(`Error reading contracts state: ${feed}`)
}
}
}