@lucid-evolution/utils 0.1.11 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +50 -4
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +48 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -52,6 +52,7 @@ __export(src_exports, {
|
|
|
52
52
|
generatePrivateKey: () => generatePrivateKey,
|
|
53
53
|
generateSeedPhrase: () => generateSeedPhrase,
|
|
54
54
|
getAddressDetails: () => getAddressDetails,
|
|
55
|
+
isEqualUTxO: () => isEqualUTxO,
|
|
55
56
|
keyHashToCredential: () => keyHashToCredential,
|
|
56
57
|
mintingPolicyToId: () => mintingPolicyToId,
|
|
57
58
|
nativeFromJson: () => nativeFromJson,
|
|
@@ -60,7 +61,9 @@ __export(src_exports, {
|
|
|
60
61
|
networkToId: () => networkToId,
|
|
61
62
|
paymentCredentialOf: () => paymentCredentialOf,
|
|
62
63
|
scriptHashToCredential: () => scriptHashToCredential,
|
|
64
|
+
selectUTxOs: () => selectUTxOs,
|
|
63
65
|
slotToUnixTime: () => slotToUnixTime,
|
|
66
|
+
sortUTxOs: () => sortUTxOs,
|
|
64
67
|
stakeCredentialOf: () => stakeCredentialOf,
|
|
65
68
|
toLabel: () => toLabel,
|
|
66
69
|
toNativeScript: () => toNativeScript,
|
|
@@ -926,8 +929,8 @@ var import_core_utils4 = require("@lucid-evolution/core-utils");
|
|
|
926
929
|
function valueToAssets(value) {
|
|
927
930
|
const assets = {};
|
|
928
931
|
assets["lovelace"] = value.coin();
|
|
929
|
-
|
|
930
|
-
|
|
932
|
+
if (value.has_multiassets()) {
|
|
933
|
+
const ma = value.multi_asset();
|
|
931
934
|
const multiAssets = ma.keys();
|
|
932
935
|
for (let j = 0; j < multiAssets.len(); j++) {
|
|
933
936
|
const policy = multiAssets.get(j);
|
|
@@ -936,7 +939,7 @@ function valueToAssets(value) {
|
|
|
936
939
|
for (let k = 0; k < assetNames.len(); k++) {
|
|
937
940
|
const policyAsset = assetNames.get(k);
|
|
938
941
|
const quantity = policyAssets.get(policyAsset);
|
|
939
|
-
const unit = policy.to_hex() + policyAsset.
|
|
942
|
+
const unit = policy.to_hex() + (0, import_core_utils4.fromText)(policyAsset.to_str());
|
|
940
943
|
assets[unit] = quantity;
|
|
941
944
|
}
|
|
942
945
|
}
|
|
@@ -1045,7 +1048,6 @@ function coreToUtxo(coreUtxo) {
|
|
|
1045
1048
|
...coreToOutRef(CML.TransactionInput.from_cbor_hex(coreUtxo.to_cbor_hex())),
|
|
1046
1049
|
...coreToTxOutput(out)
|
|
1047
1050
|
};
|
|
1048
|
-
out.free();
|
|
1049
1051
|
return utxo;
|
|
1050
1052
|
}
|
|
1051
1053
|
function coresToUtxos(utxos) {
|
|
@@ -1084,6 +1086,47 @@ function coresToTxOutputs(outputs) {
|
|
|
1084
1086
|
}
|
|
1085
1087
|
return result;
|
|
1086
1088
|
}
|
|
1089
|
+
var selectUTxOs = (utxos, totalAssets) => {
|
|
1090
|
+
const selectedUtxos = [];
|
|
1091
|
+
let isSelected = false;
|
|
1092
|
+
const assetsRequired = new Map(Object.entries(totalAssets));
|
|
1093
|
+
const sortedUtxos = sortUTxOs(utxos);
|
|
1094
|
+
for (const utxo of sortedUtxos) {
|
|
1095
|
+
isSelected = false;
|
|
1096
|
+
for (const [unit, amount] of assetsRequired) {
|
|
1097
|
+
if (Object.hasOwn(utxo.assets, unit)) {
|
|
1098
|
+
const utxoAmount = utxo.assets[unit];
|
|
1099
|
+
if (utxoAmount >= amount) {
|
|
1100
|
+
assetsRequired.delete(unit);
|
|
1101
|
+
} else {
|
|
1102
|
+
assetsRequired.set(unit, amount - utxoAmount);
|
|
1103
|
+
}
|
|
1104
|
+
isSelected = true;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
if (isSelected) {
|
|
1108
|
+
selectedUtxos.push(utxo);
|
|
1109
|
+
}
|
|
1110
|
+
if (assetsRequired.size == 0) {
|
|
1111
|
+
break;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
if (assetsRequired.size > 0)
|
|
1115
|
+
return [];
|
|
1116
|
+
return selectedUtxos;
|
|
1117
|
+
};
|
|
1118
|
+
var sortUTxOs = (utxos, order = "descending") => {
|
|
1119
|
+
return utxos.toSorted((a, b) => {
|
|
1120
|
+
if (a.assets["lovelace"] > b.assets["lovelace"]) {
|
|
1121
|
+
return order === "ascending" ? 1 : -1;
|
|
1122
|
+
}
|
|
1123
|
+
if (a.assets["lovelace"] < b.assets["lovelace"]) {
|
|
1124
|
+
return order === "ascending" ? -1 : 1;
|
|
1125
|
+
}
|
|
1126
|
+
return 0;
|
|
1127
|
+
});
|
|
1128
|
+
};
|
|
1129
|
+
var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
|
|
1087
1130
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1088
1131
|
0 && (module.exports = {
|
|
1089
1132
|
PROTOCOL_PARAMETERS_DEFAULT,
|
|
@@ -1108,6 +1151,7 @@ function coresToTxOutputs(outputs) {
|
|
|
1108
1151
|
generatePrivateKey,
|
|
1109
1152
|
generateSeedPhrase,
|
|
1110
1153
|
getAddressDetails,
|
|
1154
|
+
isEqualUTxO,
|
|
1111
1155
|
keyHashToCredential,
|
|
1112
1156
|
mintingPolicyToId,
|
|
1113
1157
|
nativeFromJson,
|
|
@@ -1116,7 +1160,9 @@ function coresToTxOutputs(outputs) {
|
|
|
1116
1160
|
networkToId,
|
|
1117
1161
|
paymentCredentialOf,
|
|
1118
1162
|
scriptHashToCredential,
|
|
1163
|
+
selectUTxOs,
|
|
1119
1164
|
slotToUnixTime,
|
|
1165
|
+
sortUTxOs,
|
|
1120
1166
|
stakeCredentialOf,
|
|
1121
1167
|
toLabel,
|
|
1122
1168
|
toNativeScript,
|
package/dist/index.d.cts
CHANGED
|
@@ -56,9 +56,21 @@ declare function utxosToCores(utxos: UTxO[]): CML.TransactionUnspentOutput[];
|
|
|
56
56
|
declare function coreToUtxo(coreUtxo: CML.TransactionUnspentOutput): UTxO;
|
|
57
57
|
declare function coresToUtxos(utxos: CML.TransactionUnspentOutput[]): UTxO[];
|
|
58
58
|
declare function coreToOutRef(input: CML.TransactionInput): OutRef;
|
|
59
|
-
declare function coresToOutRefs(inputs:
|
|
59
|
+
declare function coresToOutRefs(inputs: CML.TransactionInput[]): OutRef[];
|
|
60
60
|
declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
|
|
61
61
|
declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
|
|
62
|
+
declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
|
|
63
|
+
/**
|
|
64
|
+
* Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
|
|
65
|
+
*
|
|
66
|
+
* @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
|
|
67
|
+
* @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
|
|
68
|
+
* Use "ascending" for ascending order and "descending" for descending order.
|
|
69
|
+
* @returns {UTxO[]} - The sorted array of UTXOs.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
|
|
73
|
+
declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
|
|
62
74
|
|
|
63
75
|
declare function valueToAssets(value: CML.Value): Assets;
|
|
64
76
|
declare function assetsToValue(assets: Assets): CML.Value;
|
|
@@ -78,4 +90,4 @@ declare function fromUnit(unit: Unit): {
|
|
|
78
90
|
declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
|
|
79
91
|
declare function addAssets(...assets: Assets[]): Assets;
|
|
80
92
|
|
|
81
|
-
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, slotToUnixTime, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
|
93
|
+
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
package/dist/index.d.ts
CHANGED
|
@@ -56,9 +56,21 @@ declare function utxosToCores(utxos: UTxO[]): CML.TransactionUnspentOutput[];
|
|
|
56
56
|
declare function coreToUtxo(coreUtxo: CML.TransactionUnspentOutput): UTxO;
|
|
57
57
|
declare function coresToUtxos(utxos: CML.TransactionUnspentOutput[]): UTxO[];
|
|
58
58
|
declare function coreToOutRef(input: CML.TransactionInput): OutRef;
|
|
59
|
-
declare function coresToOutRefs(inputs:
|
|
59
|
+
declare function coresToOutRefs(inputs: CML.TransactionInput[]): OutRef[];
|
|
60
60
|
declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
|
|
61
61
|
declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
|
|
62
|
+
declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
|
|
63
|
+
/**
|
|
64
|
+
* Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
|
|
65
|
+
*
|
|
66
|
+
* @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
|
|
67
|
+
* @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
|
|
68
|
+
* Use "ascending" for ascending order and "descending" for descending order.
|
|
69
|
+
* @returns {UTxO[]} - The sorted array of UTXOs.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
|
|
73
|
+
declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
|
|
62
74
|
|
|
63
75
|
declare function valueToAssets(value: CML.Value): Assets;
|
|
64
76
|
declare function assetsToValue(assets: Assets): CML.Value;
|
|
@@ -78,4 +90,4 @@ declare function fromUnit(unit: Unit): {
|
|
|
78
90
|
declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
|
|
79
91
|
declare function addAssets(...assets: Assets[]): Assets;
|
|
80
92
|
|
|
81
|
-
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, slotToUnixTime, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
|
93
|
+
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
package/dist/index.js
CHANGED
|
@@ -845,12 +845,12 @@ function slotToUnixTime(network, slot) {
|
|
|
845
845
|
}
|
|
846
846
|
|
|
847
847
|
// src/value.ts
|
|
848
|
-
import { toText } from "@lucid-evolution/core-utils";
|
|
848
|
+
import { fromText, toText } from "@lucid-evolution/core-utils";
|
|
849
849
|
function valueToAssets(value) {
|
|
850
850
|
const assets = {};
|
|
851
851
|
assets["lovelace"] = value.coin();
|
|
852
|
-
|
|
853
|
-
|
|
852
|
+
if (value.has_multiassets()) {
|
|
853
|
+
const ma = value.multi_asset();
|
|
854
854
|
const multiAssets = ma.keys();
|
|
855
855
|
for (let j = 0; j < multiAssets.len(); j++) {
|
|
856
856
|
const policy = multiAssets.get(j);
|
|
@@ -859,7 +859,7 @@ function valueToAssets(value) {
|
|
|
859
859
|
for (let k = 0; k < assetNames.len(); k++) {
|
|
860
860
|
const policyAsset = assetNames.get(k);
|
|
861
861
|
const quantity = policyAssets.get(policyAsset);
|
|
862
|
-
const unit = policy.to_hex() + policyAsset.
|
|
862
|
+
const unit = policy.to_hex() + fromText(policyAsset.to_str());
|
|
863
863
|
assets[unit] = quantity;
|
|
864
864
|
}
|
|
865
865
|
}
|
|
@@ -968,7 +968,6 @@ function coreToUtxo(coreUtxo) {
|
|
|
968
968
|
...coreToOutRef(CML.TransactionInput.from_cbor_hex(coreUtxo.to_cbor_hex())),
|
|
969
969
|
...coreToTxOutput(out)
|
|
970
970
|
};
|
|
971
|
-
out.free();
|
|
972
971
|
return utxo;
|
|
973
972
|
}
|
|
974
973
|
function coresToUtxos(utxos) {
|
|
@@ -1007,6 +1006,47 @@ function coresToTxOutputs(outputs) {
|
|
|
1007
1006
|
}
|
|
1008
1007
|
return result;
|
|
1009
1008
|
}
|
|
1009
|
+
var selectUTxOs = (utxos, totalAssets) => {
|
|
1010
|
+
const selectedUtxos = [];
|
|
1011
|
+
let isSelected = false;
|
|
1012
|
+
const assetsRequired = new Map(Object.entries(totalAssets));
|
|
1013
|
+
const sortedUtxos = sortUTxOs(utxos);
|
|
1014
|
+
for (const utxo of sortedUtxos) {
|
|
1015
|
+
isSelected = false;
|
|
1016
|
+
for (const [unit, amount] of assetsRequired) {
|
|
1017
|
+
if (Object.hasOwn(utxo.assets, unit)) {
|
|
1018
|
+
const utxoAmount = utxo.assets[unit];
|
|
1019
|
+
if (utxoAmount >= amount) {
|
|
1020
|
+
assetsRequired.delete(unit);
|
|
1021
|
+
} else {
|
|
1022
|
+
assetsRequired.set(unit, amount - utxoAmount);
|
|
1023
|
+
}
|
|
1024
|
+
isSelected = true;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
if (isSelected) {
|
|
1028
|
+
selectedUtxos.push(utxo);
|
|
1029
|
+
}
|
|
1030
|
+
if (assetsRequired.size == 0) {
|
|
1031
|
+
break;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (assetsRequired.size > 0)
|
|
1035
|
+
return [];
|
|
1036
|
+
return selectedUtxos;
|
|
1037
|
+
};
|
|
1038
|
+
var sortUTxOs = (utxos, order = "descending") => {
|
|
1039
|
+
return utxos.toSorted((a, b) => {
|
|
1040
|
+
if (a.assets["lovelace"] > b.assets["lovelace"]) {
|
|
1041
|
+
return order === "ascending" ? 1 : -1;
|
|
1042
|
+
}
|
|
1043
|
+
if (a.assets["lovelace"] < b.assets["lovelace"]) {
|
|
1044
|
+
return order === "ascending" ? -1 : 1;
|
|
1045
|
+
}
|
|
1046
|
+
return 0;
|
|
1047
|
+
});
|
|
1048
|
+
};
|
|
1049
|
+
var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
|
|
1010
1050
|
export {
|
|
1011
1051
|
PROTOCOL_PARAMETERS_DEFAULT,
|
|
1012
1052
|
addAssets,
|
|
@@ -1030,6 +1070,7 @@ export {
|
|
|
1030
1070
|
generatePrivateKey,
|
|
1031
1071
|
generateSeedPhrase,
|
|
1032
1072
|
getAddressDetails,
|
|
1073
|
+
isEqualUTxO,
|
|
1033
1074
|
keyHashToCredential,
|
|
1034
1075
|
mintingPolicyToId,
|
|
1035
1076
|
nativeFromJson,
|
|
@@ -1038,7 +1079,9 @@ export {
|
|
|
1038
1079
|
networkToId,
|
|
1039
1080
|
paymentCredentialOf,
|
|
1040
1081
|
scriptHashToCredential,
|
|
1082
|
+
selectUTxOs,
|
|
1041
1083
|
slotToUnixTime,
|
|
1084
|
+
sortUTxOs,
|
|
1042
1085
|
stakeCredentialOf,
|
|
1043
1086
|
toLabel,
|
|
1044
1087
|
toNativeScript,
|