Skip to content

Commit

Permalink
Client fixes
Browse files Browse the repository at this point in the history
Fixes

Fixes

Fixes
  • Loading branch information
Boldizsar Mezei committed Dec 13, 2023
1 parent 796f5f2 commit 2d13d26
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/https/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/otr/datasets/TokenOtrDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
};

Expand All @@ -34,7 +34,7 @@ export class TokenOtrDataset extends DatasetClass {
new OtrRequest<TradeTokenTangleRequest>(
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<TokenStakeTangleRequest, 'requestType'>) =>
Expand Down
10 changes: 8 additions & 2 deletions packages/client/src/otr/datasets/common.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
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';
import { TokenOtrDataset } from './TokenOtrDataset';

// prettier-ignore
export type DatasetType<T extends Dataset> =
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { build5Db } from '@build-5/database';
import {
COL,
DEFAULT_NETWORK,
MAX_TOTAL_TOKEN_SUPPLY,
MilestoneTransactionEntry,
NativeToken,
SUB_COL,
Expand All @@ -12,7 +11,6 @@ import {
TokenTradeOrder,
TokenTradeOrderStatus,
TokenTradeOrderType,
Transaction,
TransactionPayloadType,
getNetworkPair,
} from '@build-5/interfaces';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Transaction>();
return (
snap.length === 1 &&
snap[0]!.payload.amount === 4 * MIN_IOTA_AMOUNT &&
snap[0]!.payload.targetAddress === faucetAddress.bech32
);
const snap = await buyQuery.get<TokenTradeOrder>();
return snap.length === 1;
});
const buy = (await buyQuery.get<TokenTradeOrder>())[0];
expect(buy.balance).toBe(4 * MIN_IOTA_AMOUNT);
expect(buy.count).toBe(2 * MIN_IOTA_AMOUNT);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -53,7 +54,15 @@ describe('Token minting', () => {
});
const snap = await query.get();
const credit = <Transaction>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()
Expand Down

0 comments on commit 2d13d26

Please sign in to comment.