ptokens-pdoge
Version:
repo holding the code for interacting with pDOGE
266 lines (260 loc) • 11.9 kB
JavaScript
'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_DOGE_REDEEMABLE = 50000000000000;
var pDOGE = function (_NodeSelector) {
_inherits(pDOGE, _NodeSelector);
var _super = _createSuper(pDOGE);
function pDOGE(_configs) {
var _this;
_classCallCheck(this, pDOGE);
var _helpers$parseParams = ptokensUtils.helpers.parseParams(_configs, ptokensUtils.constants.blockchains.Dogecoin),
hostBlockchain = _helpers$parseParams.hostBlockchain,
hostNetwork = _helpers$parseParams.hostNetwork,
nativeBlockchain = _helpers$parseParams.nativeBlockchain,
nativeNetwork = _helpers$parseParams.nativeNetwork;
_this = _super.call(this, {
pToken: ptokensUtils.constants.pTokens.pDOGE,
hostBlockchain: hostBlockchain,
hostNetwork: hostNetwork,
nativeBlockchain: nativeBlockchain,
nativeNetwork: nativeNetwork,
defaultNode: _configs.defaultNode
});
var ethPrivateKey = _configs.ethPrivateKey,
ethProvider = _configs.ethProvider;
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;
}
_this.contractAddress = null;
_this.decimals = null;
return _this;
}
_createClass(pDOGE, [{
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 Dogecoin 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, contractAddress, redeemFromEvmCompatible, hostTxHash, ethTxReceipt, broadcastedDogeTxReport, 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;
if (!(_amount < MINIMUM_DOGE_REDEEMABLE)) {
_context2.next = 5;
break;
}
promiEvent.reject("Impossible to burn less than ".concat(MINIMUM_DOGE_REDEEMABLE, " pDOGE"));
return _context2.abrupt("return");
case 5:
if (ptokensUtils.doge.isValidAddress(_ltcAddress)) {
_context2.next = 8;
break;
}
promiEvent.reject('Invalid Dogecoin 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;
hostTxHash = null;
if (!(_this2.hostBlockchain === ptokensUtils.constants.blockchains.Ethereum)) {
_context2.next = 22;
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('hostTxConfirmed', ethTxReceipt);
hostTxHash = ethTxReceipt.transactionHash;
case 22:
_context2.next = 24;
return _this2.selectedNode.monitorIncomingTransaction(hostTxHash, promiEvent.eventEmitter);
case 24:
broadcastedDogeTxReport = _context2.sent;
_context2.next = 27;
return ptokensUtils.doge.waitForTransactionConfirmation(_this2.nativeNetwork, broadcastedDogeTxReport.broadcast_tx_hash, 3000);
case 27:
broadcastedLtcTxReceipt = _context2.sent;
promiEvent.eventEmitter.emit('nativeTxConfirmed', broadcastedLtcTxReceipt);
promiEvent.resolve({
amount: _amount,
hostTx: hostTxHash,
nativeTx: broadcastedDogeTxReport.broadcast_tx_hash,
to: _ltcAddress
});
_context2.next = 35;
break;
case 32:
_context2.prev = 32;
_context2.t0 = _context2["catch"](0);
promiEvent.reject(_context2.t0);
case 35:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[0, 32]]);
}));
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 pDOGE;
}(ptokensNodeSelector.NodeSelector);
exports.pDOGE = pDOGE;