Skip to content

Commit 64a98c8

Browse files
Merge pull request #910 from ecadlabs/drain-account-example
docs(draining implicit accounts): fix Draining implicit accounts live…
2 parents ecde073 + 6f62e01 commit 64a98c8

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

docs/drain_account.md

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Once we know the associated fees, we can calculate the maximum amount that needs
1616
Finally, we can do the transfer operation and use the maximum amount we just calculated as the `amount` parameter of the `transfer` function.
1717

1818
:::note
19-
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.
19+
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.
2020

21-
**If the account to drain has already been revealed, you must not subtract this amount (1420 mutez) from the balance.**
21+
**If the account to drain has already been revealed, you must not subtract the reveal fee from the balance.**
2222
:::
2323

2424
```js live noInline
2525
// const Tezos = new TezosToolkit('https://api.tez.ie/rpc/florencenet');
26+
// import { DEFAULT_FEE } from "@taquito/taquito";
2627

2728
Tezos.signer
2829
.publicKeyHash()
@@ -33,43 +34,50 @@ Tezos.signer
3334
balance.toNumber() / 1000000
3435
} ꜩ.`
3536
);
36-
Tezos.estimate
37-
.transfer({
38-
to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr',
39-
amount: balance.toNumber() - 1420,
40-
mutez: true,
41-
})
42-
.then((estimate) => {
43-
//Subtract 1420 mutez for fees related to the reveal operation
44-
const maxAmount = balance.minus(estimate.suggestedFeeMutez).toNumber() - 1420;
45-
println(
46-
`The estimated fees related to the emptying operation are ${
47-
estimate.suggestedFeeMutez
48-
} mutez.\nConsidering the fees, the amount we need to send to empty the account is ${
49-
maxAmount / 1000000
50-
} ꜩ.`
51-
);
52-
return Tezos.contract.transfer({
37+
return Tezos.estimate
38+
.transfer({
5339
to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr',
40+
amount: balance.toNumber() - DEFAULT_FEE.REVEAL, // Remove default reveal fee
5441
mutez: true,
55-
amount: maxAmount,
56-
fee: estimate.suggestedFeeMutez,
57-
gasLimit: estimate.gasLimit,
58-
storageLimit: 0,
59-
});
60-
})
61-
.then((op) => {
62-
println(`Waiting for confirmation of the draining operation...`);
63-
return op.confirmation(1).then(() => op.hash);
64-
})
65-
.then((hash) => {
66-
println(`The account has been emptied.`);
67-
return Tezos.tz.getBalance(address);
68-
})
69-
.then((finalBalance) => {
70-
println(`The balance is now ${finalBalance.toNumber() / 1000000} ꜩ.`);
71-
});
72-
});
42+
})
43+
.then((estimate) => {
44+
Tezos.estimate.reveal()
45+
.then((actualRevealFee) => {
46+
// Subtract estimated fees related to the reveal operation
47+
const maxAmount = balance.minus(
48+
estimate.suggestedFeeMutez + actualRevealFee.suggestedFeeMutez
49+
).toNumber();
50+
println(
51+
`The estimated fees related to the emptying operation are ${
52+
estimate.suggestedFeeMutez
53+
} mutez.\nThe estimated fees related to the reveal operation are ${
54+
actualRevealFee.suggestedFeeMutez
55+
} mutez.\nConsidering those fees, the amount we need to send to empty the account is ${
56+
maxAmount / 1000000
57+
} ꜩ.`
58+
);
59+
return Tezos.contract.transfer({
60+
to: 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr',
61+
mutez: true,
62+
amount: maxAmount,
63+
fee: estimate.suggestedFeeMutez,
64+
gasLimit: estimate.gasLimit,
65+
storageLimit: 0,
66+
});
67+
})
68+
.then((op) => {
69+
println(`Waiting for confirmation of the draining operation...`);
70+
return op.confirmation(1).then(() => op.hash);
71+
})
72+
.then((hash) => {
73+
println(`The account has been emptied.`);
74+
return Tezos.tz.getBalance(address);
75+
})
76+
.then((finalBalance) => {
77+
println(`The balance is now ${finalBalance.toNumber() / 1000000} ꜩ.`);
78+
})
79+
})
80+
})
7381
})
7482
.catch((error) => println(`Error: ${JSON.stringify(error, null, 2)}`));
7583
```

website/src/theme/CodeBlock/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8-
import { TezosToolkit, MichelsonMap, compose } from '@taquito/taquito';
8+
import { TezosToolkit, MichelsonMap, compose, DEFAULT_FEE } from '@taquito/taquito';
99
import { importKey } from '@taquito/signer';
1010
import {
1111
validateAddress,
@@ -113,7 +113,8 @@ export default ({
113113
TransportU2F,
114114
compose,
115115
Schema,
116-
ParameterSchema
116+
ParameterSchema,
117+
DEFAULT_FEE
117118
}}
118119
code={children.trim()}
119120
theme={prism.theme || defaultTheme}

0 commit comments

Comments
 (0)