|
4 | 4 |
|
5 | 5 | # Send funds |
6 | 6 |
|
7 | | -Once you have a wallet and some funds ([tutorial](../tutorials/create-and-fund-a-wallet.md)), another common task is sending Dash to an address. (Sending Dash to a contact or a DPNS identity requires the Dashpay app, which has not been registered yet.) |
8 | | - |
9 | | -# Code |
10 | | - |
11 | | -:::{note} |
12 | | -:class: note |
13 | | -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. |
14 | | -::: |
15 | | - |
16 | | -```javascript |
17 | | -const setupDashClient = require('../setupDashClient'); |
18 | | - |
19 | | -const client = setupDashClient(); |
20 | | - |
21 | | -const sendFunds = async () => { |
22 | | - const account = await client.getWalletAccount(); |
23 | | - |
24 | | - const transaction = account.createTransaction({ |
25 | | - recipient: 'yP8A3cbdxRtLRduy5mXDsBnJtMzHWs6ZXr', // Testnet2 faucet |
26 | | - satoshis: 100000000, // 1 Dash |
| 7 | +The purpose of this tutorial is to walk through the steps necessary to transfer credits from one platform address to another. Platform addresses are bech32m-encoded addresses used for Dash Platform operations. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- [General prerequisites](../tutorials/introduction.md#prerequisites) (Node.js / Dash SDK installed) |
| 12 | +- A platform address with a balance: [Tutorial: Create and Fund a Wallet](../tutorials/create-and-fund-a-wallet.md) |
| 13 | +- A configured client: [Setup SDK Client](./setup-sdk-client.md) |
| 14 | + |
| 15 | +## Code |
| 16 | + |
| 17 | +```{code-block} javascript |
| 18 | +:caption: send-funds.mjs |
| 19 | +
|
| 20 | +import { setupDashClient } from './setupDashClient.mjs'; |
| 21 | +
|
| 22 | +const { sdk, addressKeyManager } = await setupDashClient(); |
| 23 | +const signer = addressKeyManager.getSigner(); |
| 24 | +
|
| 25 | +const recipient = |
| 26 | + process.env.RECIPIENT_PLATFORM_ADDRESS || |
| 27 | + 'tdash1kr2ygqnqvsms509f78t4v3uqmce2re22jqycaxh4'; |
| 28 | +const amount = 500000n; // 0.000005 DASH |
| 29 | +
|
| 30 | +try { |
| 31 | + const result = await sdk.addresses.transfer({ |
| 32 | + inputs: [ |
| 33 | + { |
| 34 | + address: addressKeyManager.primaryAddress.bech32m, |
| 35 | + amount, |
| 36 | + }, |
| 37 | + ], |
| 38 | + outputs: [ |
| 39 | + { |
| 40 | + address: recipient, |
| 41 | + amount, |
| 42 | + }, |
| 43 | + ], |
| 44 | + signer, |
27 | 45 | }); |
28 | | - return account.broadcastTransaction(transaction); |
29 | | -}; |
30 | | - |
31 | | -sendFunds() |
32 | | - .then((d) => console.log('Transaction broadcast!\nTransaction ID:', d)) |
33 | | - .catch((e) => console.error('Something went wrong:\n', e)) |
34 | | - .finally(() => client.disconnect()); |
35 | | - |
36 | | -// Handle wallet async errors |
37 | | -client.on('error', (error, context) => { |
38 | | - console.error(`Client error: ${error.name}`); |
39 | | - console.error(context); |
40 | | -}); |
| 46 | + console.log(`Transaction broadcast! Sent ${amount} credits to ${recipient}`); |
| 47 | + for (const [address, info] of result) { |
| 48 | + const addr = |
| 49 | + typeof address === 'string' ? address : address.toBech32m('testnet'); |
| 50 | + console.log(` ${addr}: ${info.balance} credits (nonce: ${info.nonce})`); |
| 51 | + } |
| 52 | +} catch (e) { |
| 53 | + console.error('Something went wrong:\n', e.message); |
| 54 | +} |
41 | 55 | ``` |
42 | 56 |
|
43 | | -# What's Happening |
| 57 | +## What's Happening |
44 | 58 |
|
45 | | -After initializing the Client, we build a new transaction with `account.createTransaction`. It requires a recipient and an amount in satoshis (often called "duffs" in Dash). 100 million satoshis equals one Dash. We pass the transaction to `account.broadcastTransaction` and wait for it to return. Then we output the result, which is a transaction ID. After that we disconnect from the Client so node can exit. |
| 59 | +After we initialize the Client via `setupDashClient()`, we get a signer from the `addressKeyManager`. We then call `sdk.addresses.transfer()` with `inputs` (the sender's address and amount to debit) and `outputs` (the recipient's address and amount to credit), along with the signer. |
0 commit comments