diff --git a/.env.example b/.env.example index 32ca015..1fdaf09 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,6 @@ IDENTITY_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg' # CONTRACT_ID comes from the "$id" found in the "contract-register-minimal.js" response CONTRACT_ID='4BRJbxsDTFY4GJGrCqM6KUjv1wSQDBuUYiGkuzgcrD5d' # NETWORK sets which network to connect to: testnet, devnet, or local -NETWORK='testnet' \ No newline at end of file +NETWORK='testnet' +# RECIPIENT_ID sets an identity ID to receive a credit transfer +RECIPIENT_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg' diff --git a/1-Identities-and-Names/identity-transfer-credits.js b/1-Identities-and-Names/identity-transfer-credits.js new file mode 100644 index 0000000..1f3d0db --- /dev/null +++ b/1-Identities-and-Names/identity-transfer-credits.js @@ -0,0 +1,37 @@ +// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/identities-and-names/transfer-credits-to-an-identity.html +const Dash = require('dash'); +const dotenv = require('dotenv'); +dotenv.config(); + +const clientOpts = { + network: process.env.NETWORK, + wallet: { + mnemonic: process.env.MNEMONIC, // A Dash wallet mnemonic with testnet funds + unsafeOptions: { + skipSynchronizationBeforeHeight: process.env.SYNC_START_HEIGHT, // only sync from early-2022 + }, + }, +}; +const client = new Dash.Client(clientOpts); + +const transferCredits = async () => { + const identityId = process.env.IDENTITY_ID; // Your identity ID + const identity = await client.platform.identities.get(identityId); + + const recipientId = process.env.RECIPIENT_ID; // Recipient's ID + const recipientIdentity = await client.platform.identities.get(recipientId); + console.log('Recipient identity balance before transfer: ', recipientIdentity.balance); + const transferAmount = 1000; // Number of credits to transfer + + await client.platform.identities.creditTransfer( + identity, + recipientId, + transferAmount, + ); + return client.platform.identities.get(recipientId); +}; + +transferCredits() + .then((d) => console.log('Recipient identity balance after transfer: ', d.balance)) + .catch((e) => console.error('Something went wrong:\n', e)) + .finally(() => client.disconnect()); diff --git a/README.md b/README.md index 451027a..872d0b6 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,9 @@ align with the tutorials section found on the After [creating an identity](./1-Identities-and-Names/identity-register.js), set the `IDENTITY_ID` value in your `.env` file to your new identity ID. After [registering a data contract](./2-Contracts-and-Documents/contract-register-minimal.js), -set the `CONTRACT_ID` value in your `.env` file to your new contract ID. +set the `CONTRACT_ID` value in your `.env` file to your new contract ID. To do +credit transfers between identities, create a second identity and set the +`RECIPIENT_ID` value in your `.env` file to its ID. ## Contributing