Skip to content

Commit

Permalink
move to/from/value logic to leap-core
Browse files Browse the repository at this point in the history
  • Loading branch information
troggy committed Jul 16, 2019
1 parent f817450 commit 666cf42
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 67 deletions.
10 changes: 6 additions & 4 deletions src/api/methods/getTransactionReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
* found in the LICENSE file in the root directory of this source tree.
*/
const { Tx } = require('leap-core');
const getTxValueAndSource = require('./utils/getTxValueAndSource');
const getPrevTx = require('./utils/getPrevTx');

module.exports = async (db, hash) => {
const txDoc = await db.getTransaction(hash);
if (!txDoc) return null;

const { txData, blockHash, height, txPos } = txDoc;

const { from, to } = await getTxValueAndSource(db, Tx.fromJSON(txData));
const tx = Tx.fromJSON(txData);

const prevTx = await getPrevTx(db, tx);

return {
transactionHash: hash,
transactionIndex: txPos,
blockHash,
blockNumber: `0x${height.toString(16)}`,
from,
to,
from: tx.from(prevTx),
to: tx.to(),
cumulativeGasUsed: '0x0',
gasUsed: '0x0',
contractAddress: null,
Expand Down
6 changes: 3 additions & 3 deletions src/api/methods/getTransactionReceipt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PRIV1 =
'0x9b63fe8147edb8d251a6a66fd18c0ed73873da9fff3f08ea202e1c0a8ead7311';
const PRIV2 =
'0xea3a59a673a9f7e74ad65e92ee04c2330fc5b905d0fa47bb2ae36c0b94af61cd';
const A1 = '0xB8205608d54cb81f44F263bE086027D8610F3C94';
const A1 = '0xb8205608d54cb81f44f263be086027d8610f3c94';
const A2 = '0xD56F7dFCd2BaFfBC1d885F0266b21C7F2912020c';

describe('getTransactionReceipt', () => {
Expand All @@ -30,11 +30,11 @@ describe('getTransactionReceipt', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV2);
).signAll(PRIV1);

const data = {
[tx.hash()]: {
Expand Down
10 changes: 6 additions & 4 deletions src/api/methods/txResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
const getTxValueAndSource = require('./utils/getTxValueAndSource');
const getPrevTx = require('./utils/getPrevTx');

module.exports = async (db, tx, blockHash, height, txPos) => {
const { value, color, from, to } = await getTxValueAndSource(db, tx);
const prevTx = await getPrevTx(db, tx);

const { value, color } = tx.value(prevTx);

return {
value: `0x${value.toString(16)}`,
color,
hash: tx.hash(),
from,
from: tx.from(prevTx),
raw: tx.hex(),
blockHash,
blockNumber: `0x${height.toString(16)}`,
transactionIndex: txPos,
to,
to: tx.to(),
gas: '0x0',
gasPrice: '0x0',
nonce: 0,
Expand Down
16 changes: 8 additions & 8 deletions src/api/methods/txResponse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PRIV1 =
'0x9b63fe8147edb8d251a6a66fd18c0ed73873da9fff3f08ea202e1c0a8ead7311';
const PRIV2 =
'0xea3a59a673a9f7e74ad65e92ee04c2330fc5b905d0fa47bb2ae36c0b94af61cd';
const A1 = '0xB8205608d54cb81f44F263bE086027D8610F3C94';
const A1 = '0xb8205608d54cb81f44f263be086027d8610f3c94';
const A2 = '0xD56F7dFCd2BaFfBC1d885F0266b21C7F2912020c';

const fakeDb = tx => ({
Expand All @@ -24,11 +24,11 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV2);
).signAll(PRIV1);
const blockHash = '0x0';
const height = 0;
const txIndex = 0;
Expand Down Expand Up @@ -67,11 +67,11 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV2);
).signAll(PRIV1);
const blockHash = '0x0';
const height = 0;
const txIndex = 0;
Expand All @@ -84,7 +84,7 @@ describe('txResponse', () => {
);
expect(response).toEqual({
hash: tx.hash(),
from: '0x0000000000000000000000000000000000000000',
from: '0xb8205608d54cb81f44f263be086027d8610f3c94',
to: A2,
value: '0x64', // hex 100
color: 0,
Expand All @@ -110,9 +110,9 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.exit(new Input(new Outpoint(prevTx.hash(), 0))).signAll(
PRIV2
PRIV1
);
const blockHash = '0x0';
const height = 0;
Expand Down
14 changes: 14 additions & 0 deletions src/api/methods/utils/getPrevTx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Tx } = require('leap-core');
const { bufferToHex } = require('ethereumjs-util');

module.exports = async (db, tx) => {
if (tx.inputs && tx.inputs.length > 0 && tx.inputs[0].prevout) {
const prevTxHash = bufferToHex(tx.inputs[0].prevout.hash);
const txDoc = await db.getTransaction(prevTxHash);
if (txDoc) {
return Tx.fromJSON(txDoc.txData);
}
}

return null;
};
48 changes: 0 additions & 48 deletions src/api/methods/utils/getTxValueAndSource.js

This file was deleted.

0 comments on commit 666cf42

Please sign in to comment.