Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cliff0412 committed Mar 5, 2019
1 parent 488b348 commit 4b8faee
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 34 deletions.
65 changes: 59 additions & 6 deletions src/utils/__snapshots__/priceUtil.test.ts.snap
Expand Up @@ -70,7 +70,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
]
Expand All @@ -84,7 +84,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
Array [
Expand All @@ -107,7 +107,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
Array [
Expand Down Expand Up @@ -149,6 +149,14 @@ Array [
"nonce": 100,
},
],
Array [
"account",
Object {
"gasLimit": 150000,
"gasPrice": 1000000000,
"nonce": 100,
},
],
]
`;

Expand All @@ -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`] = `
Expand Down Expand Up @@ -404,7 +433,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
Array [
Expand Down Expand Up @@ -445,6 +474,7 @@ Array [
"calls": Array [
Array [],
Array [],
Array [],
],
"results": Array [
Object {
Expand All @@ -461,6 +491,13 @@ Array [
"timestamp": 2234567890000,
},
},
Object {
"isThrow": false,
"value": Object {
"price": 100,
"timestamp": 2234567890000,
},
},
],
},
"isStarted": [MockFunction] {
Expand Down Expand Up @@ -576,7 +613,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
Array [
Expand Down Expand Up @@ -617,6 +654,7 @@ Array [
"calls": Array [
Array [],
Array [],
Array [],
],
"results": Array [
Object {
Expand All @@ -633,6 +671,13 @@ Array [
"timestamp": 2234567890000,
},
},
Object {
"isThrow": false,
"value": Object {
"price": 100,
"timestamp": 2234567890000,
},
},
],
},
"isStarted": [MockFunction] {
Expand Down Expand Up @@ -719,7 +764,7 @@ Array [
1234567890,
Object {
"gasLimit": 80000,
"gasPrice": 1000000000,
"gasPrice": 1500000000,
},
],
Array [
Expand Down Expand Up @@ -760,6 +805,7 @@ Array [
"calls": Array [
Array [],
Array [],
Array [],
],
"results": Array [
Object {
Expand All @@ -776,6 +822,13 @@ Array [
"timestamp": 2234567890000,
},
},
Object {
"isThrow": false,
"value": Object {
"price": 100,
"timestamp": 2234567890000,
},
},
],
},
"isStarted": [MockFunction] {
Expand Down
19 changes: 19 additions & 0 deletions src/utils/priceUtil.test.ts
Expand Up @@ -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],
Expand All @@ -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',
Expand All @@ -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],
Expand Down
60 changes: 32 additions & 28 deletions src/utils/priceUtil.ts
Expand Up @@ -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<Promise<void>> = [];
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<Promise<void>> = [];
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) {
Expand Down

0 comments on commit 4b8faee

Please sign in to comment.