Skip to content

Commit

Permalink
Merge pull request #23 from AtomicLoans/add-token-balance-check-befor…
Browse files Browse the repository at this point in the history
…e-liquidating

Add token balance check before liquidating
  • Loading branch information
matthewjablack authored Apr 25, 2020
2 parents 36a38be + 473c584 commit 23f569d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/api/routes/loan/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function defineAgentRoutes (router) {
const balance = await loanMarket.collateralClient().chain.getBalance(usedAddresses)

res.json({ btcBalance: BN(balance).dividedBy(currencies.BTC.multiplier).toFixed(8), unusedAddress, usedAddresses })
} catch(e) {
} catch (e) {
return next(res.createError(500, e))
}
}))
Expand Down
2 changes: 1 addition & 1 deletion src/worker/loan/liquidator/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function defineLiquidatorStatusJobs (agenda) {
agenda.now('update-loan-records', { loanMarketId: loanMarket.id })
}
}
} catch(e) {
} catch (e) {
console.log(e)
handleError(e)
}
Expand Down
45 changes: 29 additions & 16 deletions src/worker/loan/loans/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ async function checkLoans (loanMarket, agenda, medianBtcPrice) {
const { principal, collateral } = loanMarket

const loans = getObject('loans', principal)
const token = getObject('erc20', principal)

const principalAddress = await (web3().currentProvider.getAddresses())[0]

const currentTime = await getCurrentTime()

Expand All @@ -77,30 +80,40 @@ async function checkLoans (loanMarket, agenda, medianBtcPrice) {
const minCollateralValue = fromWei(minCollateralValueInUnits, 'ether')
console.log('minCollateralValue', minCollateralValue)

if (currentTime > loanExpiration) {
console.log('DEFAULTED')

loanModel.status = 'LIQUIDATING'
await loanModel.save()

agenda.now('liquidate-loan', { loanModelId: loanModel.id })
} else if (BN(collateralValue).isLessThan(minCollateralValue)) {
console.log('!SAFE')
console.log('LIQUIDATE')
const discountCollateralValue = await loans.methods.ddiv(await loans.methods.discountCollateralValue(numToBytes32(loanId)).call()).call()
console.log('discountCollateralValue', discountCollateralValue)

const safe = await loans.methods.safe(numToBytes32(loanId)).call()
const tokenBalance = await token.methods.balanceOf(principalAddress).call()
console.log('tokenBalance', tokenBalance)

console.log('safe', safe)
console.log('principal', principal)

if (safe) {
// update oracles
if (tokenBalance >= discountCollateralValue) {
if (currentTime > loanExpiration) {
console.log('DEFAULTED')

// agenda.now('check-liquidator-oracle')
} else {
loanModel.status = 'LIQUIDATING'
await loanModel.save()

agenda.now('liquidate-loan', { loanModelId: loanModel.id })
} else if (BN(collateralValue).isLessThan(minCollateralValue)) {
console.log('!SAFE')
console.log('LIQUIDATE')

const safe = await loans.methods.safe(numToBytes32(loanId)).call()

console.log('safe', safe)

if (safe) {
// update oracles

// agenda.now('check-liquidator-oracle')
} else {
loanModel.status = 'LIQUIDATING'
await loanModel.save()

agenda.now('liquidate-loan', { loanModelId: loanModel.id })
}
}
}
} else if (sale) {
Expand Down

0 comments on commit 23f569d

Please sign in to comment.