From 1ded77ba912c0acf42f55300b02b6106067c4225 Mon Sep 17 00:00:00 2001 From: memosys <82053242+memosys@users.noreply.github.com> Date: Wed, 22 May 2024 09:21:16 +0530 Subject: [PATCH 1/4] move to nanoid for tenant, user, apps! --- web-portal/backend/package.json | 1 + web-portal/backend/src/apps/apps.service.ts | 2 ++ web-portal/backend/src/tenant/tenant.service.ts | 2 ++ web-portal/backend/src/user/user.service.ts | 2 ++ 4 files changed, 7 insertions(+) diff --git a/web-portal/backend/package.json b/web-portal/backend/package.json index 2fc96a08..c5b3ebdd 100644 --- a/web-portal/backend/package.json +++ b/web-portal/backend/package.json @@ -37,6 +37,7 @@ "cookie-parser": "^1.4.6", "date-fns": "^3.6.0", "iron-session": "^8.0.1", + "nanoid": "^5.0.7", "nestjs-prisma": "^0.22.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", diff --git a/web-portal/backend/src/apps/apps.service.ts b/web-portal/backend/src/apps/apps.service.ts index cc9268db..bd774cc3 100644 --- a/web-portal/backend/src/apps/apps.service.ts +++ b/web-portal/backend/src/apps/apps.service.ts @@ -4,6 +4,7 @@ import { AppRule, PrismaClient, Tenant, RuleType } from '@/.generated/client'; import { UserService } from '../user/user.service'; import { createHash, randomBytes } from 'crypto'; import { Request } from 'express'; +import { nanoid } from 'nanoid'; @Injectable() export class AppsService { @@ -83,6 +84,7 @@ export class AppsService { if (!tenants) return; const newApp = await this.prisma.client.app.create({ data: { + id: nanoid(10), tenantId: tenants[0].id, name, description, diff --git a/web-portal/backend/src/tenant/tenant.service.ts b/web-portal/backend/src/tenant/tenant.service.ts index 849f50d7..13dd289f 100644 --- a/web-portal/backend/src/tenant/tenant.service.ts +++ b/web-portal/backend/src/tenant/tenant.service.ts @@ -2,6 +2,7 @@ import { Injectable, Inject, HttpException, HttpStatus } from '@nestjs/common'; import { CustomPrismaService } from 'nestjs-prisma'; import { PrismaClient } from '@/.generated/client'; import { createHash, randomBytes } from 'crypto'; +import { nanoid } from 'nanoid' @Injectable() export class TenantService { @@ -26,6 +27,7 @@ export class TenantService { const tenant = await this.prisma.client.tenant.create({ data: { + id: nanoid(10), enterpriseId: enterprise.id, secretKey: hashedKey, }, diff --git a/web-portal/backend/src/user/user.service.ts b/web-portal/backend/src/user/user.service.ts index 27afc501..e6c406eb 100644 --- a/web-portal/backend/src/user/user.service.ts +++ b/web-portal/backend/src/user/user.service.ts @@ -6,6 +6,7 @@ import { TenantService } from '../tenant/tenant.service'; import { unsealData } from 'iron-session'; import { Request } from 'express'; import { ISession, SESSION_OPTIONS } from '../siwe/siwe.service'; +import { nanoid } from 'nanoid'; @Injectable() export class UserService { @@ -64,6 +65,7 @@ export class UserService { } const newUser = await this.prisma.client.user.create({ data: { + id: nanoid(10), ethAddress: createHash('sha256').update(ethAddress).digest('hex'), orgs: { create: { From 9e300f13ae378224c683fa332d0778b29d1ea133 Mon Sep 17 00:00:00 2001 From: memosys <82053242+memosys@users.noreply.github.com> Date: Wed, 22 May 2024 11:29:28 +0530 Subject: [PATCH 2/4] added types --- web-portal/backend/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/web-portal/backend/package.json b/web-portal/backend/package.json index c5b3ebdd..444de92c 100644 --- a/web-portal/backend/package.json +++ b/web-portal/backend/package.json @@ -52,6 +52,7 @@ "@types/cookie-parser": "^1.4.6", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", + "@types/nanoid": "^3.0.0", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^6.0.0", From 3b55ef6fce202a77a5b22a0f3c108e0b5bad0d14 Mon Sep 17 00:00:00 2001 From: memosys <82053242+memosys@users.noreply.github.com> Date: Wed, 22 May 2024 11:49:48 +0530 Subject: [PATCH 3/4] fix es compatible version! --- web-portal/backend/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web-portal/backend/package.json b/web-portal/backend/package.json index 444de92c..97c3d66f 100644 --- a/web-portal/backend/package.json +++ b/web-portal/backend/package.json @@ -37,7 +37,7 @@ "cookie-parser": "^1.4.6", "date-fns": "^3.6.0", "iron-session": "^8.0.1", - "nanoid": "^5.0.7", + "nanoid": "3.3.4", "nestjs-prisma": "^0.22.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", @@ -52,7 +52,6 @@ "@types/cookie-parser": "^1.4.6", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", - "@types/nanoid": "^3.0.0", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^6.0.0", From dac69a2b5de886ebda13e18bf6dc0f667029e2d8 Mon Sep 17 00:00:00 2001 From: memosys <82053242+memosys@users.noreply.github.com> Date: Wed, 22 May 2024 22:50:01 +0530 Subject: [PATCH 4/4] updated const! --- web-portal/backend/src/apps/apps.service.ts | 3 ++- web-portal/backend/src/tenant/tenant.service.ts | 3 ++- web-portal/backend/src/user/user.service.ts | 3 ++- web-portal/backend/src/utils/const.ts | 2 ++ web-portal/backend/src/utils/utils.service.ts | 10 +++++----- web-portal/frontend/utils/consts.ts | 6 +++--- 6 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 web-portal/backend/src/utils/const.ts diff --git a/web-portal/backend/src/apps/apps.service.ts b/web-portal/backend/src/apps/apps.service.ts index bd774cc3..e17a2ab2 100644 --- a/web-portal/backend/src/apps/apps.service.ts +++ b/web-portal/backend/src/apps/apps.service.ts @@ -5,6 +5,7 @@ import { UserService } from '../user/user.service'; import { createHash, randomBytes } from 'crypto'; import { Request } from 'express'; import { nanoid } from 'nanoid'; +import { NANO_ID_LENGTH } from '../utils/const'; @Injectable() export class AppsService { @@ -84,7 +85,7 @@ export class AppsService { if (!tenants) return; const newApp = await this.prisma.client.app.create({ data: { - id: nanoid(10), + id: nanoid(NANO_ID_LENGTH), tenantId: tenants[0].id, name, description, diff --git a/web-portal/backend/src/tenant/tenant.service.ts b/web-portal/backend/src/tenant/tenant.service.ts index 13dd289f..f8e4c215 100644 --- a/web-portal/backend/src/tenant/tenant.service.ts +++ b/web-portal/backend/src/tenant/tenant.service.ts @@ -3,6 +3,7 @@ import { CustomPrismaService } from 'nestjs-prisma'; import { PrismaClient } from '@/.generated/client'; import { createHash, randomBytes } from 'crypto'; import { nanoid } from 'nanoid' +import { NANO_ID_LENGTH } from '../utils/const'; @Injectable() export class TenantService { @@ -27,7 +28,7 @@ export class TenantService { const tenant = await this.prisma.client.tenant.create({ data: { - id: nanoid(10), + id: nanoid(NANO_ID_LENGTH), enterpriseId: enterprise.id, secretKey: hashedKey, }, diff --git a/web-portal/backend/src/user/user.service.ts b/web-portal/backend/src/user/user.service.ts index e6c406eb..7cb60f93 100644 --- a/web-portal/backend/src/user/user.service.ts +++ b/web-portal/backend/src/user/user.service.ts @@ -7,6 +7,7 @@ import { unsealData } from 'iron-session'; import { Request } from 'express'; import { ISession, SESSION_OPTIONS } from '../siwe/siwe.service'; import { nanoid } from 'nanoid'; +import { NANO_ID_LENGTH } from '../utils/const'; @Injectable() export class UserService { @@ -65,7 +66,7 @@ export class UserService { } const newUser = await this.prisma.client.user.create({ data: { - id: nanoid(10), + id: nanoid(NANO_ID_LENGTH), ethAddress: createHash('sha256').update(ethAddress).digest('hex'), orgs: { create: { diff --git a/web-portal/backend/src/utils/const.ts b/web-portal/backend/src/utils/const.ts new file mode 100644 index 00000000..7525baed --- /dev/null +++ b/web-portal/backend/src/utils/const.ts @@ -0,0 +1,2 @@ +export const NANO_ID_LENGTH = 10; // https://zelark.github.io/nano-id-cc/ +export const PORTR_ADDRESS = '0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944'; diff --git a/web-portal/backend/src/utils/utils.service.ts b/web-portal/backend/src/utils/utils.service.ts index f5633591..0922799e 100644 --- a/web-portal/backend/src/utils/utils.service.ts +++ b/web-portal/backend/src/utils/utils.service.ts @@ -4,7 +4,7 @@ import { PrismaClient, TransactionType } from '@/.generated/client'; import { Cron, CronExpression } from '@nestjs/schedule'; import { parseAbiItem, fromHex, isAddress } from 'viem'; import { opClient, baseClient, gnosisClient } from './viemClient'; - +import { PORTR_ADDRESS } from './const'; interface IParsedLog { tenantId: string; amount: number; @@ -12,7 +12,7 @@ interface IParsedLog { transactionType: TransactionType; } -const portrAddress = '0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944'; + const event = parseAbiItem( 'event Applied(bytes32 indexed _identifier, uint256 _amount)', ); @@ -121,7 +121,7 @@ export class UtilsService { throw new HttpException(`Could not fetch quote`, HttpStatus.BAD_REQUEST); } const res = await fetch( - `https://${chainName}.api.0x.org/swap/v1/quote?sellToken=${sellToken}&buyToken=${portrAddress}&sellAmount=${sellAmount}`, + `https://${chainName}.api.0x.org/swap/v1/quote?sellToken=${sellToken}&buyToken=${PORTR_ADDRESS}&sellAmount=${sellAmount}`, { headers: { Accept: 'application/json', @@ -148,7 +148,7 @@ export class UtilsService { } const res = await fetch( - `https://${chainName}.api.0x.org/swap/v1/price?sellToken=${sellToken}&buyToken=${portrAddress}&sellAmount=${sellAmount}`, + `https://${chainName}.api.0x.org/swap/v1/price?sellToken=${sellToken}&buyToken=${PORTR_ADDRESS}&sellAmount=${sellAmount}`, { headers: { Accept: 'application/json', @@ -184,7 +184,7 @@ export class UtilsService { const blockNumber = await client.getBlockNumber(); const logs = await client.getLogs({ event, - address: portrAddress, + address: PORTR_ADDRESS, fromBlock: blockNumber - BigInt(1000), toBlock: blockNumber, }); diff --git a/web-portal/frontend/utils/consts.ts b/web-portal/frontend/utils/consts.ts index 4d60225a..7c7f4c57 100644 --- a/web-portal/frontend/utils/consts.ts +++ b/web-portal/frontend/utils/consts.ts @@ -52,19 +52,19 @@ export const supportedChains = [ id: "10", name: "optimism", exchangeProxy: `0xdef1abe32c034e558cdd535791643c58a13acc10` as Address, - portrAddress: "0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944" as Address, + portrAddress, }, { id: "8543", name: "base", exchangeProxy: `0xdef1c0ded9bec7f1a1670819833240f027b25eff` as Address, - portrAddress: "0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944", + portrAddress }, { id: "100", name: "gnosis", // exchangeProxy: `0xdef1c0ded9bec7f1a1670819833240f027b25eff` as Address, - portrAddress: "0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944", + portrAddress }, ];