UNPKG

@newcoin-foundation/newcoin-sdk

Version:
999 lines 78.1 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; // EOS imports import { Api, JsonRpc, RpcError } from "eosjs"; import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig'; // development only var ecc = require("eosjs-ecc-priveos"); // Extra backend services import { JsonRpc as HJsonRpc } from "@eoscafe/hyperion"; // Newcoin services import { ActionGenerator as PoolsActionGenerator, RpcApi as PoolsRpcApi } from '@newcoin-foundation/newcoin.pools-js'; import { ActionGenerator as MainDAOActionGenerator } from '@newcoin-foundation/newcoin.pool-js'; import { ActionGenerator as DaosAG, ChainApi as DaosChainApi } from '@newcoin-foundation/newcoin.daos-js'; import { ActionGenerator as sdkActionGen } from "./actions"; import fetch from 'cross-fetch'; import { default_schema, SBT_NFT_schema } from "./types"; export { default_schema }; import { normalizeUsername } from "./utils"; import { getClaimNftsActions, getClaimWinBidActions, getCreateAuctionActions, getEditAuctionActions, getEraseAuctionActions, getPlaceBidActions } from "./neftymarket"; var CREATE_ACCOUNT_DEFAULTS = { ram_amt: 8192, cpu_amount: '100.0000 NCO', net_amount: '100.0000 NCO', xfer: false, }; export var devnet_urls = { nodeos_url: "https://nodeos-dev.newcoin.org", hyperion_url: "https://hyperion-dev.newcoin.org", atomicassets_url: "https://atomic-dev.newcoin.org/" }; export var devnet_services = { eosio_contract: "eosio", token_contract: "eosio.token", maindao_contract: "pool.nco", staking_contract: "pools2.nco", daos_contract: "daos.nco", neftymarket_contract: "market.nefty", atomicassets_contract: "atomicassets" }; /** * The primary tool to interact with [https://newcoin.org](newcoin.org). * * This is an early alpha. * * See [https://docs.newcoin.org/](https://docs.newcoin.org/) for an overview of the newcoin ecosystem. */ var NCO_BlockchainAPI = /** @class */ (function () { /** * Init the api * @name newcoin-api * @param urls * @param services * @returns a Newcoin API instance */ function NCO_BlockchainAPI(urls, services, debug) { if (debug === void 0) { debug = false; } this.debug = false; this.debug = debug; this.urls = urls; if (this.debug) console.log("Init URLS " + JSON.stringify(urls)); //this.aa_api = new ExplorerApi(this.urls.atomicassets_url, "atomicassets", { fetch: node_fetch }); this.nodeos_rpc = new JsonRpc(this.urls.nodeos_url, { fetch: fetch }); this.hrpc = new HJsonRpc(this.urls.hyperion_url, { fetch: fetch }); this.cApi = new DaosChainApi(this.urls.nodeos_url, services.daos_contract, fetch); this.poolsRpcApi = new PoolsRpcApi(this.urls.nodeos_url, services.staking_contract, fetch); // this.poolRpcApi = new PoolRpcApi(this.urls.nodeos_url, services.maindao_contract, fetch) this.aGen = new DaosAG(services.daos_contract, services.staking_contract); this.mGen = new MainDAOActionGenerator(services.maindao_contract, services.token_contract); this.pGen = new PoolsActionGenerator(services.staking_contract, services.maindao_contract); this.sdkGen = new sdkActionGen(services.eosio_contract, services.token_contract); this.services = services; } // Native EOS services /** * Create a key pair assuming a secure environment (not frontend) * @params none * @returns An EOS key pair */ NCO_BlockchainAPI.prototype.createKeyPair = function () { return __awaiter(this, void 0, void 0, function () { var opts, p, t; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ecc.initialize()]; case 1: _a.sent(); opts = { secureEnv: true }; return [4 /*yield*/, ecc.randomKey(0, opts)]; case 2: p = _a.sent(); t = { prv_key: p, pub_key: ecc.privateToPublic(p) }; return [2 /*return*/, t]; } }); }); }; /** * Create a user - multistage operation creating new user account, * defailt collection, schema and template for the account * @param NCCreateUser * @returns NCReturnTxs */ NCO_BlockchainAPI.prototype.createUser = function (inpt) { return __awaiter(this, void 0, void 0, function () { var _a, newUser, newacc_pub_active_key, newacc_pub_owner_key, payer, payer_prv_key, ram_amt, net_amount, cpu_amount, xfer, res, newacc_action, buyram_action, delegatebw_action, tres; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = __assign(__assign({}, CREATE_ACCOUNT_DEFAULTS), inpt), newUser = _a.newUser, newacc_pub_active_key = _a.newacc_pub_active_key, newacc_pub_owner_key = _a.newacc_pub_owner_key, payer = _a.payer, payer_prv_key = _a.payer_prv_key, ram_amt = _a.ram_amt, net_amount = _a.net_amount, cpu_amount = _a.cpu_amount, xfer = _a.xfer; res = {}; newacc_action = this.sdkGen.newaccount(newUser, payer, newacc_pub_active_key, newacc_pub_owner_key); buyram_action = this.sdkGen.buyrambytes(newUser, payer, ram_amt); delegatebw_action = this.sdkGen.delegateBw(newUser, payer, net_amount, cpu_amount, xfer); if (this.debug) console.log("before create account transaction"); return [4 /*yield*/, this.SubmitTx([newacc_action, buyram_action, delegatebw_action], [ecc.privateToPublic(payer_prv_key)], [payer_prv_key])]; case 1: tres = _b.sent(); res.TxID_createAcc = tres.transaction_id; if (this.debug) console.log("createuser transaction complete"); return [2 /*return*/, res]; } }); }); }; NCO_BlockchainAPI.prototype.buyRam = function (inpt) { return __awaiter(this, void 0, void 0, function () { var buyram_action, tres; return __generator(this, function (_a) { switch (_a.label) { case 0: buyram_action = this.sdkGen.buyrambytes(inpt.user, inpt.payer, inpt.ram_amt); return [4 /*yield*/, this.SubmitTx([buyram_action], [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 1: tres = _a.sent(); return [2 /*return*/, { TxID_createAcc: tres.transaction_id }]; } }); }); }; /** * Create default collection for the account * @param NCCreateCollection * @returns Create Collection and template transactions' ids */ NCO_BlockchainAPI.prototype.createCollection = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res, tres, sbt_sch_name, user_public_active_key, mkt_fee, allow_notify, schema_fields, t1, template, xferable, burnable; return __generator(this, function (_a) { switch (_a.label) { case 0: res = {}; if (inpt.collection_name == undefined) inpt.collection_name = normalizeUsername(inpt.user, "z"); if (inpt.schema_name == undefined) inpt.schema_name = normalizeUsername(inpt.user, "w"); sbt_sch_name = normalizeUsername(inpt.user, "s"); user_public_active_key = ecc.privateToPublic(inpt.user_prv_active_key); mkt_fee = inpt.mkt_fee ? inpt.mkt_fee : 0.05; allow_notify = inpt.allow_notify ? inpt.allow_notify : true; t = this.sdkGen.createCollection(inpt.user, inpt.collection_name, [inpt.user], [inpt.user], mkt_fee, allow_notify); if (this.debug) console.log(t); if (this.debug) console.log("createcol transaction"); return [4 /*yield*/, this.SubmitTx([t], [user_public_active_key], [inpt.user_prv_active_key])]; case 1: tres = (_a.sent()); res.TxID_createCol = tres.transaction_id; if (this.debug) console.log(tres); if (this.debug) console.log("creating default schema "); schema_fields = inpt.schema_fields ? inpt.schema_fields : default_schema; t = this.sdkGen.createSchema(inpt.user, inpt.collection_name, inpt.schema_name, schema_fields); if (this.debug) console.log(t); if (this.debug) console.log("createsch transaction"); return [4 /*yield*/, this.SubmitTx([t], [user_public_active_key], [inpt.user_prv_active_key])]; case 2: tres = (_a.sent()); res.TxID_createSch = tres.transaction_id; if (this.debug) console.log(tres); if (this.debug) console.log("creating SBT schema"); t1 = this.sdkGen.createSchema(inpt.user, inpt.collection_name, sbt_sch_name, SBT_NFT_schema); if (this.debug) console.log(t1); if (this.debug) console.log("createsch SBT transaction"); return [4 /*yield*/, this.SubmitTx([t1], [user_public_active_key], [inpt.user_prv_active_key])]; case 3: tres = (_a.sent()); res.TxID_createSch = tres.transaction_id; if (this.debug) console.log(tres); if (this.debug) console.log("creating template"); template = inpt.template_fields ? inpt.template_fields : []; xferable = inpt.xferable ? inpt.xferable : true; burnable = inpt.burnable ? inpt.burnable : true; t = this.sdkGen.createTemplate(inpt.user, inpt.collection_name, inpt.schema_name, xferable, burnable, template); if (this.debug) console.log(t); if (this.debug) console.log("creating template transaction"); return [4 /*yield*/, this.SubmitTx([t], [user_public_active_key], [inpt.user_prv_active_key])]; case 4: tres = (_a.sent()); res.TxID_createTpl = res.TxID_createTpl; if (this.debug) console.log(tres); return [2 /*return*/, res]; } }); }); }; /** * Create a new permission subordinate to the Active permission. * (future optional: allow under owner, TBD) * @param NCCreatePermission * @returns Create permission transaction id */ NCO_BlockchainAPI.prototype.createPermission = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res, r; return __generator(this, function (_a) { switch (_a.label) { case 0: t = this.sdkGen.createPermission(inpt.author, inpt.perm_name, inpt.perm_pub_key); return [4 /*yield*/, this.SubmitTx([t], [ecc.privateToPublic(inpt.author_prv_active_key)], [inpt.author_prv_active_key])]; case 1: res = _a.sent(); r = {}; r.TxID_createPerm = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * Link a permission to a specific action of a specific contract. * @param NCLinkPerm * author: the permission's owner to be linked * code: the owner of the action to be linked * type: the action to be linked * 'active', 'owner' ... * @returns Link permission transaction id */ NCO_BlockchainAPI.prototype.linkPermission = function (inpt) { return __awaiter(this, void 0, void 0, function () { var linkauth_input, action, res, r; return __generator(this, function (_a) { switch (_a.label) { case 0: linkauth_input = { account: inpt.author, code: inpt.action_owner, type: inpt.action_to_link, requirement: inpt.perm_to_link, // }; action = { account: 'eosio', name: 'linkauth', data: linkauth_input, authorization: [{ actor: inpt.author, permission: 'active' }] }; return [4 /*yield*/, this.SubmitTx([action], [ecc.privateToPublic(inpt.author_prv_active_key)], [inpt.author_prv_active_key])]; case 1: res = _a.sent(); r = {}; r.TxID_linkPerm = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /* ===================================================================================== * Main DAO service: common staking pool transfrming NCO (external convertible currency) * into internal GNCO currency. * * Staked amount is subject to a staking fee, redemption delay and/or * instant unstaking fee. * @param NCStakeMainDao * @returns NCReturnTxs */ NCO_BlockchainAPI.prototype.stakeMainDAO = function (inpt) { return __awaiter(this, void 0, void 0, function () { var r, stakeTx, res; return __generator(this, function (_a) { switch (_a.label) { case 0: r = {}; return [4 /*yield*/, this.mGen.stake([{ actor: inpt.payer, permission: "active" }], inpt.payer, inpt.amt)]; case 1: stakeTx = _a.sent(); if (this.debug) console.log("action: " + JSON.stringify(stakeTx)); return [4 /*yield*/, this.SubmitTx(stakeTx, [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 2: res = _a.sent(); r.TxID_stakeMainDAO = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * Instant UnStake mainDAO with penalty * @param NCStakeMainDao * @returns NCReturnTxs */ NCO_BlockchainAPI.prototype.instUnstakeMainDAO = function (inpt) { return __awaiter(this, void 0, void 0, function () { var r, stakeTx, res; return __generator(this, function (_a) { switch (_a.label) { case 0: r = {}; return [4 /*yield*/, this.mGen.instunstake([{ actor: inpt.payer, permission: "active" }], inpt.payer, inpt.amt)]; case 1: stakeTx = _a.sent(); if (this.debug) console.log("action: " + JSON.stringify(stakeTx)); return [4 /*yield*/, this.SubmitTx(stakeTx, [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 2: res = _a.sent(); r.TxID_unstakeMainDAO = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * Delayed UnStake mainDAO delay without penalty * @param NCStakeMainDao * @returns NCReturnTxs */ NCO_BlockchainAPI.prototype.dldUnstakeMainDAO = function (inpt) { return __awaiter(this, void 0, void 0, function () { var r, stakeTx, res; return __generator(this, function (_a) { switch (_a.label) { case 0: r = {}; return [4 /*yield*/, this.mGen.dldunstake([{ actor: inpt.payer, permission: "active" }], inpt.payer, inpt.amt)]; case 1: stakeTx = _a.sent(); if (this.debug) console.log("action: " + JSON.stringify(stakeTx)); return [4 /*yield*/, this.SubmitTx(stakeTx, [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 2: res = _a.sent(); r.TxID_unstakeMainDAO = res.transaction_id; return [2 /*return*/, r]; } }); }); }; // ======================================================================== /** Staking pools service, issuing social tokens * * Create a staking pool for an account. * Selection of ticker and inflation/deflation optionality * @param NCCreatePool * @returns Create Pool transaction id */ NCO_BlockchainAPI.prototype.createPool = function (inpt) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { var t, res, r; return __generator(this, function (_d) { switch (_d.label) { case 0: inpt.ticker = (inpt.ticker || inpt.owner.substring(0, 5)).toUpperCase(); (_a = inpt.is_inflatable) !== null && _a !== void 0 ? _a : (inpt.is_inflatable = true); (_b = inpt.is_deflatable) !== null && _b !== void 0 ? _b : (inpt.is_deflatable = true); (_c = inpt.is_treasury) !== null && _c !== void 0 ? _c : (inpt.is_treasury = false); if (this.debug) console.log("Creating pool: " + JSON.stringify(inpt)); t = this.sdkGen.createPool(inpt.owner, inpt.ticker, inpt.is_inflatable, inpt.is_deflatable, inpt.is_treasury, "test pool for " + inpt.owner); return [4 /*yield*/, this.SubmitTx([t], [ecc.privateToPublic(inpt.owner_prv_active_key)], [inpt.owner_prv_active_key])]; case 1: res = _d.sent(); r = {}; r.TxID_createPool = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * Stake to creator pool * @param * @returns Create Pool transaction id */ NCO_BlockchainAPI.prototype.stakePool = function (inpt) { return __awaiter(this, void 0, void 0, function () { var p, r, q, t, pool_id, pool_code, stakeTx, res; return __generator(this, function (_a) { switch (_a.label) { case 0: p = { owner: inpt.owner }; r = {}; if (this.debug) console.log("Get poolbyowner: ", JSON.stringify(p)); return [4 /*yield*/, this.poolsRpcApi.getPoolByOwner(p)]; case 1: q = _a.sent(); return [4 /*yield*/, q.json()]; case 2: t = _a.sent(); pool_id = t.rows[0].id; pool_code = t.rows[0].code; if (this.debug) console.log("pool:" + JSON.stringify(t)); return [4 /*yield*/, this.pGen.stakeToPool([{ actor: inpt.payer, permission: "active" }], inpt.payer, inpt.amt, pool_id)]; case 3: stakeTx = _a.sent(); if (this.debug) console.log("action: " + JSON.stringify(stakeTx)); return [4 /*yield*/, this.SubmitTx(stakeTx, [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 4: res = _a.sent(); r.TxID_stakePool = res.transaction_id; r.pool_id = pool_id; r.pool_code = pool_code; return [2 /*return*/, r]; } }); }); }; // DAO functionality /** * Unstake creator pool * @param NCUnstakePool * @returns Create Pool transaction id */ NCO_BlockchainAPI.prototype.unstakePool = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res, r; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.pGen.withdrawFromPool([{ actor: inpt.payer, permission: "active" }], //{ actor: "io", permission: "active"} inpt.payer, inpt.amt)]; case 1: t = _a.sent(); if (this.debug) console.log("action: " + JSON.stringify(t)); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 2: res = _a.sent(); r = {}; r.TxID_unstakePool = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * DAO creation. One per account. * @param inpt : NCCreateDao * @returns NCReturnTxs.TxID_createDao, NCReturnTxs.dao_id */ NCO_BlockchainAPI.prototype.createDao = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res, p, q, w, r; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.aGen.createDao([{ actor: inpt.author, permission: "active" }], inpt.author, inpt.descr)]; case 1: t = _a.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.author_prv_key)], [inpt.author_prv_key])]; case 2: res = _a.sent(); p = { owner: inpt.author }; if (this.debug) console.log("Get dao by owner: ", JSON.stringify(p)); return [4 /*yield*/, this.cApi.getDAOByOwner(p)]; case 3: q = _a.sent(); return [4 /*yield*/, q.json()]; case 4: w = _a.sent(); if (this.debug) console.log("received from getDaoByOwner" + JSON.stringify(w)); r = {}; r.TxID_createDao = res.transaction_id; r.dao_id = w.rows[0].id.toString(); // r.dao_id = r.dao_id.toString() ; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCCreateDaoProposal * @returns NCReturnTxs.TxID_createDaoProposal, NCReturnTxs.proposal_id */ NCO_BlockchainAPI.prototype.createDaoProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r, ps; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; return [4 /*yield*/, this.aGen.createProposal([{ actor: inpt.proposer, permission: "active" }], inpt.proposer, Number(dao_id), inpt.title, inpt.summary, inpt.url, inpt.vote_start, inpt.vote_end)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.proposer_prv_key)], [inpt.proposer_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_createDaoProposal = res.transaction_id; r.dao_id = dao_id; return [4 /*yield*/, this.getDaoProposals(__assign(__assign({}, inpt), { dao_id: dao_id }))]; case 5: ps = _b.sent(); r.proposal_id = ps.rows[ps.rows.length - 1].id; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCCreateDaoUserWhitelistProposal * @returns NCReturnTxs.TxID_createDaoProposal, NCReturnTxs.proposal_id */ NCO_BlockchainAPI.prototype.createDaoUserWhitelistProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r, ps; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; return [4 /*yield*/, this.aGen.createWhiteListProposal([{ actor: inpt.proposer, permission: "active" }], inpt.proposer, Number(dao_id), inpt.user, inpt.vote_start, inpt.vote_end)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.proposer_prv_key)], [inpt.proposer_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_createDaoProposal = res.transaction_id; r.dao_id = dao_id; return [4 /*yield*/, this.getDaoWhitelistProposals({ dao_id: dao_id, proposal_author: inpt.proposer })]; case 5: ps = _b.sent(); r.proposal_id = ps.rows[ps.rows.length - 1].id; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCCreateDaoUserWhitelistProposal * @returns NCReturnTxs.TxID_createDaoProposal, NCReturnTxs.proposal_id */ NCO_BlockchainAPI.prototype.createDaoStakeProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r, ps; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; return [4 /*yield*/, this.aGen.createStakeProposal([{ actor: inpt.proposer, permission: "active" }], inpt.proposer, Number(dao_id), inpt.to, inpt.quantity, inpt.vote_start, inpt.vote_end)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.proposer_prv_key)], [inpt.proposer_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_createDaoProposal = res.transaction_id; r.dao_id = dao_id; return [4 /*yield*/, this.getDaoStakeProposals({ dao_id: dao_id, proposal_author: inpt.proposer })]; case 5: ps = _b.sent(); r.proposal_id = ps.rows[ps.rows.length - 1].id; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCApproveDaoProposal * @returns NCReturnTxs.TxID_approveDaoProposal */ NCO_BlockchainAPI.prototype.approveDaoProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; if (inpt.proposal_id == undefined) throw ("Proposal undefined ID"); return [4 /*yield*/, this.aGen.approveProposal([{ actor: inpt.approver, permission: "active" }], Number(dao_id), inpt.proposal_id)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.approver_prv_key)], [inpt.approver_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_approveDaoProposal = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCApproveDaoProposal * @returns NCReturnTxs.TxID_approveDaoProposal */ NCO_BlockchainAPI.prototype.approveDaoWhitelistProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; if (inpt.proposal_id == undefined) throw ("Proposal undefined ID"); return [4 /*yield*/, this.aGen.approveWhiteListProposal([{ actor: inpt.approver, permission: "active" }], Number(dao_id), inpt.proposal_id)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.approver_prv_key)], [inpt.approver_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_approveDaoProposal = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * * @param inpt : NCExecuteDaoProposal * @returns NCReturnTxs.TxID_executeDaoProposal */ NCO_BlockchainAPI.prototype.executeDaoProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; if (inpt.proposal_id == undefined) throw ("Proposal ID undefined"); return [4 /*yield*/, this.aGen.executeProposal([{ actor: inpt.exec, permission: "active" }], Number(dao_id), inpt.proposal_id)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.exec_prv_key)], [inpt.exec_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_executeDaoProposal = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * @param inpt : NCExecuteDaoProposal * @returns NCReturnTxs.TxID_executeDaoProposal */ NCO_BlockchainAPI.prototype.executeDaoWhitelistProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res, r; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; if (inpt.proposal_id == undefined) throw ("Proposal ID undefined"); return [4 /*yield*/, this.aGen.executeWhiteListProposal([{ actor: inpt.exec, permission: "active" }], Number(dao_id), inpt.proposal_id)]; case 3: t = _b.sent(); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.exec_prv_key)], [inpt.exec_prv_key])]; case 4: res = _b.sent(); r = {}; r.TxID_executeDaoProposal = res.transaction_id; return [2 /*return*/, r]; } }); }); }; /** * @param inpt : NCCreateDao * @returns NCReturnTxs.TxID_createDao, NCReturnTxs.dao_id */ NCO_BlockchainAPI.prototype.voteOnProposal = function (inpt) { return __awaiter(this, void 0, void 0, function () { var dao_id, _a, t, res; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = inpt.dao_id; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dao_id = _a; return [4 /*yield*/, this.aGen.vote([{ actor: inpt.voter, permission: "active" }], inpt.voter, inpt.quantity, inpt.proposal_type || "standart", dao_id, inpt.proposal_id, inpt.option)]; case 3: t = _b.sent(); if (this.debug) console.log("Vote for DAO proposal: ", JSON.stringify(t)); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.voter_prv_key)], [inpt.voter_prv_key])]; case 4: res = _b.sent(); //if(this.debug) console.log("received from VoteForDaoProposal" + JSON.stringify(res)); return [2 /*return*/, { TxID_voteDaoProposal: res.transaction_id }]; } }); }); }; NCO_BlockchainAPI.prototype.withdrawVoteDeposit = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res; return __generator(this, function (_a) { switch (_a.label) { case 0: //const dao_id = inpt.dao_id || (await this.getDaoIdByOwner(inpt.dao_owner)); if (this.debug) console.log("withdraw vote deposit make action", JSON.stringify(inpt)); return [4 /*yield*/, this.aGen.withdraw([{ actor: inpt.voter, permission: "active" }], inpt.voter, +inpt.vote_id)]; case 1: t = _a.sent(); if (this.debug) console.log("Withdraw vote deposit send action: ", JSON.stringify(t)); return [4 /*yield*/, this.SubmitTx(t, [ecc.privateToPublic(inpt.voter_prv_key)], [inpt.voter_prv_key])]; case 2: res = _a.sent(); //if(this.debug) console.log("received from withdraw: " + JSON.stringify(res)); return [2 /*return*/, { TxID_WithdrawVoteDeposit: res.transaction_id }]; } }); }); }; /** * Mint an asset * @param inpt: NCMintAsset * @returns Create Pool transaction id */ NCO_BlockchainAPI.prototype.mintAsset = function (inpt) { return __awaiter(this, void 0, void 0, function () { var t, res, r; return __generator(this, function (_a) { switch (_a.label) { case 0: if (inpt.col_name == undefined) inpt.col_name = normalizeUsername(inpt.creator, "z"); if (inpt.sch_name == undefined) inpt.sch_name = normalizeUsername(inpt.creator, "w"); if (inpt.tmpl_id == undefined) inpt.tmpl_id = -1; if (inpt.immutable_data == undefined) inpt.immutable_data = [ { key: 'name', value: ['string', inpt.creator + '_' + (new Date()).getTime()] } ]; if (inpt.mutable_data == undefined) inpt.mutable_data = []; t = this.sdkGen.mintAsset(inpt.creator, inpt.col_name, inpt.sch_name, inpt.tmpl_id, inpt.immutable_data, inpt.mutable_data); return [4 /*yield*/, this.SubmitTx([t], [ecc.privateToPublic(inpt.payer_prv_key)], [inpt.payer_prv_key])]; case 1: res = _a.sent(); r = {}; r.TxID_mintAsset = res.transaction_id; return [2 /*return*/, r]; } }); }); }; // Getters /** * @param inpt : NCCreateDao * @returns NCReturnTxs.TxID_createDao, NCReturnTxs.dao_id */ NCO_BlockchainAPI.prototype.getDaoIdByOwner = function (owner, noFail) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var p, q, w, r; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!owner) throw new Error("DAO undefined"); p = { owner: owner }; if (this.debug) console.log("Get dao by owner: ", JSON.stringify(p)); return [4 /*yield*/, this.cApi.getDAOByOwner(p)]; case 1: q = _c.sent(); return [4 /*yield*/, q.json()]; case 2: w = _c.sent(); if (this.debug) console.log("received from getDaoByOwner" + JSON.stringify(w)); if (!w.rows.length && !noFail) throw new Error('User has no dao'); r = (_b = (_a = w.rows[0]) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.toString(); if (!r && !noFail) throw new Error("DAO undefined"); return [2 /*return*/, r]; } }); }); }; NCO_BlockchainAPI.prototype.getDaoProposals = function (inpt) { var _a; return __awaiter(this, void 0, void 0, function () { var dao_id, _b, w, opt; return __generator(this, function (_c) { switch (_c.label) { case 0: if (this.debug) console.log("Get proposal list: ", JSON.stringify(inpt)); _b = inpt.dao_id; if (_b) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner, true)]; case 1: _b = (_c.sent()); _c.label = 2; case 2: dao_id = _b; if (!dao_id) return [2 /*return*/, { dao_id: null }]; if (!(inpt.proposal_author || inpt.proposal_id)) return [3 /*break*/, 5]; return [4 /*yield*/, this.cApi.getProposalByProposer(__assign(__assign({}, inpt), { daoID: dao_id }))]; case 3: return [4 /*yield*/, (_c.sent()).json()]; case 4: w = _c.sent(); return [3 /*break*/, 8]; case 5: opt = { json: true, code: "daos.nco", scope: dao_id, table: "proposals", lower_bound: inpt.lower_bound, upper_bound: inpt.upper_bound, limit: ~~((_a = inpt.limit) !== null && _a !== void 0 ? _a : "10"), reverse: inpt.reverse, index_position: "1", }; return [4 /*yield*/, this.cApi.getTableRows(opt)]; case 6: return [4 /*yield*/, (_c.sent()).json()]; case 7: w = _c.sent(); _c.label = 8; case 8: if (this.debug) console.log("received proposal list" + JSON.stringify(w)); return [2 /*return*/, __assign(__assign({}, w), { dao_id: dao_id })]; } }); }); }; NCO_BlockchainAPI.prototype.getDaoWhitelistProposals = function (inpt) { var _a; return __awaiter(this, void 0, void 0, function () { var dao_id, _b, w, opt; return __generator(this, function (_c) { switch (_c.label) { case 0: if (this.debug) console.log("Get proposal list: ", JSON.stringify(inpt)); _b = inpt.dao_id; if (_b) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDaoIdByOwner(inpt.dao_owner, true)]; case 1: _b = (_c.sent()); _c.label = 2; case 2: dao_id = _b; if (!dao_id) return [2 /*return*/, { dao_id: null }]; if (!(inpt.proposal_author || inpt.proposal_id)) return [3 /*break*/, 5]; return [4 /*yield*/, this.cApi.getWhiteListProposalByProposer(__assign(__assign({}, inpt), { daoID: dao_id }))]; case 3: return [4 /*yield*/, (_c.sent()).json()]; case 4: w = _c.sent(); return [3 /*break*/, 8]; case 5: opt = { json: true, code: "daos.nco", scope: dao_id, table: "whlistprpls", lower_bound: inpt.lower_bound, upper_bound: