-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
377 lines (332 loc) · 10.2 KB
/
utils.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
const algosdk = require('algosdk');
// Algorand minimal balance
const ALGORAND_MINIMAL_BALANCE = 100000;
/**
* Get asset id by unit name.
*
* @param {Algodv2} algodClient Instance of the Algodv2 client
* @param {Kmd} kmdClient Instance of the Kmd client
* @param {Indexer} indexerClient Instance of the Indexer client
* @param {String} name Unit name of the asset
* @return {Number} Asset id
*/
async function getAssetId(algodClient, kmdClient, indexerClient, name) {
const { assets } = await indexerClient.searchForAssets().unit(name).do();
if (assets.length) {
return assets[0].index;
}
// If asset doesn't exist, create one
const assetId = await createAsset(algodClient, kmdClient, name);
return assetId;
}
/**
* Ensure that second account in the Sandbox default wallet has
* enough asset balance to make transactions.
*
* If the account is not opted-in for the asset, an opt-in transaction is created.
* If the balance is not sufficient, it is topped up.
*
* @param {Algodv2} algodClient Instance of the Algodv2 client
* @param {Kmd} kmdClient Instance of the Kmd client
* @param {Number} assetId Asset id
*/
async function ensureSecondDefaultAccountHasAssetBalance(
algodClient,
kmdClient,
assetId
) {
const MIN_ASSET_BALANCE = 10000;
const { wallets } = await kmdClient.listWallets();
const hotWallet = wallets.find(
({ name }) => name === process.env.HOT_WALLET_NAME
);
const { wallet_handle_token: hotWalletHandle } =
await kmdClient.initWalletHandle(
hotWallet.id,
process.env.HOT_WALLET_PASSWORD
);
const {
addresses: [firstAccount, secondAccount]
} = await kmdClient.listKeys(hotWalletHandle);
const { assets } = await algodClient.accountInformation(secondAccount).do();
const asset = assets.find((asset) => asset['asset-id'] === assetId);
let assetBalance = 0;
if (asset) {
assetBalance = asset.amount;
} else {
await makeOptInTransaction(
algodClient,
kmdClient,
hotWalletHandle,
process.env.HOT_WALLET_PASSWORD,
secondAccount,
assetId
);
}
if (assetBalance < MIN_ASSET_BALANCE) {
const amount = MIN_ASSET_BALANCE - assetBalance;
const params = await algodClient.getTransactionParams().do();
const closeRemainderTo = undefined;
const revocationTarget = undefined;
const note = undefined;
const txo = algosdk.makeAssetTransferTxnWithSuggestedParams(
firstAccount,
secondAccount,
closeRemainderTo,
revocationTarget,
amount,
note,
assetId,
params
);
const blob = await kmdClient.signTransaction(
hotWalletHandle,
process.env.HOT_WALLET_PASSWORD,
txo
);
const { txId } = await algodClient.sendRawTransaction(blob).do();
await waitForConfirmation(algodClient, txId);
}
await kmdClient.releaseWalletHandle(hotWalletHandle);
}
/**
* Create a new Algorand Standard Asset (ASA).
*
* @param {Algodv2} algodClient Instance of the Algodv2 client
* @param {Kmd} kmdClient Instance of the Kmd client
* @param {String} name Asset name
* @param {Number} [totalSupply=100000] Total supply of the created asset
* @param {Number} [decimals=0] Number of decimals of created asset
* @return {Number} Created asset id
*/
async function createAsset(
algodClient,
kmdClient,
name,
totalSupply = 100000,
decimals = 0
) {
const { wallets } = await kmdClient.listWallets();
const hotWallet = wallets.find(
({ name }) => name === process.env.HOT_WALLET_NAME
);
const { wallet_handle_token: hotWalletHandle } =
await kmdClient.initWalletHandle(
hotWallet.id,
process.env.HOT_WALLET_PASSWORD
);
const {
addresses: [creatorAddress]
} = await kmdClient.listKeys(hotWalletHandle);
const note = undefined;
const defaultFrozen = false;
const manager = creatorAddress;
const reserve = creatorAddress;
const freeze = creatorAddress;
const clawback = creatorAddress;
const unitName = name;
const assetName = name;
const assetURL = '';
const assetMetadataHash = undefined;
const params = await algodClient.getTransactionParams().do();
const txo = algosdk.makeAssetCreateTxnWithSuggestedParams(
creatorAddress,
note,
totalSupply,
decimals,
defaultFrozen,
manager,
reserve,
freeze,
clawback,
unitName,
assetName,
assetURL,
assetMetadataHash,
params
);
const blob = await kmdClient.signTransaction(
hotWalletHandle,
process.env.HOT_WALLET_PASSWORD,
txo
);
await kmdClient.releaseWalletHandle(hotWalletHandle);
const { txId } = await algodClient.sendRawTransaction(blob).do();
const txInfo = await waitForConfirmation(algodClient, txId);
const assetId = txInfo['asset-index'];
return assetId;
}
/**
* Get deposit wallet object. If deposit wallet does not exist, it is created.
*
* @param {Kmd} kmdClient Instance of the Kmd client
* @return {Object} Wallet object
*/
async function getDepositWallet(kmdClient) {
const { wallets } = await kmdClient.listWallets();
let wallet = wallets.find(
({ name }) => name === process.env.DEPOSIT_WALLET_NAME
);
// If deposit_wallet does not exists, initialize one
if (!wallet) {
const masterDerivationKey = await algosdk.mnemonicToMasterDerivationKey(
process.env.DEPOSIT_WALLET_MNEMONIC
);
const result = await kmdClient.createWallet(
process.env.DEPOSIT_WALLET_NAME,
process.env.DEPOSIT_WALLET_PASSWORD,
masterDerivationKey
);
wallet = result.wallet;
}
return wallet;
}
/**
* Get first address in the Sanbox default wallet.
* It is considered as a hot wallet for demonstration purposes.
*
* @param {Kmd} kmdClient Instance of the Kmd client
* @return {String} First address in the Sanbox default wallet
*/
async function getDefaultHotWalletAddress(kmdClient) {
const { wallets } = await kmdClient.listWallets();
const hotWallet = wallets.find(
({ name }) => name === process.env.HOT_WALLET_NAME
);
const { wallet_handle_token: hotWalletHandle } =
await kmdClient.initWalletHandle(
hotWallet.id,
process.env.HOT_WALLET_PASSWORD
);
const {
addresses: [defaultAddress]
} = await kmdClient.listKeys(hotWalletHandle);
await kmdClient.releaseWalletHandle(hotWalletHandle);
return defaultAddress;
}
/**
* Execute and opt-in transaction
*
* @param {Algodv2} algodClient Instance of the Algodv2 client
* @param {Kmd} kmdClient Instance of the Kmd client
* @param {String} address Adress to opt-in
* @param {Number} assetId Asset to opt-in for
* @return {Object} Transaction information
*/
async function makeOptInTransaction(
algodClient,
kmdClient,
walletHandle,
walletPassword,
address,
assetId
) {
// Get hot wallet handle and default address
const { wallets } = await kmdClient.listWallets();
const hotWallet = wallets.find(
({ name }) => name === process.env.HOT_WALLET_NAME
);
const { wallet_handle_token: hotWalletHandle } =
await kmdClient.initWalletHandle(
hotWallet.id,
process.env.HOT_WALLET_PASSWORD
);
const {
addresses: [defaultAddress]
} = await kmdClient.listKeys(hotWalletHandle);
// Create a transaction sending necessary amount to the account we want to opt-in (the top up transaction)
const txo1 = await (async () => {
const amount = ALGORAND_MINIMAL_BALANCE * 2 + algosdk.ALGORAND_MIN_TX_FEE; // Minimal balance + transaction fee (in Algos)
const sender = defaultAddress;
const receiver = address;
const params = await algodClient.getTransactionParams().do();
const closeRemainderTo = undefined; // not used since we don't want to close the account
const note = undefined; // no additional notes
const txo = algosdk.makePaymentTxnWithSuggestedParams(
sender,
receiver,
amount,
closeRemainderTo,
note,
params
);
return txo;
})();
// Create an opt-in transaction
const txo2 = await (async () => {
const amount = 0;
const params = await algodClient.getTransactionParams().do();
const closeRemainderTo = undefined;
const revocationTarget = undefined;
const note = undefined;
// Create opt-in transaction (note that sender and receiver addresses are the same)
const txo = algosdk.makeAssetTransferTxnWithSuggestedParams(
address,
address,
closeRemainderTo,
revocationTarget,
amount,
note,
assetId,
params
);
return txo;
})();
algosdk.assignGroupID([txo1, txo2]);
// Sign the top up transaction
const blob1 = await kmdClient.signTransaction(
hotWalletHandle,
process.env.HOT_WALLET_PASSWORD,
txo1
);
// Sign the opt-in transaction
const blob2 = await kmdClient.signTransaction(
walletHandle,
walletPassword,
txo2
);
// Send both transactions as an atomic group
const { txId } = await algodClient.sendRawTransaction([blob1, blob2]).do();
const [txInfo] = await Promise.all([
waitForConfirmation(algodClient, txId),
kmdClient.releaseWalletHandle(hotWalletHandle)
]);
return txInfo;
}
/**
* Resolves with transaction information when transaction is confirmed.
*
* @param {Algodv2} algodClient Instance of the Algodv2 client
* @param {String} txId Transaction Id to watch on
* @param {Number} [timeout=60000] Waiting timeout (default: 1 minute)
* @return {Object} Transaction information
*/
async function waitForConfirmation(algodClient, txId, timeout = 60000) {
let { 'last-round': lastRound } = await algodClient.status().do();
while (timeout > 0) {
const startTime = Date.now();
// Get transaction details
const txInfo = await algodClient.pendingTransactionInformation(txId).do();
if (txInfo) {
if (txInfo['confirmed-round']) {
return txInfo;
} else if (txInfo['pool-error'] && txInfo['pool-error'].length > 0) {
throw new Error(txInfo['pool-error']);
}
}
// Wait for the next round
await algodClient.statusAfterBlock(++lastRound).do();
timeout -= Date.now() - startTime;
}
throw new Error('Timeout exceeded');
}
module.exports = {
ALGORAND_MINIMAL_BALANCE,
getAssetId,
ensureSecondDefaultAccountHasAssetBalance,
createAsset,
getDepositWallet,
getDefaultHotWalletAddress,
waitForConfirmation,
makeOptInTransaction
};