Skip to content

Commit

Permalink
fix:#686 - Add function to retrieve ad campaign costs and update calc…
Browse files Browse the repository at this point in the history
…ulations in AdvertiserTable
  • Loading branch information
achillegley committed Jan 2, 2025
1 parent c648492 commit fec19b7
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 26 deletions.
19 changes: 13 additions & 6 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getAdCampaignCosts } from 'app/src/web3/adCampaignManager'

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
Expand Down Expand Up @@ -47,16 +49,21 @@ async function calculateTotalVisits(advertiseDocRef) {
}

async function calculateAmountSpent(totalClicks, totalImpressions, totalVisits) {
return (
import.meta.env.VITE_ADVERTISE_CLICK_RATE * totalClicks +
import.meta.env.VITE_ADVERTISE_IMPRESSION_RATE * totalImpressions +
import.meta.env.VITE_ADVERTISE_VIEWS_RATE * totalVisits
)
const advertiseCosts = await getAdCampaignCosts()
if (advertiseCosts.status !== 'success') {
const result =
parseFloat(advertiseCosts.data.costPerClick) * totalClicks +
parseFloat(advertiseCosts.data.costPerImpression) * totalImpressions +
parseFloat(advertiseCosts.data.costPerImpression) * totalVisits
return result
} else {
console.log('Failed to get the cost of the advertise', advertiseCosts)
$q.notify({ type: 'negative', message: 'Failed to get the cost of the advertise' })
}
}

async function updateAdvertiseStatus(docId) {
const advertiseDocRef = db.collection('advertises').doc(docId)

const docData = (await advertiseDocRef.get()).data()
const myBudget = Number(docData.budget) ?? 0

Expand Down
75 changes: 59 additions & 16 deletions src/components/Advertiser/AdvertiserTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
no-data-label="No advertisements found."
no-results-label="No advertisements found for your search."
:filter="filter"
:rows="advertises"
:rows="localAdvertises"
:columns="advertises.length > 0 ? columns : []"
:loading="advertiseStore.isLoading"
:rows-per-page-options="[0]"
Expand Down Expand Up @@ -144,7 +144,7 @@
>
<q-tooltip>Unpublish</q-tooltip>
</q-icon>
{{ showStatus(props.row) }}
{{ props.row.status }}
</q-td>
</template>
<template #body-cell-content="props">
Expand All @@ -159,8 +159,8 @@
</template>
<template #body-cell-total_cost="props">
<q-td class="text-right">
{{ viewMatic(computeAdvertisementMatic(props.row.totalImpressions, props.row.totalClicks, props.row.totalVisits)) }}
<q-tooltip>{{ computeAdvertisementMatic(props.row.totalImpressions, props.row.totalClicks, props.row.totalVisits) }}</q-tooltip>
{{ viewMatic(props.row.totalCost) }}
<q-tooltip>{{ props.row.totalCost }}</q-tooltip>
</q-td>
</template>
</q-table>
Expand Down Expand Up @@ -250,7 +250,7 @@ import { useQuasar } from 'quasar'
import { useAdvertiseStore, useErrorStore, useUserStore } from 'src/stores'
import { useRouter } from 'vue-router'
import { getCurrentDate, calculateEndDate, computedDuration } from 'src/utils/date'
import { claimPayment, requestAndApproveWithdrawal, getEventsForCampaign } from 'app/src/web3/adCampaignManager'
import { claimPayment, requestAndApproveWithdrawal, getEventsForCampaign, getAdCampaignCosts } from 'app/src/web3/adCampaignManager'
import { customWeb3modal } from 'app/src/web3/walletConnect'
const props = defineProps({
Expand All @@ -261,6 +261,31 @@ const props = defineProps({
}
})
const localAdvertises = ref([])
// Watch for changes in props.advertises
watch(
() => props.advertises,
async (newAdvertises) => {
if (newAdvertises && newAdvertises.length > 0) {
// Compute and add totalCost for each advertisement
const advertisesWithCost = await Promise.all(
newAdvertises.map(async (advertise) => ({
...advertise,
totalCost: await computeAdvertisementMatic(advertise.totalImpressions, advertise.totalClicks, advertise.totalVisits),
status: await showStatus(advertise)
}))
)
localAdvertises.value = advertisesWithCost
}
},
{ immediate: true }
)
onMounted(async () => {
await advertiseStore.fetchAdvertises(selectedDataType?.value.label)
})
const router = useRouter()
const dialog = ref({ open: false, title: '', subTitle: '', type: '' })
const withdrawAmountSpentDialog = ref({})
Expand Down Expand Up @@ -300,11 +325,20 @@ const dataOptions = ref(
)
async function calculateAmountSpent(advertise) {
return (
import.meta.env.VITE_ADVERTISE_CLICK_RATE * advertise.totalClicks +
import.meta.env.VITE_ADVERTISE_IMPRESSION_RATE * advertise.totalImpressions +
import.meta.env.VITE_ADVERTISE_VIEWS_RATE * advertise.totalVisits
)
const advertiseCosts = await getAdCampaignCosts()
console.log('advertiseCosts====== ', advertiseCosts)
console.log('the current advertise ====== ', advertise)
if (advertiseCosts.status.includes('success')) {
const result =
parseFloat(advertiseCosts.data.costPerClick) * advertise.totalClicks +
parseFloat(advertiseCosts.data.costPerImpression) * advertise.totalImpressions +
parseFloat(advertiseCosts.data.costPerImpression) * advertise.totalVisits
console.log('result====== ', result)
return result
} else {
console.log('Failed to get the cost of the advertise', advertiseCosts)
$q.notify({ type: 'negative', message: 'Failed to get the cost of the advertise' })
}
}
async function _getEventsForCampaign(advertise) {
Expand Down Expand Up @@ -649,11 +683,20 @@ watch(selectedDataType, (newType) => {
const maticCache = new Map()
function computeAdvertisementMatic(impressions = 0, clicks = 0, views = 0) {
async function computeAdvertisementMatic(impressions = 0, clicks = 0, views = 0) {
const key = `${impressions}-${clicks}-${views}`
const impressionsMatic = impressions * import.meta.env.VITE_ADVERTISE_IMPRESSION_RATE
const clicksMatic = clicks * import.meta.env.VITE_ADVERTISE_CLICK_RATE
const viewsMatic = views * import.meta.env.VITE_ADVERTISE_VIEWS_RATE
const advertiseCosts = await getAdCampaignCosts()
let impressionsMatic = 0
let clicksMatic = 0
let viewsMatic = 0
if (advertiseCosts.status.includes('success')) {
impressionsMatic = impressions * parseFloat(advertiseCosts.data.costPerImpression)
clicksMatic = clicks * parseFloat(advertiseCosts.data.costPerClick)
viewsMatic = views * parseFloat(advertiseCosts.data.costPerImpression)
} else {
console.log('Failed to get the cost of the advertise', advertiseCosts)
$q.notify({ type: 'negative', message: 'Failed to get the cost of the advertise' })
}
if (maticCache.has(key)) {
return maticCache.get(key)
Expand All @@ -673,10 +716,10 @@ function viewMatic(matic) {
return maticSplit[0] + '.' + floatNumbers
}
function showStatus(data) {
async function showStatus(data) {
const create = calculateStatus(data.publishDate)
const ended = calculateStatus(data.endDate)
const budgetCrossed = computeAdvertisementMatic(data.totalImpressions, data.totalClicks, data.totalVisits) > Number(data.budget)
const budgetCrossed = (await computeAdvertisementMatic(data.totalImpressions, data.totalClicks, data.totalVisits)) > Number(data.budget)
if (!create) {
return 'Publish date pending'
} else if (budgetCrossed || data.status === 'Budget Crossed') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/353cdfbe7cf7c2cfe5260f6e6709c8ec.json"
"buildInfo": "../../build-info/a13839c3ea7d5da97fdb6449830b3a26.json"
}

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/web3/adCampaignManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,23 @@ export const getEventsForCampaign = async (campaignCode) => {
return { status: 'error', error: { message: errorMessage } }
}
}

// New function to retrieve costPerClick, costPerImpression, and bankContractAddress
export const getAdCampaignCosts = async () => {
try {
const adCampaignManager = await getContractInstance()
const costPerClick = await adCampaignManager.costPerClick()
const costPerImpression = await adCampaignManager.costPerImpression()

return {
status: 'success',
data: {
costPerClick: ethers.utils.formatEther(costPerClick),
costPerImpression: ethers.utils.formatEther(costPerImpression)
}
}
} catch (error) {
console.error('Error fetching ad campaign manager details:', error)
return { status: 'error', error: error }
}
}

0 comments on commit fec19b7

Please sign in to comment.