Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct fee estimate for set-max output #29

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@emurgo/cardano-serialization-lib-browser": "^11.0.0",
"@emurgo/cardano-serialization-lib-nodejs": "11.0.0"
"@emurgo/cardano-serialization-lib-browser": "^11.5.0",
"@emurgo/cardano-serialization-lib-nodejs": "11.5.0"
},
"packageManager": "[email protected]"
}
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
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -749,17 +749,17 @@ __metadata:
languageName: node
linkType: hard

"@emurgo/cardano-serialization-lib-browser@npm:^11.0.0":
version: 11.0.0
resolution: "@emurgo/cardano-serialization-lib-browser@npm:11.0.0"
checksum: 862b3177149b4fbf71aae7662e8270cc05fa41a02ee5b0d71dbdfe20317eba00ce6c7b56fc5a517256619d036a3041031c1650950ae105206f14b26d1fa7a7cb
"@emurgo/cardano-serialization-lib-browser@npm:^11.5.0":
version: 11.5.0
resolution: "@emurgo/cardano-serialization-lib-browser@npm:11.5.0"
checksum: 4c45dca4ca6cea5298f5852671084263182b83a2eabae92deefaa079e1460bd719314b4177509b8e7c890ebe8e0f35f64eb92c26bc83a91edb46c0cbacb8a173
languageName: node
linkType: hard

"@emurgo/cardano-serialization-lib-nodejs@npm:11.0.0":
version: 11.0.0
resolution: "@emurgo/cardano-serialization-lib-nodejs@npm:11.0.0"
checksum: b22b707e83ecd254a9888e24ada14d07fa8d37c749bd49e67b4b9ca13a3a42cc1b5198b1118d751cee94b479d5b7addd15af5d875cb5c91418f69f3a98d5276e
"@emurgo/cardano-serialization-lib-nodejs@npm:11.5.0":
version: 11.5.0
resolution: "@emurgo/cardano-serialization-lib-nodejs@npm:11.5.0"
checksum: f9e271704ef12eb018bc0a15b8c6f83c61db694a422674267be6c546a9a89dd8b200781aca3f97dd5bb4291b0c84f6c60177fcf7e550964188a4ed6a8efd0316
languageName: node
linkType: hard

Expand All @@ -784,8 +784,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@fivebinaries/coin-selection@workspace:."
dependencies:
"@emurgo/cardano-serialization-lib-browser": ^11.0.0
"@emurgo/cardano-serialization-lib-nodejs": 11.0.0
"@emurgo/cardano-serialization-lib-browser": ^11.5.0
"@emurgo/cardano-serialization-lib-nodejs": 11.5.0
"@swc-node/jest": ^1.5.2
"@types/jest": ^28.1.6
"@types/node": ^16.3.2
Expand Down
Loading