Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update tutorials for v1.4 #82

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _templates/sidebar-main.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
identity’s balance</a>
</li>

<li class="toctree-l2"><a class="reference internal"
href="docs/tutorials/identities-and-names/withdraw-an-identity-balance.html">Withdraw an
identity's balance</a>
</li>

<li class="toctree-l2"><a class="reference internal"
href="docs/tutorials/identities-and-names/update-an-identity.html">Update an
identity</a>
Expand All @@ -59,6 +64,11 @@
an account’s
identities</a></li>

<li class="toctree-l2"><a class="reference internal"
href="docs/tutorials/identities-and-names/transfer-credits-to-an-identity.html">Transfer to
an identity</a>
</li>

<li class="toctree-l2"><a class="reference internal"
href="docs/tutorials/identities-and-names/register-a-name-for-an-identity.html">Register
a name for an
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/connecting-to-testnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ npm install dash
### 2. Connect to Dash Platform

:::{tip}
The JavaScript Dash SDK connects to testnet by default. Mainnet can only be accessed by [connecting via address](#connect-via-address).
The JavaScript Dash SDK connects to mainnet by default. To connect to other networks,
set the `network` option when instantiating the client as shown in the following example.
:::

Create a file named `dashConnect.js` with the following contents. Then run it by typing `node dashConnect.js` from the command line:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Dash = require('dash');
const { PlatformProtocol: { Identifier } } = Dash;

const myContractId = 'a contract ID';
const client = new Dash.Client();
const client = new Dash.Client({ network: 'testnet' });

client.platform.contracts.get(myContractId)
.then((myContract) => {
Expand Down
1 change: 1 addition & 0 deletions docs/tutorials/identities-and-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The following tutorials cover creating and managing identities as well as creati
identities-and-names/register-an-identity
identities-and-names/retrieve-an-identity
identities-and-names/topup-an-identity-balance
identities-and-names/withdraw-an-identity-balance
identities-and-names/update-an-identity
identities-and-names/retrieve-an-accounts-identities
identities-and-names/transfer-credits-to-an-identity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
```{eval-rst}
.. tutorials-withdraw-identity-balance:
```

:::{attention}
Mainnet withdrawals will not be available until the activation of Dash Platform v1.4 on mainnet in late October or early November. They are already available on testnet.
:::

# Withdraw an Identity's balance

The purpose of this tutorial is to walk through the steps necessary to withdraw part of their identity's balance from Platform to a Dash address.

## Overview

Over time, users may want to convert some of their identity's [Platform credits](../../explanations/identity.md#credits) back to Dash for use on the Core chain.

## Prerequisites

- [General prerequisites](../../tutorials/introduction.md#prerequisites) (Node.js / Dash SDK installed)
- A wallet mnemonic with some funds in it: [Tutorial: Create and Fund a Wallet](../../tutorials/create-and-fund-a-wallet.md)
- A configured client: [Setup SDK Client](../setup-sdk-client.md)
- A Dash Platform Identity with a credit balance: [Tutorial: Register an Identity](../../tutorials/identities-and-names/register-an-identity.md)
- A Core chain address to receive the withdrawn credits as Dash

## Code

```javascript
const setupDashClient = require('../setupDashClient');

const client = setupDashClient();

const withdrawCredits = async () => {
const identityId = 'an identity ID goes here';
const identity = await client.platform.identities.get(identityId);

console.log('Identity balance before transfer: ', identity.balance);

const toAddress = 'a Dash address goes here';
const amount = 1000000; // Number of credits to withdraw
const amountDash = amount / (1000 * 100000000);

console.log(`Withdrawing ${amount} credits (${amountDash} DASH)`);
// Temporarily force minRelay to have a value so withdrawal succeeds
// https://github.com/dashpay/platform/issues/2233
client.wallet.storage.getDefaultChainStore().state.fees.minRelay = 1000;

const response = await client.platform.identities.withdrawCredits(
identity,
amount,
{
toAddress,
},
);
return client.platform.identities.get(identityId);
};

withdrawCredits()
.then((d) => console.log('Identity balance after withdrawal: ', d.balance))
.catch((e) => console.error('Something went wrong:\n', e))
.finally(() => client.disconnect());
```

## What's Happening

After connecting to the Client, we get an identity and set the withdrawal address and amount. We then call `platform.identities.withdrawCredits` with the identity, withdrawal amount in credits, and the destination address. This creates an unlock transaction and decreases the identity's credit balance by the relevant amount including a fee. The updated identity balance is output to the console. Once the withdrawal is processed, it is broadcast to the Core chain where the unlocked Dash is sent to the provided destination address.

:::{note}
:class: note
Since the SDK does not cache wallet information, lengthy re-syncs (5+ minutes) may be required for some Core chain wallet operations. See [Wallet Operations](../setup-sdk-client.md#wallet-operations) for options.
:::
5 changes: 3 additions & 2 deletions docs/tutorials/setup-sdk-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ In this tutorial we will show how to configure the client for use in the remaini
## Code

:::{tip}
The JavaScript Dash SDK connects to testnet by default. Mainnet can only be accessed by [connecting via address](./connecting-to-testnet.md#connect-to-a-network).
The JavaScript Dash SDK connects to mainnet by default. To connect to other networks,
set the `network` option when instantiating the client as shown in the following example.
:::

Save the following client configuration module in a file named `setupDashClient.js`. This module
Expand All @@ -30,7 +31,7 @@ will be re-used in later tutorials.

// Fully configured client options
const clientOptions = {
// The network to connect to ('testnet' or 'local')
// The network to connect to ('mainnet', 'testnet' or 'local')
network: 'testnet',

// Wallet configuration for transactions and account management
Expand Down