UNPKG

@glitterprotocol/glitter-sdk

Version:

The JavaScript SDK for Glitter

131 lines 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getContractQueryResult = exports.getContractEvents = exports.getContractAddress = exports.getCodeId = void 0; var TxAPI_1 = require("../client/lcd/api/TxAPI"); var buffer_1 = require("buffer"); function getCodeId(txResult, msgIndex) { if (msgIndex === void 0) { msgIndex = 0; } if ((0, TxAPI_1.isTxError)(txResult) || txResult.logs === undefined || txResult.logs.length === 0) { throw new Error('could not parse code id -- tx logs are empty.'); } var codeId = txResult.logs[msgIndex].eventsByType['store_code']['code_id'][0]; return codeId; } exports.getCodeId = getCodeId; function getContractAddress(txResult, msgIndex, isClassic) { if (msgIndex === void 0) { msgIndex = 0; } if (isClassic === void 0) { isClassic = false; } if ((0, TxAPI_1.isTxError)(txResult) || txResult.logs === undefined || txResult.logs.length === 0) { throw new Error('could not parse contract address -- tx logs are empty.'); } var eventName; var attributeKey; if (isClassic) { eventName = 'instantiate_contract'; attributeKey = 'contract_address'; } else { eventName = 'wasm'; attributeKey = '_contract_address'; } var contractAddress = txResult.logs[msgIndex].eventsByType[eventName][attributeKey][0]; return contractAddress; } exports.getContractAddress = getContractAddress; function getContractEvents(txResult, msgIndex, isClassic) { if (msgIndex === void 0) { msgIndex = 0; } if (isClassic === void 0) { isClassic = false; } if ((0, TxAPI_1.isTxError)(txResult) || txResult.logs === undefined || txResult.logs.length === 0) { throw new Error('could not parse contract events -- tx logs are empty.'); } var eventName; var attributeKey; if (isClassic) { eventName = 'from_contract'; attributeKey = 'contract_address'; } else { eventName = 'instantiate'; attributeKey = '_contract_address'; } var contractEvents = []; for (var _i = 0, _a = txResult.logs[msgIndex].events; _i < _a.length; _i++) { var event_1 = _a[_i]; if (event_1.type === eventName) { var eventData = { contract_address: '' }; // will be overwritten var currentContractAddress = event_1.attributes[0].value; for (var _b = 0, _c = event_1.attributes; _b < _c.length; _b++) { var att = _c[_b]; if (att.key == attributeKey && currentContractAddress !== att.value) { contractEvents.push(eventData); eventData = { contract_address: '' }; currentContractAddress = att.value; } eventData[att.key] = att.value; } contractEvents.push(eventData); return contractEvents; } } throw new Error("could not find event type ".concat(eventName, " in logs")); } exports.getContractEvents = getContractEvents; function getContractQueryResult(result) { try { var rowSet = []; for (var _i = 0, result_1 = result; _i < result_1.length; _i++) { var item = result_1[_i]; var row = item.row; for (var field in row) { if (Object.prototype.hasOwnProperty.call(row, field)) { var colVal = row[field]; var value = colVal.value; var valueType = colVal.column_value_type; if (valueType === 'IntColumn' || valueType === 'UintColumn') { row[field] = { value: parseInt(value), column_value_type: valueType, }; } else if (valueType === 'FloatColumn') { row[field] = { value: parseFloat(value), column_value_type: valueType, }; } else if (valueType === 'BoolColumn') { row[field] = { value: value === 'true', column_value_type: valueType, }; } else if (valueType === 'StringColumn') { row[field] = { value: value, column_value_type: valueType }; } else if (valueType === 'BytesColumn') { row[field] = { value: buffer_1.Buffer.from(value, 'base64').toString('utf8'), column_value_type: valueType, }; } else if (valueType === 'InvalidColumn') { row[field] = { value: value, column_value_type: valueType }; } } } rowSet.push({ row: row }); } return rowSet; } catch (error) { return result; } } exports.getContractQueryResult = getContractQueryResult; //# sourceMappingURL=contract.js.map