From 76c2edc0d23e06f17d7f85245b049d752e0d8f51 Mon Sep 17 00:00:00 2001 From: Siyuan Wang Date: Sat, 24 Feb 2024 22:03:00 +0800 Subject: [PATCH] fix(vendor-ccxt): add lock for accountInfo to avoid oscillation (#467) --- apps/vendor-ccxt/src/index.ts | 37 +++++++++++++++++-- .../@yuants/vendor-ccxt/2024-02-24-13-18.json | 10 +++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 common/changes/@yuants/vendor-ccxt/2024-02-24-13-18.json diff --git a/apps/vendor-ccxt/src/index.ts b/apps/vendor-ccxt/src/index.ts index caed8d7dc..5c582d068 100644 --- a/apps/vendor-ccxt/src/index.ts +++ b/apps/vendor-ccxt/src/index.ts @@ -10,6 +10,7 @@ import { OrderType, PositionVariant, formatTime, + UUID, } from '@yuants/data-model'; import { Terminal } from '@yuants/protocol'; import '@yuants/protocol/lib/services'; @@ -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`, @@ -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, @@ -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)), @@ -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', @@ -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; + }, + }), ); }, ); diff --git a/common/changes/@yuants/vendor-ccxt/2024-02-24-13-18.json b/common/changes/@yuants/vendor-ccxt/2024-02-24-13-18.json new file mode 100644 index 000000000..601c1c5e2 --- /dev/null +++ b/common/changes/@yuants/vendor-ccxt/2024-02-24-13-18.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@yuants/vendor-ccxt", + "comment": "fix order lock bug", + "type": "patch" + } + ], + "packageName": "@yuants/vendor-ccxt" +} \ No newline at end of file