From 1fef96ed3802d61986ce140712a1bd96d684f949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20D=C5=BEurman?= Date: Thu, 14 Sep 2023 17:47:49 +0200 Subject: [PATCH] fix(staking): de-registration wrong calculation [LW-8391] (#530) --- apps/browser-extension-wallet/package.json | 4 +- .../stores/slices/transaction-detail-slice.ts | 5 +- .../helpers/pending-tx-transformer.ts | 5 +- packages/cardano/package.json | 4 +- .../wallet/util/calculate-deposit-reclaim.ts | 13 ++ packages/cardano/src/wallet/util/index.ts | 1 + packages/staking/package.json | 4 +- .../features/drawer/StakePoolConfirmation.tsx | 10 +- yarn.lock | 133 ++++++++++++++++-- 9 files changed, 149 insertions(+), 30 deletions(-) create mode 100644 packages/cardano/src/wallet/util/calculate-deposit-reclaim.ts diff --git a/apps/browser-extension-wallet/package.json b/apps/browser-extension-wallet/package.json index b1b0ee624c..0327f898d0 100644 --- a/apps/browser-extension-wallet/package.json +++ b/apps/browser-extension-wallet/package.json @@ -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", diff --git a/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts b/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts index d703166267..808f473279 100644 --- a/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts +++ b/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts @@ -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()); diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts index 15a86b1ef2..a97ef0bdfa 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts @@ -69,8 +69,9 @@ export const txTransformer = ({ }: TxTransformerInput): Omit => { 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, diff --git a/packages/cardano/package.json b/packages/cardano/package.json index bd6dce287a..4eb71a42a0 100644 --- a/packages/cardano/package.json +++ b/packages/cardano/package.json @@ -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", diff --git a/packages/cardano/src/wallet/util/calculate-deposit-reclaim.ts b/packages/cardano/src/wallet/util/calculate-deposit-reclaim.ts new file mode 100644 index 0000000000..51f386202e --- /dev/null +++ b/packages/cardano/src/wallet/util/calculate-deposit-reclaim.ts @@ -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)); +}; diff --git a/packages/cardano/src/wallet/util/index.ts b/packages/cardano/src/wallet/util/index.ts index f6c8041db7..81841df4e2 100644 --- a/packages/cardano/src/wallet/util/index.ts +++ b/packages/cardano/src/wallet/util/index.ts @@ -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'; diff --git a/packages/staking/package.json b/packages/staking/package.json index a9f3ab9cc6..c2b56a823a 100644 --- a/packages/staking/package.json +++ b/packages/staking/package.json @@ -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", @@ -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", diff --git a/packages/staking/src/features/drawer/StakePoolConfirmation.tsx b/packages/staking/src/features/drawer/StakePoolConfirmation.tsx index 76dee38528..b0a4f7a60d 100644 --- a/packages/staking/src/features/drawer/StakePoolConfirmation.tsx +++ b/packages/staking/src/features/drawer/StakePoolConfirmation.tsx @@ -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'; @@ -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(); diff --git a/yarn.lock b/yarn.lock index 89f59ae5c0..0a49eacf33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -5434,7 +5434,7 @@ __metadata: peerDependenciesMeta: rxjs: optional: true - checksum: 92d7ceb3c2a070ede6985cf5bc9dbd48a7603d6bc260e4b78046bbabf468b392d182c0502ee2b68930dfd39573d3499c0085226c80109b09bd7720c667c30539 + checksum: 55a00357fbb16513d5e8651b8008bf32a23a976a0228daa0bd905f48b5c0571eed76848ae3899ae2c97381fca25471231f5015387d21d3d32d848a9a4e5bbcba languageName: node linkType: hard @@ -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: @@ -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" @@ -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: @@ -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" @@ -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 @@ -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 @@ -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 @@ -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