diff --git a/src/utils/__snapshots__/priceUtil.test.ts.snap b/src/utils/__snapshots__/priceUtil.test.ts.snap index a16ecdc..7520ab5 100644 --- a/src/utils/__snapshots__/priceUtil.test.ts.snap +++ b/src/utils/__snapshots__/priceUtil.test.ts.snap @@ -70,7 +70,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], ] @@ -84,7 +84,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], Array [ @@ -107,7 +107,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], Array [ @@ -149,6 +149,14 @@ Array [ "nonce": 100, }, ], + Array [ + "account", + Object { + "gasLimit": 150000, + "gasPrice": 1000000000, + "nonce": 100, + }, + ], ] `; @@ -165,6 +173,27 @@ Array [ ] `; +exports[`fetchPrice isLive 1`] = ` +Array [ + Array [ + "account", + Object { + "gasLimit": 150000, + "gasPrice": 1000000000, + "nonce": 100, + }, + ], + Array [ + "account", + Object { + "gasLimit": 150000, + "gasPrice": 1000000000, + "nonce": 100, + }, + ], +] +`; + exports[`getBasePeriod 1`] = `"invalid period"`; exports[`getPeriodPrice 1 1`] = ` @@ -404,7 +433,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], Array [ @@ -445,6 +474,7 @@ Array [ "calls": Array [ Array [], Array [], + Array [], ], "results": Array [ Object { @@ -461,6 +491,13 @@ Array [ "timestamp": 2234567890000, }, }, + Object { + "isThrow": false, + "value": Object { + "price": 100, + "timestamp": 2234567890000, + }, + }, ], }, "isStarted": [MockFunction] { @@ -576,7 +613,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], Array [ @@ -617,6 +654,7 @@ Array [ "calls": Array [ Array [], Array [], + Array [], ], "results": Array [ Object { @@ -633,6 +671,13 @@ Array [ "timestamp": 2234567890000, }, }, + Object { + "isThrow": false, + "value": Object { + "price": 100, + "timestamp": 2234567890000, + }, + }, ], }, "isStarted": [MockFunction] { @@ -719,7 +764,7 @@ Array [ 1234567890, Object { "gasLimit": 80000, - "gasPrice": 1000000000, + "gasPrice": 1500000000, }, ], Array [ @@ -760,6 +805,7 @@ Array [ "calls": Array [ Array [], Array [], + Array [], ], "results": Array [ Object { @@ -776,6 +822,13 @@ Array [ "timestamp": 2234567890000, }, }, + Object { + "isThrow": false, + "value": Object { + "price": 100, + "timestamp": 2234567890000, + }, + }, ], }, "isStarted": [MockFunction] { diff --git a/src/utils/priceUtil.test.ts b/src/utils/priceUtil.test.ts index 315289b..f85a00b 100644 --- a/src/utils/priceUtil.test.ts +++ b/src/utils/priceUtil.test.ts @@ -161,6 +161,7 @@ const resetDualClassWrapper = { test('fetchPrice, not started', async () => { global.setInterval = jest.fn(); magiWrapper.isStarted = jest.fn(() => false); + magiWrapper.web3Wrapper.isLive = jest.fn(() => false); await priceUtil.fetchPrice( 'account', [tradingDualClassWrapper, resetDualClassWrapper], @@ -173,6 +174,23 @@ test('fetchPrice, not started', async () => { test('fetchPrice gasPrice', async () => { global.setInterval = jest.fn(); magiWrapper.isStarted = jest.fn(() => true); + magiWrapper.web3Wrapper.isLive = jest.fn(() => false); + + await priceUtil.fetchPrice( + 'account', + [tradingDualClassWrapper, resetDualClassWrapper], + magiWrapper, + 1000000000 + ); + await (global.setInterval as jest.Mock).mock.calls[0][0](); + expect((tradingDualClassWrapper.fetchPrice as jest.Mock).mock.calls).toMatchSnapshot(); + expect(resetDualClassWrapper.fetchPrice as jest.Mock).not.toBeCalled(); +}); + +test('fetchPrice isLive', async () => { + global.setInterval = jest.fn(); + magiWrapper.isStarted = jest.fn(() => true); + magiWrapper.web3Wrapper.isLive = jest.fn(() => true); await priceUtil.fetchPrice( 'account', @@ -189,6 +207,7 @@ test('fetchPrice', async () => { global.setInterval = jest.fn(); magiWrapper.isStarted = jest.fn(() => true); magiWrapper.web3Wrapper.getGasPrice = jest.fn(() => Promise.resolve(1000000000)); + magiWrapper.web3Wrapper.isLive = jest.fn(() => false); await priceUtil.fetchPrice( 'account', [tradingDualClassWrapper, resetDualClassWrapper], diff --git a/src/utils/priceUtil.ts b/src/utils/priceUtil.ts index a462f52..0e0e84b 100644 --- a/src/utils/priceUtil.ts +++ b/src/utils/priceUtil.ts @@ -94,40 +94,44 @@ class PriceUtil { gasPrice: number = 0 ) { const isStarted = await magiWrapper.isStarted(); + const isLive = magiWrapper.web3Wrapper.isLive(); if (!isStarted) { util.logDebug('Magi not ready, please start Magi first'); return; } let nonce = await dualClassWrappers[0].web3Wrapper.getTransactionCount(account); - global.setInterval(async () => { - // first checking Magi current time is set correctly - const lastPrice: IContractPrice = await magiWrapper.getLastPrice(); - const promiseList: Array> = []; - const wrappersToCall: DualClassWrapper[] = []; - - for (const bw of dualClassWrappers) { - const btvStates: IDualClassStates = await bw.getStates(); - if ( - btvStates.state === WrapperConstants.CTD_TRADING && - lastPrice.timestamp - btvStates.lastPriceTime > 3000000 - ) - wrappersToCall.push(bw); - } - - if (!gasPrice) gasPrice = Number(await magiWrapper.web3Wrapper.getGasPrice()); - for (const bw of wrappersToCall) { - promiseList.push( - bw.fetchPrice(account, { - gasPrice: gasPrice, - gasLimit: WrapperConstants.FETCH_PRICE_GAS, - nonce: nonce - }) - ); - nonce++; - } + global.setInterval( + async () => { + // first checking Magi current time is set correctly + const lastPrice: IContractPrice = await magiWrapper.getLastPrice(); + const promiseList: Array> = []; + const wrappersToCall: DualClassWrapper[] = []; + + for (const bw of dualClassWrappers) { + const btvStates: IDualClassStates = await bw.getStates(); + if ( + btvStates.state === WrapperConstants.CTD_TRADING && + lastPrice.timestamp - btvStates.lastPriceTime > 3000000 + ) + wrappersToCall.push(bw); + } + const networkGasPrice = Number(await magiWrapper.web3Wrapper.getGasPrice()); + if (!gasPrice) gasPrice = isLive ? networkGasPrice * 1.5 : networkGasPrice; + for (const bw of wrappersToCall) { + promiseList.push( + bw.fetchPrice(account, { + gasPrice: gasPrice, + gasLimit: WrapperConstants.FETCH_PRICE_GAS, + nonce: nonce + }) + ); + nonce++; + } - await Promise.all(promiseList); - }, 15000); + await Promise.all(promiseList); + }, + isLive ? 30000 : 15000 + ); } public getBasePeriod(period: number) {