From 60543021dcc0740698c7d64468678c68ea5566d1 Mon Sep 17 00:00:00 2001 From: Boldizsar Mezei Date: Tue, 13 Feb 2024 20:42:54 +0100 Subject: [PATCH] Multi user --- .../tangle-service/TangleRequestService.ts | 27 +++++---- .../tangleRequest/tangle-request.spec.ts | 60 ++++++++++++++++++- .../sdk/src/otr/datasets/AuctionOtrDataset.ts | 15 +++-- .../sdk/src/otr/datasets/NftOtrDataset.ts | 30 ++++++---- 4 files changed, 102 insertions(+), 30 deletions(-) diff --git a/packages/functions/src/services/payment/tangle-service/TangleRequestService.ts b/packages/functions/src/services/payment/tangle-service/TangleRequestService.ts index 896b7e66f9..e6e81c8843 100644 --- a/packages/functions/src/services/payment/tangle-service/TangleRequestService.ts +++ b/packages/functions/src/services/payment/tangle-service/TangleRequestService.ts @@ -161,9 +161,16 @@ export class TangleRequestService extends BaseTangleService { }; private getOwner = async (senderAddress: NetworkAddress, network: Network) => { + const docRef = build5Db().doc(`${COL.MEMBER}/${senderAddress}`); + const member = await docRef.get(); + if (member) { + return senderAddress; + } + const snap = await build5Db() .collection(COL.MEMBER) .where(`validatedAddress.${network}`, '==', senderAddress) + .limit(2) .get(); if (snap.length > 1) { @@ -174,18 +181,14 @@ export class TangleRequestService extends BaseTangleService { return snap[0].uid; } - const docRef = build5Db().doc(`${COL.MEMBER}/${senderAddress}`); - const member = await docRef.get(); - if (!member) { - const memberData = { - uid: senderAddress, - nonce: getRandomNonce(), - validatedAddress: { - [network as string]: senderAddress, - }, - }; - await docRef.create(memberData); - } + const memberData = { + uid: senderAddress, + nonce: getRandomNonce(), + validatedAddress: { + [network as string]: senderAddress, + }, + }; + await docRef.create(memberData); return senderAddress; }; diff --git a/packages/functions/test-tangle/tangleRequest/tangle-request.spec.ts b/packages/functions/test-tangle/tangleRequest/tangle-request.spec.ts index 47333fcedc..b8469f38cb 100644 --- a/packages/functions/test-tangle/tangleRequest/tangle-request.spec.ts +++ b/packages/functions/test-tangle/tangleRequest/tangle-request.spec.ts @@ -8,10 +8,12 @@ import { TangleRequestType, Token, TokenStatus, + TokenTradeOrderType, Transaction, TransactionType, WenError, } from '@build-5/interfaces'; +import { MnemonicService } from '../../src/services/wallet/mnemonic'; import { Wallet } from '../../src/services/wallet/wallet'; import { AddressDetails } from '../../src/services/wallet/wallet.service'; import { getAddress } from '../../src/utils/address.utils'; @@ -57,7 +59,7 @@ describe('Tangle request spec', () => { customMetadata: { request: { requestType: TangleRequestType.BUY_TOKEN, - symbol: 'ASD', + symbol: token.symbol, count: 5, price: MIN_IOTA_AMOUNT, }, @@ -81,6 +83,58 @@ describe('Tangle request spec', () => { }); }); + it('Should use sender as owner when multiple users', async () => { + const address = await rmsWallet.getNewIotaAddressDetails(); + + await requestFundsFromFaucet(Network.RMS, address.bech32, MIN_IOTA_AMOUNT); + + await rmsWallet.send(address, tangleOrder.payload.targetAddress!, 0.2 * MIN_IOTA_AMOUNT, { + customMetadata: { + request: { + requestType: TangleRequestType.BUY_TOKEN, + symbol: token.symbol, + count: 5, + price: MIN_IOTA_AMOUNT, + }, + }, + }); + await MnemonicService.store(address.bech32, address.mnemonic); + + const query = build5Db() + .collection(COL.TOKEN_MARKET) + .where('owner', '==', address.bech32) + .where('type', '==', TokenTradeOrderType.BUY); + await wait(async () => { + const snap = await query.get(); + return snap.length === 1; + }); + + const member2 = await createMember(walletSpy); + await build5Db() + .doc(`${COL.MEMBER}/${member2}`) + .set({ validatedAddress: { [Network.RMS]: rmsAddress.bech32 } }, true); + const member3 = await createMember(walletSpy); + await build5Db() + .doc(`${COL.MEMBER}/${member3}`) + .set({ validatedAddress: { [Network.RMS]: rmsAddress.bech32 } }, true); + + await rmsWallet.send(address, tangleOrder.payload.targetAddress!, 0.2 * MIN_IOTA_AMOUNT, { + customMetadata: { + request: { + requestType: TangleRequestType.BUY_TOKEN, + symbol: token.symbol, + count: 5, + price: MIN_IOTA_AMOUNT, + }, + }, + }); + + await wait(async () => { + const snap = await query.get(); + return snap.length === 2; + }); + }); + it('Should process multiple request at the same time', async () => { const requests = Array.from(Array(10)).map(() => ({ toAddress: tangleOrder.payload.targetAddress!, @@ -136,11 +190,11 @@ const saveToken = async (space: string, guardian: string) => { uid: wallet.getRandomEthAddress(), createdBy: guardian, name: 'MyToken', - status: TokenStatus.BASE, + status: TokenStatus.MINTED, access: 0, icon: MEDIA, mintingData: { - network: Network.ATOI, + network: Network.RMS, }, }; await build5Db().doc(`${COL.TOKEN}/${token.uid}`).set(token); diff --git a/packages/sdk/src/otr/datasets/AuctionOtrDataset.ts b/packages/sdk/src/otr/datasets/AuctionOtrDataset.ts index 74052fc75a..5387f66d2f 100644 --- a/packages/sdk/src/otr/datasets/AuctionOtrDataset.ts +++ b/packages/sdk/src/otr/datasets/AuctionOtrDataset.ts @@ -24,11 +24,16 @@ export class AuctionOtrDataset extends DatasetClass { * Bid on Auction. * * @param params Use {@link OtrRequest} with data based on {@link AuctionBidTangleRequest} + * @param amount Custom amount used for the cretion of the deep link * @returns */ - bid = (params: Omit) => - new OtrRequest(this.otrAddress, { - ...params, - requestType: TangleRequestType.BID_AUCTION, - }); + bid = (params: Omit, amount = 0) => + new OtrRequest( + this.otrAddress, + { + ...params, + requestType: TangleRequestType.BID_AUCTION, + }, + amount, + ); } diff --git a/packages/sdk/src/otr/datasets/NftOtrDataset.ts b/packages/sdk/src/otr/datasets/NftOtrDataset.ts index dfb794bdee..4a57cde19f 100644 --- a/packages/sdk/src/otr/datasets/NftOtrDataset.ts +++ b/packages/sdk/src/otr/datasets/NftOtrDataset.ts @@ -17,24 +17,34 @@ export class NftOtrDataset extends DatasetClass { * Purchase NFT via OTR * * @param params Use {@link OtrRequest} with data based on {@link NftPurchaseTangleRequest} + * @param amount Custom amount used for the cretion of the deep link * @returns */ - purchase = (params: Omit) => - new OtrRequest(this.otrAddress, { - ...params, - requestType: TangleRequestType.NFT_PURCHASE, - }); + purchase = (params: Omit, amount = 0) => + new OtrRequest( + this.otrAddress, + { + ...params, + requestType: TangleRequestType.NFT_PURCHASE, + }, + amount, + ); /** * Bid on NFT Auction * * @param params Use {@link OtrRequest} with data based on {@link AddressValidationTangleRequest} + * @param amount Custom amount used for the cretion of the deep link * @returns */ - bid = (params: Omit) => - new OtrRequest(this.otrAddress, { - ...params, - requestType: TangleRequestType.NFT_BID, - }); + bid = (params: Omit, amount = 0) => + new OtrRequest( + this.otrAddress, + { + ...params, + requestType: TangleRequestType.NFT_BID, + }, + amount, + ); /** * Set NFT for sale via OTR *