From 77442720f6576a773303cc238a0287d2258c2565 Mon Sep 17 00:00:00 2001 From: Roxane Letourneau Date: Fri, 4 Jun 2021 14:13:20 -0700 Subject: [PATCH 1/2] docs(draining implicit accounts): fix Draining implicit accounts live code example Fixed the example to reflect changes made in version 9.1.0 on the reveal estimate --- docs/drain_account.md | 82 +++++++++++++++------------- website/src/theme/CodeBlock/index.js | 5 +- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/docs/drain_account.md b/docs/drain_account.md index 4194d9c456..060a7a1eea 100644 --- a/docs/drain_account.md +++ b/docs/drain_account.md @@ -16,13 +16,14 @@ Once we know the associated fees, we can calculate the maximum amount that needs Finally, we can do the transfer operation and use the maximum amount we just calculated as the `amount` parameter of the `transfer` function. :::note -In the following example, we have not revealed the account that we want to empty. We need to keep in mind that there are fees related to a reveal operation. We are subtracting 1420 mutez from the balance to cover reveal fees. +In the following example, we have not revealed the account that we want to empty. We need to keep in mind that there are fees related to a reveal operation. We are estimating the reveal fee and subtracting them from the balance to cover reveal fees. -**If the account to drain has already been revealed, you must not subtract this amount (1420 mutez) from the balance.** +**If the account to drain has already been revealed, you must not subtract reveal fee from the balance.** ::: ```js live noInline // const Tezos = new TezosToolkit('https://api.tez.ie/rpc/florencenet'); +// import { DEFAULT_FEE } from "@taquito/taquito"; Tezos.signer .publicKeyHash() @@ -33,43 +34,50 @@ Tezos.signer balance.toNumber() / 1000000 } ꜩ.` ); - Tezos.estimate - .transfer({ - to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr', - amount: balance.toNumber() - 1420, - mutez: true, - }) - .then((estimate) => { - //Subtract 1420 mutez for fees related to the reveal operation - const maxAmount = balance.minus(estimate.suggestedFeeMutez).toNumber() - 1420; - println( - `The estimated fees related to the emptying operation are ${ - estimate.suggestedFeeMutez - } mutez.\nConsidering the fees, the amount we need to send to empty the account is ${ - maxAmount / 1000000 - } ꜩ.` - ); - return Tezos.contract.transfer({ + return Tezos.estimate + .transfer({ to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr', + amount: balance.toNumber() - DEFAULT_FEE.REVEAL, // Remove default reveal fee mutez: true, - amount: maxAmount, - fee: estimate.suggestedFeeMutez, - gasLimit: estimate.gasLimit, - storageLimit: 0, - }); - }) - .then((op) => { - println(`Waiting for confirmation of the draining operation...`); - return op.confirmation(1).then(() => op.hash); - }) - .then((hash) => { - println(`The account has been emptied.`); - return Tezos.tz.getBalance(address); - }) - .then((finalBalance) => { - println(`The balance is now ${finalBalance.toNumber() / 1000000} ꜩ.`); - }); - }); + }) + .then((estimate) => { + Tezos.estimate.reveal() + .then((actualRevealFee) => { + // Subtract estimated fees related to the reveal operation + const maxAmount = balance.minus( + estimate.suggestedFeeMutez + actualRevealFee.suggestedFeeMutez + ).toNumber(); + println( + `The estimated fees related to the emptying operation are ${ + estimate.suggestedFeeMutez + } mutez.\nThe estimated fees related to the reveal operation are ${ + actualRevealFee.suggestedFeeMutez + } mutez.\nConsidering those fees, the amount we need to send to empty the account is ${ + maxAmount / 1000000 + } ꜩ.` + ); + return Tezos.contract.transfer({ + to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr', + mutez: true, + amount: maxAmount, + fee: estimate.suggestedFeeMutez, + gasLimit: estimate.gasLimit, + storageLimit: 0, + }); + }) + .then((op) => { + println(`Waiting for confirmation of the draining operation...`); + return op.confirmation(1).then(() => op.hash); + }) + .then((hash) => { + println(`The account has been emptied.`); + return Tezos.tz.getBalance(address); + }) + .then((finalBalance) => { + println(`The balance is now ${finalBalance.toNumber() / 1000000} ꜩ.`); + }) + }) + }) }) .catch((error) => println(`Error: ${JSON.stringify(error, null, 2)}`)); ``` diff --git a/website/src/theme/CodeBlock/index.js b/website/src/theme/CodeBlock/index.js index 477fa667f2..3241e370a6 100755 --- a/website/src/theme/CodeBlock/index.js +++ b/website/src/theme/CodeBlock/index.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; -import { TezosToolkit, MichelsonMap, compose } from '@taquito/taquito'; +import { TezosToolkit, MichelsonMap, compose, DEFAULT_FEE } from '@taquito/taquito'; import { importKey } from '@taquito/signer'; import { validateAddress, @@ -113,7 +113,8 @@ export default ({ TransportU2F, compose, Schema, - ParameterSchema + ParameterSchema, + DEFAULT_FEE }} code={children.trim()} theme={prism.theme || defaultTheme} From 6f62e01cab8c0c9ec16549a74f75f9a14c57a5b0 Mon Sep 17 00:00:00 2001 From: Roxane Letourneau Date: Fri, 4 Jun 2021 14:39:12 -0700 Subject: [PATCH 2/2] grammar fixes --- docs/drain_account.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/drain_account.md b/docs/drain_account.md index 060a7a1eea..0f4412508e 100644 --- a/docs/drain_account.md +++ b/docs/drain_account.md @@ -16,9 +16,9 @@ Once we know the associated fees, we can calculate the maximum amount that needs Finally, we can do the transfer operation and use the maximum amount we just calculated as the `amount` parameter of the `transfer` function. :::note -In the following example, we have not revealed the account that we want to empty. We need to keep in mind that there are fees related to a reveal operation. We are estimating the reveal fee and subtracting them from the balance to cover reveal fees. +In the following example, we have not revealed the account that we want to empty. We need to keep in mind that there are fees related to a reveal operation. We are estimating the reveal fees and subtracting them from the balance to cover reveal fees. -**If the account to drain has already been revealed, you must not subtract reveal fee from the balance.** +**If the account to drain has already been revealed, you must not subtract the reveal fee from the balance.** ::: ```js live noInline