Skip to content

Commit

Permalink
add: iexec, fix: dashboard on mobile; delete: cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
PetromirDev committed Oct 19, 2024
1 parent 3b9cbd8 commit 2cbaeaf
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 73 deletions.
126 changes: 63 additions & 63 deletions server/cron/healthFactor.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
import cron from 'node-cron'
// import cron from 'node-cron'
import sendMail from '../services/mail.js'
import { IExecWeb3mail, getWeb3Provider } from '@iexec/web3mail'
import getAAVEUserContractDataFormatted from '../../src/common/getAaveHealthFactor.js'
import { NETWORKS } from '../../src/common/networks.js'

const scheduleHealthFactorCron = () => {
const scheduleHealthFactorCron = async () => {
const provider = getWeb3Provider(process.env.PRIVATE_KEY)
const web3mail = new IExecWeb3mail(provider)

const TWENTY_SECONDS_CRON_JOB = '*/20 * * * * *'
const WEEKLY_CRON_JOB = '0 0 * * 0'
// const TWENTY_SECONDS_CRON_JOB = '*/20 * * * * *'
// const WEEKLY_CRON_JOB = '0 0 * * 0'

cron.schedule(
process.env.NODE_ENV === 'development' ? TWENTY_SECONDS_CRON_JOB : WEEKLY_CRON_JOB,
async () => {
const contactsList = await web3mail.fetchMyContacts({
isUserStrict: true
})
// cron.schedule(
// process.env.NODE_ENV === 'development' ? TWENTY_SECONDS_CRON_JOB : WEEKLY_CRON_JOB,
// async () => {
const contactsList = await web3mail.fetchMyContacts({
isUserStrict: true
})

const sendMailPromises = contactsList.map(async ({ address, owner }) => {
const healthFactorPromises = NETWORKS.map(
({ chainId, url, name, aaveLendingPoolAddress }) => {
return {
chainId,
name,
healthFactorPromise: getAAVEUserContractDataFormatted(
owner,
url,
chainId,
aaveLendingPoolAddress
)
}
}
)
// Fetch all health scores for this contact
const healthFactors = await Promise.allSettled(
healthFactorPromises.map(({ chainId, name, healthFactorPromise }) =>
healthFactorPromise
.then(({ healthFactor, block }) => ({
chainId,
name,
healthFactor,
block
}))
.catch((error) => ({
chainId,
name,
error: `Failed to fetch healthFactor: ${error.message}`
}))
const sendMailPromises = contactsList.map(async ({ address, owner }) => {
const healthFactorPromises = NETWORKS.map(
({ chainId, url, name, aaveLendingPoolAddress }) => {
return {
chainId,
name,
healthFactorPromise: getAAVEUserContractDataFormatted(
owner,
url,
chainId,
aaveLendingPoolAddress
)
)
}
}
)
// Fetch all health scores for this contact
const healthFactors = await Promise.allSettled(
healthFactorPromises.map(({ chainId, name, healthFactorPromise }) =>
healthFactorPromise
.then(({ healthFactor, block }) => ({
chainId,
name,
healthFactor,
block
}))
.catch((error) => ({
chainId,
name,
error: `Failed to fetch healthFactor: ${error.message}`
}))
)
)

const healthFactorContent = healthFactors
.filter(({ value: { healthFactor, error } }) => healthFactor && !error)
.map(({ value: { healthFactor, block, name, error } }) => {
if (error) {
return `
const healthFactorContent = healthFactors
.filter(({ value: { healthFactor, error } }) => healthFactor && !error)
.map(({ value: { healthFactor, block, name, error } }) => {
if (error) {
return `
<p>There was an error getting the Health Factor on <strong>${name}</strong>: ${error}. Please check it manually.</p>
`
} else {
return `
} else {
return `
<p>On AAVE v3 (${name}) at block <strong>${block}</strong>, the Health Factor of <strong>${owner}</strong> is <strong>${healthFactor}</strong>.</p>
`
}
})
.join('') // Remove \n for HTML and use inline spacing
}
})
.join('') // Remove \n for HTML and use inline spacing

if (!healthFactorContent) return
if (!healthFactorContent) return

const content = `
const content = `
<div>
<p>Hey,</p>
<p>With the precision of a hawk scanning the horizon 🦅, here’s a quick update for your current open positions:</p>
Expand All @@ -79,17 +79,17 @@ const scheduleHealthFactorCron = () => {
</div>
`

const subject = 'Health Factor report by SafeHawk'
// Send email for this contact
return sendMail(address, { subject, content }).catch(
(emailError) => `Failed to send email to ${address}: ${emailError.message}`
)
})
const subject = 'Health Factor report by SafeHawk'
// Send email for this contact
return sendMail(address, { subject, content }).catch(
(emailError) => `Failed to send email to ${address}: ${emailError.message}`
)
})

// Run all send mail promises, ensuring independent failures don't affect others
await Promise.allSettled(sendMailPromises)
}
)
// Run all send mail promises, ensuring independent failures don't affect others
await Promise.allSettled(sendMailPromises)
// }
// )
}

export default scheduleHealthFactorCron
42 changes: 42 additions & 0 deletions src/assets/images/networks/iexec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/common/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const NETWORKS = [

const IEXEC_NETWORK = {
id: 134,
chainId: 134,
name: 'iExec Sidechain',
aaveLendingPoolAddress: null,
url: 'https://bellecour.iex.ec'
Expand Down
8 changes: 5 additions & 3 deletions src/components/NetworkSelect/NetworkSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, useRef, useState } from 'react'
import { useAccount, useChainId, useSwitchChain } from 'wagmi'
import styles from './NetworkSelect.module.scss'
import { NETWORKS } from '@/common/networks'
import { ALL_NETWORKS } from '@/common/networks'
import { ChevronDownIcon } from '@/assets/icons'
import { config } from '@/wagmiConfig'
import { useAAVEDataProvider } from '@/context'
Expand All @@ -18,7 +18,7 @@ const NetworkSelect = ({ className }: { className?: string }) => {
const chainId = isConnected ? connectedChainId : viewOnlyChainId

const selectedNetwork = useMemo(() => {
const foundNetwork = NETWORKS.find((network) => network.chainId === chainId)
const foundNetwork = ALL_NETWORKS.find((network) => network.chainId === chainId)

return foundNetwork || null
}, [chainId])
Expand All @@ -42,6 +42,8 @@ const NetworkSelect = ({ className }: { className?: string }) => {

if (!chainId) return null

console.log(chainId)

return (
<div className={classNames(styles.wrapper, className)} ref={ref}>
<button
Expand Down Expand Up @@ -69,7 +71,7 @@ const NetworkSelect = ({ className }: { className?: string }) => {
/>
</button>
<div className={`${styles.menu} ${menuOpen ? styles.menuOpen : ''}`}>
{NETWORKS.map(({ chainId, name }) => (
{ALL_NETWORKS.map(({ chainId, name }) => (
<SelectItem
key={chainId}
name={name}
Expand Down
4 changes: 3 additions & 1 deletion src/constants/networkIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ReactComponent as EthereumIcon } from '@/assets/images/networks/ethereu
import { ReactComponent as OptimismIcon } from '@/assets/images/networks/optimism.svg'
import { ReactComponent as PolygonIcon } from '@/assets/images/networks/polygon.svg'
import { ReactComponent as ArbitrumIcon } from '@/assets/images/networks/arbitrum.svg'
import { ReactComponent as IExecIcon } from '@/assets/images/networks/iexec.svg'

const NETWORK_ICONS: {
[key: number]: React.FC<React.SVGProps<SVGSVGElement>>
Expand All @@ -11,7 +12,8 @@ const NETWORK_ICONS: {
1: EthereumIcon,
10: OptimismIcon,
137: PolygonIcon,
42161: ArbitrumIcon
42161: ArbitrumIcon,
134: IExecIcon
}

export { NETWORK_ICONS }
6 changes: 6 additions & 0 deletions src/pages/Dashboard/Dashboard.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
}
.header {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
margin-bottom: 1.5rem;
gap: 0.5rem;

.title {
font-size: 0.875rem;
Expand All @@ -26,6 +28,10 @@
font-size: 0.875rem;
color: $secondaryText;
}

@include sm {
flex-direction: row;
}
}
.placeholder {
display: flex;
Expand Down
19 changes: 13 additions & 6 deletions src/pages/Dashboard/HealthFactor/HealthFactor.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,32 @@
.score {
font-size: $font-size-title;
margin-bottom: 0.9375rem;
gap: 0.5rem;
display: flex;
justify-content: center;

.content {
display: flex;
gap: 0.625rem;
align-items: flex-end;
justify-content: center;
flex-direction: column;
width: auto;

@include lg {
align-items: center;
gap: 0.625rem;
}

.value {
font-size: 2rem;
font-weight: 700;

&.best {
color: #347C00;
color: #347c00;
}

&.good {
color: #347C00;
color: #347c00;
}

&.mid {
Expand All @@ -100,8 +102,13 @@
}

.icon {
width: 8rem;
height: 5rem;
width: 3rem;
height: 3rem;

@include md {
width: 5rem;
height: 5rem;
}
}
}
}
Expand Down

0 comments on commit 2cbaeaf

Please sign in to comment.