@firmachain/firma-js
Version:
The Official FirmaChain Javascript SDK written in Typescript
397 lines (396 loc) • 20.2 kB
JavaScript
"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 __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 (g && (g = 0, op[0] && (_ = 0)), _) 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 };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var common_1 = require("../sdk/firmachain/common");
var FirmaSDK_1 = require("../sdk/FirmaSDK");
var FirmaUtil_1 = require("../sdk/FirmaUtil");
var config_test_1 = require("./config_test");
// If test it, the properties of the chain change, so skip it.
describe('[16. Gov Tx Test]', function () {
var firma;
var aliceWallet;
var aliceAddress;
var bobWallet;
var bobAddress;
var extractValue = function (events, eventType, attrKey) {
var e_1, _a, e_2, _b;
try {
for (var events_1 = __values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
var event_1 = events_1_1.value;
if (event_1.type === eventType) {
try {
for (var _c = (e_2 = void 0, __values(event_1.attributes)), _d = _c.next(); !_d.done; _d = _c.next()) {
var attr = _d.value;
if (attr.key === attrKey) {
return attr.value;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
}
finally { if (e_2) throw e_2.error; }
}
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
}
finally { if (e_1) throw e_1.error; }
}
return "";
};
beforeEach(function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
firma = new FirmaSDK_1.FirmaSDK(config_test_1.TestChainConfig);
return [4 /*yield*/, firma.Wallet.fromMnemonic(config_test_1.aliceMnemonic)];
case 1:
aliceWallet = _a.sent();
return [4 /*yield*/, aliceWallet.getAddress()];
case 2:
aliceAddress = _a.sent();
return [4 /*yield*/, firma.Wallet.fromMnemonic(config_test_1.bobMnemonic)];
case 3:
bobWallet = _a.sent();
return [4 /*yield*/, bobWallet.getAddress()];
case 4:
bobAddress = _a.sent();
return [2 /*return*/];
}
});
});
});
// Test order
// 1. submitProposal
// 2. deposit(for pass minium deposit value)
// 3. vote
it('SubmitTextProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var initialDepositFCT, title, description, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialDepositFCT = 2500;
title = "test submit proposal";
description = "test description";
return [4 /*yield*/, firma.Gov.submitTextProposal(aliceWallet, title, description, initialDepositFCT)];
case 1:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitCommunityPoolSpendProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var initialDepositFCT, title, summary, amountFCT, recipient, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialDepositFCT = 2500;
title = "Community spend proposal1";
summary = "This is a community spend proposal";
amountFCT = 1000;
recipient = aliceAddress;
return [4 /*yield*/, firma.Gov.submitCommunityPoolSpendProposal(aliceWallet, title, summary, initialDepositFCT, amountFCT, recipient)];
case 1:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitStakingParamsUpdateProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var title, summary, initialDepositFCT, params, metadata, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
title = "Staking Parameter Change proposal";
summary = "This is a Staking Parameter change proposal";
initialDepositFCT = 2500;
return [4 /*yield*/, firma.Staking.getParamsAsStakingParams()];
case 1:
params = _a.sent();
params.maxValidators = 100;
params.historicalEntries = 10000;
params.minCommissionRate = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(params.minCommissionRate);
metadata = "";
return [4 /*yield*/, firma.Gov.submitStakingParamsUpdateProposal(aliceWallet, title, summary, initialDepositFCT, params, metadata)];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitGovParamsUpdateProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var title, summary, initialDepositFCT, params, metadata, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
title = "Gov Parameter Change proposal";
summary = "This is a Gov Parameter change proposal";
initialDepositFCT = 2500;
return [4 /*yield*/, firma.Gov.getParamAsGovParams()];
case 1:
params = _a.sent();
params.burnProposalDepositPrevote = true;
metadata = "";
return [4 /*yield*/, firma.Gov.submitGovParamsUpdateProposal(aliceWallet, title, summary, initialDepositFCT, params, metadata)];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitSoftwareUpgradeProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var initialDeposit, title, summary, plan, metadata, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialDeposit = 5000;
title = "Software upgrade proposal";
summary = "This is a Text & CancelProposal";
plan = {
name: 'v0.5.1',
time: {
seconds: BigInt(0),
nanos: 0
},
height: BigInt(1050000),
info: ''
};
metadata = "";
return [4 /*yield*/, firma.Gov.getGasEstimationSubmitSoftwareUpgradeProposal(aliceWallet, title, summary, initialDeposit, plan, metadata)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.Gov.submitSoftwareUpgradeProposal(aliceWallet, title, summary, initialDeposit, plan, metadata, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitTextProposal & CancelProposal Test', function () { return __awaiter(void 0, void 0, void 0, function () {
var initialDeposit, title, description, gas, fee, result, proposal_id;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialDeposit = 5000;
title = "CancelProposal test proposal";
description = "This is a Text & CancelProposal";
return [4 /*yield*/, firma.Gov.getGasEstimationSubmitTextProposal(aliceWallet, title, description, initialDeposit)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.Gov.submitTextProposal(aliceWallet, title, description, initialDeposit, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
proposal_id = extractValue(result.events, "submit_proposal", "proposal_id");
(0, chai_1.expect)(result.code).to.be.equal(0);
return [4 /*yield*/, firma.Gov.getGasEstimationCancelProposal(aliceWallet, proposal_id)];
case 3:
// CancelProposal
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.Gov.cancelProposal(aliceWallet, proposal_id, { gas: gas, fee: fee })];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
// TODO: get recent gov proposal list and then set proposalId for below case
var tempProposalId = 420;
// more deposit after initial deposit case
it.skip('Deposit OK', function () { return __awaiter(void 0, void 0, void 0, function () {
var proposalId, amount, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
proposalId = tempProposalId;
amount = 2500;
return [4 /*yield*/, firma.Gov.deposit(aliceWallet, proposalId, amount)];
case 1:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('Vote - alice YES', function () { return __awaiter(void 0, void 0, void 0, function () {
var proposalId, validatorWallet3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
proposalId = tempProposalId;
return [4 /*yield*/, firma.Wallet.fromMnemonic("main shallow liberty desk super palm remind throw track legal warrior client garbage type insect first token keen subway pony curtain pitch yellow arrive")];
case 1:
validatorWallet3 = _a.sent();
return [4 /*yield*/, firma.Gov.vote(validatorWallet3, proposalId, common_1.VotingOption.VOTE_OPTION_NO_WITH_VETO)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
it.skip('Vote - bob NO', function () { return __awaiter(void 0, void 0, void 0, function () {
var proposalId, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
proposalId = tempProposalId;
return [4 /*yield*/, firma.Gov.vote(bobWallet, proposalId, common_1.VotingOption.VOTE_OPTION_NO)];
case 1:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitCommunityPoolSpendProposal & Deposit & Vote scenario test', function () { return __awaiter(void 0, void 0, void 0, function () {
var initialDepositFCT, title, summary, amountFCT, recipient, result, proposalId, addDepositAmount;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialDepositFCT = 2500;
title = "Community spend proposal1";
summary = "This is a community spend proposal";
amountFCT = 1000;
recipient = aliceAddress;
return [4 /*yield*/, firma.Gov.submitCommunityPoolSpendProposal(aliceWallet, title, summary, initialDepositFCT, amountFCT, recipient)];
case 1:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
proposalId = extractValue(result.events, "submit_proposal", "proposal_id");
addDepositAmount = 2500;
return [4 /*yield*/, firma.Gov.deposit(aliceWallet, proposalId, addDepositAmount)];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [4 /*yield*/, firma.Gov.vote(aliceWallet, proposalId, common_1.VotingOption.VOTE_OPTION_YES)];
case 3:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [4 /*yield*/, firma.Gov.vote(aliceWallet, proposalId, common_1.VotingOption.VOTE_OPTION_NO)];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.equal(0);
return [2 /*return*/];
}
});
}); });
it('SubmitGovParamsUpdateProposal Test - failure case (missing parameter)', function () { return __awaiter(void 0, void 0, void 0, function () {
var title, summary, initialDeposit, proposalParams, metadata, errorMsg, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
title = "Gov Parameter Change fail proposal";
summary = "This is a Gov Parameter change proposal";
initialDeposit = 5000;
proposalParams = {
burnVoteQuorum: false
};
metadata = "";
errorMsg = "All governance parameters must be provided. Use getParamAsGovParams() to get current values and override only the parameters you want to change.";
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, firma.Gov.submitGovParamsUpdateProposal(aliceWallet, title, summary, initialDeposit, proposalParams, metadata)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
(0, chai_1.expect)(error_1.message).to.equal(errorMsg);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
it('SubmitStakingParamsUpdateProposal Test - failure case (missing parameter)', function () { return __awaiter(void 0, void 0, void 0, function () {
var title, summary, initialDeposit, proposalParams, metadata, errorMsg, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
title = "Staking Parameter Change fail proposal";
summary = "This is a Staking Parameter change proposal";
initialDeposit = 5000;
proposalParams = {
maxValidators: 100
};
metadata = "";
errorMsg = "All staking parameters must be provided. Use Staking.getParamsAsStakingParams() to get current values and override only the parameters you want to change.";
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, firma.Gov.submitStakingParamsUpdateProposal(aliceWallet, title, summary, initialDeposit, proposalParams, metadata)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
(0, chai_1.expect)(error_2.message).to.equal(errorMsg);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
});