floBlockchainAPI v2.3.2a

sendTx:
- check total balance before processing utxos
- reject "Insufficient FLO: Some UTXOs are unconfirmed" when balance is there but some utxo are used/unconfimed
This commit is contained in:
sairajzero 2022-05-21 22:19:54 +05:30
parent 8603cde17e
commit 5d7d3bdb53
2 changed files with 47 additions and 44 deletions

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floBlockchainAPI v2.3.2 (function(EXPORTS) { //floBlockchainAPI v2.3.2a
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/ /* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
'use strict'; 'use strict';
const floBlockchainAPI = EXPORTS; const floBlockchainAPI = EXPORTS;
@ -125,48 +125,52 @@
else if (typeof sendAmt !== 'number' || sendAmt <= 0) else if (typeof sendAmt !== 'number' || sendAmt <= 0)
return reject(`Invalid sendAmt : ${sendAmt}`); return reject(`Invalid sendAmt : ${sendAmt}`);
//get unconfirmed tx list getBalance(senderAddr).then(balance => {
promisedAPI(`api/addr/${senderAddr}`).then(result => { var fee = DEFAULT.fee;
readTxs(senderAddr, 0, result.unconfirmedTxApperances).then(result => { if (balance < sendAmt + fee)
let unconfirmedSpent = {}; return reject("Insufficient FLO balance!");
for (let tx of result.items) //get unconfirmed tx list
if (tx.confirmations == 0) promisedAPI(`api/addr/${senderAddr}`).then(result => {
for (let vin of tx.vin) readTxs(senderAddr, 0, result.unconfirmedTxApperances).then(result => {
if (vin.addr === senderAddr) { let unconfirmedSpent = {};
if (Array.isArray(unconfirmedSpent[vin.txid])) for (let tx of result.items)
unconfirmedSpent[vin.txid].push(vin.vout); if (tx.confirmations == 0)
else for (let vin of tx.vin)
unconfirmedSpent[vin.txid] = [vin.vout]; if (vin.addr === senderAddr) {
} if (Array.isArray(unconfirmedSpent[vin.txid]))
//get utxos list unconfirmedSpent[vin.txid].push(vin.vout);
promisedAPI(`api/addr/${senderAddr}/utxo`).then(utxos => { else
//form/construct the transaction data unconfirmedSpent[vin.txid] = [vin.vout];
var trx = bitjs.transaction(); }
var utxoAmt = 0.0; //get utxos list
var fee = DEFAULT.fee; promisedAPI(`api/addr/${senderAddr}/utxo`).then(utxos => {
for (var i = utxos.length - 1; //form/construct the transaction data
(i >= 0) && (utxoAmt < sendAmt + fee); i--) { var trx = bitjs.transaction();
//use only utxos with confirmations (strict_utxo mode) var utxoAmt = 0.0;
if (utxos[i].confirmations || !strict_utxo) { for (var i = utxos.length - 1;
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout)) (i >= 0) && (utxoAmt < sendAmt + fee); i--) {
continue; //A transaction has already used the utxo, but is unconfirmed. //use only utxos with confirmations (strict_utxo mode)
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey); if (utxos[i].confirmations || !strict_utxo) {
utxoAmt += utxos[i].amount; if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout))
}; continue; //A transaction has already used the utxo, but is unconfirmed.
} trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
if (utxoAmt < sendAmt + fee) utxoAmt += utxos[i].amount;
reject("Insufficient FLO balance!"); };
else { }
trx.addoutput(receiverAddr, sendAmt); if (utxoAmt < sendAmt + fee)
var change = utxoAmt - sendAmt - fee; reject("Insufficient FLO: Some UTXOs are unconfirmed");
if (change > 0) else {
trx.addoutput(senderAddr, change); trx.addoutput(receiverAddr, sendAmt);
trx.addflodata(floData.replace(/\n/g, ' ')); var change = utxoAmt - sendAmt - fee;
var signedTxHash = trx.sign(privKey, 1); if (change > 0)
broadcastTx(signedTxHash) trx.addoutput(senderAddr, change);
.then(txid => resolve(txid)) trx.addflodata(floData.replace(/\n/g, ' '));
.catch(error => reject(error)) var signedTxHash = trx.sign(privKey, 1);
} broadcastTx(signedTxHash)
.then(txid => resolve(txid))
.catch(error => reject(error))
}
}).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))

View File

@ -8,7 +8,6 @@
const floGlobals = { const floGlobals = {
blockchain: "FLO", blockchain: "FLO",
adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf", adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf",
fee: 0.01,
application: "TEST_MODE", application: "TEST_MODE",
} }
</script> </script>