Skip to content

Commit 65b1318

Browse files
Tomislav Horačeknikolaglumac
andauthored
[DDW-559] Implement delegation deposit from coin selection (#2332)
* [DDW-559] Introduce HW dynamic delegation deposit from coin selection * [DDW-559] CHANGELOG update * [DDW-559] Code improvements * [DDW-559] Change fee and deposit calculation logic to use BigNumber Co-authored-by: Nikola Glumac <niglumac@gmail.com>
1 parent d285c78 commit 65b1318

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Changelog
1414

1515
### Chores
1616

17+
- Updated Hardware Wallets delegation deposit calculation ([PR 2332](https://github.com/input-output-hk/daedalus/pull/2332))
1718
- Implemented dynamic TTL calculation for hardware wallets transactions ([PR 2331](https://github.com/input-output-hk/daedalus/pull/2331))
1819
- Added link to connecting issues support article on the hardware wallet "Pairing" dialog ([PR 2336](https://github.com/input-output-hk/daedalus/pull/2336))
1920
- Updated recovery phrase entry ([PR 2334](https://github.com/input-output-hk/daedalus/pull/2334))

source/renderer/app/api/api.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import { LOVELACES_PER_ADA } from '../config/numbersConfig';
100100
import {
101101
SMASH_SERVER_STATUSES,
102102
SMASH_SERVERS_LIST,
103-
DELEGATION_DEPOSIT,
104103
MIN_REWARDS_REDEMPTION_RECEIVER_BALANCE,
105104
REWARDS_REDEMPTION_FEE_CALCULATION_AMOUNT,
106105
} from '../config/stakingConfig';
@@ -891,7 +890,6 @@ export default class AdaApi {
891890
parameters: filterLogData(request),
892891
});
893892
const { walletId, payments, delegation } = request;
894-
895893
try {
896894
let data;
897895
if (delegation) {
@@ -925,13 +923,15 @@ export default class AdaApi {
925923
const outputs = concat(response.outputs, response.change);
926924

927925
// Calculate fee from inputs and outputs
928-
let totalInputs = 0;
929-
let totalOutputs = 0;
930926
const inputsData = [];
931927
const outputsData = [];
932928
const certificatesData = [];
929+
let totalInputs = new BigNumber(0);
930+
let totalOutputs = new BigNumber(0);
931+
933932
map(response.inputs, (input) => {
934-
totalInputs += input.amount.quantity;
933+
const inputAmount = new BigNumber(input.amount.quantity);
934+
totalInputs = totalInputs.plus(inputAmount);
935935
const inputData = {
936936
address: input.address,
937937
amount: input.amount,
@@ -941,15 +941,18 @@ export default class AdaApi {
941941
};
942942
inputsData.push(inputData);
943943
});
944+
944945
map(outputs, (output) => {
945-
totalOutputs += output.amount.quantity;
946+
const outputAmount = new BigNumber(output.amount.quantity);
947+
totalOutputs = totalOutputs.plus(outputAmount);
946948
const outputData = {
947949
address: output.address,
948950
amount: output.amount,
949951
derivationPath: output.derivation_path || null,
950952
};
951953
outputsData.push(outputData);
952954
});
955+
953956
if (response.certificates) {
954957
map(response.certificates, (certificate) => {
955958
const certificateData = {
@@ -960,28 +963,21 @@ export default class AdaApi {
960963
certificatesData.push(certificateData);
961964
});
962965
}
963-
const fee = new BigNumber(totalInputs - totalOutputs).dividedBy(
964-
LOVELACES_PER_ADA
965-
);
966966

967-
let transactionFee;
968-
if (delegation && delegation.delegationAction) {
969-
const delegationDeposit = new BigNumber(DELEGATION_DEPOSIT);
970-
const isDepositIncluded = fee.gt(delegationDeposit);
971-
transactionFee = isDepositIncluded ? fee.minus(delegationDeposit) : fee;
972-
} else {
973-
transactionFee = fee;
974-
}
967+
const deposits = map(response.deposits, (deposit) => deposit.quantity);
968+
const totalDeposits = deposits.length
969+
? BigNumber.sum.apply(null, deposits)
970+
: new BigNumber(0);
971+
const feeWithDeposits = totalInputs.minus(totalOutputs);
972+
const fee = feeWithDeposits.minus(totalDeposits);
975973

976-
// On first wallet delegation deposit is included in fee
977974
const extendedResponse = {
978975
inputs: inputsData,
979976
outputs: outputsData,
980977
certificates: certificatesData,
981-
feeWithDelegationDeposit: fee,
982-
fee: transactionFee,
978+
feeWithDeposits: feeWithDeposits.dividedBy(LOVELACES_PER_ADA),
979+
fee: fee.dividedBy(LOVELACES_PER_ADA),
983980
};
984-
985981
logger.debug('AdaApi::selectCoins success', { extendedResponse });
986982
return extendedResponse;
987983
} catch (error) {

source/renderer/app/api/transactions/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export type CoinSelectionsResponse = {
216216
inputs: Array<CoinSelectionInput>,
217217
outputs: Array<CoinSelectionOutput>,
218218
certificates: CoinSelectionCertificates,
219-
feeWithDelegationDeposit: BigNumber,
219+
feeWithDeposits: BigNumber,
220220
fee: BigNumber,
221221
};
222222

source/renderer/app/config/stakingConfig.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ export const REDEEM_ITN_REWARDS_STEPS: {
104104
RESULT: 'result',
105105
};
106106

107-
export const DELEGATION_DEPOSIT = 2; // 2 ADA | unit: lovelace
108-
109107
export const DELEGATION_ACTIONS: {
110108
[key: string]: DelegationAction,
111109
} = {

source/renderer/app/containers/staking/dialogs/DelegationSetupWizardDialogContainer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export default class DelegationSetupWizardDialogContainer extends Component<
260260
poolId,
261261
delegationAction: DELEGATION_ACTIONS.JOIN,
262262
});
263-
stakePoolJoinFee = coinsSelection.feeWithDelegationDeposit;
263+
stakePoolJoinFee = coinsSelection.feeWithDeposits;
264264
// Initiate Transaction (Delegation)
265265
hardwareWallets.initiateTransaction({ walletId: selectedWalletId });
266266
} else {

0 commit comments

Comments
 (0)