@fivebinaries/coin-selection
Advanced tools
Comparing version 2.0.0-beta.3 to 2.0.0-beta.4
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-browser'; | ||
export declare const dummyAddress = "addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp"; | ||
export declare const CertificateType: { | ||
@@ -4,0 +3,0 @@ readonly STAKE_REGISTRATION: 0; |
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-browser'; | ||
import { NetworkInfo } from '@emurgo/cardano-serialization-lib-browser'; | ||
export const dummyAddress = 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp'; | ||
export const CertificateType = { | ||
@@ -5,0 +4,0 @@ STAKE_REGISTRATION: 0, |
@@ -30,3 +30,3 @@ import { ERROR } from '../constants'; | ||
} | ||
const preparedOutputs = setMinUtxoValueForOutputs(txBuilder, outputs); | ||
const preparedOutputs = setMinUtxoValueForOutputs(txBuilder, outputs, changeAddress); | ||
const addUtxoToSelection = (utxo) => { | ||
@@ -47,3 +47,3 @@ const { input, address, amount } = buildTxInput(utxo); | ||
// Calculate fee and minUtxoValue for all external outputs | ||
const outputsCost = preparedOutputs.map(output => getOutputCost(txBuilder, output)); | ||
const outputsCost = preparedOutputs.map(output => getOutputCost(txBuilder, output, changeAddress)); | ||
const totalOutputsFee = outputsCost.reduce((acc, output) => (acc = acc.checked_add(output.outputFee)), bigNumFromStr('0')); | ||
@@ -59,5 +59,3 @@ // add external outputs fees to total | ||
// reset previously computed maxOutput in order to correctly calculate a potential change output | ||
preparedOutputs[maxOutputIndex] = setMinUtxoValueForOutputs(txBuilder, [ | ||
maxOutput, | ||
])[0]; | ||
preparedOutputs[maxOutputIndex] = setMinUtxoValueForOutputs(txBuilder, [maxOutput], changeAddress)[0]; | ||
} | ||
@@ -137,3 +135,3 @@ // Calculate change output | ||
preparedOutputs.forEach(output => { | ||
const txOutput = buildTxOutput(output); | ||
const txOutput = buildTxOutput(output, changeAddress); | ||
txBuilder.add_output(txOutput); | ||
@@ -145,3 +143,3 @@ }); | ||
finalOutputs.push(change); | ||
txBuilder.add_output(buildTxOutput(change)); | ||
txBuilder.add_output(buildTxOutput(change, changeAddress)); | ||
}); | ||
@@ -148,0 +146,0 @@ } |
@@ -33,8 +33,8 @@ import { ERROR } from '../constants'; | ||
}; | ||
const selection = (utxos, outputs, txBuilder) => { | ||
const selection = (utxos, outputs, txBuilder, dummyAddress) => { | ||
const utxoSelected = []; | ||
const utxoRemaining = JSON.parse(JSON.stringify(utxos)); | ||
const preparedOutputs = setMinUtxoValueForOutputs(txBuilder, outputs); | ||
const preparedOutputs = setMinUtxoValueForOutputs(txBuilder, outputs, dummyAddress); | ||
preparedOutputs.forEach(output => { | ||
const txOutput = buildTxOutput(output); | ||
const txOutput = buildTxOutput(output, dummyAddress); | ||
txBuilder.add_output(txOutput); | ||
@@ -122,3 +122,3 @@ }); | ||
const txBuilder = getTxBuilder((_a = options === null || options === void 0 ? void 0 : options.feeParams) === null || _a === void 0 ? void 0 : _a.a); | ||
const { utxoSelected, utxoRemaining, preparedOutputs } = selection(utxos, outputs, txBuilder); | ||
const { utxoSelected, utxoRemaining, preparedOutputs } = selection(utxos, outputs, txBuilder, changeAddress); | ||
// compute change and adjust for fee | ||
@@ -135,3 +135,3 @@ const { changeOutputs } = calculateChange(utxoSelected, utxoRemaining, preparedOutputs, changeAddress, options === null || options === void 0 ? void 0 : options._maxTokensPerOutput, txBuilder); | ||
finalOutputs.push(ch); | ||
txBuilder.add_output(buildTxOutput(ch)); | ||
txBuilder.add_output(buildTxOutput(ch, changeAddress)); | ||
}); | ||
@@ -138,0 +138,0 @@ const totalUserOutputsAmount = getUserOutputQuantityWithDeposit(preparedOutputs, 0); |
@@ -23,8 +23,8 @@ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-browser'; | ||
}; | ||
export declare const buildTxOutput: (output: Output) => CardanoWasm.TransactionOutput; | ||
export declare const getOutputCost: (txBuilder: CardanoWasm.TransactionBuilder, output: Output) => OutputCost; | ||
export declare const buildTxOutput: (output: Output, dummyAddress: string) => CardanoWasm.TransactionOutput; | ||
export declare const getOutputCost: (txBuilder: CardanoWasm.TransactionBuilder, output: Output, dummyAddress: string) => OutputCost; | ||
export declare const prepareWithdrawals: (withdrawals: Withdrawal[]) => CardanoWasm.Withdrawals; | ||
export declare const prepareCertificates: (certificates: Certificate[], accountKey: CardanoWasm.Bip32PublicKey) => CardanoWasm.Certificates; | ||
export declare const calculateRequiredDeposit: (certificates: Certificate[]) => number; | ||
export declare const setMinUtxoValueForOutputs: (txBuilder: CardanoWasm.TransactionBuilder, outputs: UserOutput[]) => UserOutput[]; | ||
export declare const setMinUtxoValueForOutputs: (txBuilder: CardanoWasm.TransactionBuilder, outputs: UserOutput[], dummyAddress: string) => UserOutput[]; | ||
export declare const splitChangeOutput: (txBuilder: CardanoWasm.TransactionBuilder, singleChangeOutput: OutputCost, changeAddress: string, maxTokensPerOutput?: number) => OutputCost[]; | ||
@@ -31,0 +31,0 @@ export declare const prepareChangeOutput: (txBuilder: CardanoWasm.TransactionBuilder, usedUtxos: Utxo[], preparedOutputs: Output[], changeAddress: string, utxosTotalAmount: CardanoWasm.BigNum, totalOutputAmount: CardanoWasm.BigNum, totalFeesAmount: CardanoWasm.BigNum, pickAdditionalUtxo?: (() => ReturnType<typeof getRandomUtxo>) | undefined) => OutputCost | null; |
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-browser'; | ||
import { CARDANO_PARAMS, CertificateType, dummyAddress, ERROR, MAX_TOKENS_PER_OUTPUT, MIN_UTXO_VALUE, } from '../constants'; | ||
import { CARDANO_PARAMS, CertificateType, ERROR, MAX_TOKENS_PER_OUTPUT, MIN_UTXO_VALUE, } from '../constants'; | ||
import { CoinSelectionError } from './errors'; | ||
@@ -91,3 +91,3 @@ export const bigNumFromStr = (num) => CardanoWasm.BigNum.from_str(num); | ||
}; | ||
export const buildTxOutput = (output) => { | ||
export const buildTxOutput = (output, dummyAddress) => { | ||
var _a; | ||
@@ -107,9 +107,13 @@ // this will set required minUtxoValue even if output.amount === 0 | ||
} | ||
const outputAddr = CardanoWasm.Address.from_bech32((_a = output.address) !== null && _a !== void 0 ? _a : dummyAddress); // TODO: compatibility with byron | ||
// If output.address was not defined fallback to bech32 address (useful for "precompose" tx | ||
// which doesn't have all necessary data, but we can fill in the blanks and return some info such as fee) | ||
const outputAddr = output.address && CardanoWasm.ByronAddress.is_valid(output.address) | ||
? CardanoWasm.ByronAddress.from_base58(output.address).to_address() | ||
: CardanoWasm.Address.from_bech32((_a = output.address) !== null && _a !== void 0 ? _a : dummyAddress); | ||
const txOutput = CardanoWasm.TransactionOutput.new(outputAddr, outputValue); | ||
return txOutput; | ||
}; | ||
export const getOutputCost = (txBuilder, output) => { | ||
export const getOutputCost = (txBuilder, output, dummyAddress) => { | ||
const coinsPerUtxoWord = bigNumFromStr(CARDANO_PARAMS.COINS_PER_UTXO_WORD); | ||
const txOutput = buildTxOutput(output); | ||
const txOutput = buildTxOutput(output, dummyAddress); | ||
const outputFee = txBuilder.fee_for_output(txOutput); | ||
@@ -164,6 +168,6 @@ const minAda = CardanoWasm.min_ada_required(txOutput.amount(), false, coinsPerUtxoWord); | ||
}; | ||
export const setMinUtxoValueForOutputs = (txBuilder, outputs) => { | ||
export const setMinUtxoValueForOutputs = (txBuilder, outputs, dummyAddress) => { | ||
const preparedOutputs = outputs.map(output => { | ||
// sets minimal output ADA amount in case of multi-asset output | ||
const { minOutputAmount } = getOutputCost(txBuilder, output); | ||
const { minOutputAmount } = getOutputCost(txBuilder, output, dummyAddress); | ||
const outputAmount = bigNumFromStr(output.amount || '0'); | ||
@@ -228,3 +232,3 @@ let amount; | ||
const changeOutputsCost = changeOutputs.map((partialChange, i) => { | ||
let changeOutputCost = getOutputCost(txBuilder, partialChange); | ||
let changeOutputCost = getOutputCost(txBuilder, partialChange, changeAddress); | ||
lovelaceAvailable = lovelaceAvailable.clamped_sub(bigNumFromStr(partialChange.amount).checked_add(changeOutputCost.outputFee)); | ||
@@ -240,3 +244,3 @@ if (i === changeOutputs.length - 1) { | ||
partialChange.amount = changeOutputAmount.to_str(); | ||
changeOutputCost = getOutputCost(txBuilder, partialChange); | ||
changeOutputCost = getOutputCost(txBuilder, partialChange, changeAddress); | ||
} | ||
@@ -274,3 +278,3 @@ return changeOutputCost; | ||
assets: changeOutputAssets, | ||
}); | ||
}, changeAddress); | ||
// calculate change output amount as utxosTotalAmount - totalFeesAmount - change output fee | ||
@@ -310,3 +314,3 @@ const totalSpent = totalOutputAmount | ||
assets: changeOutputAssets, | ||
}); | ||
}, changeAddress); | ||
// WARNING: It returns a change output also in a case where we don't have enough utxos to cover the output cost, but the change output is needed because it contains additional assets | ||
@@ -415,3 +419,3 @@ return { | ||
assets: changeOutputAssets, | ||
}); | ||
}, changeOutput.output.address().to_bech32()); | ||
changeOutput = Object.assign(Object.assign({}, changeOutput), { output: newChangeOutput }); | ||
@@ -418,0 +422,0 @@ } |
@@ -10,3 +10,3 @@ import { CardanoAddressParameters, CardanoInput, CardanoOutput } from '../../types/trezor'; | ||
tokenAmounts: AssetInPolicy[]; | ||
}[]; | ||
}[] | undefined; | ||
export declare const transformToTrezorInputs: (utxos: Utxo[], trezorUtxos: { | ||
@@ -13,0 +13,0 @@ txid: string; |
import { parseAsset } from '../common'; | ||
export const transformToTokenBundle = (assets) => { | ||
// prepare token bundle used in trezor output | ||
if (assets.length === 0) | ||
return undefined; | ||
const uniquePolicies = []; | ||
@@ -5,0 +7,0 @@ assets.forEach(asset => { |
{ | ||
"name": "@fivebinaries/coin-selection", | ||
"version": "2.0.0-beta.3", | ||
"version": "2.0.0-beta.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
64465
1317