diff --git a/js/src/redux/providers/requestsActions.js b/js/src/redux/providers/requestsActions.js index d31f0d1b3..970bcba91 100644 --- a/js/src/redux/providers/requestsActions.js +++ b/js/src/redux/providers/requestsActions.js @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +import BigNumber from 'bignumber.js'; + import { outTransaction } from '~/api/format/output'; import { trackRequest as trackRequestUtil, parseTransactionReceipt } from '~/util/tx'; import SavedRequests from '~/views/Application/Requests/savedRequests'; @@ -29,7 +31,7 @@ export const init = (api) => (dispatch) => { dispatch(watchRequest(request)); }); - api.on('connected', () => { + api.once('connected', () => { savedRequests.load(api).then((requests) => { requests.forEach((request) => dispatch(watchRequest(request))); }); @@ -48,7 +50,9 @@ export const watchRequest = (request) => (dispatch, getState) => { export const trackRequest = (requestId, { transactionHash = null } = {}) => (dispatch, getState) => { const { api } = getState(); - trackRequestUtil(api, { requestId, transactionHash }, (error, data) => { + trackRequestUtil(api, { requestId, transactionHash }, (error, _data = {}) => { + const data = { ..._data }; + if (error) { console.error(error); return dispatch(setRequest(requestId, { error })); @@ -61,6 +65,9 @@ export const trackRequest = (requestId, { transactionHash = null } = {}) => (dis const requestData = requests[requestId]; let blockSubscriptionId = -1; + // Set the block height to 0 at the beggining + data.blockHeight = new BigNumber(0); + // If the request was a contract deployment, // then add the contract with the saved metadata to the account if (requestData.metadata && requestData.metadata.deployment) { diff --git a/js/src/views/Application/Requests/requests.js b/js/src/views/Application/Requests/requests.js index 2c2224ea0..1054cc4e2 100644 --- a/js/src/views/Application/Requests/requests.js +++ b/js/src/views/Application/Requests/requests.js @@ -145,7 +145,7 @@ class Requests extends Component { />
diff --git a/js/src/views/Application/Requests/savedRequests.js b/js/src/views/Application/Requests/savedRequests.js index ee5f78811..54f1a7a9a 100644 --- a/js/src/views/Application/Requests/savedRequests.js +++ b/js/src/views/Application/Requests/savedRequests.js @@ -21,9 +21,28 @@ import { ERROR_CODES } from '~/api/transport/error'; export const LS_REQUESTS_KEY = '_parity::requests'; export default class SavedRequests { - load (api) { - const requests = this._get(); + network = null; + /** + * Load the network version, and then the related requests + */ + load (api) { + return api.net.version() + .then((network) => { + this.network = network; + return this.loadRequests(api); + }) + .catch((error) => { + console.error(error); + return []; + }); + } + + /** + * Load the requests of the current network + */ + loadRequests (api) { + const requests = this._get(); const promises = Object.values(requests).map((request) => { const { requestId, transactionHash } = request; @@ -67,11 +86,21 @@ export default class SavedRequests { } _get () { - return store.get(LS_REQUESTS_KEY) || {}; + const allRequests = store.get(LS_REQUESTS_KEY) || {}; + + return allRequests[this.network] || {}; } _set (requests = {}) { - return store.set(LS_REQUESTS_KEY, requests); + const allRequests = store.get(LS_REQUESTS_KEY) || {}; + + if (Object.keys(requests).length > 0) { + allRequests[this.network] = requests; + } else { + delete allRequests[this.network]; + } + + return store.set(LS_REQUESTS_KEY, allRequests); } _requestExists (api, requestId) { diff --git a/js/src/views/Application/Requests/savedRequests.spec.js b/js/src/views/Application/Requests/savedRequests.spec.js index 7563c93c8..acf3adc39 100644 --- a/js/src/views/Application/Requests/savedRequests.spec.js +++ b/js/src/views/Application/Requests/savedRequests.spec.js @@ -19,27 +19,37 @@ import store from 'store'; import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests'; +const NETWORK_ID = 42; const DEFAULT_REQUEST = { requestId: '0x1', transaction: {} }; -const api = createApi(); +const api = createApi(NETWORK_ID); +const api2 = createApi(1); const savedRequests = new SavedRequests(); -function createApi () { +function createApi (networkVersion) { return { parity: { checkRequest: sinon.stub().resolves() + }, + net: { + version: sinon.stub().resolves(networkVersion) } }; } describe('views/Application/Requests/savedRequests', () => { - beforeEach(() => { + beforeEach((done) => { store.set(LS_REQUESTS_KEY, { - [DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST + [NETWORK_ID]: { + [DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST + } }); + + savedRequests.load(api) + .then(() => done()); }); afterEach(() => { @@ -85,4 +95,11 @@ describe('views/Application/Requests/savedRequests', () => { expect(requests[0]).to.deep.equal(DEFAULT_REQUEST); }); }); + + it('loads requests from the right network', () => { + return savedRequests.load(api2) + .then((requests) => { + expect(requests).to.deep.equal([]); + }); + }); });