UNPKG

ptokens-perc20

Version:

repo holding the code for interacting with Ether or a generic Ethereum ERC20 token

649 lines (641 loc) 39.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty')); 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 BigNumber = _interopDefault(require('bignumber.js')); var Web3Utils = _interopDefault(require('web3-utils')); var _redeem, _redeem2, _redeem3, _redeem4, _redeem7, _redeem17, _redeem26, _redeem27, _redeem33, _constants$pTokens$pE; var minimumAmounts = (_constants$pTokens$pE = {}, _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pETH, { issue: 1000000000, redeem: (_redeem = {}, _defineProperty(_redeem, ptokensUtils.constants.blockchains.Eosio, 0.000000001), _defineProperty(_redeem, ptokensUtils.constants.blockchains.Telos, 0.000000001), _redeem) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pWETH, { issue: 1000000000, redeem: (_redeem2 = {}, _defineProperty(_redeem2, ptokensUtils.constants.blockchains.Eosio, 0.000000001), _defineProperty(_redeem2, ptokensUtils.constants.blockchains.Telos, 0.000000001), _redeem2) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.PNT, { issue: 1000000000, redeem: (_redeem3 = {}, _defineProperty(_redeem3, ptokensUtils.constants.blockchains.Eosio, 0.000000001), _defineProperty(_redeem3, ptokensUtils.constants.blockchains.Telos, 0.000000001), _defineProperty(_redeem3, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000), _defineProperty(_redeem3, ptokensUtils.constants.blockchains.Xdai, 1000000000), _defineProperty(_redeem3, ptokensUtils.constants.blockchains.Polygon, 1000000000), _defineProperty(_redeem3, ptokensUtils.constants.blockchains.Arbitrum, 1000000000), _redeem3) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pLINK, { issue: 1000000000, redeem: (_redeem4 = {}, _defineProperty(_redeem4, ptokensUtils.constants.blockchains.Eosio, 0.000000001), _defineProperty(_redeem4, ptokensUtils.constants.blockchains.Telos, 0.000000001), _redeem4) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pMKR, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pYFI, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.PTERIA, { issue: 1000000000, redeem: (_redeem7 = {}, _defineProperty(_redeem7, ptokensUtils.constants.blockchains.Eosio, 0.000000001), _defineProperty(_redeem7, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000), _redeem7) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pUNI, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pBAND, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.PBAL, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pCOMP, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pSNX, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pOMG, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pDAI, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pANT, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pLRC, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pUOS, { issue: 500000, redeem: (_redeem17 = {}, _defineProperty(_redeem17, ptokensUtils.constants.blockchains.Eosio, 50), _defineProperty(_redeem17, ptokensUtils.constants.blockchains.Ultra, 50), _redeem17) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pBAT, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pREP, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pZRX, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pPNK, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Eosio, 0.000000001) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pOPIUM, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pDEFIPlusPlus, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pBCP, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.CGG, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pUSDC, { issue: 5000000, redeem: (_redeem26 = {}, _defineProperty(_redeem26, ptokensUtils.constants.blockchains.Telos, 0.000000001), _defineProperty(_redeem26, ptokensUtils.constants.blockchains.Algorand, 5000000), _redeem26) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pUSDT, { issue: 5000000, redeem: (_redeem27 = {}, _defineProperty(_redeem27, ptokensUtils.constants.blockchains.Telos, 0.000000001), _defineProperty(_redeem27, ptokensUtils.constants.blockchains.Algorand, 5000000), _redeem27) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pOPEN, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.ANRX, { issue: 1000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 1000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.GALA, { issue: 1, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 10000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.ZMT, { issue: 1, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 10000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.BIST, { issue: 10000000000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 10000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.LUXO, { issue: 10000000000, redeem: (_redeem33 = {}, _defineProperty(_redeem33, ptokensUtils.constants.blockchains.Luxochain, 10000000000), _defineProperty(_redeem33, ptokensUtils.constants.blockchains.BinanceSmartChain, 10000000000), _redeem33) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pKEYS, { issue: 1000, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.BinanceSmartChain, 10000000000) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.OATH, { issue: 500e18, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Ethereum, 500e18) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pFTM, { issue: 100e18, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Ethereum, 100e18) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.pWFTM, { issue: 100e18, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Ethereum, 100e18) }), _defineProperty(_constants$pTokens$pE, ptokensUtils.constants.pTokens.NUCO, { issue: 1500e18, redeem: _defineProperty({}, ptokensUtils.constants.blockchains.Telos, 1500) }), _constants$pTokens$pE); var _pTokens$pUOS, _pTokens$pUSDT, _pTokens$pDAI$pTokens; var blockchains = ptokensUtils.constants.blockchains, networks = ptokensUtils.constants.networks, pTokens = ptokensUtils.constants.pTokens; var mapDecimals = (_pTokens$pDAI$pTokens = {}, _defineProperty(_pTokens$pDAI$pTokens, pTokens.pDAI, _defineProperty({}, blockchains.Eosio, _defineProperty({}, networks.EosioMainnet, 4))), _defineProperty(_pTokens$pDAI$pTokens, pTokens.pUOS, (_pTokens$pUOS = {}, _defineProperty(_pTokens$pUOS, blockchains.Eosio, _defineProperty({}, networks.EosioMainnet, 4)), _defineProperty(_pTokens$pUOS, blockchains.Ultra, _defineProperty({}, networks.UltraTestnet, 8)), _pTokens$pUOS)), _defineProperty(_pTokens$pDAI$pTokens, pTokens.pUSDT, (_pTokens$pUSDT = {}, _defineProperty(_pTokens$pUSDT, blockchains.Telos, _defineProperty({}, networks.TelosMainnet, 6)), _defineProperty(_pTokens$pUSDT, blockchains.Libre, _defineProperty({}, networks.LibreMainnet, 9)), _pTokens$pUSDT)), _defineProperty(_pTokens$pDAI$pTokens, pTokens.pUSDC, _defineProperty({}, blockchains.Telos, _defineProperty({}, networks.TelosMainnet, 6))), _pTokens$pDAI$pTokens); 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 pERC20 = function (_NodeSelector) { _inherits(pERC20, _NodeSelector); var _super = _createSuper(pERC20); function pERC20(_configs) { var _this; _classCallCheck(this, pERC20); var _helpers$parseParams = ptokensUtils.helpers.parseParams(_configs, _configs.nativeBlockchain || ptokensUtils.constants.blockchains.Ethereum), hostBlockchain = _helpers$parseParams.hostBlockchain, hostNetwork = _helpers$parseParams.hostNetwork, nativeBlockchain = _helpers$parseParams.nativeBlockchain, nativeNetwork = _helpers$parseParams.nativeNetwork; _this = _super.call(this, { pToken: _configs.pToken.toLowerCase() === ptokensUtils.constants.pTokens.pETH ? 'pweth' : _configs.pToken, hostBlockchain: hostBlockchain, hostNetwork: hostNetwork, nativeBlockchain: nativeBlockchain, nativeNetwork: nativeNetwork, defaultNode: _configs.defaultNode }); var ethPrivateKey = _configs.ethPrivateKey, ethProvider = _configs.ethProvider, bscPrivateKey = _configs.bscPrivateKey, bscProvider = _configs.bscProvider, xdaiPrivateKey = _configs.xdaiPrivateKey, xdaiProvider = _configs.xdaiProvider, polygonPrivateKey = _configs.polygonPrivateKey, polygonProvider = _configs.polygonProvider, eosPrivateKey = _configs.eosPrivateKey, eosRpc = _configs.eosRpc, eosSignatureProvider = _configs.eosSignatureProvider, telosPrivateKey = _configs.telosPrivateKey, telosRpc = _configs.telosRpc, telosSignatureProvider = _configs.telosSignatureProvider, ultraPrivateKey = _configs.ultraPrivateKey, ultraRpc = _configs.ultraRpc, ultraSignatureProvider = _configs.ultraSignatureProvider, arbitrumPrivateKey = _configs.arbitrumPrivateKey, arbitrumProvider = _configs.arbitrumProvider, luxochainPrivateKey = _configs.luxochainPrivateKey, luxochainProvider = _configs.luxochainProvider, algoProvider = _configs.algoProvider, algoClient = _configs.algoClient, ftmPrivateKey = _configs.ftmPrivateKey, ftmProvider = _configs.ftmProvider; if (ftmProvider) _this.web3 = new Web3(ftmProvider); if (ftmPrivateKey) { var account = _this.web3.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(ftmPrivateKey)); _this.web3.eth.defaultAccount = account.address; _this.nativePrivateKey = ptokensUtils.eth.addHexPrefix(ftmPrivateKey); } else { _this.nativePrivateKey = null; } if (nativeBlockchain === ptokensUtils.constants.blockchains.Fantom && (ethProvider || ethPrivateKey)) { 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; } } else if (ethProvider || ethPrivateKey) { if (ethProvider) _this.web3 = new Web3(ethProvider); if (ethPrivateKey) { var _account2 = _this.web3.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(ethPrivateKey)); _this.web3.eth.defaultAccount = _account2.address; _this.nativePrivateKey = ptokensUtils.eth.addHexPrefix(ethPrivateKey); } else { _this.nativePrivateKey = null; } } if (bscProvider) _this.hostApi = new Web3(bscProvider); if (bscPrivateKey) { var _account3 = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(bscPrivateKey)); _this.hostApi.eth.defaultAccount = _account3.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(bscPrivateKey); } else { _this.hostPrivateKey = null; } if (xdaiProvider) _this.hostApi = new Web3(xdaiProvider); if (xdaiPrivateKey) { var _account4 = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(xdaiPrivateKey)); _this.hostApi.eth.defaultAccount = _account4.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(xdaiPrivateKey); } else { _this.hostPrivateKey = null; } if (polygonProvider) _this.hostApi = new Web3(polygonProvider); if (polygonPrivateKey) { var _account5 = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(polygonPrivateKey)); _this.hostApi.eth.defaultAccount = _account5.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(polygonPrivateKey); } 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); } if (telosSignatureProvider) { _this.hostApi = ptokensUtils.eos.getApi(null, telosRpc, telosSignatureProvider); } else if (telosPrivateKey && telosRpc) { _this.hostApi = ptokensUtils.eos.getApi(telosPrivateKey, telosRpc, null); _this.hostPrivateKey = telosPrivateKey; } else if (!telosSignatureProvider && !telosPrivateKey && telosRpc) { _this.hostApi = ptokensUtils.eos.getApi(null, telosRpc, null); } if (ultraSignatureProvider) { _this.hostApi = ptokensUtils.eos.getApi(null, ultraRpc, ultraSignatureProvider); } else if (ultraPrivateKey && ultraRpc) { _this.hostApi = ptokensUtils.eos.getApi(ultraPrivateKey, ultraRpc, null); _this.hostPrivateKey = ultraPrivateKey; } else if (!ultraSignatureProvider && !ultraPrivateKey && ultraRpc) { _this.hostApi = ptokensUtils.eos.getApi(null, ultraRpc, null); } if (arbitrumProvider) _this.hostApi = new Web3(arbitrumProvider); if (arbitrumPrivateKey) { var _account6 = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(arbitrumPrivateKey)); _this.hostApi.eth.defaultAccount = _account6.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(arbitrumPrivateKey); } else { _this.hostPrivateKey = null; } if (luxochainProvider) _this.hostApi = new Web3(luxochainProvider); if (luxochainPrivateKey) { var _account7 = _this.hostApi.eth.accounts.privateKeyToAccount(ptokensUtils.eth.addHexPrefix(luxochainPrivateKey)); _this.hostApi.eth.defaultAccount = _account7.address; _this.hostPrivateKey = ptokensUtils.eth.addHexPrefix(luxochainPrivateKey); } else { _this.hostPrivateKey = null; } if (algoProvider) _this.hostApi = algoProvider; if (algoClient) _this.algoClient = algoClient; _this._peginEth = _configs.pToken.toLowerCase() === ptokensUtils.constants.pTokens.pETH || _configs.pToken.toLowerCase() === ptokensUtils.constants.pTokens.pFTM; return _this; } _createClass(pERC20, [{ key: "issue", value: function issue(_amount, _hostAccount) { var _options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return this._issue(_amount, _hostAccount, null, _options); } }, { key: "issueWithMetadata", value: function issueWithMetadata(_amount, _hostAccount, _metadata) { var _options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; return this._issue(_amount, _hostAccount, _metadata, _options); } }, { key: "_issue", value: function _issue(_amount, _hostAccount, _metadata) { var _this2 = this; var _options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var promiEvent = Web3PromiEvent(); var start = function () { var _ref = _asyncToGenerator( _regeneratorRuntime.mark(function _callee() { var _isValidAddress, gas, gasPrice, blockchains, minimumAmount, isValidAddress, destinationChainId, ethTxHash, waitForEthTransaction, ethTxReceipt, incomingTxReport, hostTxReceipt; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; gas = _options.gas, gasPrice = _options.gasPrice; blockchains = ptokensUtils.constants.blockchains; _context.next = 5; return _this2._loadData(); case 5: minimumAmount = minimumAmounts[_this2.pToken.toLowerCase()].issue; if (!BigNumber(_amount).isLessThan(minimumAmount)) { _context.next = 9; break; } promiEvent.reject("Impossible to issue less than ".concat(minimumAmount)); return _context.abrupt("return"); case 9: isValidAddress = (_isValidAddress = {}, _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Ethereum, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.BinanceSmartChain, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Xdai, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Polygon, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Arbitrum, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Luxochain, function (_address) { return Web3Utils.isAddress(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Eosio, function (_address) { return ptokensUtils.eos.isValidAccountName(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Telos, function (_address) { return ptokensUtils.eos.isValidAccountName(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Ultra, function (_address) { return ptokensUtils.eos.isValidAccountName(_address); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Algorand, function (_address) { return ptokensUtils.algo.isValidAddress(_address, true); }), _defineProperty(_isValidAddress, ptokensUtils.constants.blockchains.Libre, function (_address) { return ptokensUtils.eos.isValidAccountName(_address); }), _isValidAddress); if (isValidAddress[_this2.hostBlockchain](_hostAccount)) { _context.next = 13; break; } promiEvent.reject('Invalid host account'); return _context.abrupt("return"); case 13: if (_this2.selectedNode) { _context.next = 16; break; } _context.next = 16; return _this2.select(); case 16: destinationChainId = _this2.version === 'v2' ? ptokensUtils.constants.chainIds[_this2.hostBlockchain][_this2.hostNetwork] : null; ethTxHash = null; waitForEthTransaction = function waitForEthTransaction() { return new Promise(function (_resolve, _reject) { ptokensUtils.eth[_this2.nativePrivateKey ? 'sendSignedMethodTx' : 'makeContractSend'](_this2.web3, _this2._peginEth ? 'pegInEth' : 'pegIn', { privateKey: _this2.nativePrivateKey, abi: _this2.version === 'v1' ? ptokensUtils.abi.pERC20Vault : ptokensUtils.abi.pERC20VaultV2, gas: gas, gasPrice: gasPrice, contractAddress: ptokensUtils.eth.addHexPrefix(_this2.nativeVaultAddress), value: _this2._peginEth ? _amount : 0 }, _this2.version === 'v1' ? _metadata ? _this2._peginEth ? [_hostAccount, _metadata] : [_amount, _this2.nativeContractAddress, _hostAccount, _metadata] : _this2._peginEth ? [_hostAccount] : [_amount, _this2.nativeContractAddress, _hostAccount] : _metadata ? _this2._peginEth ? [_hostAccount, destinationChainId, _metadata] : [_amount, _this2.nativeContractAddress, _hostAccount, _metadata, destinationChainId] : _this2._peginEth ? [_hostAccount, destinationChainId] : [_amount, _this2.nativeContractAddress, _hostAccount, destinationChainId]).once('transactionHash', function (_hash) { ethTxHash = _hash; promiEvent.eventEmitter.emit('nativeTxBroadcasted', ethTxHash); }).once('receipt', _resolve).once('error', _reject); }); }; _context.next = 21; return waitForEthTransaction(); case 21: ethTxReceipt = _context.sent; promiEvent.eventEmitter.emit('nativeTxConfirmed', ethTxReceipt); _context.next = 25; return _this2.selectedNode.monitorIncomingTransaction(ethTxHash, promiEvent.eventEmitter); case 25: incomingTxReport = _context.sent; if (!(_this2.hostBlockchain === blockchains.Eosio || _this2.hostBlockchain === blockchains.Telos || _this2.hostBlockchain === blockchains.Libre)) { _context.next = 30; break; } _context.next = 29; return ptokensUtils.eos.waitForTransactionConfirmation(_this2.hostApi, incomingTxReport.broadcast_tx_hash); case 29: hostTxReceipt = _context.sent; case 30: if (!(_this2.hostBlockchain === blockchains.Xdai || _this2.hostBlockchain === blockchains.BinanceSmartChain || _this2.hostBlockchain === blockchains.Polygon || _this2.hostBlockchain === blockchains.Arbitrum || _this2.hostBlockchain === blockchains.Luxochain || _this2.hostBlockchain === blockchains.Ethereum)) { _context.next = 34; break; } _context.next = 33; return ptokensUtils.eth.waitForTransactionConfirmation(_this2.hostApi, incomingTxReport.broadcast_tx_hash); case 33: hostTxReceipt = _context.sent; case 34: if (!(_this2.hostBlockchain === blockchains.Algorand)) { _context.next = 37; break; } _context.next = 37; return new Promise(function (resolve) { return setTimeout(resolve, 5000); }); case 37: promiEvent.eventEmitter.emit('hostTxConfirmed', hostTxReceipt); promiEvent.resolve({ to: _hostAccount, nativeTx: ethTxHash, hostTx: incomingTxReport.broadcast_tx_hash, amount: BigNumber(_amount).toFixed() }); _context.next = 44; break; case 41: _context.prev = 41; _context.t0 = _context["catch"](0); promiEvent.reject(_context.t0); case 44: case "end": return _context.stop(); } } }, _callee, null, [[0, 41]]); })); return function start() { return _ref.apply(this, arguments); }; }(); start(); return promiEvent.eventEmitter; } }, { key: "redeem", value: function redeem(_amount, _nativeAccount) { var _options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return this._redeem(_amount, _nativeAccount, null, _options); } }, { key: "redeemWithMetadata", value: function redeemWithMetadata(_amount, _nativeAccount, _metadata) { var _options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; return this._redeem(_amount, _nativeAccount, _metadata, _options); } }, { key: "_redeem", value: function _redeem(_amount, _nativeAccount, _metadata) { var _this3 = this; var _options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var promiEvent = Web3PromiEvent(); var start = function () { var _ref2 = _asyncToGenerator( _regeneratorRuntime.mark(function _callee2() { var blocksBehind, expireSeconds, permission, _gas, _gasPrice, actor, from, swapInfo, blockchains, pTokens, minimumAmount, redeemFromEosio, redeemFromEvmCompatible, redeemFromAlgorand, _destinationChainId, hostTxHash, hostTxReceipt, _hostTxReceipt, _hostTxReceipt2, incomingTxReport, nativeTxReceipt; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; blocksBehind = _options.blocksBehind, expireSeconds = _options.expireSeconds, permission = _options.permission, _gas = _options.gas, _gasPrice = _options.gasPrice, actor = _options.actor, from = _options.from, swapInfo = _options.swapInfo; blockchains = ptokensUtils.constants.blockchains, pTokens = ptokensUtils.constants.pTokens; _context2.next = 5; return _this3._loadData(); case 5: minimumAmount = minimumAmounts[_this3.pToken.toLowerCase()].redeem[_this3.hostBlockchain]; if (!BigNumber(_amount).isLessThan(minimumAmount)) { _context2.next = 9; break; } promiEvent.reject("Impossible to redeem less than ".concat(minimumAmount)); return _context2.abrupt("return"); case 9: if (!(_this3.nativeBlockchain === blockchains.Ethereum && !Web3Utils.isAddress(_nativeAccount))) { _context2.next = 12; break; } promiEvent.reject('Invalid native account'); return _context2.abrupt("return"); case 12: if (_this3.selectedNode) { _context2.next = 15; break; } _context2.next = 15; return _this3.select(); case 15: redeemFromEosio = ptokensUtils.redeemFrom.redeemFromEosio, redeemFromEvmCompatible = ptokensUtils.redeemFrom.redeemFromEvmCompatible, redeemFromAlgorand = ptokensUtils.redeemFrom.redeemFromAlgorand; _destinationChainId = _this3.version === 'v2' ? ptokensUtils.constants.chainIds[_this3.nativeBlockchain][_this3.nativeNetwork] : null; if (!(_this3.hostBlockchain === blockchains.BinanceSmartChain || _this3.hostBlockchain === blockchains.Xdai || _this3.hostBlockchain === blockchains.Polygon || _this3.hostBlockchain === blockchains.Arbitrum || _this3.hostBlockchain === blockchains.Luxochain || _this3.hostBlockchain === blockchains.Ethereum)) { _context2.next = 23; break; } _context2.next = 20; return redeemFromEvmCompatible(_this3.hostApi, { privateKey: _this3.hostPrivateKey, gas: _gas, gasPrice: _gasPrice, contractAddress: _this3.hostContractAddress, value: 0 }, _this3.version === 'v1' ? _metadata ? [_amount, _metadata, _nativeAccount] : [_amount, _nativeAccount] : _metadata ? [_amount, _metadata, _nativeAccount, _destinationChainId] : [_amount, _nativeAccount, _destinationChainId], promiEvent, 'hostTxBroadcasted', _this3.version || 'v1'); case 20: hostTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('hostTxConfirmed', hostTxReceipt); hostTxHash = hostTxReceipt.transactionHash; case 23: if (!(_this3.hostBlockchain === blockchains.Eosio || _this3.hostBlockchain === blockchains.Telos || _this3.hostBlockchain === blockchains.Ultra || _this3.hostBlockchain === blockchains.Libre)) { _context2.next = 29; break; } _context2.next = 26; return redeemFromEosio(_this3.hostApi, _amount, _nativeAccount, mapDecimals[_this3.pToken.toLowerCase()] ? mapDecimals[_this3.pToken.toLowerCase()][_this3.hostBlockchain][_this3.hostNetwork] : 9, _this3.hostContractAddress, _this3.pToken === pTokens.pWETH ? 'peth' : _this3.pToken, { blocksBehind: blocksBehind, expireSeconds: expireSeconds, permission: permission, actor: actor, destinationChainId: _destinationChainId, version: _this3.version }); case 26: _hostTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('hostTxConfirmed', _hostTxReceipt); hostTxHash = _hostTxReceipt.transaction_id; case 29: if (!(_this3.hostBlockchain === ptokensUtils.constants.blockchains.Algorand)) { _context2.next = 35; break; } _context2.next = 32; return redeemFromAlgorand({ provider: _this3.hostApi, client: _this3.algoClient, amount: _amount, to: _this3.hostIdentity, assetIndex: _this3.hostContractAddress, nativeAccount: _nativeAccount, from: from, destinationChainId: _destinationChainId, eventEmitter: promiEvent.eventEmitter, swapInfo: swapInfo }); case 32: _hostTxReceipt2 = _context2.sent; promiEvent.eventEmitter.emit('hostTxConfirmed', _hostTxReceipt2); hostTxHash = _hostTxReceipt2.txID().toString(); case 35: _context2.next = 37; return _this3.selectedNode.monitorIncomingTransaction(hostTxHash, promiEvent.eventEmitter); case 37: incomingTxReport = _context2.sent; _context2.next = 40; return ptokensUtils.eth.waitForTransactionConfirmation(_this3.web3, incomingTxReport.broadcast_tx_hash); case 40: nativeTxReceipt = _context2.sent; promiEvent.eventEmitter.emit('nativeTxConfirmed', nativeTxReceipt); promiEvent.resolve({ to: _nativeAccount, nativeTx: nativeTxReceipt.transactionHash, hostTx: hostTxHash, amount: BigNumber(_amount).toFixed() }); _context2.next = 48; break; case 45: _context2.prev = 45; _context2.t0 = _context2["catch"](0); promiEvent.reject(_context2.t0); case 48: case "end": return _context2.stop(); } } }, _callee2, null, [[0, 45]]); })); return function start() { return _ref2.apply(this, arguments); }; }(); start(); return promiEvent.eventEmitter; } }, { key: "_loadData", value: function () { var _loadData2 = _asyncToGenerator( _regeneratorRuntime.mark(function _callee3() { var _yield$this$selectedN, native_smart_contract_address, host_smart_contract_address, native_vault_address, versions, host_identity; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; if (this.selectedNode) { _context3.next = 4; break; } _context3.next = 4; return this.select(); case 4: if (!(!this.nativeContractAddress || !this.hostContractAddress)) { _context3.next = 18; break; } _context3.next = 7; return this.selectedNode.getInfo(); case 7: _yield$this$selectedN = _context3.sent; native_smart_contract_address = _yield$this$selectedN.native_smart_contract_address; host_smart_contract_address = _yield$this$selectedN.host_smart_contract_address; native_vault_address = _yield$this$selectedN.native_vault_address; versions = _yield$this$selectedN.versions; host_identity = _yield$this$selectedN.host_identity; this.nativeContractAddress = ptokensUtils.eth.addHexPrefix(native_smart_contract_address); this.hostContractAddress = this.hostBlockchain === ptokensUtils.constants.blockchains.Eosio || this.hostBlockchain === ptokensUtils.constants.blockchains.Telos || this.hostBlockchain === ptokensUtils.constants.blockchains.Ultra || this.hostBlockchain === ptokensUtils.constants.blockchains.Algorand || this.hostBlockchain === ptokensUtils.constants.blockchains.Libre ? host_smart_contract_address : ptokensUtils.eth.addHexPrefix(host_smart_contract_address); this.nativeVaultAddress = native_vault_address ? ptokensUtils.eth.addHexPrefix(native_vault_address) : null; this.version = versions && versions.network ? versions.network : 'v1'; this.hostIdentity = host_identity; case 18: _context3.next = 23; break; case 20: _context3.prev = 20; _context3.t0 = _context3["catch"](0); throw new Error("Error during loading data: ".concat(_context3.t0.message)); case 23: case "end": return _context3.stop(); } } }, _callee3, this, [[0, 20]]); })); function _loadData() { return _loadData2.apply(this, arguments); } return _loadData; }() }]); return pERC20; }(ptokensNodeSelector.NodeSelector); exports.pERC20 = pERC20;