diff --git a/.env.example b/.env.example index eb31c60..e49c633 100644 --- a/.env.example +++ b/.env.example @@ -11,6 +11,9 @@ DATA_CONTRACT_ID='' # RECIPIENT_ID is an identity ID for credit transfer tutorials RECIPIENT_ID='' +# RECIPIENT_PLATFORM_ADDRESS is a bech32m platform address (tdash1...) for send-funds tutorial +RECIPIENT_PLATFORM_ADDRESS='' + # WITHDRAWAL_ADDRESS is a Core chain address for credit withdrawal tutorials WITHDRAWAL_ADDRESS='' diff --git a/send-funds.js b/send-funds.js deleted file mode 100644 index 400be88..0000000 --- a/send-funds.js +++ /dev/null @@ -1,26 +0,0 @@ -// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/send-funds.html - -const setupDashClient = require('./setupDashClient'); - -const client = setupDashClient(); - -const sendFunds = async () => { - const account = await client.getWalletAccount(); - - const transaction = account.createTransaction({ - recipient: 'yP8A3cbdxRtLRduy5mXDsBnJtMzHWs6ZXr', // Testnet faucet - satoshis: 10000000, // 0.1 Dash - }); - return account.broadcastTransaction(transaction); -}; - -sendFunds() - .then((d) => console.log('Transaction broadcast!\nTransaction ID:', d)) - .catch((e) => console.error('Something went wrong:\n', e)) - .finally(() => client.disconnect()); - -// Handle wallet async errors -client.on('error', (error, context) => { - console.error(`Client error: ${error.name}`); - console.error(context); -}); diff --git a/send-funds.mjs b/send-funds.mjs new file mode 100644 index 0000000..09f9604 --- /dev/null +++ b/send-funds.mjs @@ -0,0 +1,36 @@ +// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/send-funds.html +import { setupDashClient } from './setupDashClient.mjs'; + +const { sdk, addressKeyManager } = await setupDashClient(); +const signer = addressKeyManager.getSigner(); + +const recipient = + process.env.RECIPIENT_PLATFORM_ADDRESS || + 'tdash1kr2ygqnqvsms509f78t4v3uqmce2re22jqycaxh4'; +const amount = 500000n; // 0.000005 DASH + +try { + const result = await sdk.addresses.transfer({ + inputs: [ + { + address: addressKeyManager.primaryAddress.bech32m, + amount, + }, + ], + outputs: [ + { + address: recipient, + amount, + }, + ], + signer, + }); + console.log(`Transaction broadcast! Sent ${amount} credits to ${recipient}`); + for (const [address, info] of result) { + const addr = + typeof address === 'string' ? address : address.toBech32m('testnet'); + console.log(` ${addr}: ${info.balance} credits (nonce: ${info.nonce})`); + } +} catch (e) { + console.error('Something went wrong:\n', e.message); +} diff --git a/test/read-write.test.mjs b/test/read-write.test.mjs index f8d18a2..9069570 100644 --- a/test/read-write.test.mjs +++ b/test/read-write.test.mjs @@ -11,6 +11,19 @@ import { const state = {}; describe('Write tutorials (sequential)', { concurrency: 1 }, () => { + // ----------------------------------------------------------------------- + // Phase 0: Address transfers (no identity needed) + // ----------------------------------------------------------------------- + + it('send-funds', { timeout: 120_000 }, async () => { + const result = await runTutorial('send-funds.mjs', { timeoutMs: 120_000 }); + assertTutorialSuccess(result, { + name: 'send-funds', + expectedPatterns: ['Transaction broadcast!'], + errorPatterns: ['Something went wrong'], + }); + }); + // ----------------------------------------------------------------------- // Phase 1: Identity // -----------------------------------------------------------------------