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

Add gitcoin adapter for integration #1006

Merged
merged 6 commits into from
May 29, 2023
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
5 changes: 5 additions & 0 deletions config/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,8 @@ OPTIMISM_NODE_HTTP_URL=https://optimism-mainnet.public.blastapi.io/
ENABLE_INSTANT_BOOSTING_UPDATE=true
# OPTIONAL - default: Every 5 minutes
INSTANT_BOOSTING_UPDATE_CRONJOB_EXPRESSION=0 */5 * * * *

// Gitcoin API
GITCOIN_ADAPTER=mock
GITCOIN_PASSPORT_API=
GITCOIN_SCORER_ID=
2 changes: 2 additions & 0 deletions config/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ DONATION_VERIFICAITON_EXPIRATION_HOURS=24
# We need it for monoswap
POLYGON_MAINNET_NODE_HTTP_URL=https://polygon-rpc.com
OPTIMISM_NODE_HTTP_URL=https://optimism-mainnet.public.blastapi.io

GITCOIN_ADAPTER=mock
31 changes: 31 additions & 0 deletions migration/1685248368673-AddPassportScoreAndStampssToUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AddPassportScoreAndStampssToUser1685248368673
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn(
'user',
new TableColumn({
name: 'passportStamps',
type: 'integer',
isNullable: true,
}),
);

await queryRunner.addColumn(
'user',
new TableColumn({
name: 'passportScore',
type: 'integer',
default: false,
isNullable: true,
}),
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumn('user', 'passportStamps');
await queryRunner.dropColumn('user', 'passportScore');
}
}
16 changes: 16 additions & 0 deletions src/adapters/adaptersFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { GivPowerSubgraphAdapterMock } from './givpowerSubgraph/givPowerSubgraph
import { ChainvineAdapter } from './chainvine/chainvineAdapter';
import { ChainvineMockAdapter } from './chainvine/chainvineMockAdapter';
import { IGivPowerSubgraphAdapter } from './givpowerSubgraph/IGivPowerSubgraphAdapter';
import { GitcoinAdapter } from './gitcoin/gitcoinAdapter';
import { GitcoinMockAdapter } from './gitcoin/gitcoinMockAdapter';

const discordAdapter = new DiscordAdapter();
const googleAdapter = new GoogleAdapter();
Expand Down Expand Up @@ -85,3 +87,17 @@ export const getChainvineAdapter = () => {
return mockChainvineAdapter;
}
};

const gitcoinAdapter = new GitcoinAdapter();
const mockGitcoinAdapter = new GitcoinMockAdapter();

export const getGitcoinAdapter = () => {
switch (process.env.GITCOIN_ADAPTER) {
case 'gitcoin':
return gitcoinAdapter;
case 'mock':
return mockGitcoinAdapter;
default:
return mockGitcoinAdapter;
}
};
148 changes: 148 additions & 0 deletions src/adapters/gitcoin/gitcoinAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import axios from 'axios';
import {
GitcoinAdapterInterface,
SigningMessageAndNonceResponse,
SubmitPassportInput,
SubmittedPassportResponse,
SubmittedPassportsResponse,
GetPassportStampsResponse,
} from './gitcoinAdapterInterface';
import { logger } from '../../utils/logger';
import { i18n, translationErrorMessagesKeys } from '../../utils/errorMessages';

const GITCOIN_API_BASE_URL = 'https://api.scorer.gitcoin.co';

export class GitcoinAdapter implements GitcoinAdapterInterface {
private GitcoinApiKey;
private ScorerID;

constructor() {
this.GitcoinApiKey = process.env.GITCOIN_PASSPORT_API || '';
this.ScorerID = process.env.GITCOIN_SCORER_ID || '';
}

async getWalletAddressScore(
address: string,
): Promise<SubmittedPassportResponse> {
try {
const result = await axios.get(
`${GITCOIN_API_BASE_URL}/registry/score/${this.ScorerID}/${address}`,
{
headers: {
'X-API-KEY': this.GitcoinApiKey,
},
},
);

if (
result.data.error !== undefined ||
result.data.error !== null ||
result.data.error !== ''
) {
logger.error('getWalletAddressScore error', result.data.error);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
return result.data;
} catch (e) {
logger.error('getWalletAddressScore error', e);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
}

async getListOfScores(): Promise<SubmittedPassportsResponse> {
try {
const result = await axios.get(
`${GITCOIN_API_BASE_URL}/registry/score/${this.ScorerID}`,
{
headers: {
'X-API-KEY': this.GitcoinApiKey,
},
},
);
return result.data;
} catch (e) {
logger.error('getListOfScores error', e);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
}

async getSigningMessageAndNonce(): Promise<SigningMessageAndNonceResponse> {
try {
const result = await axios.get(
`${GITCOIN_API_BASE_URL}/registry/signing-message`,
{
headers: {
'X-API-KEY': this.GitcoinApiKey,
},
},
);
return result.data;
} catch (e) {
logger.error('getSigningMessageAndNonce error', e);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
}

async submitPassport(
params: SubmitPassportInput,
): Promise<SubmittedPassportResponse> {
try {
const result = await axios.post(
`${GITCOIN_API_BASE_URL}/registry/submit-passport`,
{
variables: params,
},
{
headers: {
'X-API-KEY': this.GitcoinApiKey,
Accept: 'application/json',
'Content-Type': 'application/json',
},
},
);
if (
result.data.error !== undefined ||
result.data.error !== null ||
result.data.error !== ''
) {
logger.error('submitPassport error', result.data.error);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
return result.data;
} catch (e) {
logger.error('submitPassport error', e);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
}

async getPassportStamps(address: string): Promise<GetPassportStampsResponse> {
try {
const result = await axios.get(
`${GITCOIN_API_BASE_URL}/registry/stamps/${address}`,
{
headers: {
'X-API-KEY': this.GitcoinApiKey,
},
},
);
return result.data;
} catch (e) {
logger.error('getPassportStamps error', e);
throw new Error(
i18n.__(translationErrorMessagesKeys.GITCOIN_ERROR_FETCHING_DATA),
);
}
}
}
46 changes: 46 additions & 0 deletions src/adapters/gitcoin/gitcoinAdapterInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export interface SigningMessageAndNonceResponse {
message: string;
nonce: string;
}

export interface SubmitPassportInput {
address: string;
scorer: string;
signature: string;
nonce: string;
}

export interface SubmittedPassportResponse {
address: string;
score: string | undefined | null;
status: string;
last_score_timestamp: string;
evidence: any | undefined | null;
error: string | undefined | null;
}

export interface SubmittedPassportsResponse {
items: SubmittedPassportResponse[];
count: number;
}

interface StampInterface {
version: string;
credential: any;
}

export interface GetPassportStampsResponse {
next: string | null | undefined;
prev: string | null | undefined;
items: StampInterface[];
}

export interface GitcoinAdapterInterface {
getWalletAddressScore(address: string): Promise<SubmittedPassportResponse>;
getListOfScores(): Promise<SubmittedPassportsResponse>;
getSigningMessageAndNonce(): Promise<SigningMessageAndNonceResponse>;
submitPassport(
params: SubmitPassportInput,
): Promise<SubmittedPassportResponse>;
getPassportStamps(address: string): Promise<GetPassportStampsResponse>;
}
96 changes: 96 additions & 0 deletions src/adapters/gitcoin/gitcoinMockAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { generateRandomEtheriumAddress } from '../../../test/testUtils';
import {
GitcoinAdapterInterface,
SigningMessageAndNonceResponse,
SubmitPassportInput,
SubmittedPassportResponse,
SubmittedPassportsResponse,
GetPassportStampsResponse,
} from './gitcoinAdapterInterface';

export const cachedReferralAddresses = {};

export class GitcoinMockAdapter implements GitcoinAdapterInterface {
async getWalletAddressScore(
address: string,
): Promise<SubmittedPassportResponse> {
if (cachedReferralAddresses[address]) {
return Promise.resolve({
address,
score: '10',
status: 'ok',
last_score_timestamp: 'string',
evidence: undefined,
error: undefined,
});
}
return Promise.resolve({
address,
score: undefined,
status: 'Error',
last_score_timestamp: 'string',
evidence: undefined,
error: 'Invalid address',
});
}
async getListOfScores(): Promise<SubmittedPassportsResponse> {
return Promise.resolve({
items: [
{
address: 'string',
score: 'string',
status: 'string',
last_score_timestamp: 'string',
evidence: undefined,
error: undefined,
},
],
count: 1,
});
}
async getSigningMessageAndNonce(): Promise<SigningMessageAndNonceResponse> {
return Promise.resolve({
message: 'string',
nonce: 'string',
});
}
// Use this method to register in the cache the address for testing
async submitPassport(
params: SubmitPassportInput,
): Promise<SubmittedPassportResponse> {
if (!cachedReferralAddresses[params.address]) {
cachedReferralAddresses[params.address] = params.address;
}
return Promise.resolve({
address: 'string',
score: 'string',
status: 'string',
last_score_timestamp: 'string',
evidence: undefined,
error: undefined,
});
}
async getPassportStamps(address: string): Promise<GetPassportStampsResponse> {
if (cachedReferralAddresses[address]) {
return Promise.resolve({
next: 'string',
prev: 'string',
items: [
{
version: '1',
credential: 'any',
},
{
version: '1',
credential: 'any',
},
],
});
}
return Promise.resolve({
next: null,
prev: null,
items: [],
});
}
}
8 changes: 8 additions & 0 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export class User extends BaseEntity {
@Column({ nullable: true })
url?: string;

@Field(type => Number, { nullable: true })
@Column({ nullable: true, default: null })
passportScore?: number;

@Field(type => Number, { nullable: true })
@Column({ nullable: true, default: null })
passportStamps?: number;

@Field(type => String, { nullable: true })
@Column({ nullable: true })
location?: string;
Expand Down
Loading