2018-09-09 21:25:54 +02:00
|
|
|
import assert from 'assert'
|
|
|
|
import sinon from 'sinon'
|
2018-11-05 18:01:46 +01:00
|
|
|
import proxyquire from 'proxyquire'
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
const fakeLocalStorage = {}
|
2018-11-05 18:01:46 +01:00
|
|
|
|
2019-03-22 00:03:30 +01:00
|
|
|
const GasDuck = proxyquire('./gas.duck.js', {
|
2019-07-04 21:18:12 +02:00
|
|
|
'../../../lib/local-storage-helpers': fakeLocalStorage,
|
2018-11-05 18:01:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const {
|
2018-09-09 21:25:54 +02:00
|
|
|
basicGasEstimatesLoadingStarted,
|
|
|
|
basicGasEstimatesLoadingFinished,
|
|
|
|
setBasicGasEstimateData,
|
|
|
|
setCustomGasPrice,
|
|
|
|
setCustomGasLimit,
|
|
|
|
setCustomGasTotal,
|
|
|
|
setCustomGasErrors,
|
|
|
|
resetCustomGasState,
|
2018-11-13 17:36:52 +01:00
|
|
|
fetchBasicGasAndTimeEstimates,
|
2018-12-10 22:51:00 +01:00
|
|
|
fetchBasicGasEstimates,
|
2018-10-09 18:35:54 +02:00
|
|
|
gasEstimatesLoadingStarted,
|
|
|
|
gasEstimatesLoadingFinished,
|
|
|
|
setPricesAndTimeEstimates,
|
2018-10-24 08:31:45 +02:00
|
|
|
fetchGasEstimates,
|
|
|
|
setApiEstimatesLastRetrieved,
|
2018-11-05 18:01:46 +01:00
|
|
|
} = GasDuck
|
|
|
|
const GasReducer = GasDuck.default
|
2018-09-09 21:25:54 +02:00
|
|
|
|
|
|
|
describe('Gas Duck', () => {
|
|
|
|
let tempFetch
|
2018-10-24 08:31:45 +02:00
|
|
|
let tempDateNow
|
|
|
|
const mockEthGasApiResponse = {
|
2018-11-27 18:30:41 +01:00
|
|
|
average: 20,
|
2018-10-24 08:31:45 +02:00
|
|
|
avgWait: 'mockAvgWait',
|
|
|
|
block_time: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
2018-11-27 18:30:41 +01:00
|
|
|
fast: 30,
|
|
|
|
fastest: 40,
|
2018-10-24 08:31:45 +02:00
|
|
|
fastestWait: 'mockFastestWait',
|
|
|
|
fastWait: 'mockFastWait',
|
2018-11-27 18:30:41 +01:00
|
|
|
safeLow: 10,
|
2018-10-24 08:31:45 +02:00
|
|
|
safeLowWait: 'mockSafeLowWait',
|
|
|
|
speed: 'mockSpeed',
|
2018-12-10 22:51:00 +01:00
|
|
|
standard: 20,
|
2018-10-24 08:31:45 +02:00
|
|
|
}
|
|
|
|
const mockPredictTableResponse = [
|
2018-11-13 18:36:35 +01:00
|
|
|
{ expectedTime: 400, expectedWait: 40, gasprice: 0.25, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 200, expectedWait: 20, gasprice: 0.5, somethingElse: 'foobar' },
|
2018-10-24 08:31:45 +02:00
|
|
|
{ expectedTime: 100, expectedWait: 10, gasprice: 1, somethingElse: 'foobar' },
|
2018-11-13 18:36:35 +01:00
|
|
|
{ expectedTime: 75, expectedWait: 7.5, gasprice: 1.5, somethingElse: 'foobar' },
|
2018-10-24 08:31:45 +02:00
|
|
|
{ expectedTime: 50, expectedWait: 5, gasprice: 2, somethingElse: 'foobar' },
|
2018-11-13 18:36:35 +01:00
|
|
|
{ expectedTime: 35, expectedWait: 4.5, gasprice: 3, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 34, expectedWait: 4.4, gasprice: 3.1, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 25, expectedWait: 4.2, gasprice: 3.5, somethingElse: 'foobar' },
|
2018-10-24 08:31:45 +02:00
|
|
|
{ expectedTime: 20, expectedWait: 4, gasprice: 4, somethingElse: 'foobar' },
|
2018-11-13 18:36:35 +01:00
|
|
|
{ expectedTime: 19, expectedWait: 3.9, gasprice: 4.1, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 15, expectedWait: 3, gasprice: 7, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 14, expectedWait: 2.9, gasprice: 7.1, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 12, expectedWait: 2.5, gasprice: 8, somethingElse: 'foobar' },
|
2018-10-24 08:31:45 +02:00
|
|
|
{ expectedTime: 10, expectedWait: 2, gasprice: 10, somethingElse: 'foobar' },
|
2018-11-13 18:36:35 +01:00
|
|
|
{ expectedTime: 9, expectedWait: 1.9, gasprice: 10.1, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 5, expectedWait: 1, gasprice: 15, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 4, expectedWait: 0.9, gasprice: 15.1, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 2, expectedWait: 0.8, gasprice: 17, somethingElse: 'foobar' },
|
|
|
|
{ expectedTime: 1.1, expectedWait: 0.6, gasprice: 19.9, somethingElse: 'foobar' },
|
2018-10-24 08:31:45 +02:00
|
|
|
{ expectedTime: 1, expectedWait: 0.5, gasprice: 20, somethingElse: 'foobar' },
|
|
|
|
]
|
2019-07-04 21:18:12 +02:00
|
|
|
const fakeFetch = (url) => new Promise(resolve => {
|
2018-12-10 22:51:00 +01:00
|
|
|
const dataToResolve = url.match(/ethgasAPI|gasexpress/)
|
2018-10-24 08:31:45 +02:00
|
|
|
? mockEthGasApiResponse
|
|
|
|
: mockPredictTableResponse
|
|
|
|
resolve({
|
|
|
|
json: () => new Promise(resolve => resolve(dataToResolve)),
|
|
|
|
})
|
2019-07-04 21:18:12 +02:00
|
|
|
})
|
2018-09-09 21:25:54 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
tempFetch = global.fetch
|
2018-10-24 08:31:45 +02:00
|
|
|
tempDateNow = global.Date.now
|
2019-07-04 21:18:12 +02:00
|
|
|
|
|
|
|
fakeLocalStorage.loadLocalStorageData = sinon.stub()
|
|
|
|
fakeLocalStorage.saveLocalStorageData = sinon.spy()
|
|
|
|
global.fetch = sinon.stub().callsFake(fakeFetch)
|
2018-10-24 08:31:45 +02:00
|
|
|
global.Date.now = () => 2000000
|
2018-09-09 21:25:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-07-04 21:18:12 +02:00
|
|
|
sinon.restore()
|
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
global.fetch = tempFetch
|
2018-10-24 08:31:45 +02:00
|
|
|
global.Date.now = tempDateNow
|
2018-09-09 21:25:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const mockState = {
|
|
|
|
gas: {
|
|
|
|
mockProp: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const initState = {
|
|
|
|
customData: {
|
2018-09-13 10:47:05 +02:00
|
|
|
price: null,
|
2018-12-12 15:54:03 +01:00
|
|
|
limit: null,
|
2018-09-09 21:25:54 +02:00
|
|
|
},
|
|
|
|
basicEstimates: {
|
|
|
|
average: null,
|
|
|
|
fastestWait: null,
|
|
|
|
fastWait: null,
|
|
|
|
fast: null,
|
|
|
|
safeLowWait: null,
|
|
|
|
blockNum: null,
|
|
|
|
avgWait: null,
|
|
|
|
blockTime: null,
|
|
|
|
speed: null,
|
|
|
|
fastest: null,
|
|
|
|
safeLow: null,
|
|
|
|
},
|
|
|
|
basicEstimateIsLoading: true,
|
|
|
|
errors: {},
|
2018-10-09 18:35:54 +02:00
|
|
|
gasEstimatesLoading: true,
|
|
|
|
priceAndTimeEstimates: [],
|
2018-10-24 08:31:45 +02:00
|
|
|
priceAndTimeEstimatesLastRetrieved: 0,
|
2018-11-13 17:36:52 +01:00
|
|
|
basicPriceAndTimeEstimatesLastRetrieved: 0,
|
2018-12-10 22:51:00 +01:00
|
|
|
basicPriceEstimatesLastRetrieved: 0,
|
2018-09-09 21:25:54 +02:00
|
|
|
}
|
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED'
|
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED'
|
2018-10-09 18:35:54 +02:00
|
|
|
const GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/GAS_ESTIMATE_LOADING_FINISHED'
|
|
|
|
const GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/GAS_ESTIMATE_LOADING_STARTED'
|
2018-09-09 21:25:54 +02:00
|
|
|
const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE'
|
|
|
|
const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA'
|
|
|
|
const SET_CUSTOM_GAS_ERRORS = 'metamask/gas/SET_CUSTOM_GAS_ERRORS'
|
|
|
|
const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT'
|
|
|
|
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'
|
|
|
|
const SET_CUSTOM_GAS_TOTAL = 'metamask/gas/SET_CUSTOM_GAS_TOTAL'
|
2018-10-09 18:35:54 +02:00
|
|
|
const SET_PRICE_AND_TIME_ESTIMATES = 'metamask/gas/SET_PRICE_AND_TIME_ESTIMATES'
|
2018-10-24 08:31:45 +02:00
|
|
|
const SET_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_API_ESTIMATES_LAST_RETRIEVED'
|
2018-11-13 17:36:52 +01:00
|
|
|
const SET_BASIC_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_API_ESTIMATES_LAST_RETRIEVED'
|
2018-12-10 22:51:00 +01:00
|
|
|
const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED'
|
2018-09-09 21:25:54 +02:00
|
|
|
|
|
|
|
describe('GasReducer()', () => {
|
|
|
|
it('should initialize state', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer({}),
|
|
|
|
initState
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return state unchanged if it does not match a dispatched actions type', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: 'someOtherAction',
|
|
|
|
value: 'someValue',
|
|
|
|
}),
|
|
|
|
Object.assign({}, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set basicEstimateIsLoading to true when receiving a BASIC_GAS_ESTIMATE_LOADING_STARTED action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_STARTED,
|
|
|
|
}),
|
|
|
|
Object.assign({basicEstimateIsLoading: true}, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set basicEstimateIsLoading to false when receiving a BASIC_GAS_ESTIMATE_LOADING_FINISHED action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_FINISHED,
|
|
|
|
}),
|
|
|
|
Object.assign({basicEstimateIsLoading: false}, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
it('should set gasEstimatesLoading to true when receiving a GAS_ESTIMATE_LOADING_STARTED action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: GAS_ESTIMATE_LOADING_STARTED,
|
|
|
|
}),
|
|
|
|
Object.assign({gasEstimatesLoading: true}, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set gasEstimatesLoading to false when receiving a GAS_ESTIMATE_LOADING_FINISHED action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: GAS_ESTIMATE_LOADING_FINISHED,
|
|
|
|
}),
|
|
|
|
Object.assign({gasEstimatesLoading: false}, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
it('should return a new object (and not just modify the existing state object)', () => {
|
|
|
|
assert.deepEqual(GasReducer(mockState), mockState.gas)
|
|
|
|
assert.notEqual(GasReducer(mockState), mockState.gas)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set basicEstimates when receiving a SET_BASIC_GAS_ESTIMATE_DATA action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: { someProp: 'someData123' },
|
|
|
|
}),
|
|
|
|
Object.assign({basicEstimates: {someProp: 'someData123'} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
it('should set priceAndTimeEstimates when receiving a SET_PRICE_AND_TIME_ESTIMATES action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_PRICE_AND_TIME_ESTIMATES,
|
|
|
|
value: { someProp: 'someData123' },
|
|
|
|
}),
|
|
|
|
Object.assign({priceAndTimeEstimates: {someProp: 'someData123'} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
it('should set customData.price when receiving a SET_CUSTOM_GAS_PRICE action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_PRICE,
|
|
|
|
value: 4321,
|
|
|
|
}),
|
|
|
|
Object.assign({customData: {price: 4321} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set customData.limit when receiving a SET_CUSTOM_GAS_LIMIT action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_LIMIT,
|
|
|
|
value: 9876,
|
|
|
|
}),
|
|
|
|
Object.assign({customData: {limit: 9876} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set customData.total when receiving a SET_CUSTOM_GAS_TOTAL action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_TOTAL,
|
|
|
|
value: 10000,
|
|
|
|
}),
|
|
|
|
Object.assign({customData: {total: 10000} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-11-13 17:36:52 +01:00
|
|
|
it('should set priceAndTimeEstimatesLastRetrieved when receiving a SET_API_ESTIMATES_LAST_RETRIEVED action', () => {
|
2018-10-24 08:31:45 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_API_ESTIMATES_LAST_RETRIEVED,
|
|
|
|
value: 1500000000000,
|
|
|
|
}),
|
|
|
|
Object.assign({ priceAndTimeEstimatesLastRetrieved: 1500000000000 }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-11-13 17:36:52 +01:00
|
|
|
it('should set priceAndTimeEstimatesLastRetrieved when receiving a SET_BASIC_API_ESTIMATES_LAST_RETRIEVED action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED,
|
|
|
|
value: 1700000000000,
|
|
|
|
}),
|
|
|
|
Object.assign({ basicPriceAndTimeEstimatesLastRetrieved: 1700000000000 }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
it('should set errors when receiving a SET_CUSTOM_GAS_ERRORS action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_ERRORS,
|
|
|
|
value: { someError: 'error_error' },
|
|
|
|
}),
|
|
|
|
Object.assign({errors: {someError: 'error_error'} }, mockState.gas)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the initial state in response to a RESET_CUSTOM_GAS_STATE action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
GasReducer(mockState, {
|
|
|
|
type: RESET_CUSTOM_GAS_STATE,
|
|
|
|
}),
|
|
|
|
Object.assign({}, initState)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('basicGasEstimatesLoadingStarted', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
basicGasEstimatesLoadingStarted(),
|
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('basicGasEstimatesLoadingFinished', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
basicGasEstimatesLoadingFinished(),
|
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-12-10 22:51:00 +01:00
|
|
|
describe('fetchBasicGasEstimates', () => {
|
|
|
|
it('should call fetch with the expected params', async () => {
|
2019-07-04 21:18:12 +02:00
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
|
2018-12-10 22:51:00 +01:00
|
|
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{ basicPriceAEstimatesLastRetrieved: 1000000 }
|
|
|
|
) }))
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
global.fetch.getCall(0).args,
|
|
|
|
[
|
|
|
|
'https://dev.blockscale.net/api/gasexpress.json',
|
|
|
|
{
|
|
|
|
'headers': {},
|
|
|
|
'referrer': 'https://dev.blockscale.net/api/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
|
|
|
'mode': 'cors',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ]
|
|
|
|
)
|
2019-07-04 21:18:12 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
|
|
|
average: 20,
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 30,
|
|
|
|
fastest: 40,
|
|
|
|
safeLow: 10,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(3).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fetch recently retrieved estimates from local storage', async () => {
|
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
.returns(2000000 - 1) // one second ago from "now"
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_PRICE_ESTIMATES')
|
|
|
|
.returns({
|
|
|
|
average: 25,
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 35,
|
|
|
|
fastest: 45,
|
|
|
|
safeLow: 15,
|
|
|
|
})
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{}
|
|
|
|
) }))
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.ok(global.fetch.notCalled)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
|
|
|
average: 25,
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 35,
|
|
|
|
fastest: 45,
|
|
|
|
safeLow: 15,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fallback to network if retrieving estimates from local storage fails', async () => {
|
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
.returns(2000000 - 1) // one second ago from "now"
|
|
|
|
|
|
|
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{}
|
|
|
|
) }))
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
global.fetch.getCall(0).args,
|
|
|
|
[
|
|
|
|
'https://dev.blockscale.net/api/gasexpress.json',
|
|
|
|
{
|
|
|
|
'headers': {},
|
|
|
|
'referrer': 'https://dev.blockscale.net/api/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
|
|
|
'mode': 'cors',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ]
|
|
|
|
)
|
2018-12-10 22:51:00 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
|
|
|
average: 20,
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 30,
|
|
|
|
fastest: 40,
|
|
|
|
safeLow: 10,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(3).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-13 17:36:52 +01:00
|
|
|
describe('fetchBasicGasAndTimeEstimates', () => {
|
2018-09-09 21:25:54 +02:00
|
|
|
it('should call fetch with the expected params', async () => {
|
2019-07-04 21:18:12 +02:00
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
|
2018-11-13 17:36:52 +01:00
|
|
|
await fetchBasicGasAndTimeEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{ basicPriceAndTimeEstimatesLastRetrieved: 1000000 }
|
2019-03-22 15:50:12 +01:00
|
|
|
),
|
2019-07-31 22:17:11 +02:00
|
|
|
metamask: { provider: { type: 'ropsten' } },
|
2019-03-22 15:50:12 +01:00
|
|
|
}))
|
2018-09-09 21:25:54 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
global.fetch.getCall(0).args,
|
|
|
|
[
|
|
|
|
'https://ethgasstation.info/json/ethgasAPI.json',
|
|
|
|
{
|
|
|
|
'headers': {},
|
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
|
|
|
'mode': 'cors',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
2018-11-13 17:36:52 +01:00
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
2018-11-13 17:36:52 +01:00
|
|
|
[{ type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
2018-09-09 21:25:54 +02:00
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
2018-11-27 18:30:41 +01:00
|
|
|
average: 2,
|
2018-09-09 21:25:54 +02:00
|
|
|
avgWait: 'mockAvgWait',
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
2018-11-27 18:30:41 +01:00
|
|
|
fast: 3,
|
|
|
|
fastest: 4,
|
2018-09-09 21:25:54 +02:00
|
|
|
fastestWait: 'mockFastestWait',
|
|
|
|
fastWait: 'mockFastWait',
|
2018-11-27 18:30:41 +01:00
|
|
|
safeLow: 1,
|
2018-09-09 21:25:54 +02:00
|
|
|
safeLowWait: 'mockSafeLowWait',
|
|
|
|
speed: 'mockSpeed',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
2018-11-13 17:36:52 +01:00
|
|
|
mockDistpatch.getCall(3).args,
|
2018-09-09 21:25:54 +02:00
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
it('should fetch recently retrieved estimates from local storage', async () => {
|
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
.returns(2000000 - 1) // one second ago from "now"
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_GAS_AND_TIME_API_ESTIMATES')
|
|
|
|
.returns({
|
|
|
|
average: 5,
|
|
|
|
avgWait: 'mockAvgWait',
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 6,
|
|
|
|
fastest: 7,
|
|
|
|
fastestWait: 'mockFastestWait',
|
|
|
|
fastWait: 'mockFastWait',
|
|
|
|
safeLow: 1,
|
|
|
|
safeLowWait: 'mockSafeLowWait',
|
|
|
|
speed: 'mockSpeed',
|
|
|
|
})
|
|
|
|
|
|
|
|
await fetchBasicGasAndTimeEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{}
|
|
|
|
),
|
2019-07-31 22:17:11 +02:00
|
|
|
metamask: { provider: { type: 'ropsten' } },
|
2019-07-04 21:18:12 +02:00
|
|
|
}))
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.ok(global.fetch.notCalled)
|
2018-11-05 18:01:46 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
|
|
|
average: 5,
|
|
|
|
avgWait: 'mockAvgWait',
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 6,
|
|
|
|
fastest: 7,
|
|
|
|
fastestWait: 'mockFastestWait',
|
|
|
|
fastWait: 'mockFastWait',
|
|
|
|
safeLow: 1,
|
|
|
|
safeLowWait: 'mockSafeLowWait',
|
|
|
|
speed: 'mockSpeed',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
2018-11-05 18:01:46 +01:00
|
|
|
})
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
it('should fallback to network if retrieving estimates from local storage fails', async () => {
|
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
fakeLocalStorage.loadLocalStorageData
|
|
|
|
.withArgs('BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
.returns(2000000 - 1) // one second ago from "now"
|
|
|
|
|
|
|
|
await fetchBasicGasAndTimeEstimates()(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{}
|
|
|
|
),
|
2019-07-31 22:17:11 +02:00
|
|
|
metamask: { provider: { type: 'ropsten' } },
|
2019-07-04 21:18:12 +02:00
|
|
|
}))
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
global.fetch.getCall(0).args,
|
|
|
|
[
|
|
|
|
'https://ethgasstation.info/json/ethgasAPI.json',
|
|
|
|
{
|
|
|
|
'headers': {},
|
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
|
|
|
'mode': 'cors',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{ type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
|
|
|
average: 2,
|
|
|
|
avgWait: 'mockAvgWait',
|
|
|
|
blockTime: 'mockBlock_time',
|
|
|
|
blockNum: 'mockBlockNum',
|
|
|
|
fast: 3,
|
|
|
|
fastest: 4,
|
|
|
|
fastestWait: 'mockFastestWait',
|
|
|
|
fastWait: 'mockFastWait',
|
|
|
|
safeLow: 1,
|
|
|
|
safeLowWait: 'mockSafeLowWait',
|
|
|
|
speed: 'mockSpeed',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(3).args,
|
|
|
|
[{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fetchGasEstimates', () => {
|
2018-10-24 08:31:45 +02:00
|
|
|
it('should call fetch with the expected params', async () => {
|
2019-07-04 21:18:12 +02:00
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
|
2018-10-24 08:31:45 +02:00
|
|
|
await fetchGasEstimates(5)(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{ priceAndTimeEstimatesLastRetrieved: 1000000 }
|
2019-03-22 15:50:12 +01:00
|
|
|
),
|
2019-07-31 22:17:11 +02:00
|
|
|
metamask: { provider: { type: 'ropsten' } },
|
2019-03-22 15:50:12 +01:00
|
|
|
}))
|
2018-10-24 08:31:45 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
global.fetch.getCall(0).args,
|
|
|
|
[
|
|
|
|
'https://ethgasstation.info/json/predictTable.json',
|
|
|
|
{
|
|
|
|
'headers': {},
|
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
|
|
|
'mode': 'cors',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{ type: SET_API_ESTIMATES_LAST_RETRIEVED, value: 2000000 }]
|
|
|
|
)
|
|
|
|
|
2018-11-13 18:36:35 +01:00
|
|
|
const { type: thirdDispatchCallType, value: priceAndTimeEstimateResult } = mockDistpatch.getCall(2).args[0]
|
|
|
|
assert.equal(thirdDispatchCallType, SET_PRICE_AND_TIME_ESTIMATES)
|
|
|
|
assert(priceAndTimeEstimateResult.length < mockPredictTableResponse.length * 3 - 2)
|
|
|
|
assert(!priceAndTimeEstimateResult.find(d => d.expectedTime > 100))
|
2019-05-08 21:51:33 +02:00
|
|
|
assert(!priceAndTimeEstimateResult.find((d, _, a) => a[a + 1] && d.expectedTime > a[a + 1].expectedTime))
|
|
|
|
assert(!priceAndTimeEstimateResult.find((d, _, a) => a[a + 1] && d.gasprice > a[a + 1].gasprice))
|
2018-10-24 08:31:45 +02:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(3).args,
|
|
|
|
[{ type: GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
2018-11-05 18:01:46 +01:00
|
|
|
|
|
|
|
it('should not call fetch if the estimates were retrieved < 75000 ms ago', async () => {
|
2019-07-04 21:18:12 +02:00
|
|
|
const mockDistpatch = sinon.spy()
|
|
|
|
|
2018-11-05 18:01:46 +01:00
|
|
|
await fetchGasEstimates(5)(mockDistpatch, () => ({ gas: Object.assign(
|
|
|
|
{},
|
|
|
|
initState,
|
|
|
|
{
|
|
|
|
priceAndTimeEstimatesLastRetrieved: Date.now(),
|
|
|
|
priceAndTimeEstimates: [{
|
|
|
|
expectedTime: '10',
|
|
|
|
expectedWait: 2,
|
|
|
|
gasprice: 50,
|
|
|
|
}],
|
|
|
|
}
|
2019-03-22 15:50:12 +01:00
|
|
|
),
|
2019-07-31 22:17:11 +02:00
|
|
|
metamask: { provider: { type: 'ropsten' } },
|
2019-03-22 15:50:12 +01:00
|
|
|
}))
|
2018-11-05 18:01:46 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(0).args,
|
|
|
|
[{ type: GAS_ESTIMATE_LOADING_STARTED} ]
|
|
|
|
)
|
|
|
|
assert.equal(global.fetch.callCount, 0)
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(1).args,
|
|
|
|
[{
|
|
|
|
type: SET_PRICE_AND_TIME_ESTIMATES,
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
expectedTime: '10',
|
|
|
|
expectedWait: 2,
|
|
|
|
gasprice: 50,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
mockDistpatch.getCall(2).args,
|
|
|
|
[{ type: GAS_ESTIMATE_LOADING_FINISHED }]
|
|
|
|
)
|
|
|
|
})
|
2018-10-24 08:31:45 +02:00
|
|
|
})
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
describe('gasEstimatesLoadingStarted', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
gasEstimatesLoadingStarted(),
|
|
|
|
{ type: GAS_ESTIMATE_LOADING_STARTED }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('gasEstimatesLoadingFinished', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
gasEstimatesLoadingFinished(),
|
|
|
|
{ type: GAS_ESTIMATE_LOADING_FINISHED }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setPricesAndTimeEstimates', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setPricesAndTimeEstimates('mockPricesAndTimeEstimates'),
|
|
|
|
{ type: SET_PRICE_AND_TIME_ESTIMATES, value: 'mockPricesAndTimeEstimates' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2018-09-09 21:25:54 +02:00
|
|
|
|
|
|
|
describe('setBasicGasEstimateData', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setBasicGasEstimateData('mockBasicEstimatData'),
|
|
|
|
{ type: SET_BASIC_GAS_ESTIMATE_DATA, value: 'mockBasicEstimatData' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setCustomGasPrice', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setCustomGasPrice('mockCustomGasPrice'),
|
|
|
|
{ type: SET_CUSTOM_GAS_PRICE, value: 'mockCustomGasPrice' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setCustomGasLimit', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setCustomGasLimit('mockCustomGasLimit'),
|
|
|
|
{ type: SET_CUSTOM_GAS_LIMIT, value: 'mockCustomGasLimit' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setCustomGasTotal', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setCustomGasTotal('mockCustomGasTotal'),
|
|
|
|
{ type: SET_CUSTOM_GAS_TOTAL, value: 'mockCustomGasTotal' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setCustomGasErrors', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setCustomGasErrors('mockErrorObject'),
|
|
|
|
{ type: SET_CUSTOM_GAS_ERRORS, value: 'mockErrorObject' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-10-24 08:31:45 +02:00
|
|
|
describe('setApiEstimatesLastRetrieved', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
setApiEstimatesLastRetrieved(1234),
|
|
|
|
{ type: SET_API_ESTIMATES_LAST_RETRIEVED, value: 1234 }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-09-09 21:25:54 +02:00
|
|
|
describe('resetCustomGasState', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
resetCustomGasState(),
|
|
|
|
{ type: RESET_CUSTOM_GAS_STATE }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|