Skip to content
Snippets Groups Projects
AeternityService.js 3.59 KiB
Newer Older
  • Learn to ignore specific revisions
  • Igor Markin's avatar
    Igor Markin committed
    "use strict";
    
    Igor Markin's avatar
    Igor Markin committed
    var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
        function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    
    Igor Markin's avatar
    Igor Markin committed
        return new (P || (P = Promise))(function (resolve, reject) {
    
    Igor Markin's avatar
    Igor Markin committed
            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());
    
    Igor Markin's avatar
    Igor Markin committed
        });
    
    Igor Markin's avatar
    Igor Markin committed
    };
    
    Igor Markin's avatar
    Igor Markin committed
    Object.defineProperty(exports, "__esModule", { value: true });
    const axios_1 = require("axios");
    class AeternityService {
    
    Igor Markin's avatar
    Igor Markin committed
        constructor(nodeUrl, compilerUrl, contractBytecode) {
            this._nodeUrl = "";
            this._compilerUrl = "";
            this._contractBytecode = "";
            this._nodeUrl = nodeUrl;
            this._compilerUrl = compilerUrl;
            this._contractBytecode = contractBytecode;
            if (!nodeUrl) {
                throw new Error("Aeternity node URL not defined");
            }
            if (!compilerUrl) {
                throw new Error("Aeternity compiler URL not defined");
            }
            if (!contractBytecode) {
                throw new Error("Aeternity contract bytecode not defined");
            }
    
    Igor Markin's avatar
    Igor Markin committed
        }
    
    Igor Markin's avatar
    Igor Markin committed
        getTxDataByHash(hash) {
            return __awaiter(this, void 0, void 0, function* () {
                const { data } = yield axios_1.default(`${this._nodeUrl}/v2/transactions/${hash}`);
                return {
                    hash: data.hash,
                    blockHeight: data.block_height,
                    tx: {
                        callData: data.tx.call_data,
                    },
                };
            });
    
    Igor Markin's avatar
    Igor Markin committed
        }
    
    Igor Markin's avatar
    Igor Markin committed
        decodeContractCallData(callData) {
            var _a, _b, _c, _d, _e, _f;
            return __awaiter(this, void 0, void 0, function* () {
                const { data } = yield axios_1.default({
                    url: `${this._compilerUrl}/decode-calldata/bytecode/`,
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        calldata: callData,
                        bytecode: this._contractBytecode,
                    },
                });
                return {
                    merkleeTreeFileName: (_c = (_b = (_a = data.arguments[0]) === null || _a === void 0 ? void 0 : _a.value[0]) === null || _b === void 0 ? void 0 : _b.key) === null || _c === void 0 ? void 0 : _c.value,
                    rootNodeHash: (_f = (_e = (_d = data.arguments[0]) === null || _d === void 0 ? void 0 : _d.value[0]) === null || _e === void 0 ? void 0 : _e.val) === null || _f === void 0 ? void 0 : _f.value,
                };
            });
        }
        getBlockData(height) {
            return __awaiter(this, void 0, void 0, function* () {
                const [blockInfo, blockchainHeight] = yield Promise.all([
                    axios_1.default(`${this._nodeUrl}/v2/key-blocks/height/${height}`).then(({ data }) => data),
                    this.getBlockhainHeight(),
                ]);
                return {
                    blockHeight: blockInfo.height,
                    timestamp: blockInfo.time,
                    confirmationsAmount: blockchainHeight - blockInfo.height,
                };
            });
        }
        getBlockhainHeight() {
            return __awaiter(this, void 0, void 0, function* () {
                const { data } = yield axios_1.default(`${this._nodeUrl}/v2/key-blocks/current/height`);
                return data.height;
            });
    
    Igor Markin's avatar
    Igor Markin committed
        }
    }
    exports.default = AeternityService;