UNPKG

ptokens-pltc

Version:

repo holding the code for interacting with PLTC

296 lines (290 loc) 13.7 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator')); var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck')); var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass')); var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits')); var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn')); var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf')); var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator')); var Web3 = _interopDefault(require('web3')); var Web3PromiEvent = _interopDefault(require('web3-core-promievent')); var ptokensNodeSelector = require('ptokens-node-selector'); var ptokensUtils = require('ptokens-utils'); var ptokensDepositAddress = require('ptokens-deposit-address'); var Web3Utils = _interopDefault(require('web3-utils')); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var MINIMUM_LTC_REDEEMABLE = 0.00005; var pLTC = function (_NodeSelector) { _inherits(pLTC, _NodeSelector); var _super = _createSuper(pLTC); function pLTC(_configs) { var _this; _classCallCheck(this, pLTC); var _helpers$parseParams = ptokensUtils.helpers.parseParams(_configs, ptokensUtils.constants.blockchains.Litecoin), hostBlockchain = _helpers$parseParams.hostBlockchain, hostNetwork = _helpers$parseParams.hostNetwork, nativeBlockchain = _helpers$parseParams.nativeBlockchain, nativeNetwork = _helpers$parseParams.nativeNetwork; _this = _super.call(this, { pToken: ptokensUtils.constants.pTokens.pLTC, hostBlockchain: hostBlockchain, hostNetwork: hostNetwork, nativeBlockchain: nativeBlockchain, nativeNetwork: nativeNetwork, defaultNode: _configs.defaultNode }); var ethPrivateKey = _configs.ethPrivateKey, ethProvider = _configs.ethProvider, eosPrivateKey = _configs.eosPrivateKey, eosRpc = _configs.eosRpc, eosSignatureProvider = _configs.eosSignatureProvider; if (ethProvider) _this.hostApi = new Web3(ethProvider); if (ethPrivateKey) { var account = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(ethPrivateKey)); _this.hostApi.eth.defaultAccount = account.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(ethPrivateKey); } else { _this.hostPrivateKey = null; } if (eosSignatureProvider) { _this.hostApi = ptokensUtils.eos.getApi(null, eosRpc, eosSignatureProvider); } else if (eosPrivateKey && eosRpc) { _this.hostApi = ptokensUtils.eos.getApi(eosPrivateKey, eosRpc, null); _this.hostPrivateKey = eosPrivateKey; } else if (!eosSignatureProvider && !eosPrivateKey && eosRpc) { _this.hostApi = ptokensUtils.eos.getApi(null, eosRpc, null); } _this.contractAddress = null; _this.decimals = null; return _this; } _createClass(pLTC, [{ key: "getDepositAddress", value: function () { var _getDepositAddress = _asyncToGenerator( _regeneratorRuntime.mark(function _callee(_hostAddress) { var selectedNode, depositAddress; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(this.hostBlockchain === ptokensUtils.constants.blockchains.Ethereum && !Web3Utils.isAddress(_hostAddress))) { _context.next = 2; break; } throw new Error('Invalid Ethereum Address'); case 2: if (!this.selectedNode) { _context.next = 6; break; } _context.t0 = this.selectedNode; _context.next = 9; break; case 6: _context.next = 8; return this.select(); case 8: _context.t0 = _context.sent; case 9: selectedNode = _context.t0; if (selectedNode) { _context.next = 12; break; } throw new Error('No node selected. Impossible to generate a LTC deposit Address.'); case 12: depositAddress = new ptokensDepositAddress.DepositAddress({ node: selectedNode, nativeBlockchain: this.nativeBlockchain, nativeNetwork: this.nativeNetwork, hostBlockchain: this.hostBlockchain, hostNetwork: this.hostNetwork, hostApi: this.hostApi }); _context.next = 15; return depositAddress.generate(_hostAddress); case 15: if (depositAddress.verify()) { _context.next = 17; break; } throw new Error('Node deposit address does not match expected address'); case 17: return _context.abrupt("return", depositAddress); case 18: case "end": return _context.stop(); } } }, _callee, this); })); function getDepositAddress(_x) { return _getDepositAddress.apply(this, arguments); } return getDepositAddress; }() }, { key: "redeem", value: function redeem(_amount, _ltcAddress) { var _this2 = this; var _options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var promiEvent = Web3PromiEvent(); var start = function () { var _ref = _asyncToGenerator( _regeneratorRuntime.mark(function _callee2() { var gas, gasPrice, blocksBehind, expireSeconds, permission, actor, contractAddress, redeemFromEvmCompatible, redeemFromEosio, hostTxHash, ethTxReceipt, eosTxReceipt, broadcastedLtcTxReport, broadcastedLtcTxReceipt; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; gas = _options.gas, gasPrice = _options.gasPrice, blocksBehind = _options.blocksBehind, expireSeconds = _options.expireSeconds, permission = _options.permission, actor = _options.actor; if (!(_amount < MINIMUM_LTC_REDEEMABLE)) { _context2.next = 5; break; } promiEvent.reject("Impossible to burn less than ".concat(MINIMUM_LTC_REDEEMABLE, " pLTC")); return _context2.abrupt("return"); case 5: if (ptokensUtils.ltc.isValidAddress(_ltcAddress)) { _context2.next = 8; break; } promiEvent.reject('Invalid Litecoin Address'); return _context2.abrupt("return"); case 8: if (_this2.selectedNode) { _context2.next = 11; break; } _context2.next = 11; return _this2.select(); case 11: _context2.next = 13; return _this2._getContractAddress(); case 13: contractAddress = _context2.sent; redeemFromEvmCompatible = ptokensUtils.redeemFrom.redeemFromEvmCompatible, redeemFromEosio = ptokensUtils.redeemFrom.redeemFromEosio; hostTxHash = null; if (!(_this2.hostBlockchain === ptokensUtils.constants.blockchains.Ethereum)) { _context2.next = 23; break; } _context2.next = 19; return redeemFromEvmCompatible(_this2.hostApi, { privateKey: _this2.hostPrivateKey, gas: gas, gasPrice: gasPrice, contractAddress: contractAddress, value: 0 }, [_amount, _ltcAddress], promiEvent, 'hostTxBroadcasted'); case 19: ethTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('onEthTxConfirmed', ethTxReceipt); promiEvent.eventEmitter.emit('hostTxConfirmed', ethTxReceipt); hostTxHash = ethTxReceipt.transactionHash; case 23: if (!(_this2.hostBlockchain === ptokensUtils.constants.blockchains.Eosio)) { _context2.next = 30; break; } _context2.next = 26; return redeemFromEosio(_this2.hostApi, _amount, _ltcAddress, 8, contractAddress, ptokensUtils.constants.pTokens.pLTC, { blocksBehind: blocksBehind, expireSeconds: expireSeconds, permission: permission, actor: actor }); case 26: eosTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('onEosTxConfirmed', eosTxReceipt); promiEvent.eventEmitter.emit('hostTxConfirmed', eosTxReceipt); hostTxHash = eosTxReceipt.transaction_id; case 30: _context2.next = 32; return _this2.selectedNode.monitorIncomingTransaction(hostTxHash, promiEvent.eventEmitter); case 32: broadcastedLtcTxReport = _context2.sent; _context2.next = 35; return ptokensUtils.ltc.waitForTransactionConfirmation(_this2.nativeNetwork, broadcastedLtcTxReport.broadcast_tx_hash, 3000); case 35: broadcastedLtcTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('nativeTxConfirmed', broadcastedLtcTxReceipt); promiEvent.eventEmitter.emit('onLtcTxConfirmed', broadcastedLtcTxReceipt); promiEvent.resolve({ amount: _amount, hostTx: hostTxHash, nativeTx: broadcastedLtcTxReport.broadcast_tx_hash, to: _ltcAddress }); _context2.next = 44; break; case 41: _context2.prev = 41; _context2.t0 = _context2["catch"](0); promiEvent.reject(_context2.t0); case 44: case "end": return _context2.stop(); } } }, _callee2, null, [[0, 41]]); })); return function start() { return _ref.apply(this, arguments); }; }(); start(); return promiEvent.eventEmitter; } }, { key: "_getContractAddress", value: function () { var _getContractAddress2 = _asyncToGenerator( _regeneratorRuntime.mark(function _callee3() { var _yield$this$selectedN, smart_contract_address; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; if (this.contractAddress) { _context3.next = 10; break; } if (this.selectedNode) { _context3.next = 5; break; } _context3.next = 5; return this.select(); case 5: _context3.next = 7; return this.selectedNode.getInfo(); case 7: _yield$this$selectedN = _context3.sent; smart_contract_address = _yield$this$selectedN.smart_contract_address; this.contractAddress = smart_contract_address; case 10: return _context3.abrupt("return", this.contractAddress); case 13: _context3.prev = 13; _context3.t0 = _context3["catch"](0); throw new Error("Error during getting contract address: ".concat(_context3.t0.message)); case 16: case "end": return _context3.stop(); } } }, _callee3, this, [[0, 13]]); })); function _getContractAddress() { return _getContractAddress2.apply(this, arguments); } return _getContractAddress; }() }]); return pLTC; }(ptokensNodeSelector.NodeSelector); exports.pLTC = pLTC;