Fixing All, Sent, Mining classification
This commit is contained in:
parent
a0df5f1986
commit
637c14952f
@ -49,63 +49,80 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatTx(address, tx) {
|
function formatTx(address, tx) {
|
||||||
let result = {
|
const result = {
|
||||||
time: tx.time,
|
time: tx.time,
|
||||||
block: tx.blockheight,
|
block: tx.blockheight,
|
||||||
blockhash: tx.blockhash,
|
blockhash: tx.blockhash,
|
||||||
txid: tx.txid,
|
txid: tx.txid,
|
||||||
floData: tx.floData,
|
floData: tx.floData,
|
||||||
confirmations: tx.confirmations
|
confirmations: tx.confirmations
|
||||||
}
|
};
|
||||||
|
|
||||||
//format receivers
|
// ---- Receivers (outputs) ----
|
||||||
let receivers = {};
|
const receivers = {};
|
||||||
for (let vout of tx.vout) {
|
for (const vout of tx.vout || []) {
|
||||||
if (vout.scriptPubKey.isAddress) {
|
const outAddrs =
|
||||||
let id = vout.scriptPubKey.addresses[0];
|
(vout.addresses && vout.addresses.length ? vout.addresses :
|
||||||
if (id in receivers)
|
vout.scriptPubKey && vout.scriptPubKey.addresses ? vout.scriptPubKey.addresses : null);
|
||||||
receivers[id] += vout.value;
|
if (outAddrs && outAddrs.length) {
|
||||||
else receivers[id] = vout.value;
|
const id = outAddrs[0];
|
||||||
}
|
receivers[id] = (receivers[id] || 0) + Number(vout.value || 0);
|
||||||
}
|
}
|
||||||
result.receivers = receivers;
|
}
|
||||||
//format senders (or mined)
|
result.receivers = receivers;
|
||||||
if (!tx.vin[0].isAddress) { //mined (ie, coinbase)
|
|
||||||
let coinbase = tx.vin[0].coinbase;
|
|
||||||
result.mine = coinbase;
|
|
||||||
result.mined = { [coinbase]: tx.valueOut }
|
|
||||||
} else {
|
|
||||||
result.sender = tx.vin[0].addresses[0];
|
|
||||||
result.receiver = tx.vout[0].scriptPubKey.addresses[0];
|
|
||||||
result.fees = tx.fees;
|
|
||||||
let senders = {};
|
|
||||||
for (let vin of tx.vin) {
|
|
||||||
if (vin.isAddress) {
|
|
||||||
let id = vin.addresses[0];
|
|
||||||
if (id in senders)
|
|
||||||
senders[id] += vin.value;
|
|
||||||
else senders[id] = vin.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.senders = senders;
|
|
||||||
|
|
||||||
//remove change amounts
|
// ---- Coinbase vs normal ----
|
||||||
for (let id in senders) {
|
const isCoinbase = !!(tx.vin && tx.vin[0] && tx.vin[0].coinbase);
|
||||||
if (id in receivers) {
|
|
||||||
if (senders[id] > receivers[id]) {
|
|
||||||
senders[id] -= receivers[id];
|
|
||||||
delete receivers[id];
|
|
||||||
} else if (senders[id] < receivers[id]) { //&& id != address
|
|
||||||
receivers[id] -= senders[id];
|
|
||||||
delete senders[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (isCoinbase) {
|
||||||
|
const coinbase = tx.vin[0].coinbase; // string
|
||||||
|
result.mine = coinbase;
|
||||||
|
result.mined = { [coinbase]: Number(tx.valueOut || 0) };
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Normal tx: senders, fees, first-party heuristics ----
|
||||||
|
result.fees = tx.fees;
|
||||||
|
|
||||||
|
const firstInAddr = (tx.vin && tx.vin[0] && tx.vin[0].addresses && tx.vin[0].addresses[0]) || undefined;
|
||||||
|
const firstOutAddrs =
|
||||||
|
(tx.vout && tx.vout[0] &&
|
||||||
|
((tx.vout[0].addresses && tx.vout[0].addresses[0]) ||
|
||||||
|
(tx.vout[0].scriptPubKey && tx.vout[0].scriptPubKey.addresses && tx.vout[0].scriptPubKey.addresses[0]))) || undefined;
|
||||||
|
if (firstInAddr) result.sender = firstInAddr;
|
||||||
|
if (firstOutAddrs) result.receiver = firstOutAddrs;
|
||||||
|
|
||||||
|
const senders = {};
|
||||||
|
for (const vin of tx.vin || []) {
|
||||||
|
const inAddrs = vin.addresses;
|
||||||
|
if (inAddrs && inAddrs.length) {
|
||||||
|
const id = inAddrs[0];
|
||||||
|
senders[id] = (senders[id] || 0) + Number(vin.value || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.senders = senders;
|
||||||
|
|
||||||
|
// ---- Remove change (net flow) ----
|
||||||
|
for (const id of Object.keys(senders)) {
|
||||||
|
if (receivers[id] != null) {
|
||||||
|
if (senders[id] > receivers[id]) {
|
||||||
|
senders[id] -= receivers[id];
|
||||||
|
delete receivers[id];
|
||||||
|
} else if (senders[id] < receivers[id]) {
|
||||||
|
receivers[id] -= senders[id];
|
||||||
|
delete senders[id];
|
||||||
|
} else {
|
||||||
|
// equal -> cancel both
|
||||||
|
delete senders[id];
|
||||||
|
delete receivers[id];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
floWebWallet.listTransactions = function (address, page_options = {}) {
|
floWebWallet.listTransactions = function (address, page_options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let options = {};
|
let options = {};
|
||||||
@ -141,4 +158,4 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
})(window.floWebWallet = {});
|
})(window.floWebWallet = {});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user