Skip to content

Commit

Permalink
refactor: move getConfirmedUTXOs
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming committed May 10, 2024
1 parent 845789d commit 47c8508
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
28 changes: 0 additions & 28 deletions packages/engine/src/vaults/VaultBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,34 +786,6 @@ export abstract class VaultBase extends VaultBaseChainOnly {
return nextNonce;
}

getConfirmedUTXOs<T extends { value: string | number }>(
utxos: T[],
amount: string,
minTransferAmount = '0',
): T[] {
const transactionAmount = new BigNumber(amount).plus(minTransferAmount);
const confirmedUTXOs = utxos.sort((a, b) =>
new BigNumber(b.value).gt(a.value) ? 1 : -1,
);
let sum = new BigNumber(0);
let i = 0;
for (i = 0; i < confirmedUTXOs.length; i += 1) {
sum = sum.plus(confirmedUTXOs[i].value);
if (sum.gt(transactionAmount)) {
break;
}
}
if (sum.lt(transactionAmount)) {
if (sum.gt(amount)) {
break;
}
}
if (sum.lt(amount)) {
return [];
}
return confirmedUTXOs.slice(0, i + 1);
}

validateSendAmount(amount: string, tokenBalance: string, to: string) {
return Promise.resolve(true);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/engine/src/vaults/impl/nexa/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ export default class Vault extends VaultBase {
return Promise.resolve({} as IDecodedTxLegacy);
}

getConfirmedUTXOs<T extends { value: string | number }>(
utxos: T[],
amount: string,
minTransferAmount = '0',
): T[] {
const transactionAmount = new BigNumber(amount).plus(minTransferAmount);
const confirmedUTXOs = utxos.sort((a, b) =>
new BigNumber(b.value).gt(a.value) ? 1 : -1,
);
let sum = new BigNumber(0);
let i = 0;
for (i = 0; i < confirmedUTXOs.length; i += 1) {
sum = sum.plus(confirmedUTXOs[i].value);
if (sum.gt(transactionAmount)) {
break;
}
}
if (sum.lt(transactionAmount)) {
return [];
}
return confirmedUTXOs.slice(0, i + 1);
}

override async buildEncodedTxFromTransfer(
transferInfo: ITransferInfo,
): Promise<IEncodedTxNexa> {
Expand Down

0 comments on commit 47c8508

Please sign in to comment.