UNPKG

@firmachain/firma-js

Version:

The Official FirmaChain Javascript SDK written in Typescript

458 lines (457 loc) 24.2 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 __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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FirmaCosmWasmCwMarketplaceService = exports.CwMarketplaceMsgData = void 0; var FirmaUtil_1 = require("./FirmaUtil"); var CosmWasmTxClient_1 = require("./firmachain/cosmwasm/CosmWasmTxClient"); // staic util var noFunds = []; var CwMarketplaceMsgData = /** @class */ (function () { function CwMarketplaceMsgData() { } CwMarketplaceMsgData.getMsgDataChangeOwner = function (new_owner) { return JSON.stringify({ "change_owner": { new_owner: new_owner, } }); }; CwMarketplaceMsgData.getMsgDataRegisterSaleFCT = function (amount) { return { action: "registerSaleFct", cw20_address: "", amount: amount }; }; CwMarketplaceMsgData.getMsgDataRegisterSaleCw20 = function (cw20_address, amount) { return { action: "registerSaleCw20", cw20_address: cw20_address, amount: amount }; }; CwMarketplaceMsgData.getMsgDataPurchaseSaleFCT = function (token_id) { return JSON.stringify({ "purchase_sale_fct": { token_id: token_id, } }); }; CwMarketplaceMsgData.getMsgDataPurchaseSaleCw20 = function (token_id) { return { action: "purchaseSaleCW20", token_id: token_id, }; }; CwMarketplaceMsgData.getMsgDataCancelSale = function (token_id) { return JSON.stringify({ "cancel_sale": { token_id: token_id, } }); }; return CwMarketplaceMsgData; }()); exports.CwMarketplaceMsgData = CwMarketplaceMsgData; var FirmaCosmWasmCwMarketplaceService = /** @class */ (function () { function FirmaCosmWasmCwMarketplaceService(config, cosmwasmService, cw721Service, cw20Service) { this.config = config; this.cosmwasmService = cosmwasmService; this.cw721Service = cw721Service; this.cw20Service = cw20Service; } FirmaCosmWasmCwMarketplaceService.prototype.getCwMarketplaceMsgData = function () { return CwMarketplaceMsgData; }; FirmaCosmWasmCwMarketplaceService.prototype.changeOwner = function (wallet, contractAddress, new_owner, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = CwMarketplaceMsgData.getMsgDataChangeOwner(new_owner); return [4 /*yield*/, this.cosmwasmService.executeContract(wallet, contractAddress, msgData, noFunds, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getUnsignedTxChangeOwner = function (wallet, contractAddress, new_owner) { return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = JSON.stringify({ "change_owner": { new_owner: new_owner, } }); return [4 /*yield*/, this.cosmwasmService.getUnsignedTxExecuteContract(wallet, contractAddress, msgData, noFunds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.registerSaleFCT = function (wallet, contractAddress, cw721ContractAddress, tokenId, fctAmount, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataRegisterSaleFCT(amount); return [4 /*yield*/, this.cw721Service.sendNft(wallet, cw721ContractAddress, contractAddress, tokenId, msgData, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getUnsignedTxRegisterSaleFCT = function (wallet, contractAddress, cw721ContractAddress, tokenId, fctAmount) { return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataRegisterSaleFCT(amount); return [4 /*yield*/, this.cw721Service.getUnsignedTxSendNft(wallet, cw721ContractAddress, contractAddress, tokenId, msgData)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.registerSaleCw20 = function (wallet, contractAddress, cw721ContractAddress, tokenId, cw20Address, cw20Amount, cw20Decimal, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(cw20Amount, cw20Decimal); msgData = CwMarketplaceMsgData.getMsgDataRegisterSaleCw20(cw20Address, amount); return [4 /*yield*/, this.cw721Service.sendNft(wallet, cw721ContractAddress, contractAddress, tokenId, msgData, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.purchaseSaleFCT = function (wallet, contractAddress, tokenId, fctAmount, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var amount, msgData, funds; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleFCT(tokenId); funds = [{ denom: this.config.denom, amount: amount }]; return [4 /*yield*/, this.cosmwasmService.executeContract(wallet, contractAddress, msgData, funds, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getUnsignedTxPurchaseSaleFCT = function (wallet, contractAddress, cw721ContractAddress, tokenId, fctAmount) { return __awaiter(this, void 0, void 0, function () { var amount, msgData, funds; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCTStr(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleFCT(amount); funds = [{ denom: this.config.denom, amount: amount }]; return [4 /*yield*/, this.cosmwasmService.getUnsignedTxExecuteContract(wallet, contractAddress, msgData, funds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.purchaseSaleCw20 = function (wallet, contractAddress, tokenId, cw20Address, cw20Amount, cw20Decimal, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(cw20Amount, cw20Decimal); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleCw20(tokenId); return [4 /*yield*/, this.cw20Service.send(wallet, cw20Address, contractAddress, amount, msgData, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getUnsignedTxPurchaseSaleCw20 = function (wallet, contractAddress, cw721ContractAddress, tokenId, cw20Address, cw20Amount, cw20Decimal) { return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(cw20Amount, cw20Decimal); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleCw20(amount); return [4 /*yield*/, this.cw20Service.getUnsignedTxSend(wallet, cw20Address, contractAddress, amount, msgData)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.cancelSale = function (wallet, contractAddress, tokenId, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = CwMarketplaceMsgData.getMsgDataCancelSale(tokenId); return [4 /*yield*/, this.cosmwasmService.executeContract(wallet, contractAddress, msgData, noFunds, txMisc)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getUnsignedTxCancelSale = function (wallet, contractAddress, tokenId) { return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = CwMarketplaceMsgData.getMsgDataCancelSale(tokenId); return [4 /*yield*/, this.cosmwasmService.getUnsignedTxExecuteContract(wallet, contractAddress, msgData, noFunds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.signAndBroadcast = function (wallet, msgList, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var txClient, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); txClient = new CosmWasmTxClient_1.CosmWasmTxClient(wallet, this.config.rpcAddress); return [4 /*yield*/, txClient.signAndBroadcast(msgList, (0, FirmaUtil_1.getSignAndBroadcastOption)(this.config.denom, txMisc))]; case 1: return [2 /*return*/, _a.sent()]; case 2: error_1 = _a.sent(); FirmaUtil_1.FirmaUtil.printLog(error_1); throw error_1; case 3: return [2 /*return*/]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationSignAndBroadcast = function (wallet, msgList, txMisc) { if (txMisc === void 0) { txMisc = FirmaUtil_1.DefaultTxMisc; } return __awaiter(this, void 0, void 0, function () { var txClient, txRaw, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); txClient = new CosmWasmTxClient_1.CosmWasmTxClient(wallet, this.config.rpcAddress); return [4 /*yield*/, txClient.sign(msgList, (0, FirmaUtil_1.getSignAndBroadcastOption)(this.config.denom, txMisc))]; case 1: txRaw = _a.sent(); return [4 /*yield*/, FirmaUtil_1.FirmaUtil.estimateGas(txRaw)]; case 2: return [2 /*return*/, _a.sent()]; case 3: error_2 = _a.sent(); FirmaUtil_1.FirmaUtil.printLog(error_2); throw error_2; case 4: return [2 /*return*/]; } }); }); }; // gas FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationChangeOwner = function (wallet, contractAddress, new_owner) { return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = CwMarketplaceMsgData.getMsgDataChangeOwner(new_owner); return [4 /*yield*/, this.cosmwasmService.getGasEstimationExecuteContract(wallet, contractAddress, msgData, noFunds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationRegisterSaleFCT = function (wallet, contractAddress, cw721ContractAddress, tokenId, fctAmount) { return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataRegisterSaleFCT(amount); return [4 /*yield*/, this.cw721Service.getGasEstimationSendNft(wallet, cw721ContractAddress, contractAddress, tokenId, msgData)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationRegisterSaleCw20 = function (wallet, contractAddress, cw721ContractAddress, tokenId, cw20Address, cw20Amount, cw20Decimal) { return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(cw20Amount, cw20Decimal); msgData = CwMarketplaceMsgData.getMsgDataRegisterSaleCw20(cw20Address, amount); return [4 /*yield*/, this.cw721Service.getGasEstimationSendNft(wallet, cw721ContractAddress, contractAddress, tokenId, msgData)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationPurchaseSaleFCT = function (wallet, contractAddress, tokenId, fctAmount) { return __awaiter(this, void 0, void 0, function () { var amount, msgData, funds; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(fctAmount); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleFCT(tokenId); funds = [{ denom: this.config.denom, amount: amount }]; return [4 /*yield*/, this.cosmwasmService.getGasEstimationExecuteContract(wallet, contractAddress, msgData, funds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationPurchaseSaleCw20 = function (wallet, contractAddress, tokenId, cw20Address, cw20Amount, cw20Decimal) { return __awaiter(this, void 0, void 0, function () { var amount, msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: amount = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(cw20Amount, cw20Decimal); msgData = CwMarketplaceMsgData.getMsgDataPurchaseSaleCw20(tokenId); return [4 /*yield*/, this.cw20Service.getGasEstimationSend(wallet, cw20Address, contractAddress, amount, msgData)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getGasEstimationCancelSale = function (wallet, contractAddress, tokenId) { return __awaiter(this, void 0, void 0, function () { var msgData; return __generator(this, function (_a) { switch (_a.label) { case 0: msgData = CwMarketplaceMsgData.getMsgDataCancelSale(tokenId); return [4 /*yield*/, this.cosmwasmService.getGasEstimationExecuteContract(wallet, contractAddress, msgData, noFunds)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; // query FirmaCosmWasmCwMarketplaceService.prototype.getConfig = function (contractAddress) { return __awaiter(this, void 0, void 0, function () { var query, result; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "{\"get_config\": {}}"; return [4 /*yield*/, this.cosmwasmService.getContractSmartQueryData(contractAddress, query)]; case 1: result = _a.sent(); return [2 /*return*/, JSON.parse(result)]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getOwner = function (contractAddress) { return __awaiter(this, void 0, void 0, function () { var query, result; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "{\"get_owner\": {}}"; return [4 /*yield*/, this.cosmwasmService.getContractSmartQueryData(contractAddress, query)]; case 1: result = _a.sent(); return [2 /*return*/, JSON.parse(result)]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getRegisterList = function (contractAddress, limit, start_after) { if (limit === void 0) { limit = 10; } if (start_after === void 0) { start_after = null; } return __awaiter(this, void 0, void 0, function () { var query, result; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "{\"get_register_list\": { \"limit\": ".concat(limit, ", \"start_after\": ").concat(start_after !== null ? "\"".concat(start_after, "\"") : null, "}}"); return [4 /*yield*/, this.cosmwasmService.getContractSmartQueryData(contractAddress, query)]; case 1: result = _a.sent(); return [2 /*return*/, JSON.parse(result)]; } }); }); }; FirmaCosmWasmCwMarketplaceService.prototype.getRegisterListByAddress = function (contractAddress, address, limit, start_after) { if (limit === void 0) { limit = 10; } if (start_after === void 0) { start_after = null; } return __awaiter(this, void 0, void 0, function () { var query, result; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "{\"get_register_list_by_address\": { \"address\": \"".concat(address, "\", \"limit\": ").concat(limit, ",\"start_after\": ").concat(start_after !== null ? "\"".concat(start_after, "\"") : null, "}}"); return [4 /*yield*/, this.cosmwasmService.getContractSmartQueryData(contractAddress, query)]; case 1: result = _a.sent(); return [2 /*return*/, JSON.parse(result)]; } }); }); }; return FirmaCosmWasmCwMarketplaceService; }()); exports.FirmaCosmWasmCwMarketplaceService = FirmaCosmWasmCwMarketplaceService;