UNPKG

@ledgerhq/coin-icon

Version:
103 lines 3.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDelegation = exports.getStepPrice = exports.getFees = exports.submit = exports.broadcastTransaction = exports.getDebugRpcUrl = exports.getRpcUrl = void 0; const bignumber_js_1 = require("bignumber.js"); const icon_sdk_js_1 = __importDefault(require("icon-sdk-js")); const logic_1 = require("../logic"); const constants_1 = require("../constants"); const config_1 = require("../config"); const { HttpProvider } = icon_sdk_js_1.default; const { IconBuilder } = icon_sdk_js_1.default; /** * Returns Testnet RPC URL if the current currency is testnet * @param {currency} currency */ function getRpcUrl(currency) { const currencyConfig = (0, config_1.getCoinConfig)(); let rpcUrl = currencyConfig.infra.node_endpoint; if ((0, logic_1.isTestnet)(currency)) { rpcUrl = currencyConfig.infra.node_testnet_endpoint; } return rpcUrl; } exports.getRpcUrl = getRpcUrl; function getDebugRpcUrl(currency) { const currencyConfig = (0, config_1.getCoinConfig)(); let rpcUrl = currencyConfig.infra.debug_endpoint; if ((0, logic_1.isTestnet)(currency)) { rpcUrl = currencyConfig.infra.debug_testnet_endpoint; } return rpcUrl; } exports.getDebugRpcUrl = getDebugRpcUrl; /** * Broadcast blob to blockchain */ const broadcastTransaction = async (signedOperation, currency) => { const { hash } = await (0, exports.submit)(signedOperation, currency); return { hash }; }; exports.broadcastTransaction = broadcastTransaction; const submit = async (signedOperation, currency) => { const rpcURL = getRpcUrl(currency); const httpProvider = new HttpProvider(rpcURL); const iconService = new icon_sdk_js_1.default(httpProvider); const signedTransaction = { getProperties: () => signedOperation.rawData, getSignature: () => signedOperation.signature, }; const response = await iconService .sendTransaction(signedTransaction) .execute(); return { hash: response, }; }; exports.submit = submit; /** * Obtain fees from blockchain */ const getFees = async (unsigned, account) => { const debugRpcUrl = getDebugRpcUrl(account.currency); const httpProvider = new HttpProvider(debugRpcUrl); const iconService = new icon_sdk_js_1.default(httpProvider); const res = await iconService.estimateStep(unsigned).execute(); return new bignumber_js_1.BigNumber(res); }; exports.getFees = getFees; /** * Get step price from governance contract */ const getStepPrice = async (account) => { const rpcURL = getRpcUrl(account.currency); const httpProvider = new HttpProvider(rpcURL); const iconService = new icon_sdk_js_1.default(httpProvider); const txBuilder = new IconBuilder.CallBuilder(); const stepPriceTx = txBuilder.to(constants_1.GOVERNANCE_SCORE_ADDRESS).method("getStepPrice").build(); const res = await iconService.call(stepPriceTx).execute(); return new bignumber_js_1.BigNumber(res); }; exports.getStepPrice = getStepPrice; const getDelegation = async (address, currency) => { const rpcURL = getRpcUrl(currency); const httpProvider = new HttpProvider(rpcURL); const iconService = new icon_sdk_js_1.default(httpProvider); const delegationTx = new IconBuilder.CallBuilder() .to(constants_1.IISS_SCORE_ADDRESS) .method("getDelegation") .params({ address, }) .build(); const res = await iconService.call(delegationTx).execute(); return { delegations: res.delegations, totalDelegated: new bignumber_js_1.BigNumber(res.totalDelegated), votingPower: new bignumber_js_1.BigNumber(res.votingPower), }; }; exports.getDelegation = getDelegation; //# sourceMappingURL=node.js.map