@meshsdk/mesh-csl
Version:
Cardano Off-chain Code APIs built on cardano-serialization-lib
58 lines (57 loc) • 3.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeBFUTxO = exports.locateUTxOWithAddressAndPolicyId = exports.locateUTxOWithPolicyId = exports.locateUTxOWithAddress = exports.getOutputLovelace = void 0;
const csl_1 = require("../csl");
const getOutputLovelace = (output) => {
const amounts = (output === null || output === void 0 ? void 0 : output.amount) || [];
let result = '0';
amounts.forEach((a) => {
if (a.unit === 'lovelace')
result = (a === null || a === void 0 ? void 0 : a.quantity) || '0';
});
return result;
};
exports.getOutputLovelace = getOutputLovelace;
const locateUTxOWithAddress = (output, targetAddress) => output.find((o) => o.address === targetAddress);
exports.locateUTxOWithAddress = locateUTxOWithAddress;
const locateUTxOWithPolicyId = (output, targetPolicyId) => output.find((o) => { var _a; return ((_a = o.amount) === null || _a === void 0 ? void 0 : _a.findIndex((a) => { var _a; return (_a = a.unit) === null || _a === void 0 ? void 0 : _a.startsWith(targetPolicyId); })) !== -1; });
exports.locateUTxOWithPolicyId = locateUTxOWithPolicyId;
const locateUTxOWithAddressAndPolicyId = (output, targetAddress, targetPolicyId) => {
const utxoAtAddress = (0, exports.locateUTxOWithPolicyId)(output, targetPolicyId);
if (utxoAtAddress) {
return (0, exports.locateUTxOWithAddress)([utxoAtAddress], targetAddress);
}
return undefined;
};
exports.locateUTxOWithAddressAndPolicyId = locateUTxOWithAddressAndPolicyId;
const serializeBFUTxO = (bfData) => {
const serUTxOs = [];
const serCollateral = [];
bfData.forEach((blockfrostUtxo) => {
let lovelaceAmount = '0';
const valueAmounts = [];
blockfrostUtxo.amount.forEach((asset) => {
if (asset.unit === 'lovelace') {
lovelaceAmount = asset.quantity;
}
else {
valueAmounts.push(asset);
}
});
const cslValue = csl_1.csl.Value.new(csl_1.csl.BigNum.from_str(lovelaceAmount));
const cslMultiAsset = csl_1.csl.MultiAsset.new();
valueAmounts.forEach((asset) => {
const cslAssets = csl_1.csl.Assets.new();
cslAssets.insert(csl_1.csl.AssetName.new(Buffer.from(asset.unit.slice(56), 'hex')), csl_1.csl.BigNum.from_str(asset.quantity));
cslMultiAsset.insert(csl_1.csl.ScriptHash.from_hex(asset.unit.slice(0, 56)), cslAssets);
});
cslValue.set_multiasset(cslMultiAsset);
const cslUtxo = csl_1.csl.TransactionUnspentOutput.new(csl_1.csl.TransactionInput.new(csl_1.csl.TransactionHash.from_hex(blockfrostUtxo.tx_hash), blockfrostUtxo.tx_index), csl_1.csl.TransactionOutput.new(csl_1.csl.Address.from_bech32(blockfrostUtxo.address), cslValue));
serUTxOs.push(cslUtxo.to_hex());
if (blockfrostUtxo.amount.length === 1 && serCollateral.length < 3) {
serCollateral.push(cslUtxo.to_hex());
}
});
return [serUTxOs, serCollateral];
};
exports.serializeBFUTxO = serializeBFUTxO;
;