@kubiklabs/wasmkit
Version:
Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.
67 lines (66 loc) • 3.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBalanceChanges = exports.supportChangeTokenBalances = void 0;
const chalk_1 = __importDefault(require("chalk"));
const context_1 = require("../../../internal/context");
const errors_1 = require("../../../internal/core/errors");
const errors_list_1 = require("../../../internal/core/errors-list");
const client_1 = require("../../client");
function supportChangeTokenBalances(Assertion) {
Assertion.addMethod('changeTokenBalances', function (// eslint-disable-line @typescript-eslint/no-explicit-any
accounts, token, balanceChanges, logResponse) {
const subject = this._obj;
if (accounts[0].account !== undefined) {
accounts = accounts.map((account) => account.account);
}
const accountAddresses = accounts.map((account) => account.address !== undefined
? account.address : account);
const derivedPromise = Promise.all([
getBalanceChanges(subject, accountAddresses, token, logResponse)
]).then(([actualChanges]) => {
this.assert(actualChanges.every((change, ind) => change === balanceChanges[ind]), `Expected ${accountAddresses.toString()} to change balance by ${balanceChanges.toString()} ${token}, ` +
`but it has changed by ${actualChanges.toString()} ${token}`, `Expected ${accountAddresses.toString()} to not change balance by ${balanceChanges.toString()} ${token},`, balanceChanges.map((balanceChange) => balanceChange.toString()), actualChanges.map((actualChange) => actualChange.toString()));
});
this.then = derivedPromise.then.bind(derivedPromise);
this.catch = derivedPromise.catch.bind(derivedPromise);
this.promise = derivedPromise;
return this;
});
}
exports.supportChangeTokenBalances = supportChangeTokenBalances;
function extractTokenBalance(balances, denom) {
for (const coin of balances) {
if (coin.denom === denom) {
return Number(coin.amount);
}
}
return 0;
}
async function getBalances(accountAddresses, token) {
const client = await (0, client_1.getClient)(context_1.WasmkitContext.getWasmkitContext().getRuntimeEnv().network);
if (client === undefined) {
throw new errors_1.WasmkitError(errors_list_1.ERRORS.GENERAL.CLIENT_NOT_LOADED);
}
return await Promise.all(accountAddresses.map(async (accountAddr) => {
return extractTokenBalance(await (0, client_1.getBalance)(client, accountAddr, context_1.WasmkitContext.getWasmkitContext().getRuntimeEnv().network), token);
}));
}
async function getBalanceChanges(transaction, // eslint-disable-line @typescript-eslint/no-explicit-any
accountAddresses, token, logResponse) {
if (typeof transaction !== 'function') {
throw new errors_1.WasmkitError(errors_list_1.ERRORS.GENERAL.NOT_A_FUNCTION, {
param: transaction
});
}
const balancesBefore = await getBalances(accountAddresses, token);
const txResponse = await transaction();
if (logResponse === true) {
console.log(`${chalk_1.default.green("Transaction response:")} ${txResponse}`);
}
const balancesAfter = await getBalances(accountAddresses, token);
return balancesAfter.map((balance, ind) => balance - balancesBefore[ind]);
}
exports.getBalanceChanges = getBalanceChanges;