From ed0761d82c7b04f1a4dc04478d88ba6c2e501c17 Mon Sep 17 00:00:00 2001 From: thephez Date: Tue, 12 Dec 2023 16:52:14 -0500 Subject: [PATCH 1/5] feat: add credit transfer tutorial --- .../identity-transfer-credits.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1-Identities-and-Names/identity-transfer-credits.js 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..a6d9503 --- /dev/null +++ b/1-Identities-and-Names/identity-transfer-credits.js @@ -0,0 +1,36 @@ +// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/identities-and-names/transfer-credits.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); + console.log('Identity balance before transfer: ', identity.balance); + + const recipientID = process.env.RECIPIENT_ID; // Recipient's ID + const transferAmount = 10; // Number of credits to transfer + + await client.platform.identities.creditTransfer( + identity, + recipientID, + transferAmount, + ); + return client.platform.identities.get(identityId); +}; + +transferCredits() + .then((d) => console.log('Identity balance after transfer: ', d.balance)) + .catch((e) => console.error('Something went wrong:\n', e)) + .finally(() => client.disconnect()); From 9620bbeae1767ab803db180bebadae725bbe5f1d Mon Sep 17 00:00:00 2001 From: thephez Date: Tue, 12 Dec 2023 17:00:52 -0500 Subject: [PATCH 2/5] docs: update readme and example env --- .env.example | 4 +++- README.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 32ca015..51703af 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' \ No newline at end of file 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 From 03d1c13e2493ad2ab3479221e3b63db2ecc89004 Mon Sep 17 00:00:00 2001 From: thephez Date: Wed, 10 Jan 2024 15:17:34 -0500 Subject: [PATCH 3/5] refactor: display recipient balance before and after --- 1-Identities-and-Names/identity-transfer-credits.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/1-Identities-and-Names/identity-transfer-credits.js b/1-Identities-and-Names/identity-transfer-credits.js index a6d9503..3b3dc44 100644 --- a/1-Identities-and-Names/identity-transfer-credits.js +++ b/1-Identities-and-Names/identity-transfer-credits.js @@ -1,4 +1,4 @@ -// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/identities-and-names/transfer-credits.html +// 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(); @@ -17,20 +17,21 @@ 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); - console.log('Identity balance before transfer: ', identity.balance); const recipientID = process.env.RECIPIENT_ID; // Recipient's ID - const transferAmount = 10; // Number of credits to transfer + 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(identityId); + return client.platform.identities.get(recipientID); }; transferCredits() - .then((d) => console.log('Identity balance after transfer: ', d.balance)) + .then((d) => console.log('Recipient identity balance after transfer: ', d.balance)) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect()); From b673f1872e592bd0fffedcc7ff827adeeaa9b58d Mon Sep 17 00:00:00 2001 From: thephez Date: Wed, 10 Jan 2024 15:19:00 -0500 Subject: [PATCH 4/5] chore: fix eof --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 51703af..1fdaf09 100644 --- a/.env.example +++ b/.env.example @@ -7,4 +7,4 @@ CONTRACT_ID='4BRJbxsDTFY4GJGrCqM6KUjv1wSQDBuUYiGkuzgcrD5d' # NETWORK sets which network to connect to: testnet, devnet, or local NETWORK='testnet' # RECIPIENT_ID sets an identity ID to receive a credit transfer -RECIPIENT_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg' \ No newline at end of file +RECIPIENT_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg' From 3b643850d08f5a1c277e3aebc466594deda96ef4 Mon Sep 17 00:00:00 2001 From: thephez Date: Wed, 10 Jan 2024 15:32:10 -0500 Subject: [PATCH 5/5] refactor: fix name --- 1-Identities-and-Names/identity-transfer-credits.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-Identities-and-Names/identity-transfer-credits.js b/1-Identities-and-Names/identity-transfer-credits.js index 3b3dc44..1f3d0db 100644 --- a/1-Identities-and-Names/identity-transfer-credits.js +++ b/1-Identities-and-Names/identity-transfer-credits.js @@ -18,17 +18,17 @@ 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); + 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, + recipientId, transferAmount, ); - return client.platform.identities.get(recipientID); + return client.platform.identities.get(recipientId); }; transferCredits()