Check pending request on Node local transactions (#5564)
* Check pending request on Node * Linting * Liting * Fix tests
This commit is contained in:
parent
710339d0a8
commit
8146cbdae7
@ -16,22 +16,11 @@
|
|||||||
|
|
||||||
import store from 'store';
|
import store from 'store';
|
||||||
|
|
||||||
import { ERROR_CODES } from '~/api/transport/error';
|
|
||||||
|
|
||||||
export const LS_REQUESTS_KEY = '_parity::requests';
|
export const LS_REQUESTS_KEY = '_parity::requests';
|
||||||
|
|
||||||
export default class SavedRequests {
|
export default class SavedRequests {
|
||||||
network = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the network version, and then the related requests
|
|
||||||
*/
|
|
||||||
load (api) {
|
load (api) {
|
||||||
return api.net.version()
|
return this.loadRequests(api)
|
||||||
.then((network) => {
|
|
||||||
this.network = network;
|
|
||||||
return this.loadRequests(api);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return [];
|
return [];
|
||||||
@ -43,28 +32,38 @@ export default class SavedRequests {
|
|||||||
*/
|
*/
|
||||||
loadRequests (api) {
|
loadRequests (api) {
|
||||||
const requests = this._get();
|
const requests = this._get();
|
||||||
|
|
||||||
|
return api.parity.localTransactions()
|
||||||
|
.then((localTransactions) => {
|
||||||
const promises = Object.values(requests).map((request) => {
|
const promises = Object.values(requests).map((request) => {
|
||||||
const { requestId, transactionHash } = request;
|
const { requestId, transactionHash } = request;
|
||||||
|
|
||||||
// The request hasn't been signed yet
|
|
||||||
if (transactionHash) {
|
if (transactionHash) {
|
||||||
return request;
|
// The transaction might be from an other
|
||||||
}
|
// chain
|
||||||
|
if (!localTransactions[transactionHash]) {
|
||||||
return this._requestExists(api, requestId)
|
this.remove(requestId);
|
||||||
.then((exists) => {
|
|
||||||
if (!exists) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
})
|
}
|
||||||
.catch(() => {
|
|
||||||
|
// The request hasn't been signed yet
|
||||||
|
return this._requestExists(api, requestId)
|
||||||
|
.then((exists) => {
|
||||||
|
if (!exists) {
|
||||||
this.remove(requestId);
|
this.remove(requestId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(promises).then((requests) => requests.filter((request) => request));
|
return Promise.all(promises);
|
||||||
|
})
|
||||||
|
.then((requests) => requests.filter((request) => request));
|
||||||
}
|
}
|
||||||
|
|
||||||
save (requestId, requestData) {
|
save (requestId, requestData) {
|
||||||
@ -86,33 +85,23 @@ export default class SavedRequests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_get () {
|
_get () {
|
||||||
const allRequests = store.get(LS_REQUESTS_KEY) || {};
|
return store.get(LS_REQUESTS_KEY) || {};
|
||||||
|
|
||||||
return allRequests[this.network] || {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_set (requests = {}) {
|
_set (requests = {}) {
|
||||||
const allRequests = store.get(LS_REQUESTS_KEY) || {};
|
|
||||||
|
|
||||||
if (Object.keys(requests).length > 0) {
|
if (Object.keys(requests).length > 0) {
|
||||||
allRequests[this.network] = requests;
|
return store.set(LS_REQUESTS_KEY, requests);
|
||||||
} else {
|
|
||||||
delete allRequests[this.network];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return store.set(LS_REQUESTS_KEY, allRequests);
|
return store.remove(LS_REQUESTS_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
_requestExists (api, requestId) {
|
_requestExists (api, requestId) {
|
||||||
return api.parity
|
return api.parity
|
||||||
.checkRequest(requestId)
|
.checkRequest(requestId)
|
||||||
.then(() => true)
|
.then(() => true)
|
||||||
.catch((error) => {
|
.catch(() => {
|
||||||
if (error.code === ERROR_CODES.REQUEST_NOT_FOUND) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,23 +19,25 @@ import store from 'store';
|
|||||||
|
|
||||||
import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests';
|
import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests';
|
||||||
|
|
||||||
const NETWORK_ID = 42;
|
|
||||||
const DEFAULT_REQUEST = {
|
const DEFAULT_REQUEST = {
|
||||||
requestId: '0x1',
|
requestId: '0x1',
|
||||||
transaction: {}
|
transaction: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const api = createApi(NETWORK_ID);
|
const SIGNED_REQUEST = {
|
||||||
const api2 = createApi(1);
|
requestId: '0x2',
|
||||||
|
transactionHash: '0xabcdef',
|
||||||
|
transaction: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const api = createApi();
|
||||||
const savedRequests = new SavedRequests();
|
const savedRequests = new SavedRequests();
|
||||||
|
|
||||||
function createApi (networkVersion) {
|
function createApi () {
|
||||||
return {
|
return {
|
||||||
parity: {
|
parity: {
|
||||||
checkRequest: sinon.stub().resolves()
|
checkRequest: sinon.stub().resolves(),
|
||||||
},
|
localTransactions: sinon.stub().resolves([])
|
||||||
net: {
|
|
||||||
version: sinon.stub().resolves(networkVersion)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -43,9 +45,8 @@ function createApi (networkVersion) {
|
|||||||
describe('views/Application/Requests/savedRequests', () => {
|
describe('views/Application/Requests/savedRequests', () => {
|
||||||
beforeEach((done) => {
|
beforeEach((done) => {
|
||||||
store.set(LS_REQUESTS_KEY, {
|
store.set(LS_REQUESTS_KEY, {
|
||||||
[NETWORK_ID]: {
|
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST,
|
||||||
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST
|
[SIGNED_REQUEST.requestId]: SIGNED_REQUEST
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
savedRequests.load(api)
|
savedRequests.load(api)
|
||||||
@ -75,7 +76,7 @@ describe('views/Application/Requests/savedRequests', () => {
|
|||||||
|
|
||||||
const requests = savedRequests._get();
|
const requests = savedRequests._get();
|
||||||
|
|
||||||
expect(requests).to.deep.equal({});
|
expect(requests[DEFAULT_REQUEST.requestId]).to.be.undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('saves new requests', () => {
|
it('saves new requests', () => {
|
||||||
@ -92,14 +93,7 @@ describe('views/Application/Requests/savedRequests', () => {
|
|||||||
it('loads requests', () => {
|
it('loads requests', () => {
|
||||||
return savedRequests.load(api)
|
return savedRequests.load(api)
|
||||||
.then((requests) => {
|
.then((requests) => {
|
||||||
expect(requests[0]).to.deep.equal(DEFAULT_REQUEST);
|
expect(requests).to.deep.equal([ DEFAULT_REQUEST ]);
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('loads requests from the right network', () => {
|
|
||||||
return savedRequests.load(api2)
|
|
||||||
.then((requests) => {
|
|
||||||
expect(requests).to.deep.equal([]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user