-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtoken-register.mjs
More file actions
137 lines (124 loc) · 4.3 KB
/
Copy pathtoken-register.mjs
File metadata and controls
137 lines (124 loc) · 4.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/tokens/register-a-token-contract.html
import {
AuthorizedActionTakers,
ChangeControlRules,
DataContract,
TokenConfiguration,
TokenConfigurationConvention,
TokenConfigurationLocalization,
TokenDistributionRules,
TokenKeepsHistoryRules,
TokenMarketplaceRules,
TokenTradeMode,
} from '@dashevo/evo-sdk';
import { setupDashClient } from '../setupDashClient.mjs';
const { sdk, keyManager } = await setupDashClient();
const { identity, identityKey, signer } = await keyManager.getAuth();
const TOKEN_POSITION = 0;
const TOKEN_NAME = 'TutorialToken';
const TOKEN_PLURAL = 'TutorialTokens';
const TOKEN_BASE_SUPPLY = 100n; // Token amounts are bigint values
const TOKEN_MAX_SUPPLY = 1000n;
// This contract includes one small document type so learners can still use the
// standard document tutorials with the same contract if they want to.
const documentSchemas = {
note: {
type: 'object',
properties: {
message: {
type: 'string',
position: 0,
},
},
additionalProperties: false,
},
};
function createTutorialTokenConfiguration(ownerId) {
const contractOwner = AuthorizedActionTakers.ContractOwner();
const noOne = AuthorizedActionTakers.NoOne();
const ownerRules = new ChangeControlRules({
authorizedToMakeChange: contractOwner,
adminActionTakers: contractOwner,
isChangingAuthorizedActionTakersToNoOneAllowed: true,
isChangingAdminActionTakersToNoOneAllowed: true,
isSelfChangingAdminActionTakersAllowed: true,
});
const lockedRules = new ChangeControlRules({
authorizedToMakeChange: noOne,
adminActionTakers: noOne,
});
return new TokenConfiguration({
conventions: new TokenConfigurationConvention(
{
en: new TokenConfigurationLocalization(false, TOKEN_NAME, TOKEN_PLURAL),
},
0,
),
conventionsChangeRules: ownerRules,
baseSupply: TOKEN_BASE_SUPPLY,
maxSupply: TOKEN_MAX_SUPPLY,
keepsHistory: new TokenKeepsHistoryRules({
isKeepingBurningHistory: true,
isKeepingMintingHistory: true,
isKeepingTransferHistory: true,
}),
maxSupplyChangeRules: lockedRules,
distributionRules: new TokenDistributionRules({
newTokensDestinationIdentity: ownerId,
newTokensDestinationIdentityRules: ownerRules,
mintingAllowChoosingDestination: false,
mintingAllowChoosingDestinationRules: ownerRules,
perpetualDistributionRules: lockedRules,
changeDirectPurchasePricingRules: lockedRules,
}),
marketplaceRules: new TokenMarketplaceRules(
TokenTradeMode.NotTradeable(),
lockedRules,
),
// Minting and burning are enabled so the next tutorials can demonstrate
// the normal issuer-managed token lifecycle.
manualMintingRules: ownerRules,
manualBurningRules: ownerRules,
freezeRules: lockedRules,
unfreezeRules: lockedRules,
destroyFrozenFundsRules: lockedRules,
emergencyActionRules: lockedRules,
mainControlGroupCanBeModified: noOne,
description: 'Issuer-managed token for Platform token tutorials.',
});
}
try {
const identityNonce = await sdk.identities.nonce(identity.id.toString());
const dataContract = new DataContract({
ownerId: identity.id,
identityNonce: (identityNonce || 0n) + 1n,
schemas: documentSchemas,
tokens: {
[TOKEN_POSITION]: createTutorialTokenConfiguration(
identity.id.toString(),
),
},
fullValidation: true,
});
const publishedContract = await sdk.contracts.publish({
dataContract,
identityKey,
signer,
});
const contractId =
publishedContract.id?.toString() || publishedContract.toJSON?.()?.id;
if (!contractId) {
const publishResult = publishedContract.toJSON?.() ?? publishedContract;
throw new Error(
`Contract publish returned no id: ${JSON.stringify(publishResult)}`,
);
}
const tokenId = await sdk.tokens.calculateId(contractId, TOKEN_POSITION);
console.log('Token contract registered:\n', publishedContract.toJSON());
console.log('Token position:', TOKEN_POSITION);
console.log('Token ID:', tokenId);
console.log('Initial owner token balance:', TOKEN_BASE_SUPPLY.toString());
console.log('Maximum token supply:', TOKEN_MAX_SUPPLY.toString());
} catch (e) {
console.error('Something went wrong:\n', e.message);
}