From 2d13d268f4fb0658c9df8f03a3df389a3aa6844f Mon Sep 17 00:00:00 2001 From: Boldizsar Mezei Date: Wed, 13 Dec 2023 13:03:40 +0200 Subject: [PATCH] Client fixes Fixes Fixes Fixes --- packages/client/src/https/index.ts | 2 +- .../src/otr/datasets/TokenOtrDataset.ts | 4 ++-- packages/client/src/otr/datasets/common.ts | 10 ++++++-- .../payment/token/token-trade.service.ts | 8 +++---- .../base-token-trading_1.spec.ts | 23 +++++++++---------- .../minted-token-trade_9.spec.ts | 11 ++++++++- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/packages/client/src/https/index.ts b/packages/client/src/https/index.ts index e4768b4037..7b7accfee7 100644 --- a/packages/client/src/https/index.ts +++ b/packages/client/src/https/index.ts @@ -9,7 +9,7 @@ import { import axios from 'axios'; import { ProjectWrapper } from './https'; -export const https = (origin = Build5.PROD) => new HttpsWrapper(origin); +export const https = (origin: string = Build5.PROD) => new HttpsWrapper(origin as Build5); class HttpsWrapper { constructor(private readonly origin: Build5) {} diff --git a/packages/client/src/otr/datasets/TokenOtrDataset.ts b/packages/client/src/otr/datasets/TokenOtrDataset.ts index 82ad18a4b2..1d663eb4ef 100644 --- a/packages/client/src/otr/datasets/TokenOtrDataset.ts +++ b/packages/client/src/otr/datasets/TokenOtrDataset.ts @@ -18,7 +18,7 @@ export class TokenOtrDataset extends DatasetClass { ...params, requestType: TangleRequestType.SELL_TOKEN, }, - Math.floor((params.count || 0) * params.price), + Math.floor((params.count || 0) * (params.price || 0)), ); }; @@ -34,7 +34,7 @@ export class TokenOtrDataset extends DatasetClass { new OtrRequest( this.otrAddress, { ...params, requestType: TangleRequestType.BUY_TOKEN }, - Math.floor((params.count || 0) * params.price), + Math.floor((params.count || 0) * (params.price || 0)), ); stake = (tokenId: string, count: number, params: Omit) => diff --git a/packages/client/src/otr/datasets/common.ts b/packages/client/src/otr/datasets/common.ts index 3fd66d37d3..02301c6eda 100644 --- a/packages/client/src/otr/datasets/common.ts +++ b/packages/client/src/otr/datasets/common.ts @@ -1,6 +1,9 @@ import { Dataset, Network } from '@build-5/interfaces'; import { v4 as uuid } from 'uuid'; +import { AuctionOtrDataset } from './AuctionOtrDataset'; +import { AwardOtrDataset } from './AwardOtrDataset'; import { MemberOtrDataset } from './MemberOtrDataset'; +import { NftOtrDataset } from './NftOtrDataset'; import { ProposalOtrDataset } from './ProposalOtrDataset'; import { SpaceOtrDataset } from './SpaceOtrDataset'; import { StamptOtrDataset } from './StampOtrDataset'; @@ -8,11 +11,14 @@ import { TokenOtrDataset } from './TokenOtrDataset'; // prettier-ignore export type DatasetType = + T extends Dataset.AUCTION ? AuctionOtrDataset: + T extends Dataset.AWARD ? AwardOtrDataset: T extends Dataset.MEMBER ? MemberOtrDataset: - T extends Dataset.SPACE ? SpaceOtrDataset: - T extends Dataset.TOKEN ? TokenOtrDataset: + T extends Dataset.NFT ? NftOtrDataset: T extends Dataset.PROPOSAL ? ProposalOtrDataset: + T extends Dataset.SPACE ? SpaceOtrDataset: T extends Dataset.STAMP ? StamptOtrDataset: + T extends Dataset.TOKEN ? TokenOtrDataset: unknown; export interface INativeToken { diff --git a/packages/functions/src/services/payment/token/token-trade.service.ts b/packages/functions/src/services/payment/token/token-trade.service.ts index 3aacb40e5c..ce14462b6a 100644 --- a/packages/functions/src/services/payment/token/token-trade.service.ts +++ b/packages/functions/src/services/payment/token/token-trade.service.ts @@ -2,7 +2,6 @@ import { build5Db } from '@build-5/database'; import { COL, DEFAULT_NETWORK, - MAX_TOTAL_TOKEN_SUPPLY, MilestoneTransactionEntry, NativeToken, SUB_COL, @@ -12,7 +11,6 @@ import { TokenTradeOrder, TokenTradeOrderStatus, TokenTradeOrderType, - Transaction, TransactionPayloadType, getNetworkPair, } from '@build-5/interfaces'; @@ -57,7 +55,7 @@ export class TokenTradeService extends BaseService { ? TokenTradeOrderType.SELL : TokenTradeOrderType.BUY; const price = get(order, 'payload.price', 0); - const count = getCount(order, tranEntry, type); + const count = getCount(tranEntry, type, price); const data: TokenTradeOrder = { project, @@ -117,12 +115,12 @@ export class TokenTradeService extends BaseService { } const getCount = ( - order: Transaction, tranEntry: MilestoneTransactionEntry, type: TokenTradeOrderType, + price: number, ) => { if (type === TokenTradeOrderType.SELL) { return Number(head(tranEntry.nativeTokens)?.amount || 0) || tranEntry.amount; } - return get(order, 'payload.count', MAX_TOTAL_TOKEN_SUPPLY); + return Math.floor(tranEntry.amount / price); }; diff --git a/packages/functions/test-tangle/base-token-trading/base-token-trading_1.spec.ts b/packages/functions/test-tangle/base-token-trading/base-token-trading_1.spec.ts index c95b655d13..9015ea1f1d 100644 --- a/packages/functions/test-tangle/base-token-trading/base-token-trading_1.spec.ts +++ b/packages/functions/test-tangle/base-token-trading/base-token-trading_1.spec.ts @@ -219,7 +219,7 @@ describe('Base token trading', () => { expect(dayjs(buy.expiresAt.toDate()).isSame(dayjs(expiresAt.toDate()))).toBe(true); }); - it('Should credit buy order with expiration unlock, wrong amount', async () => { + it('Should not credit buy order with expiration unlock, custom amount', async () => { const date = dayjs().add(2, 'h').millisecond(0).toDate(); const expiresAt = dateToTimestamp(date) as Timestamp; @@ -230,24 +230,23 @@ describe('Base token trading', () => { type: TokenTradeOrderType.BUY, }); const buyOrder = await testEnv.wrap(tradeToken)({}); - const { faucetAddress } = await requestFundsFromFaucet( + await requestFundsFromFaucet( helper.targetNetwork, buyOrder.payload.targetAddress, 4 * MIN_IOTA_AMOUNT, expiresAt, ); + const buyQuery = build5Db() + .collection(COL.TOKEN_MARKET) + .where('type', '==', TokenTradeOrderType.BUY) + .where('owner', '==', helper.buyer?.uid); await wait(async () => { - const snap = await build5Db() - .collection(COL.TRANSACTION) - .where('type', '==', TransactionType.CREDIT) - .where('member', '==', helper.buyer?.uid) - .get(); - return ( - snap.length === 1 && - snap[0]!.payload.amount === 4 * MIN_IOTA_AMOUNT && - snap[0]!.payload.targetAddress === faucetAddress.bech32 - ); + const snap = await buyQuery.get(); + return snap.length === 1; }); + const buy = (await buyQuery.get())[0]; + expect(buy.balance).toBe(4 * MIN_IOTA_AMOUNT); + expect(buy.count).toBe(2 * MIN_IOTA_AMOUNT); }); }); diff --git a/packages/functions/test-tangle/minted-token-trade/minted-token-trade_9.spec.ts b/packages/functions/test-tangle/minted-token-trade/minted-token-trade_9.spec.ts index f3e5241959..8dc60fc7e6 100644 --- a/packages/functions/test-tangle/minted-token-trade/minted-token-trade_9.spec.ts +++ b/packages/functions/test-tangle/minted-token-trade/minted-token-trade_9.spec.ts @@ -9,6 +9,7 @@ import { TransactionType, } from '@build-5/interfaces'; import { tradeToken } from '../../src/runtime/firebase/token/trading'; +import { packBasicOutput } from '../../src/utils/basic-output.utils'; import { mockWalletReturnValue, wait } from '../../test/controls/common'; import { testEnv } from '../../test/set-up'; import { awaitTransactionConfirmationsForToken } from '../common'; @@ -53,7 +54,15 @@ describe('Token minting', () => { }); const snap = await query.get(); const credit = snap[0]; - expect(credit.payload.amount).toBe(sellOrder.payload.amount); + const output = await packBasicOutput( + helper.walletService!, + sellOrder.payload.targetAddress, + 0, + { + nativeTokens: [{ amount: BigInt(10), id: helper.token!.mintingData?.tokenId! }], + }, + ); + expect(credit.payload.amount).toBe(Number(output.amount)); expect(credit.payload.nativeTokens![0].id).toBe(MINTED_TOKEN_ID); expect(credit.payload.nativeTokens![0].amount).toBe(10); const sellSnap = await build5Db()