Skip to content

Commit

Permalink
fix: use correct fee estimate for set-max output
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Nov 24, 2023
1 parent beef51f commit 0e0ca8e
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- UTXO_BALANCE_INSUFFICIENT while using `setMax` on ADA output

## [2.2.0] - 2023-10-06

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/badge-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/methods/largestFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export const largestFirst = (
let forceAnotherRound = false;
while (!sufficientUtxos) {
if (maxOutput) {
// reset previously computed maxOutput in order to correctly calculate a potential change output
// Reset previously computed maxOutput in order to correctly calculate a potential change output
// when new utxo is added to the set
preparedOutputs[maxOutputIndex] = setMinUtxoValueForOutputs(
txBuilder,
[maxOutput],
Expand All @@ -145,6 +146,7 @@ export const largestFirst = (
if (maxOutput) {
// set amount for a max output from a changeOutput calculated above
const { maxOutput: newMaxOutput } = setMaxOutput(
txBuilder,
maxOutput,
singleChangeOutput,
);
Expand Down
18 changes: 15 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ export const getInitialUtxoSet = (
};

export const setMaxOutput = (
txBuilder: CardanoWasm.TransactionBuilder,
maxOutput: UserOutput,
changeOutput: OutputCost | null,
): {
Expand All @@ -648,17 +649,28 @@ export const setMaxOutput = (
if (maxOutputAsset === 'lovelace') {
// set maxOutput for ADA
if (changeOutput) {
// Calculate the cost of previous dummy set-max output
const previousMaxOutputCost = getOutputCost(
txBuilder,
maxOutput,
maxOutput.address ?? changeOutput.output.address().to_bech32(),
);
newMaxAmount = changeOutput.output.amount().coin();

if (changeOutputAssets.length === 0) {
// we don't need the change output anymore
newMaxAmount = newMaxAmount.checked_add(changeOutput.outputFee);
// Add a fee that was previously consumed by the dummy max output.
// Cost calculated for the change output will be greater (due to larger coin amount
// than in dummy output - which is 0) than the cost of the dummy set-max output.
newMaxAmount = newMaxAmount.checked_add(
previousMaxOutputCost.outputFee,
);
changeOutput = null;
} else {
newMaxAmount = newMaxAmount.clamped_sub(changeOutput.minOutputAmount);

const txOutput = CardanoWasm.TransactionOutput.new(
changeOutput.output.address(),
CardanoWasm.Value.new(newMaxAmount), // TODO: 0 before
CardanoWasm.Value.new(newMaxAmount),
);
const minUtxoVal = CardanoWasm.min_ada_for_output(
txOutput,
Expand Down
99 changes: 99 additions & 0 deletions tests/fixtures/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,102 @@ export const utxo8 = Object.freeze({
],
}),
});

export const setMaxAdaInputs = [
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 2,
amount: [
{
quantity: '2611207363',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 5,
amount: [
{
quantity: '1305603682',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 8,
amount: [
{
quantity: '652801841',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 11,
amount: [
{
quantity: '326400920',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 14,
amount: [
{
quantity: '163200460',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 17,
amount: [
{
quantity: '81600230',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 21,
amount: [
{
quantity: '40800115',
unit: 'lovelace',
},
],
},
{
address:
'addr_test1qzq0nckg3ekgzuqg7w5p9mvgnd9ym28qh5grlph8xd2z92sj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsu8d9w5',
txHash: 'd6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c',
outputIndex: 22,
amount: [
{
quantity: '40800115',
unit: 'lovelace',
},
],
},
];
39 changes: 39 additions & 0 deletions tests/methods/fixtures/largestFirst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Certificate } from '../../../src/types/types';
import {
changeAddress,
setMaxAdaInputs,
utxo1,
utxo2,
utxo3,
Expand Down Expand Up @@ -118,6 +119,44 @@ export const nonFinalCompose = [
];

export const coinSelection = [
{
description: 'send max ada only utxos',
utxos: setMaxAdaInputs,
outputs: [
{
address:
'addr_test1qr9tax9jxzt05y65m8xanngng36mh7hpf23jy53xwyd9y5qj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfms0kcepv',
amount: undefined,
assets: [],
setMax: true,
},
],
changeAddress: changeAddress,
certificates: [],
withdrawals: [],
accountPubKey:
'd507c8f866691bd96e131334c355188b1a1d0b2fa0ab11545075aab332d77d9eb19657ad13ee581b56b0f8d744d66ca356b93d42fe176b3de007d53e9c4c4e7a',
ttl: 66578367,
options: {},
result: {
totalSpent: '5222414726',
fee: '176721',
tx: {
body: 'a40088825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c02825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c05825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c08825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c0b825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c0e825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c11825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c15825820d6de3f33c3b421167eb1726c48129990ec16512dd829ad2239751ba49773b30c16018182583900cabe98b23096fa1354d9cdd9cd134475bbfae14aa3225226711a5250122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b42771b0000000137450735021a0002b251031a03f7e7bf',
hash: '74a66a069530d96224b56d955eb1e58dde84774168f3d1fb4b5a972431cc18fa',
size: 481,
},
inputs: setMaxAdaInputs,
outputs: [
{
address:
'addr_test1qr9tax9jxzt05y65m8xanngng36mh7hpf23jy53xwyd9y5qj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfms0kcepv',
amount: '5222238005',
assets: [],
},
],
},
},
{
description: 'send max sundae',
utxos: [
Expand Down

0 comments on commit 0e0ca8e

Please sign in to comment.