chore: move normalize order and normalize transfer to orders and accounts libs
This commit is contained in:
parent
7af3358e85
commit
813ace4412
@ -19,7 +19,6 @@ import {
|
||||
TradingButton,
|
||||
} from '@vegaprotocol/ui-toolkit';
|
||||
import type { Transfer } from '@vegaprotocol/wallet';
|
||||
import { normalizeTransfer } from '@vegaprotocol/wallet';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
@ -27,6 +26,7 @@ import { Controller, useForm } from 'react-hook-form';
|
||||
import { AssetOption, Balance } from '@vegaprotocol/assets';
|
||||
import { AccountType, AccountTypeMapping } from '@vegaprotocol/types';
|
||||
import { useTransferFeeQuery } from './__generated__/TransferFee';
|
||||
import { normalizeTransfer } from './utils';
|
||||
|
||||
interface FormFields {
|
||||
toVegaKey: string;
|
||||
|
26
libs/accounts/src/lib/utils.ts
Normal file
26
libs/accounts/src/lib/utils.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import type { Exact } from 'type-fest';
|
||||
import { type Transfer } from '@vegaprotocol/wallet';
|
||||
import { removeDecimal } from '@vegaprotocol/utils';
|
||||
import { type AccountType } from '@vegaprotocol/types';
|
||||
|
||||
export const normalizeTransfer = <T extends Exact<Transfer, T>>(
|
||||
address: string,
|
||||
amount: string,
|
||||
fromAccountType: AccountType,
|
||||
toAccountType: AccountType,
|
||||
asset: {
|
||||
id: string;
|
||||
decimals: number;
|
||||
}
|
||||
): Transfer => {
|
||||
return {
|
||||
to: address,
|
||||
fromAccountType,
|
||||
toAccountType,
|
||||
asset: asset.id,
|
||||
amount: removeDecimal(amount, asset.decimals),
|
||||
// oneOff or recurring required otherwise wallet will error
|
||||
// default oneOff is immediate transfer
|
||||
oneOff: {},
|
||||
};
|
||||
};
|
@ -3,7 +3,6 @@ import { type AgGridReact } from 'ag-grid-react';
|
||||
import { Pagination, type useDataGridEvents } from '@vegaprotocol/datagrid';
|
||||
import { Splash } from '@vegaprotocol/ui-toolkit';
|
||||
import { useDataProvider } from '@vegaprotocol/data-provider';
|
||||
import { normalizeOrderAmendment } from '@vegaprotocol/wallet';
|
||||
import { useVegaTransactionStore } from '@vegaprotocol/web3';
|
||||
import type { OrderTxUpdateFieldsFragment } from '@vegaprotocol/web3';
|
||||
import { OrderEditDialog } from '../order-list/order-edit-dialog';
|
||||
@ -12,6 +11,7 @@ import { OrderViewDialog } from '../order-list/order-view-dialog';
|
||||
import { OrderListTable } from '../order-list';
|
||||
import { ordersWithMarketProvider } from '../order-data-provider/order-data-provider';
|
||||
import { useT } from '../../use-t';
|
||||
import { normalizeOrderAmendment } from '../../utils';
|
||||
|
||||
export enum Filter {
|
||||
'Open' = 'Open',
|
||||
|
56
libs/orders/src/lib/utils.spec.ts
Normal file
56
libs/orders/src/lib/utils.spec.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { OrderTimeInForce } from '@vegaprotocol/types';
|
||||
import { normalizeOrderAmendment } from './utils';
|
||||
|
||||
describe('normalizeOrderAmendment', () => {
|
||||
type Order = Parameters<typeof normalizeOrderAmendment>[0];
|
||||
type Market = Parameters<typeof normalizeOrderAmendment>[1];
|
||||
const order: Order = {
|
||||
id: '123',
|
||||
timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTT,
|
||||
size: '100',
|
||||
expiresAt: '2022-01-01T00:00:00.000Z',
|
||||
};
|
||||
const market: Market = {
|
||||
id: '456',
|
||||
decimalPlaces: 1,
|
||||
positionDecimalPlaces: 1,
|
||||
};
|
||||
|
||||
it('sets and formats order id, market id, expires and timeInForce as given', () => {
|
||||
const orderAmendment = normalizeOrderAmendment(order, market, '1', '1');
|
||||
expect(orderAmendment.orderId).toEqual('123');
|
||||
expect(orderAmendment.marketId).toEqual('456');
|
||||
expect(orderAmendment.expiresAt).toEqual('1640995200000000000');
|
||||
expect(orderAmendment.timeInForce).toEqual(
|
||||
OrderTimeInForce.TIME_IN_FORCE_GTT
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['1.1', 1, '11'],
|
||||
['1.1', 2, '110'],
|
||||
['0.001', 8, '100000'],
|
||||
])('sets and formats price', (price, decimalPlaces, output) => {
|
||||
const orderAmendment = normalizeOrderAmendment(
|
||||
order,
|
||||
{ ...market, decimalPlaces },
|
||||
price,
|
||||
'1'
|
||||
);
|
||||
expect(orderAmendment.price).toEqual(output);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['9', 1, -10],
|
||||
['90', 2, 8900],
|
||||
['0.001', 8, 99900],
|
||||
])('sets and formats size delta', (size, positionDecimalPlaces, output) => {
|
||||
const orderAmendment = normalizeOrderAmendment(
|
||||
order,
|
||||
{ ...market, positionDecimalPlaces },
|
||||
'1',
|
||||
size
|
||||
);
|
||||
expect(orderAmendment.sizeDelta).toEqual(output);
|
||||
});
|
||||
});
|
25
libs/orders/src/lib/utils.ts
Normal file
25
libs/orders/src/lib/utils.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import type { Exact } from 'type-fest';
|
||||
import { type OrderAmendment } from '@vegaprotocol/wallet';
|
||||
import { removeDecimal, toNanoSeconds } from '@vegaprotocol/utils';
|
||||
import { type Market, type Order } from '@vegaprotocol/types';
|
||||
|
||||
export const normalizeOrderAmendment = <T extends Exact<OrderAmendment, T>>(
|
||||
order: Pick<Order, 'id' | 'timeInForce' | 'size' | 'expiresAt'>,
|
||||
market: Pick<Market, 'id' | 'decimalPlaces' | 'positionDecimalPlaces'>,
|
||||
price: string,
|
||||
size: string
|
||||
): OrderAmendment => ({
|
||||
orderId: order.id,
|
||||
marketId: market.id,
|
||||
price: removeDecimal(price, market.decimalPlaces),
|
||||
timeInForce: order.timeInForce,
|
||||
sizeDelta: size
|
||||
? new BigNumber(removeDecimal(size, market.positionDecimalPlaces))
|
||||
.minus(order.size)
|
||||
.toNumber()
|
||||
: 0,
|
||||
expiresAt: order.expiresAt
|
||||
? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds
|
||||
: undefined,
|
||||
});
|
@ -1,5 +1,4 @@
|
||||
import type { StateCreator } from 'zustand';
|
||||
import { act } from 'react-dom/test-utils';
|
||||
const { create: actualCreate } = jest.requireActual('zustand'); // if using jest
|
||||
|
||||
// a variable to hold reset functions for all stores declared in the app
|
||||
@ -17,5 +16,5 @@ export const create =
|
||||
|
||||
// Reset all stores after each test run
|
||||
beforeEach(() => {
|
||||
act(() => storeResetFns.forEach((resetFn) => resetFn()));
|
||||
storeResetFns.forEach((resetFn) => resetFn());
|
||||
});
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { determineId, normalizeOrderAmendment } from './utils';
|
||||
import * as Schema from '@vegaprotocol/types';
|
||||
import { determineId } from './utils';
|
||||
|
||||
describe('determineId', () => {
|
||||
it('produces a known result for an ID', () => {
|
||||
@ -11,57 +10,3 @@ describe('determineId', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeOrderAmendment', () => {
|
||||
type Order = Parameters<typeof normalizeOrderAmendment>[0];
|
||||
type Market = Parameters<typeof normalizeOrderAmendment>[1];
|
||||
const order: Order = {
|
||||
id: '123',
|
||||
timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_GTT,
|
||||
size: '100',
|
||||
expiresAt: '2022-01-01T00:00:00.000Z',
|
||||
};
|
||||
const market: Market = {
|
||||
id: '456',
|
||||
decimalPlaces: 1,
|
||||
positionDecimalPlaces: 1,
|
||||
};
|
||||
|
||||
it('sets and formats order id, market id, expires and timeInForce as given', () => {
|
||||
const orderAmendment = normalizeOrderAmendment(order, market, '1', '1');
|
||||
expect(orderAmendment.orderId).toEqual('123');
|
||||
expect(orderAmendment.marketId).toEqual('456');
|
||||
expect(orderAmendment.expiresAt).toEqual('1640995200000000000');
|
||||
expect(orderAmendment.timeInForce).toEqual(
|
||||
Schema.OrderTimeInForce.TIME_IN_FORCE_GTT
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['1.1', 1, '11'],
|
||||
['1.1', 2, '110'],
|
||||
['0.001', 8, '100000'],
|
||||
])('sets and formats price', (price, decimalPlaces, output) => {
|
||||
const orderAmendment = normalizeOrderAmendment(
|
||||
order,
|
||||
{ ...market, decimalPlaces },
|
||||
price,
|
||||
'1'
|
||||
);
|
||||
expect(orderAmendment.price).toEqual(output);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['9', 1, -10],
|
||||
['90', 2, 8900],
|
||||
['0.001', 8, 99900],
|
||||
])('sets and formats size delta', (size, positionDecimalPlaces, output) => {
|
||||
const orderAmendment = normalizeOrderAmendment(
|
||||
order,
|
||||
{ ...market, positionDecimalPlaces },
|
||||
'1',
|
||||
size
|
||||
);
|
||||
expect(orderAmendment.sizeDelta).toEqual(output);
|
||||
});
|
||||
});
|
||||
|
@ -1,9 +1,5 @@
|
||||
import { removeDecimal, toNanoSeconds } from '@vegaprotocol/utils';
|
||||
import { type AccountType, type Market, type Order } from '@vegaprotocol/types';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import { ethers } from 'ethers';
|
||||
import { sha3_256 } from 'js-sha3';
|
||||
import type { Exact } from 'type-fest';
|
||||
import {
|
||||
type ApplyReferralCode,
|
||||
type BatchMarketInstructionSubmissionBody,
|
||||
@ -16,8 +12,6 @@ import {
|
||||
type TransferBody,
|
||||
type UpdateMarginModeBody,
|
||||
type WithdrawSubmissionBody,
|
||||
type Transfer,
|
||||
type OrderAmendment,
|
||||
type Transaction,
|
||||
} from './transaction-types';
|
||||
|
||||
@ -38,48 +32,6 @@ export const encodeTransaction = (tx: Transaction): string => {
|
||||
);
|
||||
};
|
||||
|
||||
export const normalizeOrderAmendment = <T extends Exact<OrderAmendment, T>>(
|
||||
order: Pick<Order, 'id' | 'timeInForce' | 'size' | 'expiresAt'>,
|
||||
market: Pick<Market, 'id' | 'decimalPlaces' | 'positionDecimalPlaces'>,
|
||||
price: string,
|
||||
size: string
|
||||
): OrderAmendment => ({
|
||||
orderId: order.id,
|
||||
marketId: market.id,
|
||||
price: removeDecimal(price, market.decimalPlaces),
|
||||
timeInForce: order.timeInForce,
|
||||
sizeDelta: size
|
||||
? new BigNumber(removeDecimal(size, market.positionDecimalPlaces))
|
||||
.minus(order.size)
|
||||
.toNumber()
|
||||
: 0,
|
||||
expiresAt: order.expiresAt
|
||||
? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds
|
||||
: undefined,
|
||||
});
|
||||
|
||||
export const normalizeTransfer = <T extends Exact<Transfer, T>>(
|
||||
address: string,
|
||||
amount: string,
|
||||
fromAccountType: AccountType,
|
||||
toAccountType: AccountType,
|
||||
asset: {
|
||||
id: string;
|
||||
decimals: number;
|
||||
}
|
||||
): Transfer => {
|
||||
return {
|
||||
to: address,
|
||||
fromAccountType,
|
||||
toAccountType,
|
||||
asset: asset.id,
|
||||
amount: removeDecimal(amount, asset.decimals),
|
||||
// oneOff or recurring required otherwise wallet will error
|
||||
// default oneOff is immediate transfer
|
||||
oneOff: {},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* TODO: We may want to create a package similar to @metamask/detect-ethereum-provider as this wont suffice
|
||||
* if called immeidately, and before the extension has been able to add the vega object to the window
|
||||
|
Loading…
Reference in New Issue
Block a user