@firmachain/firma-js
Version:
The Official FirmaChain Javascript SDK written in Typescript
477 lines (476 loc) • 24.7 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.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = __importDefault(require("fs"));
var chai_1 = require("chai");
var FirmaSDK_1 = require("../sdk/FirmaSDK");
var FirmaUtil_1 = require("../sdk/FirmaUtil");
var FirmaCosmWasmService_1 = require("../sdk/FirmaCosmWasmService");
var config_test_1 = require("./config_test");
describe('[34. Bridge tx Test]', function () {
var firma;
var aliceWallet;
var bobWallet;
var aliceAddress;
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*/, firma.Wallet.fromMnemonic(config_test_1.bobMnemonic)];
case 2:
bobWallet = _a.sent();
return [4 /*yield*/, aliceWallet.getAddress()];
case 3:
aliceAddress = _a.sent();
return [4 /*yield*/, bobWallet.getAddress()];
case 4:
bobAddress = _a.sent();
return [2 /*return*/];
}
});
});
});
var codeId = "";
var cw721ContractAddress = "";
var bridgeContractAddress = "";
it('Cosmwasm Cw721 setup', function () { return __awaiter(void 0, void 0, void 0, function () {
var wasmFile, array, storeGas, storeFee, everyBodyAccessConfig, storeCodeResult, admin, label, instantiateGas, instantiateFee, noFunds, testData, instantiateResult;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
wasmFile = fs_1.default.readFileSync("./test/sample/cw721_base.wasm");
array = new Uint8Array(wasmFile.buffer);
storeGas = 3000000;
storeFee = FirmaUtil_1.FirmaUtil.getUFCTFromFCT(0.3);
everyBodyAccessConfig = {
permission: FirmaCosmWasmService_1.AccessType.ACCESS_TYPE_EVERYBODY,
address: "",
addresses: []
};
return [4 /*yield*/, firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeGas, fee: storeFee })];
case 1:
storeCodeResult = _a.sent();
codeId = extractValue(storeCodeResult.events, "store_code", "code_id");
(0, chai_1.expect)(storeCodeResult.code).to.be.equal(0);
return [4 /*yield*/, aliceWallet.getAddress()];
case 2:
admin = _a.sent();
label = "test1";
instantiateGas = 3000000;
instantiateFee = FirmaUtil_1.FirmaUtil.getUFCTFromFCT(0.3);
noFunds = [];
testData = JSON.stringify({
minter: aliceAddress,
name: "My Awesome NFT Collection",
symbol: "MAWESOME"
});
return [4 /*yield*/, firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: instantiateGas, fee: instantiateFee })];
case 3:
instantiateResult = _a.sent();
cw721ContractAddress = extractValue(instantiateResult.events, "instantiate", "_contract_address");
(0, chai_1.expect)(instantiateResult.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('CosmWasm Cw bridge StoreCode', function () { return __awaiter(void 0, void 0, void 0, function () {
var wasmFile, array, gas, fee, everyBodyAccessConfig, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
wasmFile = fs_1.default.readFileSync("./test/sample/bridge_contract.wasm");
array = new Uint8Array(wasmFile.buffer);
gas = 3000000;
fee = FirmaUtil_1.FirmaUtil.getUFCTFromFCT(0.3);
everyBodyAccessConfig = {
permission: FirmaCosmWasmService_1.AccessType.ACCESS_TYPE_EVERYBODY,
address: "",
addresses: []
};
return [4 /*yield*/, firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: gas, fee: fee })];
case 1:
result = _a.sent();
codeId = extractValue(result.events, "store_code", "code_id");
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('CosmWasm Cw bridge InstantiateContract', function () { return __awaiter(void 0, void 0, void 0, function () {
var admin, label, gas, fee, noFunds, testData, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
admin = aliceAddress;
label = "CwBridge";
gas = 3000000;
fee = FirmaUtil_1.FirmaUtil.getUFCTFromFCT(0.3);
noFunds = [];
testData = JSON.stringify({
owner: admin,
cw721_address: cw721ContractAddress
});
return [4 /*yield*/, firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee })];
case 1:
result = _a.sent();
bridgeContractAddress = extractValue(result.events, "instantiate", "_contract_address");
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge mint temp', function () { return __awaiter(void 0, void 0, void 0, function () {
var owner, tokenId1, tokenUri1, gas1, fee1, result1, tokenId2, tokenUri2, gas2, fee2, result2, tokenId3, tokenUri3, gas3, fee3, result3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
owner = aliceAddress;
tokenId1 = "1";
tokenUri1 = "https://meta.nft.io/uri/" + tokenId1;
return [4 /*yield*/, firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, tokenId1, tokenUri1)];
case 1:
gas1 = _a.sent();
fee1 = Math.ceil(gas1 * 0.1);
return [4 /*yield*/, firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, tokenId1, tokenUri1, { gas: gas1, fee: fee1 })];
case 2:
result1 = _a.sent();
(0, chai_1.expect)(result1.code).to.be.equal(0);
tokenId2 = "2";
tokenUri2 = "https://meta.nft.io/uri/" + tokenId2;
return [4 /*yield*/, firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, tokenId2, tokenUri2)];
case 3:
gas2 = _a.sent();
fee2 = Math.ceil(gas2 * 0.1);
return [4 /*yield*/, firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, tokenId2, tokenUri2, { gas: gas2, fee: fee2 })];
case 4:
result2 = _a.sent();
(0, chai_1.expect)(result2.code).to.be.equal(0);
tokenId3 = "3";
tokenUri3 = "https://meta.nft.io/uri/" + tokenId3;
return [4 /*yield*/, firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, tokenId3, tokenUri3)];
case 5:
gas3 = _a.sent();
fee3 = Math.ceil(gas3 * 0.1);
return [4 /*yield*/, firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, tokenId3, tokenUri3, { gas: gas3, fee: fee3 })];
case 6:
result3 = _a.sent();
(0, chai_1.expect)(result3.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge add_authorized_user', function () { return __awaiter(void 0, void 0, void 0, function () {
var user, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
user = aliceAddress;
return [4 /*yield*/, firma.CwBridge.getGasEstimationAddAuthorizedUser(aliceWallet, bridgeContractAddress, user)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.addAuthorizedUser(aliceWallet, bridgeContractAddress, user, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge lock', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id = "1";
return [4 /*yield*/, firma.CwBridge.getGasEstimationLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.lock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('cw bridge lock bulk', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id1, token_id2, tx1, tx2, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id1 = "2";
token_id2 = "3";
return [4 /*yield*/, firma.CwBridge.getUnsignedTxLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id1)];
case 1:
tx1 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getUnsignedTxLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id2)];
case 2:
tx2 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, [tx1, tx2])];
case 3:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.signAndBroadcast(aliceWallet, [tx1, tx2], { gas: gas, fee: fee })];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge unlock', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id = "1";
return [4 /*yield*/, firma.CwBridge.getGasEstimationUnlock(aliceWallet, bridgeContractAddress, token_id)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.unlock(aliceWallet, bridgeContractAddress, token_id, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge unlock bulk', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id1, token_id2, tx1, tx2, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id1 = "2";
token_id2 = "3";
return [4 /*yield*/, firma.CwBridge.getUnsignedTxUnlock(aliceWallet, bridgeContractAddress, token_id1)];
case 1:
tx1 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getUnsignedTxUnlock(aliceWallet, bridgeContractAddress, token_id2)];
case 2:
tx2 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, [tx1, tx2])];
case 3:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.signAndBroadcast(aliceWallet, [tx1, tx2], { gas: gas, fee: fee })];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge deposit', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id = "1";
return [4 /*yield*/, firma.CwBridge.getGasEstimationDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, bobAddress)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.deposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, bobAddress, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge deposit bulk', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id1, token_id2, tx1, tx2, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id1 = "2";
token_id2 = "3";
return [4 /*yield*/, firma.CwBridge.getUnsignedTxDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id1, bobAddress)];
case 1:
tx1 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getUnsignedTxDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id2, bobAddress)];
case 2:
tx2 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, [tx1, tx2])];
case 3:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.signAndBroadcast(aliceWallet, [tx1, tx2], { gas: gas, fee: fee })];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge withdraw', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id = "1";
return [4 /*yield*/, firma.CwBridge.getGasEstimationWithdraw(bobWallet, bridgeContractAddress, token_id)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.withdraw(bobWallet, bridgeContractAddress, token_id, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge withdraw bulk', function () { return __awaiter(void 0, void 0, void 0, function () {
var token_id1, token_id2, tx1, tx2, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_id1 = "2";
token_id2 = "3";
return [4 /*yield*/, firma.CwBridge.getUnsignedTxWithdraw(bobWallet, bridgeContractAddress, token_id1)];
case 1:
tx1 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getUnsignedTxWithdraw(bobWallet, bridgeContractAddress, token_id2)];
case 2:
tx2 = _a.sent();
return [4 /*yield*/, firma.CwBridge.getGasEstimationSignAndBroadcast(bobWallet, [tx1, tx2])];
case 3:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.signAndBroadcast(bobWallet, [tx1, tx2], { gas: gas, fee: fee })];
case 4:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge remove_authorized_user', function () { return __awaiter(void 0, void 0, void 0, function () {
var user, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
user = aliceAddress;
return [4 /*yield*/, firma.CwBridge.getGasEstimationRemoveAuthorizedUser(aliceWallet, bridgeContractAddress, user)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.removeAuthorizedUser(aliceWallet, bridgeContractAddress, user, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
it('Cw bridge chage_owner', function () { return __awaiter(void 0, void 0, void 0, function () {
var new_owner, gas, fee, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
new_owner = bobAddress;
return [4 /*yield*/, firma.CwBridge.getGasEstimationChangeOwner(aliceWallet, bridgeContractAddress, new_owner)];
case 1:
gas = _a.sent();
fee = Math.ceil(gas * 0.1);
return [4 /*yield*/, firma.CwBridge.changeOwner(aliceWallet, bridgeContractAddress, new_owner, { gas: gas, fee: fee })];
case 2:
result = _a.sent();
(0, chai_1.expect)(result.code).to.be.equal(0);
return [2 /*return*/];
}
});
}); });
});