-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtoken-transfer.mjs
More file actions
60 lines (50 loc) · 1.71 KB
/
Copy pathtoken-transfer.mjs
File metadata and controls
60 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/tokens/transfer-tokens-to-an-identity.html
import { setupDashClient } from '../setupDashClient.mjs';
const { sdk, keyManager } = await setupDashClient();
const { identity, identityKey, signer } = await keyManager.getTransfer();
// TOKEN_CONTRACT_ID comes from token-register.mjs.
const dataContractId = process.env.TOKEN_CONTRACT_ID;
const tokenPosition = 0;
// Default recipient (testnet). Replace or override via RECIPIENT_ID.
const recipientId =
process.env.RECIPIENT_ID || '7XcruVSsGQVSgTcmPewaE4tXLutnW1F6PXxwMbo8GYQC';
const amount = 1n;
try {
if (!dataContractId) {
throw new Error(
'Set TOKEN_CONTRACT_ID in .env from token-register.mjs output.',
);
}
const senderId = identity.id.toString();
if (recipientId === senderId) {
throw new Error('Cannot transfer tokens to yourself.');
}
const tokenId = await sdk.tokens.calculateId(dataContractId, tokenPosition);
const balancesBefore = await sdk.tokens.identityBalances(recipientId, [
tokenId,
]);
console.log(
`Recipient token balance before transfer: ${balancesBefore.get(tokenId) ?? 0n}`,
);
await sdk.tokens.transfer({
dataContractId,
tokenPosition,
amount,
senderId,
recipientId,
identityKey,
signer,
});
const balancesAfter = await sdk.tokens.identityBalances(recipientId, [
tokenId,
]);
console.log(
`Transferred ${amount} token${amount === 1n ? '' : 's'} from ${senderId} to ${recipientId}`,
);
console.log('Token ID:', tokenId);
console.log(
`Recipient token balance after transfer: ${balancesAfter.get(tokenId) ?? 0n}`,
);
} catch (e) {
console.error('Something went wrong:\n', e.message);
}