Skip to content

Commit

Permalink
fix(vendor-ccxt): add lock for accountInfo to avoid oscillation (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrimbda authored Feb 24, 2024
1 parent 81c9586 commit 76c2edc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
37 changes: 33 additions & 4 deletions apps/vendor-ccxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OrderType,
PositionVariant,
formatTime,
UUID,
} from '@yuants/data-model';
import { Terminal } from '@yuants/protocol';
import '@yuants/protocol/lib/services';
Expand Down Expand Up @@ -68,6 +69,8 @@ interface IGeneralSpecificRelation {
// @ts-ignore
const ex: Exchange = new ccxt[EXCHANGE_ID](CCXT_PARAMS);

let accountInfoLock = false;

console.info(
formatTime(Date.now()),
`FeatureCheck`,
Expand Down Expand Up @@ -102,7 +105,10 @@ interface IGeneralSpecificRelation {
}

const terminal_id =
process.env.TERMINAL_ID || `CCXT-${EXCHANGE_ID}-${PUBLIC_ONLY ? 'PUBLIC' : account_id}-${CURRENCY}`;
process.env.TERMINAL_ID ||
`CCXT-${EXCHANGE_ID}-${PUBLIC_ONLY ? 'PUBLIC' : account_id}-${CURRENCY}${
PUBLIC_ONLY ? `-${UUID()}` : ''
}`;

const terminal = new Terminal(process.env.HOST_URL!, {
terminal_id,
Expand Down Expand Up @@ -331,7 +337,7 @@ interface IGeneralSpecificRelation {
? defer(() => ex.watchTicker(symbol))
: defer(() => ex.fetchTicker(symbol)).pipe(
//
repeat({ delay: 500 }),
repeat({ delay: 1000 }),
)
).pipe(
combineLatestWith(useFundingRate(symbol)),
Expand Down Expand Up @@ -497,12 +503,24 @@ interface IGeneralSpecificRelation {
);
}),
timeout(30_000),
tap({ error: (e) => console.error(formatTime(Date.now()), 'accountInfo$', e) }),
tap({
error: (e) => console.error(formatTime(Date.now()), 'accountInfo$', e),
next: () => {
if (accountInfoLock) {
console.info(formatTime(Date.now()), 'accountInfo$ is locked');
}
},
}),
retry({ delay: 1000 }),
shareReplay(1),
);

terminal.provideAccountInfo(accountInfo$);
terminal.provideAccountInfo(
accountInfo$.pipe(
// stuck on submit order to prevent duplicated order
filter(() => !accountInfoLock),
),
);

terminal.provideService(
'SubmitOrder',
Expand Down Expand Up @@ -544,6 +562,17 @@ interface IGeneralSpecificRelation {
map(() => {
return { res: { code: 0, message: 'OK' } };
}),
tap({
subscribe: () => {
if (accountInfoLock) {
throw new Error('accountInfo is locked');
}
accountInfoLock = true;
},
finalize: () => {
accountInfoLock = false;
},
}),
);
},
);
Expand Down
10 changes: 10 additions & 0 deletions common/changes/@yuants/vendor-ccxt/2024-02-24-13-18.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/vendor-ccxt",
"comment": "fix order lock bug",
"type": "patch"
}
],
"packageName": "@yuants/vendor-ccxt"
}

0 comments on commit 76c2edc

Please sign in to comment.