Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlitskiy committed May 17, 2024
2 parents da981fd + 529e97f commit cf32cfd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

-

### v1.1.0

- Add sending nft list of the event to the check rxb event

### v1.0.0

- Product ready version
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qr-scanner-rxb",
"version": "1.0.0",
"version": "1.1.0",
"private": true,
"repository": "git://github.com/cere-io/nft-marketplace-client",
"dependencies": {
Expand Down
12 changes: 4 additions & 8 deletions rxb/check-event-query.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
{
"size": 1000,
"query": {
"bool": {
"must": [
"filter": [
{
"match": {
"payload.trigger": "DAVINCI_QR_CODE_VALIDATOR_USE_TICKET"
"event_type": "DAVINCI_QR_CODE_VALIDATOR_USE_TICKET"
}
},
{
"match": {
"payload.nftId": "{{event.payload.nftId}}"
"payload.eventId": "{{event.payload.eventId}}"
}
},
{
"match": {
"payload.wallet": "{{event.payload.wallet}}"
}
},
{
"match": {
"payload.collectionId": "{{event.payload.collectionId}}"
}
}
]
}
Expand Down
49 changes: 34 additions & 15 deletions rxb/integration-scripts/scanner-check.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function compareInLowerCase(a, b) {
return String(a).toLowerCase() === String(b).toLowerCase();
}

function getFreeportOwnedNfts(wallet) {
if (!wallet) {
throw new Error('Wallet address is not defined');
Expand Down Expand Up @@ -37,38 +41,48 @@ function getFreeportOwnedNfts(wallet) {
});
}

async function checkData(collectionId, nftId, wallet, usedTicketCount) {
async function getNftForUsing(eventNfts, wallet, usedNfts) {
let userNfts;
try {
userNfts = await getFreeportOwnedNfts(wallet);
} catch (e) {
throw new Error(`Cannot get onwned nfts for the wallet ${wallet}`);
}

const userNft = userNfts.find(
(nft) => nft.collection.address.toLowerCase() === collectionId.toLowerCase() && nft.nftId === nftId,
);
if (!userNft) {
throw new Error(`Wallet doesn't have this NFT`);
}
const enrichedEventNfts = (eventNfts || [])?.map((eventNft) => {
const walletCount = userNfts
.filter(
(userNft) =>
userNft?.nftId === eventNft.nftId && compareInLowerCase(userNft?.collection?.address, eventNft.collection),
)
.map((userNft) => userNft.balance)
.reduce((acc, val) => +val + acc, 0);

const walletUsedCount = usedNfts.filter(
(nft) => nft.nftId === eventNft.nftId && compareInLowerCase(nft.collectionId, eventNft.collection),
).length;

return {...eventNft, walletCount, walletUsedCount};
});

const result = enrichedEventNfts.find((nft) => nft.walletCount > nft.walletUsedCount);

if (usedTicketCount >= userNft?.balance) {
throw new Error(`The number of tickets used is greater than the number of tickets the user has`);
if (!result) {
throw new Error(`Wallet doesn't any unused NFT for this event`);
}

return {userNft};
return result;
}

sdk.transform(async (params) => {
let errorMessage;
const collectionId = params.event.payload?.collectionId;
const nftId = params.event.payload?.nftId;
const wallet = params.event.payload?.wallet;
const usedTicketCount = params.queryData[0]?.hits?.total?.value || 0;
const eventNfts = params.event.payload?.eventNfts;
const usedNfts = (params.queryData[0]?.hits?.hits || []).map((hit) => hit?._source?.payload);

let checkResult;
try {
checkResult = await checkData(collectionId, nftId, wallet, usedTicketCount);
checkResult = await getNftForUsing(eventNfts, wallet, usedNfts);
} catch (e) {
errorMessage = e?.message || JSON.stringify(e);
}
Expand All @@ -79,7 +93,12 @@ sdk.transform(async (params) => {
errorMessage,
}
: {
data: {collectionId, nftId, wallet, checkResult, usedTicketCount},
data: {
collectionId: checkResult.collection,
nftId: checkResult.nftId,
walletCount: checkResult.walletCount,
walletUsedCount: checkResult.walletUsedCount,
},
result: true,
};

Expand Down
2 changes: 1 addition & 1 deletion src/stores/app.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export class AppStore {
this.notificationStore = new NotificationStore();
this.userStore = new UserStore(this.notificationStore);
this.eventStore = new EventStore(this.notificationStore, this.userStore);
this.scannerStore = new ScannerStore(this.notificationStore, this.userStore);
this.scannerStore = new ScannerStore(this.eventStore, this.userStore);
}
}
27 changes: 23 additions & 4 deletions src/stores/scanner.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import {makeAutoObservable, reaction} from 'mobx';
import {UserStore} from './user.store';
import {SdkTriggerEnum} from '../enums/sdk-trigger.enum';
import {ScannerStatusEnum} from '../enums/scanner-status.enum';
import {NotificationStore} from './notification.store';
import {EventStore} from './event.store';

export class ScannerStore {
private _eventSubscription: UnsubscribeEngagementHandler | undefined;
private _scanResult: Record<string, any> | undefined;
private _status: ScannerStatusEnum = ScannerStatusEnum.INIT;
private _errorMessage: string | undefined = undefined;
private _nftForUsing: {collectionId: string; nftId: string} | undefined;
constructor(
private notificationStore: NotificationStore,
private eventStore: EventStore,
private userStore: UserStore,
) {
makeAutoObservable(this);
Expand All @@ -36,6 +37,10 @@ export class ScannerStore {
if (response.trigger === SdkTriggerEnum.TICKET_CHECK) {
if (response?.data?.result === true) {
this._status = ScannerStatusEnum.SUCCESS;
this._nftForUsing = {
collectionId: response?.data?.data?.collectionId,
nftId: response?.data?.data?.nftId,
};
} else if (response?.data?.result === false) {
this._throwError(response?.data?.errorMessage);
} else {
Expand Down Expand Up @@ -67,8 +72,16 @@ export class ScannerStore {
return;
}

const eventData = this.eventStore.events?.find((e) => e.id === eventId);
// @ts-ignore
const eventNfts = (eventData?.nfts || [])
.filter((nft: {cmsNft?: {collection: string; nftId: string}}) => nft?.cmsNft?.collection && nft?.cmsNft?.nftId)
.map((nft: {cmsNft: {collection: string; nftId: string}}) => ({
collection: nft.cmsNft.collection,
nftId: nft.cmsNft.nftId,
}));
const trigger = SdkTriggerEnum.TICKET_CHECK;
this.userStore.sdkInstance?.sendEvent(trigger, {...data, trigger});
this.userStore.sdkInstance?.sendEvent(trigger, {...data, trigger, eventNfts});
this._status = ScannerStatusEnum.PROCESSING;
this._scanResult = data;
}
Expand All @@ -80,12 +93,18 @@ export class ScannerStore {
this._status = ScannerStatusEnum.READY;
this._scanResult = undefined;
this._errorMessage = undefined;
this._nftForUsing = undefined;
}

public useTicket(): void {
if (this._status === ScannerStatusEnum.SUCCESS) {
const trigger = SdkTriggerEnum.TICKET_USE;
this.userStore.sdkInstance?.sendEvent(trigger, {...this._scanResult, trigger});
this.userStore.sdkInstance?.sendEvent(trigger, {
...this._scanResult,
collectionId: this._nftForUsing?.collectionId,
nftId: this._nftForUsing?.nftId,
trigger,
});
this._status = ScannerStatusEnum.USE_TICKET_PROCESSING;
}
}
Expand Down

0 comments on commit cf32cfd

Please sign in to comment.