Skip to content

Commit

Permalink
Multi user
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldizsar Mezei committed Feb 13, 2024
1 parent 5887627 commit 6054302
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,16 @@ export class TangleRequestService extends BaseTangleService<TangleResponse> {
};

private getOwner = async (senderAddress: NetworkAddress, network: Network) => {
const docRef = build5Db().doc(`${COL.MEMBER}/${senderAddress}`);
const member = await docRef.get<Member>();
if (member) {
return senderAddress;
}

const snap = await build5Db()
.collection(COL.MEMBER)
.where(`validatedAddress.${network}`, '==', senderAddress)
.limit(2)
.get<Member>();

if (snap.length > 1) {
Expand All @@ -174,18 +181,14 @@ export class TangleRequestService extends BaseTangleService<TangleResponse> {
return snap[0].uid;
}

const docRef = build5Db().doc(`${COL.MEMBER}/${senderAddress}`);
const member = <Member | undefined>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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
Expand All @@ -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<Transaction>();
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<Transaction>();
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!,
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions packages/sdk/src/otr/datasets/AuctionOtrDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuctionBidTangleRequest, 'requestType'>) =>
new OtrRequest<AuctionBidTangleRequest>(this.otrAddress, {
...params,
requestType: TangleRequestType.BID_AUCTION,
});
bid = (params: Omit<AuctionBidTangleRequest, 'requestType'>, amount = 0) =>
new OtrRequest<AuctionBidTangleRequest>(
this.otrAddress,
{
...params,
requestType: TangleRequestType.BID_AUCTION,
},
amount,
);
}
30 changes: 20 additions & 10 deletions packages/sdk/src/otr/datasets/NftOtrDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NftPurchaseTangleRequest, 'requestType'>) =>
new OtrRequest<NftPurchaseTangleRequest>(this.otrAddress, {
...params,
requestType: TangleRequestType.NFT_PURCHASE,
});
purchase = (params: Omit<NftPurchaseTangleRequest, 'requestType'>, amount = 0) =>
new OtrRequest<NftPurchaseTangleRequest>(
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<NftBidTangleRequest, 'requestType'>) =>
new OtrRequest<NftBidTangleRequest>(this.otrAddress, {
...params,
requestType: TangleRequestType.NFT_BID,
});
bid = (params: Omit<NftBidTangleRequest, 'requestType'>, amount = 0) =>
new OtrRequest<NftBidTangleRequest>(
this.otrAddress,
{
...params,
requestType: TangleRequestType.NFT_BID,
},
amount,
);
/**
* Set NFT for sale via OTR
*
Expand Down

0 comments on commit 6054302

Please sign in to comment.