Skip to content

Commit

Permalink
Merge pull request #46 from GeneralMagicio/optimise-privado-api
Browse files Browse the repository at this point in the history
Optimise-privado-api
  • Loading branch information
aminlatifi authored Sep 4, 2024
2 parents dd69925 + ac8c51c commit 24b3766
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
SOLANA_DEVNET_NODE_RPC_URL: ${{ secrets.SOLANA_DEVNET_NODE_RPC_URL }}
SOLANA_MAINNET_NODE_RPC_URL: ${{ secrets.SOLANA_MAINNET_NODE_RPC_URL }}
MPETH_GRAPHQL_PRICES_URL: ${{ secrets.MPETH_GRAPHQL_PRICES_URL }}
PRIVADO_VERIFIER_NETWORK_ID: ${{ secrets.PRIVADO_VERIFIER_NETWORK_ID }}

publish:
needs: test
Expand Down Expand Up @@ -161,4 +162,4 @@ jobs:
git pull
docker compose -f docker-compose-production.yml pull
docker compose -f docker-compose-production.yml up -d
docker image prune -a --force
docker image prune -a --force
3 changes: 2 additions & 1 deletion .github/workflows/staging-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
SOLANA_DEVNET_NODE_RPC_URL: ${{ secrets.SOLANA_DEVNET_NODE_RPC_URL }}
SOLANA_MAINNET_NODE_RPC_URL: ${{ secrets.SOLANA_MAINNET_NODE_RPC_URL }}
MPETH_GRAPHQL_PRICES_URL: ${{ secrets.MPETH_GRAPHQL_PRICES_URL }}
PRIVADO_VERIFIER_NETWORK_ID: ${{ secrets.PRIVADO_VERIFIER_NETWORK_ID }}

publish:
needs: test
Expand Down Expand Up @@ -161,4 +162,4 @@ jobs:
git pull
docker compose -f docker-compose-staging.yml pull
docker compose -f docker-compose-staging.yml up -d
docker image prune -a --force
docker image prune -a --force
1 change: 0 additions & 1 deletion config/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ QACC_DONATION_TOKEN_NAME=
QACC_EARLY_ACCESS_ROUND_FINISH_TIMESTAMP=
ABC_LAUNCHER_ADAPTER=

PRIVADO_VERIFIER_ADAPTER=
PRIVADO_VERIFIER_NETWORK_ID=
PRIVADO_VERIFIER_CONTRACT_ADDRESS=
PRIVADO_REQUEST_ID=
3 changes: 1 addition & 2 deletions src/adapters/adaptersFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { SuperFluidAdapterInterface } from './superFluid/superFluidAdapterInterf
import { AbcLauncherAdapter } from './abcLauncher/abcLauncherAdapter';
import { AbcLauncherMockAdapter } from './abcLauncher/abcLauncherMockAdapter';
import { PrivadoAdapter } from './privado/privadoAdapter';
import { IPrivadoAdapter } from './privado/privadoAdapterInterface';

const discordAdapter = new DiscordAdapter();
const googleAdapter = new GoogleAdapter();
Expand Down Expand Up @@ -129,4 +128,4 @@ export const getAbcLauncherAdapter = () => {
}
};

export const privadoAdapter: IPrivadoAdapter = new PrivadoAdapter();
export const privadoAdapter = new PrivadoAdapter();
32 changes: 20 additions & 12 deletions src/adapters/privado/privadoAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { ethers } from 'ethers';
import config from '../../config';
import { getProvider } from '../../provider';
import { IPrivadoAdapter } from './privadoAdapterInterface';
import { findUserById } from '../../repositories/userRepository';
import { User } from '../../entities/user';
import { logger } from '../../utils/logger';
const PRIVADO_VERIFIER_NETWORK_ID = +config.get(
'PRIVADO_VERIFIER_NETWORK_ID',
) as number;
const PRIVADO_VERIFIER_CONTRACT_ADDRESS = config.get(
'PRIVADO_VERIFIER_CONTRACT_ADDRESS',
) as string;
const PRIVADO_REQUEST_ID = +config.get('PRIVADO_REQUEST_ID') as number;
export class PrivadoAdapter implements IPrivadoAdapter {
export class PrivadoAdapter {
private provider;

constructor() {
this.provider = getProvider(PRIVADO_VERIFIER_NETWORK_ID);
}

private async checkVerificationOnchain(address: string): Promise<boolean> {
const provider = getProvider(PRIVADO_VERIFIER_NETWORK_ID);
const abi = [
{
inputs: [
Expand All @@ -29,40 +35,42 @@ export class PrivadoAdapter implements IPrivadoAdapter {
const contract = new ethers.Contract(
PRIVADO_VERIFIER_CONTRACT_ADDRESS,
abi,
provider,
this.provider,
);
return contract.isProofVerified(address, this.privadoRequestId());
return contract.isProofVerified(address, PrivadoAdapter.privadoRequestId());
}

async checkUserVerified(userId: number): Promise<boolean> {
logger.debug('Checking Privado verification for user', { userId });

const user = await findUserById(userId);
const requestId = this.privadoRequestId();
if (!user || !user.walletAddress) {
throw new Error('No user or wallet address');
}

if (user.privadoVerifiedRequestIds.includes(requestId)) {
if (PrivadoAdapter.isUserVerified(user)) {
return true;
}

const response = await this.checkVerificationOnchain(user.walletAddress);
if (response) {
user.privadoVerifiedRequestIds = [
requestId,
PrivadoAdapter.privadoRequestId(),
...user.privadoVerifiedRequestIds,
];
await user.save();
}
return response;
}

async isUserVerified(userId: number): Promise<boolean> {
const user = await findUserById(userId);
static isUserVerified(user: User): boolean {
return (
user?.privadoVerifiedRequestIds.includes(this.privadoRequestId()) || false
user?.privadoVerifiedRequestIds.includes(
PrivadoAdapter.privadoRequestId(),
) || false
);
}
privadoRequestId(): number {
static privadoRequestId(): number {
return PRIVADO_REQUEST_ID;
}
}
5 changes: 0 additions & 5 deletions src/adapters/privado/privadoAdapterInterface.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ProjectStatusHistory } from './projectStatusHistory';
import { ProjectVerificationForm } from './projectVerificationForm';
import { ReferredEvent } from './referredEvent';
import { NOTIFICATIONS_EVENT_NAMES } from '../analytics/analytics';
import { PrivadoAdapter } from '../adapters/privado/privadoAdapter';

export const publicSelectionFields = [
'user.id',
Expand Down Expand Up @@ -207,6 +208,13 @@ export class User extends BaseEntity {
@Column('integer', { array: true, default: [] })
privadoVerifiedRequestIds: number[];

@Field(_type => Boolean, { nullable: true })
privadoVerified(): boolean {
return this.privadoVerifiedRequestIds.includes(
PrivadoAdapter.privadoRequestId(),
);
}

@Field(_type => Int, { nullable: true })
async donationsCount() {
return await Donation.createQueryBuilder('donation')
Expand Down
28 changes: 18 additions & 10 deletions src/resolvers/userResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from '../../test/testUtils';
import {
checkUserPrivadoVerifiedState,
isUserPrivadoVerified,
refreshUserScores,
updateUser,
userByAddress,
Expand All @@ -30,6 +29,7 @@ import { getGitcoinAdapter, privadoAdapter } from '../adapters/adaptersFactory';
import { updateUserTotalDonated } from '../services/userService';
import { getUserEmailConfirmationFields } from '../repositories/userRepository';
import { UserEmailVerification } from '../entities/userEmailVerification';
import { PrivadoAdapter } from '../adapters/privado/privadoAdapter';

describe('updateUser() test cases', updateUserTestCases);
describe('userByAddress() test cases', userByAddressTestCases);
Expand Down Expand Up @@ -905,12 +905,15 @@ function checkUserPrivadoVerfiedStateTestCases() {
await user.save();

const accessToken = await generateTestAccessToken(user.id);
sinon.stub(privadoAdapter, 'privadoRequestId').returns(2);
sinon.stub(PrivadoAdapter, 'privadoRequestId').returns(2);

const result = await axios.post(
graphqlUrl,
{
query: isUserPrivadoVerified,
query: userByAddress,
variables: {
address: user.walletAddress,
},
},
{
headers: {
Expand All @@ -919,7 +922,8 @@ function checkUserPrivadoVerfiedStateTestCases() {
},
);

assert.isTrue(result.data.data.isUserPrivadoVerified);
assert.isOk(result.data.data.userByAddress);
assert.isTrue(result.data.data.userByAddress.privadoVerified);
});

it('should return false if the user does not has request privadoVerifiedRequestIds', async () => {
Expand All @@ -928,12 +932,15 @@ function checkUserPrivadoVerfiedStateTestCases() {
await user.save();

const accessToken = await generateTestAccessToken(user.id);
sinon.stub(privadoAdapter, 'privadoRequestId').returns(4);
sinon.stub(PrivadoAdapter, 'privadoRequestId').returns(4);

const result = await axios.post(
graphqlUrl,
{
query: isUserPrivadoVerified,
query: userByAddress,
variables: {
address: user.walletAddress,
},
},
{
headers: {
Expand All @@ -942,7 +949,8 @@ function checkUserPrivadoVerfiedStateTestCases() {
},
);

assert.isFalse(result.data.data.isUserPrivadoVerified);
assert.isOk(result.data.data.userByAddress);
assert.isFalse(result.data.data.userByAddress.privadoVerified);
});

it('should add request ID to privadoVerifiedRequestIds if user is verified', async () => {
Expand All @@ -952,7 +960,7 @@ function checkUserPrivadoVerfiedStateTestCases() {

const accessToken = await generateTestAccessToken(user.id);
sinon.stub(privadoAdapter, 'checkVerificationOnchain').returns(true);
sinon.stub(privadoAdapter, 'privadoRequestId').returns(4);
sinon.stub(PrivadoAdapter, 'privadoRequestId').returns(4);

const result = await axios.post(
graphqlUrl,
Expand Down Expand Up @@ -984,7 +992,7 @@ function checkUserPrivadoVerfiedStateTestCases() {

const accessToken = await generateTestAccessToken(user.id);
sinon.stub(privadoAdapter, 'checkVerificationOnchain').returns(false);
sinon.stub(privadoAdapter, 'privadoRequestId').returns(4);
sinon.stub(PrivadoAdapter, 'privadoRequestId').returns(4);

const result = await axios.post(
graphqlUrl,
Expand Down Expand Up @@ -1017,7 +1025,7 @@ function checkUserPrivadoVerfiedStateTestCases() {

const accessToken = await generateTestAccessToken(user.id);
sinon.stub(privadoAdapter, 'checkVerificationOnchain').returns(true);
sinon.stub(privadoAdapter, 'privadoRequestId').returns(2);
sinon.stub(PrivadoAdapter, 'privadoRequestId').returns(2);

const result = await axios.post(
graphqlUrl,
Expand Down
10 changes: 0 additions & 10 deletions src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,6 @@ export class UserResolver {
}
}

@Query(_return => Boolean)
async isUserPrivadoVerified(
@Ctx() { req: { user } }: ApolloContext,
): Promise<boolean> {
if (!user)
throw new Error(
i18n.__(translationErrorMessagesKeys.AUTHENTICATION_REQUIRED),
);
return await privadoAdapter.isUserVerified(user.userId);
}
@Mutation(_returns => Boolean)
async checkUserPrivadoVerifiedState(
@Ctx() { req: { user } }: ApolloContext,
Expand Down
6 changes: 1 addition & 5 deletions test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ export const userByAddress = `
projectsCount
passportScore
passportStamps
privadoVerified
}
}
`;
Expand Down Expand Up @@ -2062,11 +2063,6 @@ export const fetchActiveEarlyAccessRoundQuery = `
}
`;

export const isUserPrivadoVerified = `
query {
isUserPrivadoVerified
}
`;
export const checkUserPrivadoVerifiedState = `
mutation {
checkUserPrivadoVerifiedState
Expand Down

0 comments on commit 24b3766

Please sign in to comment.