feat(deal-ticket): improve no collateral validation (#4122)
This commit is contained in:
parent
d1fabb31a3
commit
c740c11eaa
@ -1,6 +1,10 @@
|
|||||||
import * as Schema from '@vegaprotocol/types';
|
import * as Schema from '@vegaprotocol/types';
|
||||||
import { aliasGQLQuery } from '@vegaprotocol/cypress';
|
import { aliasGQLQuery } from '@vegaprotocol/cypress';
|
||||||
import { accountsQuery, amendGeneralAccountBalance } from '@vegaprotocol/mock';
|
import {
|
||||||
|
accountsQuery,
|
||||||
|
amendGeneralAccountBalance,
|
||||||
|
amendMarginAccountBalance,
|
||||||
|
} from '@vegaprotocol/mock';
|
||||||
import type { OrderSubmission } from '@vegaprotocol/wallet';
|
import type { OrderSubmission } from '@vegaprotocol/wallet';
|
||||||
import { createOrder } from '../support/create-order';
|
import { createOrder } from '../support/create-order';
|
||||||
|
|
||||||
@ -12,8 +16,9 @@ describe(
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.setVegaWallet();
|
cy.setVegaWallet();
|
||||||
cy.mockTradingPage();
|
cy.mockTradingPage();
|
||||||
const accounts = accountsQuery();
|
let accounts = accountsQuery();
|
||||||
amendGeneralAccountBalance(accounts, 'market-0', '0');
|
accounts = amendMarginAccountBalance(accounts, 'market-0', '1000');
|
||||||
|
accounts = amendGeneralAccountBalance(accounts, 'market-0', '0');
|
||||||
cy.mockGQL((req) => {
|
cy.mockGQL((req) => {
|
||||||
aliasGQLQuery(req, 'Accounts', accounts);
|
aliasGQLQuery(req, 'Accounts', accounts);
|
||||||
});
|
});
|
||||||
@ -23,6 +28,11 @@ describe(
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should show an error if your balance is zero', () => {
|
it('should show an error if your balance is zero', () => {
|
||||||
|
const accounts = accountsQuery();
|
||||||
|
amendMarginAccountBalance(accounts, 'market-0', '0');
|
||||||
|
cy.mockGQL((req) => {
|
||||||
|
aliasGQLQuery(req, 'Accounts', accounts);
|
||||||
|
});
|
||||||
// 7002-SORD-060
|
// 7002-SORD-060
|
||||||
cy.getByTestId('place-order').should('be.enabled');
|
cy.getByTestId('place-order').should('be.enabled');
|
||||||
// 7002-SORD-003
|
// 7002-SORD-003
|
||||||
@ -40,8 +50,9 @@ describe(
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.setVegaWallet();
|
cy.setVegaWallet();
|
||||||
cy.mockTradingPage();
|
cy.mockTradingPage();
|
||||||
const accounts = accountsQuery();
|
let accounts = accountsQuery();
|
||||||
amendGeneralAccountBalance(accounts, 'market-0', '1');
|
accounts = amendMarginAccountBalance(accounts, 'market-0', '1000');
|
||||||
|
accounts = amendGeneralAccountBalance(accounts, 'market-0', '1');
|
||||||
cy.mockGQL((req) => {
|
cy.mockGQL((req) => {
|
||||||
aliasGQLQuery(req, 'Accounts', accounts);
|
aliasGQLQuery(req, 'Accounts', accounts);
|
||||||
});
|
});
|
||||||
|
@ -151,19 +151,53 @@ export const amendGeneralAccountBalance = (
|
|||||||
marketId: string,
|
marketId: string,
|
||||||
balance: string
|
balance: string
|
||||||
) => {
|
) => {
|
||||||
if (accounts.party?.accountsConnection?.edges) {
|
if (!accounts.party?.accountsConnection?.edges) {
|
||||||
const marginAccount = accounts.party.accountsConnection.edges.find(
|
return accounts;
|
||||||
(edge) => edge?.node.market?.id === marketId
|
|
||||||
);
|
|
||||||
if (marginAccount) {
|
|
||||||
const generalAccount = accounts.party.accountsConnection.edges.find(
|
|
||||||
(edge) =>
|
|
||||||
edge?.node.asset.id === marginAccount.node.asset.id &&
|
|
||||||
!edge?.node.market
|
|
||||||
);
|
|
||||||
if (generalAccount) {
|
|
||||||
generalAccount.node.balance = balance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const marginAccount = accounts.party?.accountsConnection?.edges?.find(
|
||||||
|
(edge) => edge?.node.market?.id === marketId
|
||||||
|
);
|
||||||
|
if (marginAccount) {
|
||||||
|
const edges = accounts.party.accountsConnection.edges.map((edge) =>
|
||||||
|
edge?.node.asset.id === marginAccount.node.asset.id && !edge?.node.market
|
||||||
|
? { ...edge, node: { ...edge.node, balance } }
|
||||||
|
: edge
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
...accounts,
|
||||||
|
party: {
|
||||||
|
...accounts.party,
|
||||||
|
accountsConnection: {
|
||||||
|
...accounts.party.accountsConnection,
|
||||||
|
edges,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return accounts;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const amendMarginAccountBalance = (
|
||||||
|
accounts: AccountsQuery,
|
||||||
|
marketId: string,
|
||||||
|
balance: string
|
||||||
|
) => {
|
||||||
|
if (!accounts.party?.accountsConnection?.edges) {
|
||||||
|
return accounts;
|
||||||
|
}
|
||||||
|
const edges = accounts.party?.accountsConnection?.edges?.map((edge) =>
|
||||||
|
edge?.node.market?.id === marketId
|
||||||
|
? { ...edge, node: { ...edge?.node, balance } }
|
||||||
|
: edge
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
...accounts,
|
||||||
|
party: {
|
||||||
|
...accounts.party,
|
||||||
|
accountsConnection: {
|
||||||
|
...accounts.party.accountsConnection,
|
||||||
|
edges,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
@ -195,7 +195,7 @@ export const DealTicket = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hasNoBalance =
|
const hasNoBalance =
|
||||||
!generalAccountBalance || !BigInt(generalAccountBalance);
|
!BigInt(generalAccountBalance) && !BigInt(marginAccountBalance);
|
||||||
if (hasNoBalance) {
|
if (hasNoBalance) {
|
||||||
setError('summary', {
|
setError('summary', {
|
||||||
message: SummaryValidationType.NoCollateral,
|
message: SummaryValidationType.NoCollateral,
|
||||||
@ -219,6 +219,7 @@ export const DealTicket = ({
|
|||||||
marketState,
|
marketState,
|
||||||
marketTradingMode,
|
marketTradingMode,
|
||||||
generalAccountBalance,
|
generalAccountBalance,
|
||||||
|
marginAccountBalance,
|
||||||
pubKey,
|
pubKey,
|
||||||
setError,
|
setError,
|
||||||
clearErrors,
|
clearErrors,
|
||||||
|
Loading…
Reference in New Issue
Block a user