UNPKG

@simbachain/simbats

Version:
1,233 lines (1,232 loc) 70.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Simba = void 0; const axios_1 = __importDefault(require("axios")); const config_1 = require("./config"); const request_handler_1 = require("./request_handler"); const simba_contract_1 = require("./simba_contract"); const utf8_1 = __importDefault(require("utf8")); const utils_1 = require("./utils"); const filehandler_1 = require("./filehandler"); /** * main class for interacting with SIMBA platform */ class Simba { constructor(baseApiUrl = config_1.SimbaConfig.baseURL, requestHandler = new request_handler_1.RequestHandler()) { this.baseApiUrl = baseApiUrl; this.requestHandler = requestHandler; } /** * generate SimbaContract class instance * @param appName * @param contractName * @returns {SimbaContract} */ getSimbaContract(appName, contractName) { const params = { appName, contractName, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const simbaContract = new simba_contract_1.SimbaContract(this.baseApiUrl, appName, contractName); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return simbaContract; } /** * return user information * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async whoAmI(parseDataFromResponse = true) { const params = { parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, "/user/whoami/"); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : ${res.data}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * fund wallet * @param blockchain * @param address * @param amount * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async fund(blockchain, address, amount, parseDataFromResponse = true) { const params = { blockchain, address, amount, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/user/account/${blockchain}/fund/`); const data = { address, amount, }; const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get balance from wallet * @param blockchain * @param address * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async balance(blockchain, address, parseDataFromResponse = true) { const params = { blockchain, address, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/user/account/${blockchain}/balance/${address}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * set wallet - admin perms required * @param userID * @param blockchain * @param pub * @param priv * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async adminSetWallet(userID, blockchain, pub, priv, parseDataFromResponse = true) { const params = { userID, blockchain, pub, priv, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/admin/users/${userID}/wallet/set/`); const data = { blockchain, identities: [{ pub, priv, }] }; const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * set wallet - no admin perms required * @param blockchain * @param pub * @param priv * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async setWallet(blockchain, pub, priv, parseDataFromResponse = true) { const params = { blockchain, pub, priv, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/user/wallet/set/`); const data = { blockchain, identities: [{ pub, priv, }] }; const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get wallet * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getWallet(parseDataFromResponse = true) { const params = { parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/user/wallet/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * create organisation * @param name * @param display * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async createOrg(name, display, parseDataFromResponse = true) { const params = { name, display, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/`); const data = { name, display_name: display, }; const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * * @param orgName * @param appName * @param display * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any> | void>} */ async createApp(orgName, appName, display, parseDataFromResponse = true) { const params = { orgName, appName, display, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const getURL = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/${orgName}/applications/${appName}/`); const postURL = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/${orgName}/applications/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(getURL, options, parseDataFromResponse); } catch (error) { if (axios_1.default.isAxiosError(error) && error.response && error.response.status === 404) { const data = { name: appName, display_name: display, }; try { const res = await this.requestHandler.doPostRequest(postURL, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } const message = `app ${appName} for org ${orgName} already exists`; config_1.SimbaConfig.log.error(`${message}`); throw new Error(message); } /** * get application * @param orgName * @param appName * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getApplication(orgName, appName, parseDataFromResponse = true) { const params = { orgName, appName, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/${orgName}/applications/${appName}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get transactions for application * @param appName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getApplicationTransactions(appName, queryParams, parseDataFromResponse = true) { const params = { appName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/transactions/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get contract from application * @param appName * @param contractName * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getApplicationContract(appName, contractName, parseDataFromResponse = true) { const params = { appName, contractName, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get transactions for contract * @param appName * @param contractName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getContractTransactions(appName, contractName, queryParams, parseDataFromResponse = true) { const params = { appName, contractName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/transactions/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get contracts for application * @param appName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getContracts(appName, queryParams, parseDataFromResponse = true) { const params = { appName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contracts/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * validate bundleHash * @param appName * @param contractName * @param bundleHash * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async validateBundleHash(appName, contractName, bundleHash, parseDataFromResponse = true) { const params = { appName, contractName, bundleHash, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/validate/${contractName}/${bundleHash}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get bundle from bundleHash and contractName and appName * @param appName * @param contractName * @param bundleHash * @param downloadLocation * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any> | unknown>} */ async getBundle(appName, contractName, bundleHash, downloadLocation, parseDataFromResponse = true) { const params = { appName, contractName, bundleHash, downloadLocation, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/bundle/${bundleHash}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const responseType = "stream"; const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse, responseType); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); await filehandler_1.FileHandler.download(res, downloadLocation); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { if (axios_1.default.isAxiosError(error)) { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } else { config_1.SimbaConfig.log.error(`${error}`); } } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get file from fileName, bundleHash, contractName, and appName * @param appName * @param contractName * @param bundleHash * @param fileName * @param downloadLocation * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any> | unknown>} */ async getBundleFile(appName, contractName, bundleHash, fileName, downloadLocation, parseDataFromResponse = true) { const params = { appName, contractName, bundleHash, fileName, downloadLocation, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/bundle/${bundleHash}/filename/${fileName}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const responseType = "stream"; const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse, responseType); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); await filehandler_1.FileHandler.download(res, downloadLocation); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get manifest for bundle from bundleHash * @param appName * @param contractName * @param bundleHash * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getManifestForBundleFromBundleHash(appName, contractName, bundleHash, parseDataFromResponse = true) { const params = { appName, contractName, bundleHash, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/bundle/${bundleHash}/manifest/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get contract info from contractName and appName * @param appName * @param contractName * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getContractInfo(appName, contractName, parseDataFromResponse = true) { const params = { appName, contractName, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/info/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get events from appName, contractName, and eventName * @param appName * @param contractName * @param eventName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getEvents(appName, contractName, eventName, queryParams, parseDataFromResponse = true) { const params = { appName, contractName, eventName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/events/${eventName}/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get events - admin perms required * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async adminGetEvents(queryParams, parseDataFromResponse = true) { const params = { queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/admin/events/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get receipt from appName, contractName, and receiptHash * @param appName * @param contractName * @param receiptHash * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getReceipt(appName, contractName, receiptHash, parseDataFromResponse = true) { const params = { appName, contractName, receiptHash, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/receipt/${receiptHash}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get transaction from appName, contractName, and transactionHash * @param appName * @param contractName * @param transactionHash * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getTransaction(appName, contractName, transactionHash, parseDataFromResponse = true) { const params = { appName, contractName, transactionHash, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/transaction/${transactionHash}/`); const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get transactions by method * @param appName * @param contractName * @param methodName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getTransactionsByMethod(appName, contractName, methodName, queryParams, parseDataFromResponse = true) { const params = { appName, contractName, methodName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/${methodName}/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * get transactions by contract * @param appName * @param contractName * @param queryParams * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async getTransactionsByContract(appName, contractName, queryParams, parseDataFromResponse = true) { const params = { appName, contractName, queryParams, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/transactions/`); const options = await this.requestHandler.getAuthAndOptions(undefined, queryParams); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * submit contract method * @param appName * @param contractName * @param methodName * @param inputs * @param filePaths * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async submitContractMethod(appName, contractName, methodName, inputs, filePaths, parseDataFromResponse = true) { const params = { appName, contractName, methodName, inputs, filePaths, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/${methodName}/`); // addContentType needs to be false below, or it botches formData boundaries const options = await this.requestHandler.getAuthAndOptions(undefined, undefined, true); inputs = inputs || {}; let data = inputs; if (!filePaths) { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: EXIT : res: ${JSON.stringify(res)}`); return res; } const formData = this.requestHandler.formDataFromFilePathsAndInputs(inputs, filePaths); const headers = this.requestHandler.formDataHeaders(options, formData); try { const res = await this.requestHandler.doPostRequestWithFormData(url, formData, headers); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * submit contract method synchronously * @param appName * @param contractName * @param methodName * @param inputs * @param filePaths * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async submitContractMethodSync(appName, contractName, methodName, inputs, filePaths, parseDataFromResponse = true) { const params = { appName, contractName, methodName, inputs, filePaths, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/sync/contract/${contractName}/${methodName}/`); // addContentType needs to be false below, or it botches formData boundaries const options = await this.requestHandler.getAuthAndOptions(undefined, undefined, false); inputs = inputs || {}; let data = inputs; if (!filePaths) { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: EXIT : res.data: ${JSON.stringify(res.data)}`); return res; } const formData = this.requestHandler.formDataFromFilePathsAndInputs(inputs, filePaths); const headers = this.requestHandler.formDataHeaders(options, formData); try { const res = await this.requestHandler.doPostRequestWithFormData(url, formData, headers, parseDataFromResponse); config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${error}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * returns previous invocations of contract method * @param appName * @param contractName * @param methodName * @param args * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async callContractMethod(appName, contractName, methodName, args, parseDataFromResponse = true) { const params = { appName, contractName, methodName, args, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/contract/${contractName}/${methodName}/`); const options = await this.requestHandler.getAuthAndOptions(undefined, args); try { const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * submit signed transaction for contract method * @param appName * @param txnId * @param txn * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async submitSignedTransaction(appName, txnId, txn, parseDataFromResponse = true) { const params = { appName, txnId, txn, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/apps/${appName}/transactions/${txnId}/`); const data = { transaction: txn, }; const options = await this.requestHandler.getAuthAndOptions(); try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * save contract design to SIMBA platform * @param orgName * @param name * @param code * @param designID * @param targetContract * @param libraries * @param encode * @param model * @param binaryTargets * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async saveDesign(orgName, name, code, designID, targetContract, libraries, encode = true, model, binaryTargets, parseDataFromResponse = true) { const params = { orgName, name, designID, code, targetContract, libraries, encode, model, binaryTargets, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const putURL = `/v2/organisations/${orgName}/contract_designs/${designID}/`; const postURL = `/v2/organisations/${orgName}/contract_designs/`; const options = await this.requestHandler.getAuthAndOptions(); if (encode) { const utf8Code = utf8_1.default.encode(code); code = Buffer.from(utf8Code).toString('base64'); } const data = { name, code, language: "solidity", }; if (targetContract) { data.target_contract = targetContract; } if (libraries) { data.libraries = libraries; } if (model) { data.model = model; } if (binaryTargets) { data.binary_targets = binaryTargets; } let res; try { if (designID) { const url = this.requestHandler.buildURL(this.baseApiUrl, putURL); res = this.requestHandler.doPutRequest(url, options, data, parseDataFromResponse); } else { const url = this.requestHandler.buildURL(this.baseApiUrl, postURL); res = this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * wait for deployment to be COMPLETED state * @param orgName * @param uid * @param totalTime * @param maxTime * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async waitForDeployment(orgName, uid, totalTime = 0, maxTime = 480, parseDataFromResponse = true) { const params = { orgName, uid, totalTime, maxTime, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/${orgName}/deployments/${uid}/`); const options = await this.requestHandler.getAuthAndOptions(); try { // parseDataFromResponse NEEDS to be true here, for res.state to exist const res = await this.requestHandler.doGetRequest(url, options, parseDataFromResponse); const state = res.state; if (state === "FAILED") { const message = `:: SIMBA : EXIT : transaction failed: ${JSON.stringify(res)}`; config_1.SimbaConfig.log.error(`:: SIMBA : EXIT : ${message}`); throw (message); } if (state === "COMPLETED") { config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT : res : ${res}`); return res; } else { if (totalTime > maxTime) { const message = `waited too long`; config_1.SimbaConfig.log.error(`:: SIMBA : EXIT : ${message}`); throw (message); } await new Promise(resolve => setTimeout(resolve, 2000)); totalTime += 2; return await this.waitForDeployment(orgName, uid, totalTime, maxTime, parseDataFromResponse); } } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else { config_1.SimbaConfig.log.error(`${JSON.stringify(error)}`); } config_1.SimbaConfig.log.debug(`:: SIMBA : EXIT :`); throw (error); } } /** * deploy contract design * @param orgName * @param appName * @param apiName * @param designID * @param blockchain * @param storage * @param displayName * @param args * @param parseDataFromResponse * @returns {Promise<AxiosResponse<any> | Record<any, any>>} */ async deployDesign(orgName, appName, apiName, designID, blockchain, storage = "no_storage", displayName, args, parseDataFromResponse = true) { const params = { orgName, appName, apiName, designID, blockchain, storage, displayName, args, parseDataFromResponse, }; config_1.SimbaConfig.log.debug(`:: SIMBA : ENTER : params : ${JSON.stringify(params)}`); const url = this.requestHandler.buildURL(this.baseApiUrl, `/v2/organisations/${orgName}/contract_designs/${designID}/deploy/`); const options = await this.requestHandler.getAuthAndOptions(); const data = { blockchain, storage, api_name: apiName, app_name: appName, singleton: true, }; if (displayName) { data.display_name = displayName; } if (args) { data.args = args; } try { const res = await this.requestHandler.doPostRequest(url, options, data, parseDataFromResponse); return res; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { config_1.SimbaConfig.log.error(`${JSON.stringify(error.response.data)}`); } else {