@lucid-evolution/utils 0.1.11 → 0.1.13

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 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
- const ma = value.multi_asset();
930
- if (ma) {
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.to_cbor_hex();
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,46 @@ 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
+ for (const utxo of utxos) {
1094
+ isSelected = false;
1095
+ for (const [unit, amount] of assetsRequired) {
1096
+ if (Object.hasOwn(utxo.assets, unit)) {
1097
+ const utxoAmount = utxo.assets[unit];
1098
+ if (utxoAmount >= amount) {
1099
+ assetsRequired.delete(unit);
1100
+ } else {
1101
+ assetsRequired.set(unit, amount - utxoAmount);
1102
+ }
1103
+ isSelected = true;
1104
+ }
1105
+ }
1106
+ if (isSelected) {
1107
+ selectedUtxos.push(utxo);
1108
+ }
1109
+ if (assetsRequired.size == 0) {
1110
+ break;
1111
+ }
1112
+ }
1113
+ if (assetsRequired.size > 0)
1114
+ return [];
1115
+ return selectedUtxos;
1116
+ };
1117
+ var sortUTxOs = (utxos, order = "descending") => {
1118
+ return utxos.toSorted((a, b) => {
1119
+ if (a.assets["lovelace"] > b.assets["lovelace"]) {
1120
+ return order === "ascending" ? 1 : -1;
1121
+ }
1122
+ if (a.assets["lovelace"] < b.assets["lovelace"]) {
1123
+ return order === "ascending" ? -1 : 1;
1124
+ }
1125
+ return 0;
1126
+ });
1127
+ };
1128
+ var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
1087
1129
  // Annotate the CommonJS export names for ESM import in node:
1088
1130
  0 && (module.exports = {
1089
1131
  PROTOCOL_PARAMETERS_DEFAULT,
@@ -1108,6 +1150,7 @@ function coresToTxOutputs(outputs) {
1108
1150
  generatePrivateKey,
1109
1151
  generateSeedPhrase,
1110
1152
  getAddressDetails,
1153
+ isEqualUTxO,
1111
1154
  keyHashToCredential,
1112
1155
  mintingPolicyToId,
1113
1156
  nativeFromJson,
@@ -1116,7 +1159,9 @@ function coresToTxOutputs(outputs) {
1116
1159
  networkToId,
1117
1160
  paymentCredentialOf,
1118
1161
  scriptHashToCredential,
1162
+ selectUTxOs,
1119
1163
  slotToUnixTime,
1164
+ sortUTxOs,
1120
1165
  stakeCredentialOf,
1121
1166
  toLabel,
1122
1167
  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: Array<CML.TransactionInput>): OutRef[];
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: Array<CML.TransactionInput>): OutRef[];
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
- const ma = value.multi_asset();
853
- if (ma) {
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.to_cbor_hex();
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,46 @@ 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
+ for (const utxo of utxos) {
1014
+ isSelected = false;
1015
+ for (const [unit, amount] of assetsRequired) {
1016
+ if (Object.hasOwn(utxo.assets, unit)) {
1017
+ const utxoAmount = utxo.assets[unit];
1018
+ if (utxoAmount >= amount) {
1019
+ assetsRequired.delete(unit);
1020
+ } else {
1021
+ assetsRequired.set(unit, amount - utxoAmount);
1022
+ }
1023
+ isSelected = true;
1024
+ }
1025
+ }
1026
+ if (isSelected) {
1027
+ selectedUtxos.push(utxo);
1028
+ }
1029
+ if (assetsRequired.size == 0) {
1030
+ break;
1031
+ }
1032
+ }
1033
+ if (assetsRequired.size > 0)
1034
+ return [];
1035
+ return selectedUtxos;
1036
+ };
1037
+ var sortUTxOs = (utxos, order = "descending") => {
1038
+ return utxos.toSorted((a, b) => {
1039
+ if (a.assets["lovelace"] > b.assets["lovelace"]) {
1040
+ return order === "ascending" ? 1 : -1;
1041
+ }
1042
+ if (a.assets["lovelace"] < b.assets["lovelace"]) {
1043
+ return order === "ascending" ? -1 : 1;
1044
+ }
1045
+ return 0;
1046
+ });
1047
+ };
1048
+ var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
1010
1049
  export {
1011
1050
  PROTOCOL_PARAMETERS_DEFAULT,
1012
1051
  addAssets,
@@ -1030,6 +1069,7 @@ export {
1030
1069
  generatePrivateKey,
1031
1070
  generateSeedPhrase,
1032
1071
  getAddressDetails,
1072
+ isEqualUTxO,
1033
1073
  keyHashToCredential,
1034
1074
  mintingPolicyToId,
1035
1075
  nativeFromJson,
@@ -1038,7 +1078,9 @@ export {
1038
1078
  networkToId,
1039
1079
  paymentCredentialOf,
1040
1080
  scriptHashToCredential,
1081
+ selectUTxOs,
1041
1082
  slotToUnixTime,
1083
+ sortUTxOs,
1042
1084
  stakeCredentialOf,
1043
1085
  toLabel,
1044
1086
  toNativeScript,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/utils",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",