Skip to content

Commit

Permalink
fix(staking): de-registration wrong calculation [LW-8391] (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzurman authored Sep 14, 2023
1 parent 7190267 commit 1fef96e
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 30 deletions.
4 changes: 2 additions & 2 deletions apps/browser-extension-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"dependencies": {
"@ant-design/icons": "^4.7.0",
"@cardano-sdk/cardano-services-client": "0.12.2",
"@cardano-sdk/core": "0.19.0",
"@cardano-sdk/core": "0.19.1",
"@cardano-sdk/dapp-connector": "0.9.15",
"@cardano-sdk/input-selection": "0.11.8",
"@cardano-sdk/input-selection": "0.11.9",
"@cardano-sdk/tx-construction": "0.11.2",
"@cardano-sdk/util": "0.14.0",
"@cardano-sdk/wallet": "0.21.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ const getTransactionDetail =
implicitCoin.deposit && type === 'delegationRegistration'
? Wallet.util.lovelacesToAdaString(implicitCoin.deposit.toString())
: undefined;
const depositReclaimValue = Wallet.util.calculateDepositReclaim(implicitCoin);
const depositReclaim =
// since one tx can be split into two (delegation, de-registration) actions,
// ensure only the de-registration tx carries the reclaimed deposit
implicitCoin.input && type === 'delegationDeregistration'
? Wallet.util.lovelacesToAdaString(implicitCoin.input.toString())
depositReclaimValue && type === 'delegationDeregistration'
? Wallet.util.lovelacesToAdaString(depositReclaimValue.toString())
: undefined;
const feeInAda = Wallet.util.lovelacesToAdaString(tx.body.fee.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export const txTransformer = ({
}: TxTransformerInput): Omit<AssetActivityItemProps, 'onClick'> => {
const implicitCoin = Wallet.Cardano.util.computeImplicitCoin(protocolParameters, tx.body);
const deposit = implicitCoin.deposit ? Wallet.util.lovelacesToAdaString(implicitCoin.deposit.toString()) : undefined;
const depositReclaim = implicitCoin.input
? Wallet.util.lovelacesToAdaString(implicitCoin.input.toString())
const depositReclaimValue = Wallet.util.calculateDepositReclaim(implicitCoin);
const depositReclaim = depositReclaimValue
? Wallet.util.lovelacesToAdaString(depositReclaimValue.toString())
: undefined;
const { coins, assets } = inspectTxValues({
addresses: walletAddresses,
Expand Down
4 changes: 2 additions & 2 deletions packages/cardano/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
},
"dependencies": {
"@cardano-sdk/cardano-services-client": "0.12.2",
"@cardano-sdk/core": "0.19.0",
"@cardano-sdk/crypto": "0.1.12",
"@cardano-sdk/core": "0.19.1",
"@cardano-sdk/crypto": "0.1.13",
"@cardano-sdk/hardware-ledger": "0.3.3",
"@cardano-sdk/key-management": "0.10.2",
"@cardano-sdk/util": "0.14.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/cardano/src/wallet/util/calculate-deposit-reclaim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Cardano } from '@cardano-sdk/core';

// SDK's ImplicitCoin's "input" field contains summed up withdrawals and stake key reclaim deposits.
// This function calculates just the stake key reclaim deposit.
export const calculateDepositReclaim = ({
input,
withdrawals
}: Cardano.util.ImplicitCoin): Cardano.Lovelace | undefined => {
if (!input) return undefined;

// eslint-disable-next-line consistent-return
return input - (withdrawals || BigInt(0));
};
1 change: 1 addition & 0 deletions packages/cardano/src/wallet/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './unit-converters';
export * from './observable';
export * from './asset-balance';
export * from './stake-pool-transformer';
export * from './calculate-deposit-reclaim';
4 changes: 2 additions & 2 deletions packages/staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"zustand": "^4.4.1"
},
"devDependencies": {
"@cardano-sdk/input-selection": "0.11.8",
"@cardano-sdk/input-selection": "0.11.9",
"@cardano-sdk/tx-construction": "0.11.2",
"@cardano-sdk/util": "0.14.0",
"@lace/cardano": "^0.1.0",
Expand Down Expand Up @@ -95,7 +95,7 @@
"wait-on": "^7.0.1"
},
"peerDependencies": {
"@cardano-sdk/input-selection": "0.11.7",
"@cardano-sdk/input-selection": "0.11.9",
"@cardano-sdk/tx-construction": "0.11.1",
"@cardano-sdk/util": "0.14.0",
"@cardano-sdk/wallet": "0.21.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/staking/src/features/drawer/StakePoolConfirmation.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable react/no-multi-comp */
import Icon from '@ant-design/icons';
import { InputSelectionFailure } from '@cardano-sdk/input-selection';
import { BigIntMath } from '@cardano-sdk/util';
import { Wallet } from '@lace/cardano';
import { Banner, Button, Ellipsis, useObservable } from '@lace/common';
import { RowContainer, renderAmountInfo, renderLabel } from '@lace/core';
Expand Down Expand Up @@ -216,12 +215,9 @@ export const StakePoolConfirmation = (): React.ReactElement => {
const txBuilder = inMemoryWallet.createTxBuilder();
const pools = draftPortfolio.map((pool) => ({ id: pool.id, weight: pool.weight }));
const tx = await txBuilder.delegatePortfolio({ pools }).build().inspect();
const { deposit: newDelegationTxDeposit, input } = Wallet.Cardano.util.computeImplicitCoin(
protocolParameters,
tx.body
);
const newDelegationTxReclaim =
(input || BigInt(0)) - BigIntMath.sum((tx.body.withdrawals || []).map(({ quantity }) => quantity));
const implicitCoin = Wallet.Cardano.util.computeImplicitCoin(protocolParameters, tx.body);
const newDelegationTxDeposit = implicitCoin.deposit;
const newDelegationTxReclaim = Wallet.util.calculateDepositReclaim(implicitCoin) || BigInt(0);
setDelegationTxBuilder(txBuilder);
setDelegationTxFee(tx.body.fee.toString());
setStakingError();
Expand Down
133 changes: 120 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5413,13 +5413,13 @@ __metadata:
languageName: node
linkType: hard

"@cardano-sdk/core@npm:0.19.0, @cardano-sdk/core@npm:~0.19.0":
version: 0.19.0
resolution: "@cardano-sdk/core@npm:0.19.0"
"@cardano-sdk/core@npm:0.19.1, @cardano-sdk/core@npm:~0.19.1":
version: 0.19.1
resolution: "@cardano-sdk/core@npm:0.19.1"
dependencies:
"@cardano-ogmios/client": 5.6.0
"@cardano-ogmios/schema": 5.6.0
"@cardano-sdk/crypto": ~0.1.12
"@cardano-sdk/crypto": ~0.1.13
"@cardano-sdk/util": ~0.14.0
"@dcspark/cardano-multiplatform-lib-nodejs": ^3.1.1
"@foxglove/crc": ^0.0.3
Expand All @@ -5434,7 +5434,7 @@ __metadata:
peerDependenciesMeta:
rxjs:
optional: true
checksum: 92d7ceb3c2a070ede6985cf5bc9dbd48a7603d6bc260e4b78046bbabf468b392d182c0502ee2b68930dfd39573d3499c0085226c80109b09bd7720c667c30539
checksum: 55a00357fbb16513d5e8651b8008bf32a23a976a0228daa0bd905f48b5c0571eed76848ae3899ae2c97381fca25471231f5015387d21d3d32d848a9a4e5bbcba
languageName: node
linkType: hard

Expand Down Expand Up @@ -5462,7 +5462,61 @@ __metadata:
languageName: node
linkType: hard

"@cardano-sdk/crypto@npm:0.1.12, @cardano-sdk/crypto@npm:~0.1.12":
"@cardano-sdk/core@npm:~0.19.0":
version: 0.19.0
resolution: "@cardano-sdk/core@npm:0.19.0"
dependencies:
"@cardano-ogmios/client": 5.6.0
"@cardano-ogmios/schema": 5.6.0
"@cardano-sdk/crypto": ~0.1.12
"@cardano-sdk/util": ~0.14.0
"@dcspark/cardano-multiplatform-lib-nodejs": ^3.1.1
"@foxglove/crc": ^0.0.3
"@scure/base": ^1.1.1
fraction.js: 4.0.1
ip-address: ^8.1.0
lodash: ^4.17.21
ts-custom-error: ^3.2.0
ts-log: ^2.2.4
peerDependencies:
rxjs: ^7.4.0
peerDependenciesMeta:
rxjs:
optional: true
checksum: 92d7ceb3c2a070ede6985cf5bc9dbd48a7603d6bc260e4b78046bbabf468b392d182c0502ee2b68930dfd39573d3499c0085226c80109b09bd7720c667c30539
languageName: node
linkType: hard

"@cardano-sdk/crypto@npm:0.1.13, @cardano-sdk/crypto@npm:~0.1.13":
version: 0.1.13
resolution: "@cardano-sdk/crypto@npm:0.1.13"
dependencies:
"@cardano-sdk/util": ~0.14.0
blake2b: ^2.1.4
bn.js: ^5.2.1
i: ^0.3.7
libsodium-wrappers-sumo: ^0.7.5
lodash: ^4.17.21
npm: ^9.3.0
pbkdf2: ^3.1.2
ts-custom-error: ^3.2.0
ts-log: ^2.2.4
peerDependencies:
"@dcspark/cardano-multiplatform-lib-asmjs": ^3.1.1
"@dcspark/cardano-multiplatform-lib-browser": ^3.1.1
"@dcspark/cardano-multiplatform-lib-nodejs": ^3.1.1
peerDependenciesMeta:
"@dcspark/cardano-multiplatform-lib-asmjs":
optional: true
"@dcspark/cardano-multiplatform-lib-browser":
optional: true
"@dcspark/cardano-multiplatform-lib-nodejs":
optional: true
checksum: 771cbfd7fd30cf96227baa7eccc5e7fcd297d6c8d45824198e9f3646337a22bcfdca89f7c71478ec7abd67e61d2391827c83964a67366dffa3d56b99d21e34a2
languageName: node
linkType: hard

"@cardano-sdk/crypto@npm:~0.1.12":
version: 0.1.12
resolution: "@cardano-sdk/crypto@npm:0.1.12"
dependencies:
Expand Down Expand Up @@ -5506,6 +5560,21 @@ __metadata:
languageName: node
linkType: hard

"@cardano-sdk/dapp-connector@npm:~0.9.16":
version: 0.9.16
resolution: "@cardano-sdk/dapp-connector@npm:0.9.16"
dependencies:
"@cardano-sdk/core": ~0.19.1
"@cardano-sdk/crypto": ~0.1.13
"@cardano-sdk/util": ~0.14.0
lodash: ^4.17.21
ts-custom-error: ^3.2.0
ts-log: ^2.2.4
webextension-polyfill: ^0.8.0
checksum: 16d6cf6b631091ed2a0c9076e4861e3b4fa0757d8523249004add6e796cbf8c8f779a4613e3b566b3004aed6c2ad55b8987dc2fefe2adc614f1a36be43f9cb83
languageName: node
linkType: hard

"@cardano-sdk/hardware-ledger@npm:0.3.3, @cardano-sdk/hardware-ledger@npm:~0.3.3":
version: 0.3.3
resolution: "@cardano-sdk/hardware-ledger@npm:0.3.3"
Expand All @@ -5525,7 +5594,21 @@ __metadata:
languageName: node
linkType: hard

"@cardano-sdk/input-selection@npm:0.11.8, @cardano-sdk/input-selection@npm:~0.11.8":
"@cardano-sdk/input-selection@npm:0.11.9":
version: 0.11.9
resolution: "@cardano-sdk/input-selection@npm:0.11.9"
dependencies:
"@cardano-sdk/core": ~0.19.1
"@cardano-sdk/key-management": ~0.10.3
"@cardano-sdk/util": ~0.14.0
bignumber.js: ^9.1.1
lodash: ^4.17.21
ts-custom-error: ^3.2.0
checksum: 80a538a3e8177544eec77cddd80ea7e92ea35349a61f5720ae9facfccaa37b2155a5c9a6a2c10854d906cc4e28a05ea0bccc2d850e41d78fc40fb73720381a7d
languageName: node
linkType: hard

"@cardano-sdk/input-selection@npm:~0.11.8":
version: 0.11.8
resolution: "@cardano-sdk/input-selection@npm:0.11.8"
dependencies:
Expand Down Expand Up @@ -5563,6 +5646,30 @@ __metadata:
languageName: node
linkType: hard

"@cardano-sdk/key-management@npm:~0.10.3":
version: 0.10.3
resolution: "@cardano-sdk/key-management@npm:0.10.3"
dependencies:
"@cardano-foundation/ledgerjs-hw-app-cardano": ^6.0.0
"@cardano-sdk/core": ~0.19.1
"@cardano-sdk/crypto": ~0.1.13
"@cardano-sdk/dapp-connector": ~0.9.16
"@cardano-sdk/util": ~0.14.0
"@emurgo/cardano-message-signing-nodejs": ^1.0.1
"@trezor/connect": 9.0.11
"@trezor/connect-web": 9.0.11
bip39: ^3.0.4
chacha: ^2.1.0
get-random-values: ^2.0.0
lodash: ^4.17.21
pbkdf2: ^3.1.2
rxjs: ^7.4.0
ts-custom-error: ^3.2.0
ts-log: ^2.2.4
checksum: 4db4003d249be16fba0a5da5099250fd387386d7e89b1f4797314e8fe2375ec23f46ae87f877e27748d30f35467a7fc1802d7d4abe52ba6f89bb1f8e8a5f7fe2
languageName: node
linkType: hard

"@cardano-sdk/tx-construction@npm:0.11.2, @cardano-sdk/tx-construction@npm:~0.11.2":
version: 0.11.2
resolution: "@cardano-sdk/tx-construction@npm:0.11.2"
Expand Down Expand Up @@ -7850,9 +7957,9 @@ __metadata:
dependencies:
"@ant-design/icons": ^4.7.0
"@cardano-sdk/cardano-services-client": 0.12.2
"@cardano-sdk/core": 0.19.0
"@cardano-sdk/core": 0.19.1
"@cardano-sdk/dapp-connector": 0.9.15
"@cardano-sdk/input-selection": 0.11.8
"@cardano-sdk/input-selection": 0.11.9
"@cardano-sdk/tx-construction": 0.11.2
"@cardano-sdk/util": 0.14.0
"@cardano-sdk/wallet": 0.21.1
Expand Down Expand Up @@ -7921,8 +8028,8 @@ __metadata:
resolution: "@lace/cardano@workspace:packages/cardano"
dependencies:
"@cardano-sdk/cardano-services-client": 0.12.2
"@cardano-sdk/core": 0.19.0
"@cardano-sdk/crypto": 0.1.12
"@cardano-sdk/core": 0.19.1
"@cardano-sdk/crypto": 0.1.13
"@cardano-sdk/hardware-ledger": 0.3.3
"@cardano-sdk/key-management": 0.10.2
"@cardano-sdk/util": 0.14.0
Expand Down Expand Up @@ -8047,7 +8154,7 @@ __metadata:
resolution: "@lace/staking@workspace:packages/staking"
dependencies:
"@ant-design/icons": ^4.7.0
"@cardano-sdk/input-selection": 0.11.8
"@cardano-sdk/input-selection": 0.11.9
"@cardano-sdk/tx-construction": 0.11.2
"@cardano-sdk/util": 0.14.0
"@lace/cardano": ^0.1.0
Expand Down Expand Up @@ -8087,7 +8194,7 @@ __metadata:
wait-on: ^7.0.1
zustand: ^4.4.1
peerDependencies:
"@cardano-sdk/input-selection": 0.11.7
"@cardano-sdk/input-selection": 0.11.9
"@cardano-sdk/tx-construction": 0.11.1
"@cardano-sdk/util": 0.14.0
"@cardano-sdk/wallet": 0.21.0
Expand Down

0 comments on commit 1fef96e

Please sign in to comment.