Skip to content

Commit

Permalink
mod - fixing tx/history queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Eric Bassett committed Jul 1, 2020
1 parent 63c3470 commit 59cc1d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/index.ts
Expand Up @@ -163,8 +163,8 @@ const txHistory = async (req: Request, res: Response) => {
const [referenceTx, referenceBlock] = (body.after && [body.after.tx, body.after.block]) || [];
const referenceBestBlock = body.untilBlock;

const afterBlockNum = await askBlockNumByTxHash(referenceTx ? referenceTx : "");
const untilBlockNum = await askBlockNumByHash(referenceBestBlock);
const afterBlockNum = await askBlockNumByTxHash(referenceTx );
const maybeTxs = await askTransactionHistory(limit, body.addresses, afterBlockNum, untilBlockNum);
switch(maybeTxs.kind) {
case "ok":
Expand Down Expand Up @@ -211,15 +211,12 @@ const txHistory = async (req: Request, res: Response) => {
}
}
res.send(txs);

return;
case "error":
console.log(maybeTxs.errMsg);
return;
default: return utils.assertNever(maybeTxs);

}

return;
case "error":
console.log(verifiedBody.errMsg);
Expand Down
21 changes: 11 additions & 10 deletions src/services/transactionHistory.ts
Expand Up @@ -65,7 +65,7 @@ export const askTransactionHistory = async (
includedAt: desc
}
) {
id
hash
block {
number
hash
Expand Down Expand Up @@ -100,7 +100,10 @@ export const askTransactionHistory = async (
};


export const askBlockNumByTxHash = async (hash : string): Promise<UtilEither<number>> => {
export const askBlockNumByTxHash = async (hash : string|undefined): Promise<UtilEither<number>> => {
if(!hash)
return {kind:'error', errMsg: 'no hash given'};

const query = `
query BlockNumByTxHash($hashId: Hash32HexString!) {
transactions(
Expand Down Expand Up @@ -138,24 +141,22 @@ export const askBlockNumByHash = async (hash : string) : Promise<UtilEither<numb
query BlockNumByHash($id: Hash32HexString!) {
blocks(
where: {
id: {
hash: {
_eq: $id
}
}
) {
id
number
}
}
`;
const ret = (await axios.post(graphqlEndpoint,
JSON.stringify({ 'query': query, 'variables': {'hashId':hash} }),
contentTypeHeaders));
const ret = await axios.post(graphqlEndpoint,
JSON.stringify({ 'query': query, 'variables': {'id':hash} }),
contentTypeHeaders);
if('data' in ret
&& 'data' in ret.data
&& 'transactions' in ret.data.data
&& ret.data.data.transactions[0]
&& 'block' in ret.data.data.transactions[0]
&& 'blocks' in ret.data.data
&& ret.data.data.blocks[0]
&& 'number' in ret.data.data.transactions[0].block
&& typeof ret.data.data.transaction[0].block.number === 'number')
return {kind:'ok', value:ret.data.data.transaction[0].block.number};
Expand Down
23 changes: 23 additions & 0 deletions tests/txHistory.test.ts
@@ -0,0 +1,23 @@
import axios from 'axios';
import { should } from 'chai';
import { config, Config } from './config'

const endpoint = config.apiUrl;
const s = should();

const addresses =
[ 'DdzFFzCqrht4wFnWC5TJA5UUVE54JC9xZWq589iKyCrWa6hek3KKevyaXzQt6FsdunbkZGzBFQhwZi1MDpijwRoC7kj1MkEPh2Uu5Ssz',
'DdzFFzCqrhtBBX4VvncQ6Zxn8UHawaqSB4jf9EELRBuWUT9gZTmCDWCNTVMotEdof1g26qbrDc8qcHZvtntxR4FaBN1iKxQ5ttjZSZoj',
'DdzFFzCqrht62k6YFcieBUwxkq2CLSi4Pdvt3bd6ghq5P7fTgp8n5pRyQK45gN8A2Udyaj9mFRdK1eUoxy1QjcU5AuCix5uJB3zdBgkf',
'Ae2tdPwUPEZ1zsFUP2eYpyRJooGpYSBzR1jZsdK6ioAqR9vUcBiwQgyeRfB',
'DdzFFzCqrht2Hw9qp1YcqsMJfwjMXiJR46RXU8KFALErRXnjHnjwBPCP8FDjwgUQkZeGghu69YP71ZU67EDsXa5G3g8D2Kr5XZ7Jc42b'];

describe('/txs/history', function() {
this.timeout(10000);
it('returns', async function() {
let postData = { addresses: addresses, untilBlock: "c7bb4e7b6fcfe2887eb4e9521822622a48ceff1f66b4eedbfaa7a09806c0ca04" };
let result = await axios.post(endpoint+"txs/history", postData);
s.exist(result.data);
result.data.should.be.an('array');
});
});

0 comments on commit 59cc1d6

Please sign in to comment.