mainnet-js 1.1.8 → 1.1.10

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/src/wallet/Wif.ts CHANGED
@@ -61,7 +61,11 @@ import {
61
61
  BalanceResponse,
62
62
  } from "../util/balanceObjectFromSatoshi.js";
63
63
  import { checkWifNetwork } from "../util/checkWifNetwork.js";
64
- import { deriveCashaddr, deriveTokenaddr } from "../util/deriveCashaddr.js";
64
+ import {
65
+ deriveCashaddr,
66
+ deriveTokenaddr,
67
+ toTokenaddr,
68
+ } from "../util/deriveCashaddr.js";
65
69
  import {
66
70
  derivePrefix,
67
71
  derivePublicKeyHash,
@@ -1165,21 +1169,49 @@ export class Wallet extends BaseWallet {
1165
1169
  (prev, cur) => prev + cur.amount,
1166
1170
  0
1167
1171
  );
1168
- const change = inputAmountSum - outputAmountSum;
1169
- if (change < 0) {
1172
+
1173
+ const diff = inputAmountSum - outputAmountSum;
1174
+ if (diff < 0) {
1170
1175
  throw new Error("Not enough token amount to send");
1171
1176
  }
1172
- if (change > 0) {
1173
- outputs.push(
1174
- new TokenSendRequest({
1175
- cashaddr: changeAddress || this.tokenaddr!,
1176
- amount: change,
1177
- tokenId: tokenId,
1178
- commitment: tokenOutputs[0].commitment,
1179
- capability: tokenOutputs[0].capability,
1180
- value: tokenOutputs[0].value,
1181
- })
1182
- );
1177
+ if (diff > 0) {
1178
+ let available = 0;
1179
+ let change = 0;
1180
+ const ensureUtxos: UtxoI[] = [];
1181
+ for (const token of tokenInputs.filter((val) => val.token?.amount)) {
1182
+ ensureUtxos.push(token);
1183
+ available += token.token?.amount!;
1184
+ if (available >= outputAmountSum) {
1185
+ change = available - outputAmountSum;
1186
+ break;
1187
+ }
1188
+ }
1189
+ if (ensureUtxos.length) {
1190
+ if (!options) {
1191
+ options = {};
1192
+ }
1193
+ options!.ensureUtxos = [
1194
+ ...(options.ensureUtxos ?? []),
1195
+ ...ensureUtxos,
1196
+ ].filter(
1197
+ (val, index, array) =>
1198
+ array.findIndex(
1199
+ (other) => other.txid === val.txid && other.vout === val.vout
1200
+ ) === index
1201
+ );
1202
+ }
1203
+ if (change > 0) {
1204
+ outputs.push(
1205
+ new TokenSendRequest({
1206
+ cashaddr: toTokenaddr(changeAddress) || this.tokenaddr!,
1207
+ amount: change,
1208
+ tokenId: tokenId,
1209
+ commitment: tokenOutputs[0].commitment,
1210
+ capability: tokenOutputs[0].capability,
1211
+ value: tokenOutputs[0].value,
1212
+ })
1213
+ );
1214
+ }
1183
1215
  }
1184
1216
  }
1185
1217
  };
@@ -1196,6 +1228,7 @@ export class Wallet extends BaseWallet {
1196
1228
  }
1197
1229
 
1198
1230
  const relayFeePerByteInSatoshi = await getRelayFeeCache(this.provider!);
1231
+ // console.trace(utxos.slice().filter(val => val.token).reverse().slice(0, 10))
1199
1232
  const feeEstimate = await getFeeAmount({
1200
1233
  utxos: utxos,
1201
1234
  sendRequests: sendRequests,
@@ -1206,6 +1239,7 @@ export class Wallet extends BaseWallet {
1206
1239
  feePaidBy: feePaidBy,
1207
1240
  });
1208
1241
 
1242
+ // console.warn(utxos.slice().filter(val => val.token).reverse().slice(0, 10))
1209
1243
  const fundingUtxos = await getSuitableUtxos(
1210
1244
  utxos,
1211
1245
  BigInt(spendAmount) + BigInt(feeEstimate),
@@ -1215,6 +1249,7 @@ export class Wallet extends BaseWallet {
1215
1249
  options?.ensureUtxos || [],
1216
1250
  options?.tokenOperation
1217
1251
  );
1252
+ // console.log(fundingUtxos.slice().filter(val => val.token).reverse().slice(0, 10))
1218
1253
  if (fundingUtxos.length === 0) {
1219
1254
  throw Error(
1220
1255
  "The available inputs couldn't satisfy the request with fees"
@@ -258,16 +258,16 @@ export type NftCategoryField = {
258
258
  encoding:
259
259
  | {
260
260
  type:
261
- | 'binary'
262
- | 'boolean'
263
- | 'hex'
264
- | 'https-url'
265
- | 'ipfs-cid'
266
- | 'utf8'
261
+ | "binary"
262
+ | "boolean"
263
+ | "hex"
264
+ | "https-url"
265
+ | "ipfs-cid"
266
+ | "utf8"
267
267
  | `locktime`;
268
268
  }
269
269
  | {
270
- type: 'number';
270
+ type: "number";
271
271
 
272
272
  /**
273
273
  * The `aggregate` property indicates that aggregating this
@@ -284,7 +284,7 @@ export type NftCategoryField = {
284
284
  * bitwise AND, bitwise OR, and bitwise XOR, etc. – only `add`
285
285
  * is currently supported.
286
286
  */
287
- aggregate?: 'add';
287
+ aggregate?: "add";
288
288
 
289
289
  /**
290
290
  * An integer between `0` and `18` (inclusive) indicating the
@@ -533,7 +533,7 @@ export type IdentitySnapshot = {
533
533
  * if the burned identity represented a token type – consider burning any
534
534
  * remaining tokens of that category to reclaim funds from those outputs.
535
535
  */
536
- status?: 'active' | 'burned' | 'inactive';
536
+ status?: "active" | "burned" | "inactive";
537
537
 
538
538
  /**
539
539
  * The split ID of this identity's chain of record.
@@ -594,7 +594,7 @@ export type IdentitySnapshot = {
594
594
  * native currency unit (name, description, symbol, icon, etc.) as can be
595
595
  * provided for other registered tokens.
596
596
  */
597
- export type ChainSnapshot = Omit<IdentitySnapshot, 'migrated' | 'token'> & {
597
+ export type ChainSnapshot = Omit<IdentitySnapshot, "migrated" | "token"> & {
598
598
  /**
599
599
  * A data structure indicating how the chain's native currency units should be
600
600
  * displayed in user interfaces.
@@ -694,7 +694,7 @@ export type IdentityHistory = RegistryTimestampKeyedValues<IdentitySnapshot>;
694
694
 
695
695
  export type OffChainRegistryIdentity = Pick<
696
696
  IdentitySnapshot,
697
- 'name' | 'description' | 'uris' | 'tags' | 'extensions'
697
+ "name" | "description" | "uris" | "tags" | "extensions"
698
698
  >;
699
699
 
700
700
  /**
@@ -890,4 +890,4 @@ export type Registry = {
890
890
  * https://github.com/bitjson/chip-bcmr#locales-extension for details.
891
891
  */
892
892
  extensions?: Extensions;
893
- };
893
+ };