UNPKG

@waves/node-state

Version:
104 lines 4.84 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const waves_transactions_1 = require("@waves/waves-transactions"); const constants_1 = require("../constants"); const utils_1 = require("../utils"); const console_1 = __importDefault(require("../utils/console")); function default_1(accounts) { return Promise.all(Object.entries(accounts).map(([key, account]) => __awaiter(this, void 0, void 0, function* () { const seed = account.seed || waves_transactions_1.libs.crypto.randomSeed(); const address = waves_transactions_1.libs.crypto.address(seed, constants_1.CHAIN_ID); const publicKey = waves_transactions_1.libs.crypto.publicKey(seed); const userAlias = account.alias ? typeof account.alias === 'string' ? account.alias : `${key}@${Date.now()}`.toLocaleLowerCase() : undefined; console_1.default.log(`Add account ${key} ${address}`); yield setBalance(address, 100 * Math.pow(10, 8)); if (userAlias) { const tx = waves_transactions_1.alias({ chainId: constants_1.CHAIN_ID, alias: userAlias, additionalFee: 0.001 * Math.pow(10, 8) }, seed); yield utils_1.broadcastAndWait(tx); } if (account.data) { yield Promise.all(Object.entries(account.data).map(([key, { type, value }]) => __awaiter(this, void 0, void 0, function* () { const tx = waves_transactions_1.data({ chainId: constants_1.CHAIN_ID, data: [{ key, type, value }] }, seed); yield utils_1.broadcastAndWait(tx); }))); } if (account.script) { const script = typeof account.script === 'boolean' ? constants_1.ACCOUNT_SCRIPT : account.script === 'dApp' ? constants_1.DAP_SCRIPT : account.script; yield addScript(seed, script); } if (account.lease) { const randomAddress = waves_transactions_1.libs.crypto.address(waves_transactions_1.libs.crypto.randomSeed(), constants_1.CHAIN_ID); const amount = Math.pow(10, 8); yield setLeasing(randomAddress, amount); } const { available } = yield waves_transactions_1.nodeInteraction.balanceDetails(address, constants_1.NODE_URL); const toSend = 100 * Math.pow(10, 8) - (+available); yield setBalance(address, toSend); return { [key]: { seed, alias: userAlias, address, publicKey, scripted: !!account.script, data: account.data, lease: account.lease } }; }))) .then(list => list.reduce((acc, item) => Object.assign(acc, item), Object.create(null))); } exports.default = default_1; function setBalance(recipient, amount, assetId) { return __awaiter(this, void 0, void 0, function* () { const balanceTx = waves_transactions_1.transfer({ recipient, amount, assetId, additionalFee: 0.004 * Math.pow(10, 8) }, constants_1.MASTER_ACCOUNT_SEED); yield utils_1.broadcastAndWait(balanceTx); }); } function setLeasing(recipient, amount) { return __awaiter(this, void 0, void 0, function* () { const leaseTx = waves_transactions_1.lease({ chainId: constants_1.CHAIN_ID, recipient, amount, additionalFee: 0.004 * Math.pow(10, 8) }, constants_1.MASTER_ACCOUNT_SEED); yield utils_1.broadcastAndWait(leaseTx); }); } function addScript(seed, script) { return __awaiter(this, void 0, void 0, function* () { const tx = waves_transactions_1.setScript({ chainId: constants_1.CHAIN_ID, script, additionalFee: 0.004 * Math.pow(10, 8) }, seed); yield utils_1.broadcastAndWait(tx); }); } //# sourceMappingURL=createAccounts.js.map