Skip to content

Commit

Permalink
PLT-1397 Added Mint POAP on POAP package (#60)
Browse files Browse the repository at this point in the history
* PLT-1397 Added provider

* PLT-1397 Added intreface in POAP Client to claim sync

* PLT-1397 Updated yarn

* Added different ways to claim poaps

* Eslint fix

* Changed Camel Case to pascalCase

* Changed pascalCase to CamelCase

* Rename Status to MintingStatus

* Eslint fix

* Eslint fix

* Eslint fix

* Eslint fix

* Eslint fix

* PLT-1397 changed how to create poapTokenApi

* PLT-1397 Some documentation and variable refactor

* PLT-1397 Refactor code

* Changed camel case

* Updated yarn

* Updated test

* Changed name

* Changed name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed variable name

* Changed names

* Changed version
  • Loading branch information
rlajous authored Sep 25, 2023
1 parent f960384 commit bdbd9a8
Show file tree
Hide file tree
Showing 62 changed files with 1,239 additions and 238 deletions.
4 changes: 4 additions & 0 deletions examples/drops/backend/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CLIENT_ID=
CLIENT_SECRET=
API_KEY=
OAUTH_SERVER_DOMAIN=
1 change: 1 addition & 0 deletions examples/drops/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@poap-xyz/utils": "*",
"@types/node": "^18.16.0",
"axios": "^1.3.5",
"dotenv": "^16.0.3",
"form-data": "^4.0.0",
"stream": "^0.0.2"
},
Expand Down
9 changes: 7 additions & 2 deletions examples/drops/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { PoapCompass, PoapDropApi } from '@poap-xyz/providers';
import { fetch_multiple_drops } from './methods/fetch_multiple_drops';
import { fetch_single_drop } from './methods/fetch_single_drop';
import { create_drop } from './methods/create_drop';
import { getRequiredEnvVar } from './methods/get_required_env_var';

import dotenv from 'dotenv';

dotenv.config();

async function main(): Promise<void> {
// Use your library here
const client = new DropsClient(
new PoapCompass('you_api_key'),
new PoapDropApi('your_api_key'),
new PoapCompass({ apiKey: getRequiredEnvVar('API_KEY') }),
new PoapDropApi({ apiKey: getRequiredEnvVar('API_KEY') }),
);
// Multiple Drops
await measurePerformance(
Expand Down
18 changes: 9 additions & 9 deletions examples/drops/backend/src/methods/create_drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ export const create_drop = async (client: DropsClient): Promise<void> => {
description: 'Description',
city: 'Buenos Aires',
country: 'Argentina',
start_date: toPOAPdate(today),
end_date: toPOAPdate(oneMonthFromToday),
expiry_date: toPOAPdate(twoMonthsFromToday),
event_url: 'https://poap.xyz/',
virtual_event: true,
secret_code: '123456',
startDate: toPOAPdate(today),
endDate: toPOAPdate(oneMonthFromToday),
expiryDate: toPOAPdate(twoMonthsFromToday),
eventUrl: 'https://poap.xyz/',
virtualEvent: true,
secretCode: '123456',
image: await fs.promises.readFile('src/assets/poap.png'),
filename: 'file.png',
contentType: 'image/png',
event_template_id: 1,
eventTemplateId: 1,
email: '[email protected]',
requested_codes: 10,
private_event: true,
requestedCodes: 10,
privateEvent: true,
};

try {
Expand Down
4 changes: 2 additions & 2 deletions examples/drops/backend/src/methods/fetch_multiple_drops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const fetch_multiple_drops = async (
): Promise<void> => {
try {
const data: PaginatedResult<Drop> = await client.fetch({
sort_field: DropsSortFields.Id,
sort_dir: Order.ASC,
sortField: DropsSortFields.Id,
sortDir: Order.ASC,
limit: 10,
offset: 1,
});
Expand Down
4 changes: 2 additions & 2 deletions examples/drops/backend/src/methods/fetch_single_drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Order, PaginatedResult } from '@poap-xyz/utils';
export const fetch_single_drop = async (client: DropsClient): Promise<void> => {
try {
const data: PaginatedResult<Drop> = await client.fetch({
sort_field: DropsSortFields.Id,
sort_dir: Order.ASC,
sortField: DropsSortFields.Id,
sortDir: Order.ASC,
limit: 10,
offset: 0,
ids: [1],
Expand Down
7 changes: 7 additions & 0 deletions examples/drops/backend/src/methods/get_required_env_var.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getRequiredEnvVar = (envVarName: string): string => {
const envVar = process.env[envVarName];
if (envVar === undefined) {
throw new Error(`Environment variable ${envVarName} is required`);
}
return envVar;
};
5 changes: 5 additions & 0 deletions examples/poaps/backend/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CLIENT_ID=
CLIENT_SECRET=
API_KEY=
OAUTH_SERVER_DOMAIN=
POAP_TOKEN_BASE_URL=https://api.poap.tech
1 change: 1 addition & 0 deletions examples/poaps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@poap-xyz/utils": "*",
"@types/node": "^18.16.0",
"axios": "^1.3.5",
"dotenv": "^16.0.3",
"form-data": "^4.0.0",
"perf_hooks": "^0.0.1",
"stream": "^0.0.2"
Expand Down
37 changes: 35 additions & 2 deletions examples/poaps/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { measurePerformance } from '@poap-xyz/performance';
import { PoapsClient } from '@poap-xyz/poaps';
import { PoapCompass } from '@poap-xyz/providers';
import {
AuthenticationProviderHttp,
PoapCompass,
PoapTokenApi,
} from '@poap-xyz/providers';
import { fetch_multiple_poaps } from './methods/fetch_multiple_poaps';
import { fetch_single_poap } from './methods/fetch_single_poap';
import { fetch_multiple_poaps_by_collector } from './methods/fetch_multiple_poaps_by_collector';
import { fetch_multiple_poaps_by_drop_id } from './methods/fetch_multiple_poaps_by_drop_id';
import { mint_sync_poap } from './methods/mint_sync_poap';
import { getRequiredEnvVar } from './methods/get_required_env_var';
import { mint_async_poap } from './methods/mint_async_poap';
import { email_reservation_poap } from './methods/email_reservation_poap';
import dotenv from 'dotenv';

dotenv.config();

async function main(): Promise<void> {
// Use your library here
const client = new PoapsClient(new PoapCompass('you_api_key'));
const client = new PoapsClient(
new PoapCompass({
apiKey: getRequiredEnvVar('API_KEY'),
}),
new PoapTokenApi({
apiKey: getRequiredEnvVar('API_KEY'),
baseUrl: getRequiredEnvVar('POAP_TOKEN_BASE_URL'),
authenticationProvider: new AuthenticationProviderHttp(
getRequiredEnvVar('CLIENT_ID'),
getRequiredEnvVar('CLIENT_SECRET'),
getRequiredEnvVar('OAUTH_SERVER_DOMAIN'),
),
}),
);
// Multiple Poaps
await measurePerformance(
() => fetch_multiple_poaps(client),
Expand All @@ -29,6 +53,15 @@ async function main(): Promise<void> {
() => fetch_multiple_poaps_by_drop_id(client),
'fetch_multiple_poaps_by_drop_id',
);
// mint Sync Poap
await measurePerformance(() => mint_sync_poap(client), 'mint_sync_poap');
// mint Async Poap
await measurePerformance(() => mint_async_poap(client), 'mint_async_poap');
// Email Reservation Poap
await measurePerformance(
() => email_reservation_poap(client),
'email_reservation_poap',
);
}

main().catch((error) => {
Expand Down
17 changes: 17 additions & 0 deletions examples/poaps/backend/src/methods/email_reservation_poap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PoapsClient, POAPReservation } from '@poap-xyz/poaps';
import { handleError } from '../utils/handleError';

export const email_reservation_poap = async (
client: PoapsClient,
): Promise<void> => {
try {
const data: POAPReservation = await client.emailReservation({
mintCode: 'your_poap_code',
email: '[email protected]',
sendEmail: true,
});
console.log(data);
} catch (error) {
handleError(error);
}
};
4 changes: 2 additions & 2 deletions examples/poaps/backend/src/methods/fetch_multiple_poaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const fetch_multiple_poaps = async (
): Promise<void> => {
try {
const data: PaginatedResult<POAP> = await client.fetch({
sort_field: PoapsSortFields.MintedOn,
sort_dir: Order.ASC,
sortField: PoapsSortFields.MintedOn,
sortDir: Order.ASC,
limit: 10,
offset: 0,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const fetch_multiple_poaps_by_collector = async (
): Promise<void> => {
try {
const data: PaginatedResult<POAP> = await client.fetch({
sort_field: PoapsSortFields.MintedOn,
sort_dir: Order.ASC,
sortField: PoapsSortFields.MintedOn,
sortDir: Order.ASC,
limit: 10,
offset: 0,
collector_address: '0xf6B6F07862A02C85628B3A9688beae07fEA9C863',
collectorAddress: '0xf6B6F07862A02C85628B3A9688beae07fEA9C863',
});
console.log(data);
console.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export const fetch_multiple_poaps_by_drop_id = async (
): Promise<void> => {
try {
const data: PaginatedResult<POAP> = await client.fetch({
sort_field: PoapsSortFields.MintedOn,
sort_dir: Order.DESC,
sortField: PoapsSortFields.MintedOn,
sortDir: Order.DESC,
limit: 10,
offset: 0,
drop_id: 3,
filter_by_zero_address: true,
dropId: 3,
filterByZeroAddress: true,
});
console.log(data);
console.log('The first 10 POAP tokens minted for the drop 14.');
Expand Down
4 changes: 2 additions & 2 deletions examples/poaps/backend/src/methods/fetch_single_poap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { PaginatedResult, Order } from '@poap-xyz/utils';
export const fetch_single_poap = async (client: PoapsClient): Promise<void> => {
try {
const data: PaginatedResult<POAP> = await client.fetch({
sort_field: PoapsSortFields.MintedOn,
sort_dir: Order.ASC,
sortField: PoapsSortFields.MintedOn,
sortDir: Order.ASC,
limit: 10,
offset: 0,
ids: [1],
Expand Down
7 changes: 7 additions & 0 deletions examples/poaps/backend/src/methods/get_required_env_var.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getRequiredEnvVar = (envVarName: string): string => {
const envVar = process.env[envVarName];
if (envVar === undefined) {
throw new Error(`Environment variable ${envVarName} is required`);
}
return envVar;
};
44 changes: 44 additions & 0 deletions examples/poaps/backend/src/methods/mint_async_poap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { PoapsClient } from '@poap-xyz/poaps';
import { handleError } from '../utils/handleError';

/**
* Attempts to mint a POAP (Proof of Attendance Protocol) token asynchronously based on a predefined QR hash and address.
* After successfully minting, the function fetches and logs the details of the minted POAP.
* In the event of an error during the process, the error is captured and managed by a separate utility function.
*
* Note: Replace 'your_mint_code' and 'your_address' placeholders with appropriate values.
*
* @async
* @function
* @param {PoapsClient} client - An instance of the PoapsClient to interface with the POAP service.
* @returns {Promise<void>} Resolves when the operation completes, either with a minted POAP or an error.
*/
export const mint_async_poap = async (client: PoapsClient): Promise<void> => {
try {
// Initiate the asynchronous mint process
const queueUid: string = await client.mintAsync({
mintCode: 'your_mint_code',
address: 'your_address',
});

// Wait for the mint's status to transition from 'IN_PROCESS' or 'PENDING' states
await client.waitMintStatus(queueUid, 'your_mint_code');

// Wait for the minted POAP to be indexed and fetch the mint code information related to the QR hash
const getMintCodeResponse = await client.waitPoapIndexed('your_mint_code');

// Retrieve and log the specifics of the minted POAP
console.log(
(
await client.fetch({
limit: 1,
offset: 0,
ids: [getMintCodeResponse.poapId],
})
).items[0],
);
} catch (error) {
// Address any errors using the designated utility function
handleError(error);
}
};
14 changes: 14 additions & 0 deletions examples/poaps/backend/src/methods/mint_sync_poap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { POAP, PoapsClient } from '@poap-xyz/poaps';
import { handleError } from '../utils/handleError';

export const mint_sync_poap = async (client: PoapsClient): Promise<void> => {
try {
const data: POAP = await client.mintSync({
mintCode: 'your_poap_code',
address: 'your_address',
});
console.log(data);
} catch (error) {
handleError(error);
}
};
35 changes: 35 additions & 0 deletions examples/poaps/backend/src/utils/handleError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
CodeAlreadyMintedError,
CodeExpiredError,
FinishedWithError,
} from '@poap-xyz/poaps';

/**
* Handles specific POAP-related errors by logging them.
*
* Errors handled:
* - CodeAlreadyMintedError: Thrown when a POAP mint code has already been minted.
* - CodeExpiredError: Thrown when a POAP mint code has expired and is no longer valid for minting.
* - FinishedWithError: Thrown when the POAP mint process completes but encounters an error.
*
* @param {unknown} error - The error object to be checked and handled.
*/
export const handleError = (error: unknown): void => {
if (
// Checks if the error is an instance of CodeAlreadyClaimedError.
// This error occurs when a user attempts to mint a POAP that has already been minted by someone else.
error instanceof CodeAlreadyMintedError ||
// Checks if the error is an instance of CodeExpiredError.
// This error is thrown when the mint code for a POAP has expired.
error instanceof CodeExpiredError ||
// Checks if the error is an instance of FinishedWithError.
// This error indicates that the POAP mint process finished but encountered an unexpected error.
error instanceof FinishedWithError
) {
// Logs the specific error message.
console.error(error);
} else {
// Logs the generic error message.
console.error('An unexpected error occurred:', error);
}
};
6 changes: 3 additions & 3 deletions packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.0.38",
"version": "0.0.39",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.0.38",
"@poap-xyz/utils": "0.0.38"
"@poap-xyz/providers": "0.0.39",
"@poap-xyz/utils": "0.0.39"
}
}
Loading

0 comments on commit bdbd9a8

Please sign in to comment.