Skip to content

Commit d9078ae

Browse files
authored
feat: add credit transfer tutorial (#27)
* feat: add credit transfer tutorial * docs: update readme and example env * refactor: display recipient balance before and after * chore: fix eof * refactor: fix name
1 parent b71b259 commit d9078ae

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ IDENTITY_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg'
55
# CONTRACT_ID comes from the "$id" found in the "contract-register-minimal.js" response
66
CONTRACT_ID='4BRJbxsDTFY4GJGrCqM6KUjv1wSQDBuUYiGkuzgcrD5d'
77
# NETWORK sets which network to connect to: testnet, devnet, or local
8-
NETWORK='testnet'
8+
NETWORK='testnet'
9+
# RECIPIENT_ID sets an identity ID to receive a credit transfer
10+
RECIPIENT_ID='6cSbshXPYDA2CmBtD31X4uo7YLwtef4mVDt15zRok8Xg'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/identities-and-names/transfer-credits-to-an-identity.html
2+
const Dash = require('dash');
3+
const dotenv = require('dotenv');
4+
dotenv.config();
5+
6+
const clientOpts = {
7+
network: process.env.NETWORK,
8+
wallet: {
9+
mnemonic: process.env.MNEMONIC, // A Dash wallet mnemonic with testnet funds
10+
unsafeOptions: {
11+
skipSynchronizationBeforeHeight: process.env.SYNC_START_HEIGHT, // only sync from early-2022
12+
},
13+
},
14+
};
15+
const client = new Dash.Client(clientOpts);
16+
17+
const transferCredits = async () => {
18+
const identityId = process.env.IDENTITY_ID; // Your identity ID
19+
const identity = await client.platform.identities.get(identityId);
20+
21+
const recipientId = process.env.RECIPIENT_ID; // Recipient's ID
22+
const recipientIdentity = await client.platform.identities.get(recipientId);
23+
console.log('Recipient identity balance before transfer: ', recipientIdentity.balance);
24+
const transferAmount = 1000; // Number of credits to transfer
25+
26+
await client.platform.identities.creditTransfer(
27+
identity,
28+
recipientId,
29+
transferAmount,
30+
);
31+
return client.platform.identities.get(recipientId);
32+
};
33+
34+
transferCredits()
35+
.then((d) => console.log('Recipient identity balance after transfer: ', d.balance))
36+
.catch((e) => console.error('Something went wrong:\n', e))
37+
.finally(() => client.disconnect());

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ align with the tutorials section found on the
4444
After [creating an identity](./1-Identities-and-Names/identity-register.js), set
4545
the `IDENTITY_ID` value in your `.env` file to your new identity ID. After
4646
[registering a data contract](./2-Contracts-and-Documents/contract-register-minimal.js),
47-
set the `CONTRACT_ID` value in your `.env` file to your new contract ID.
47+
set the `CONTRACT_ID` value in your `.env` file to your new contract ID. To do
48+
credit transfers between identities, create a second identity and set the
49+
`RECIPIENT_ID` value in your `.env` file to its ID.
4850

4951
## Contributing
5052

0 commit comments

Comments
 (0)