Skip to content

Commit

Permalink
updated billing for base and gnosis!
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsayo committed May 20, 2024
1 parent 6a34ca6 commit 1fcdb19
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
1 change: 1 addition & 0 deletions services/postgres/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ model PaymentLedger {
tenant Tenant @relation(fields: [tenantId], references: [id])
tenantId String
referenceId String @unique
network String
amount BigInt
transactionType TransactionType
createdAt DateTime @default(now())
Expand Down
74 changes: 50 additions & 24 deletions web-portal/backend/src/utils/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { CustomPrismaService } from 'nestjs-prisma';
import { PrismaClient, TransactionType } from '@/.generated/client';
import { Cron, CronExpression } from '@nestjs/schedule';
import { parseAbiItem, fromHex, isAddress } from 'viem';
import { viemClient } from './viemClient';
import { opClient, baseClient, gnosisClient } from './viemClient';

interface IParsedLog {
tenantId: string;
amount: number;
referenceId: string;
transactionType: TransactionType;
network: string;
}

const portrAddress = '0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944';
Expand All @@ -22,7 +23,7 @@ export class UtilsService {
constructor(
@Inject('Postgres')
private prisma: CustomPrismaService<PrismaClient>, // <-- Inject the PrismaClient
) {}
) { }

async getChains() {
const chains = this.prisma.client.products.findMany({
Expand Down Expand Up @@ -168,39 +169,64 @@ export class UtilsService {

@Cron(CronExpression.EVERY_MINUTE)
async watchEvent() {
console.log('Getting Event Logs');
const blockNumber = await viemClient.getBlockNumber();
const logs = await viemClient.getLogs({
event,
address: portrAddress,
fromBlock: blockNumber - BigInt(1000),
toBlock: blockNumber,
});
console.log('Getting Event Logs from all clients');

// Define clients and their respective names
const clients = [
{ client: opClient, name: 'optimism' },
{ client: gnosisClient, name: 'gnosis' },
{ client: baseClient, name: 'base' }
];

// Fetch and parse logs for all clients
const allParsedLogs = await Promise.all(
clients.map(async ({ client, name }) => {
console.log(`Getting Event Logs from ${name}`);
const blockNumber = await client.getBlockNumber();
const logs = await client.getLogs({
event,
address: portrAddress,
fromBlock: blockNumber - BigInt(1000),
toBlock: blockNumber,
});

return this.parseLogs(logs, name);
})
);

const parsedLogs: IParsedLog[] = logs.map((log: any) => ({
tenantId: fromHex(log?.args?._identifier, 'string').replaceAll(
`\x00`,
'',
),
amount: Number(log?.args?._amount),
referenceId: log.transactionHash!,
transactionType: TransactionType.CREDIT!,
}));
const [parsedLogsOP, parsedLogsGnosis, parsedLogsBase] = allParsedLogs;

console.log({ parsedLogs });
console.log({ parsedLogsOP, parsedLogsGnosis, parsedLogsBase });

if (!parsedLogs) console.log('No New Redemptions');
if (!parsedLogsOP.length && !parsedLogsGnosis.length && !parsedLogsBase.length) {
console.log('No New Redemptions');
return;
}

// Create records for unique logs
const appliedLogs = await this.prisma.client.paymentLedger.createMany({
skipDuplicates: true,
data: parsedLogs,
data: [...parsedLogsOP, ...parsedLogsGnosis, ...parsedLogsBase],
});

console.log({ appliedLogs });

if (!appliedLogs) console.log('Error Applying logs');
if (!appliedLogs) {
console.log('Error Applying logs');
} else {
console.log('Applied New logs');
}
}

console.log('Applied New logs');
// Helper function to parse logs
parseLogs(logs: any[], network: string): IParsedLog[] {
return logs.map((log: any) => ({
tenantId: fromHex(log?.args?._identifier, 'string').replaceAll(`\x00`, ''),
amount: Number(log?.args?._amount),
referenceId: log.transactionHash!,
transactionType: TransactionType.CREDIT!,
network
}));
}

}
14 changes: 12 additions & 2 deletions web-portal/backend/src/utils/viemClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { createPublicClient, http } from 'viem';
import { optimism } from 'viem/chains';
import { optimism, base, gnosis } from 'viem/chains';

export const viemClient = createPublicClient({
export const opClient = createPublicClient({
chain: optimism,
transport: http(),
});

export const baseClient = createPublicClient({
chain: base,
transport: http(),
});

export const gnosisClient = createPublicClient({
chain: gnosis,
transport: http(),
});
4 changes: 2 additions & 2 deletions web-portal/frontend/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const supportedChains = [
id: "8543",
name: "base",
exchangeProxy: `0xdef1c0ded9bec7f1a1670819833240f027b25eff` as Address,
portrAddress: "to-be-deployed",
portrAddress: "0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944",
},
];

Expand Down Expand Up @@ -112,4 +112,4 @@ export const timeOptions = [
option: "30d",
format: "MM/dd",
},
];
];
1 change: 1 addition & 0 deletions web-portal/frontend/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface IBill {
createdAt?: string;
transactionType?: string;
productId?: string;
network?: string;
}

export interface IToken {
Expand Down

0 comments on commit 1fcdb19

Please sign in to comment.