Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move to nanoid for tenant, user, apps! #275

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web-portal/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"cookie-parser": "^1.4.6",
"date-fns": "^3.6.0",
"iron-session": "^8.0.1",
"nanoid": "3.3.4",
"nestjs-prisma": "^0.22.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
Expand Down
3 changes: 3 additions & 0 deletions web-portal/backend/src/apps/apps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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';
import { NANO_ID_LENGTH } from '../utils/const';

@Injectable()
export class AppsService {
Expand Down Expand Up @@ -83,6 +85,7 @@ export class AppsService {
if (!tenants) return;
const newApp = await this.prisma.client.app.create({
data: {
id: nanoid(NANO_ID_LENGTH),
tenantId: tenants[0].id,
name,
description,
Expand Down
3 changes: 3 additions & 0 deletions web-portal/backend/src/tenant/tenant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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'
import { NANO_ID_LENGTH } from '../utils/const';

@Injectable()
export class TenantService {
Expand All @@ -26,6 +28,7 @@ export class TenantService {

const tenant = await this.prisma.client.tenant.create({
data: {
id: nanoid(NANO_ID_LENGTH),
enterpriseId: enterprise.id,
secretKey: hashedKey,
},
Expand Down
3 changes: 3 additions & 0 deletions web-portal/backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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';
import { NANO_ID_LENGTH } from '../utils/const';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -64,6 +66,7 @@ export class UserService {
}
const newUser = await this.prisma.client.user.create({
data: {
id: nanoid(NANO_ID_LENGTH),
ethAddress: createHash('sha256').update(ethAddress).digest('hex'),
orgs: {
create: {
Expand Down
2 changes: 2 additions & 0 deletions web-portal/backend/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const NANO_ID_LENGTH = 10; // https://zelark.github.io/nano-id-cc/
export const PORTR_ADDRESS = '0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944';
10 changes: 5 additions & 5 deletions web-portal/backend/src/utils/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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;
referenceId: string;
transactionType: TransactionType;
}

const portrAddress = '0x54d5f8a0e0f06991e63e46420bcee1af7d9fe944';

const event = parseAbiItem(
'event Applied(bytes32 indexed _identifier, uint256 _amount)',
);
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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,
});
Expand Down
6 changes: 3 additions & 3 deletions web-portal/frontend/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
];

Expand Down