Skip to content

Commit

Permalink
Merge pull request #81 from Bitcoin-com/ct-tokenUtxoDetails
Browse files Browse the repository at this point in the history
tokenUtxoDetails: fixed bug where UTXO array elems would switch
  • Loading branch information
christroutner authored Dec 16, 2019
2 parents 8b267af + 28a1c35 commit 0de9b5c
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 116 deletions.
3 changes: 2 additions & 1 deletion lib/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ var __extends = (this && this.__extends) || (function () {
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/SLPDB.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/TokenType1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down
38 changes: 22 additions & 16 deletions lib/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ var __extends = (this && this.__extends) || (function () {
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down Expand Up @@ -546,7 +547,7 @@ var Util = /** @class */ (function (_super) {
// with additional SLP information attached.
Util.prototype.tokenUtxoDetails = function (utxos) {
return __awaiter(this, void 0, void 0, function () {
var i, thisUtxo, txids, validations, _loop_2, this_2, i, error_11;
var i, thisUtxo, txids, validations, outAry, _loop_2, this_2, i, error_11;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -568,21 +569,26 @@ var Util = /** @class */ (function (_super) {
txids = utxos.map(function (x) { return x.txid; });
return [4 /*yield*/, this.validateTxid(txids)
// console.log(`validations: ${JSON.stringify(validations, null, 2)}`)
// Extract the boolean result
];
case 1:
validations = _a.sent();
// console.log(`validations: ${JSON.stringify(validations, null, 2)}`)
// Extract the boolean result
validations = validations.map(function (x) { return x.valid; });
outAry = [] // Output Array
;
_loop_2 = function (i) {
var thisValidation, thisUtxo, slpData, genesisData, voutMatch, genesisData;
var thisUtxo, thisValidation, slpData, genesisData, voutMatch, genesisData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
thisValidation = validations[i];
thisUtxo = utxos[i];
if (!thisValidation) return [3 /*break*/, 7];
thisValidation = validations.filter(function (x) { return x.txid === thisUtxo.txid; });
// console.log(
// `thisValidation: ${JSON.stringify(thisValidation, null, 2)}`
// )
// If the utxo is not SLP, then skip the loop.
if (!thisValidation[0].valid) {
outAry[i] = false;
return [2 /*return*/, "continue"];
}
return [4 /*yield*/, this_2.decodeOpReturn(thisUtxo.txid)
// console.log(`slpData: ${JSON.stringify(slpData, null, 2)}`)
// Handle Genesis SLP transactions.
Expand All @@ -596,7 +602,7 @@ var Util = /** @class */ (function (_super) {
thisUtxo.vout !== 1 // UTXO is not the reciever of the genesis or mint tokens.
) {
// Can safely be marked as false.
validations[i] = false;
outAry[i] = false;
}
// If this is a valid SLP UTXO, then return the decoded OP_RETURN data.
else {
Expand All @@ -608,15 +614,15 @@ var Util = /** @class */ (function (_super) {
thisUtxo.tokenDocumentHash = slpData.documentHash;
thisUtxo.decimals = slpData.decimals;
// something
validations[i] = thisUtxo;
outAry[i] = thisUtxo;
}
}
if (!(slpData.transactionType === "mint")) return [3 /*break*/, 4];
if (!(thisUtxo.vout !== slpData.mintBatonVout && // UTXO is not a mint baton output.
thisUtxo.vout !== 1) // UTXO is not the reciever of the genesis or mint tokens.
) return [3 /*break*/, 2]; // UTXO is not the reciever of the genesis or mint tokens.
// Can safely be marked as false.
validations[i] = false;
outAry[i] = false;
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this_2.decodeOpReturn(slpData.tokenId)
// Hydrate the UTXO object with information about the SLP token.
Expand All @@ -637,13 +643,13 @@ var Util = /** @class */ (function (_super) {
// Calculate the real token quantity.
thisUtxo.tokenQty =
slpData.quantity / Math.pow(10, thisUtxo.decimals);
validations[i] = thisUtxo;
outAry[i] = thisUtxo;
_a.label = 4;
case 4:
if (!(slpData.transactionType === "send")) return [3 /*break*/, 7];
voutMatch = slpData.spendData.filter(function (x) { return thisUtxo.vout === x.vout; });
if (!(voutMatch.length === 0)) return [3 /*break*/, 5];
validations[i] = false;
outAry[i] = false;
return [3 /*break*/, 7];
case 5: return [4 /*yield*/, this_2.decodeOpReturn(slpData.tokenId)
// console.log(
Expand All @@ -667,7 +673,7 @@ var Util = /** @class */ (function (_super) {
// Calculate the real token quantity.
thisUtxo.tokenQty =
voutMatch[0].quantity / Math.pow(10, thisUtxo.decimals);
validations[i] = thisUtxo;
outAry[i] = thisUtxo;
_a.label = 7;
case 7: return [2 /*return*/];
}
Expand All @@ -685,7 +691,7 @@ var Util = /** @class */ (function (_super) {
case 4:
i++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/, validations];
case 5: return [2 /*return*/, outAry];
case 6:
error_11 = _a.sent();
if (error_11.response && error_11.response.data)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"mkdirp": "^0.5.1",
"node-emoji": "^1.8.1",
"repl.history": "^0.1.4",
"slpjs": "0.22.0",
"slpjs": "^0.23.0",
"touch": "^3.1.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 0de9b5c

Please sign in to comment.