UNPKG

@hydro-protocol/hydro-client-js

Version:
241 lines (240 loc) 11.6 kB
"use strict"; var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var bignumber_js_1 = __importDefault(require("bignumber.js")); var web3_1 = __importDefault(require("web3")); var erc20_json_1 = __importDefault(require("../abi/erc20.json")); var weth_json_1 = __importDefault(require("../abi/weth.json")); /** * Handle building requests to the server, including authentication * in the request header. */ var Web3Handler = /** @class */ (function () { function Web3Handler(account, options) { this.PROXY_ADDRESS = '0x74622073a4821dbfd046e9aa2ccf691341a076e1'; this.account = account; if (options && options.web3Url) { this.web3 = new web3_1.default(new web3_1.default.providers.HttpProvider(options.web3Url)); } } Web3Handler.prototype.getBalance = function (address) { return __awaiter(this, void 0, void 0, function () { var web3, balance; return __generator(this, function (_a) { switch (_a.label) { case 0: web3 = this.getWeb3(); if (!address) return [3 /*break*/, 2]; return [4 /*yield*/, this.getERC20Contract(address) .methods.balanceOf(this.account.address) .call()]; case 1: balance = _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, web3.eth.getBalance(this.account.address)]; case 3: balance = _a.sent(); _a.label = 4; case 4: return [2 /*return*/, web3.utils.fromWei(new bignumber_js_1.default(balance).toString()).toString()]; } }); }); }; Web3Handler.prototype.wrapEth = function (wethAddress, amount, wait) { return __awaiter(this, void 0, void 0, function () { var contract, data, web3; return __generator(this, function (_a) { contract = this.getWethContract(wethAddress); data = contract.methods.deposit().encodeABI(); web3 = this.getWeb3(); return [2 /*return*/, this.signAndSendTransaction({ data: data, to: wethAddress, value: web3.utils.toHex(web3.utils.toWei(amount)), }, wait)]; }); }); }; Web3Handler.prototype.unwrapEth = function (wethAddress, amount, wait) { return __awaiter(this, void 0, void 0, function () { var contract, web3, data; return __generator(this, function (_a) { contract = this.getWethContract(wethAddress); web3 = this.getWeb3(); data = contract.methods.withdraw(web3.utils.toWei(amount)).encodeABI(); return [2 /*return*/, this.signAndSendTransaction({ data: data, to: wethAddress, value: web3.utils.toHex(0), }, wait)]; }); }); }; Web3Handler.prototype.getAllowance = function (address) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.getERC20Contract(address) .methods.allowance(this.account.address, this.PROXY_ADDRESS) .call()]; }); }); }; Web3Handler.prototype.enableToken = function (address, wait) { return __awaiter(this, void 0, void 0, function () { var contract, data; return __generator(this, function (_a) { contract = this.getERC20Contract(address); data = contract.methods .approve(this.PROXY_ADDRESS, new bignumber_js_1.default(2) .pow(256) .minus(1) .toFixed()) .encodeABI(); return [2 /*return*/, this.signAndSendTransaction({ data: data, to: address, value: 0, }, wait)]; }); }); }; Web3Handler.prototype.disableToken = function (address, wait) { return __awaiter(this, void 0, void 0, function () { var contract, data; return __generator(this, function (_a) { contract = this.getERC20Contract(address); data = contract.methods.approve(this.PROXY_ADDRESS, 0).encodeABI(); return [2 /*return*/, this.signAndSendTransaction({ data: data, to: address, value: 0, }, wait)]; }); }); }; Web3Handler.prototype.getERC20Contract = function (address) { var web3 = this.getWeb3(); // @ts-ignore return new web3.eth.Contract(erc20_json_1.default, address); }; Web3Handler.prototype.getWethContract = function (address) { var web3 = this.getWeb3(); // @ts-ignore return new web3.eth.Contract(weth_json_1.default, address); }; Web3Handler.prototype.getWeb3 = function () { if (!this.web3) { throw new Error('No web3 url provided, cannot use mainnet functions!'); } return this.web3; }; Web3Handler.prototype.signAndSendTransaction = function (tx, wait) { return __awaiter(this, void 0, void 0, function () { var web3, sig; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: web3 = this.getWeb3(); return [4 /*yield*/, this.constructTransaction(tx)]; case 1: tx = _a.sent(); return [4 /*yield*/, this.account.signTransaction(tx)]; case 2: sig = _a.sent(); return [2 /*return*/, new Promise(function (res, rej) { return __awaiter(_this, void 0, void 0, function () { var p; return __generator(this, function (_a) { p = web3.eth .sendSignedTransaction(sig) .once('transactionHash', function (hash) { if (!wait) { res(hash); } }) .once('confirmation', function (_, receipt) { if (wait) { res(receipt.transactionHash); } }) .on('error', function (error) { return rej(error.message); }); return [2 /*return*/]; }); }); })]; } }); }); }; Web3Handler.prototype.constructTransaction = function (tx) { return __awaiter(this, void 0, void 0, function () { var web3, from, _a, chainId, gasPrice, nonce, gas; return __generator(this, function (_b) { switch (_b.label) { case 0: web3 = this.getWeb3(); from = this.account.address; return [4 /*yield*/, Promise.all([ web3.eth.net.getId(), web3.eth.getGasPrice(), web3.eth.getTransactionCount(from), ])]; case 1: _a = _b.sent(), chainId = _a[0], gasPrice = _a[1], nonce = _a[2]; tx = __assign({}, tx, { chainId: chainId, from: from, gas: 100000000, gasPrice: web3.utils.toHex(gasPrice), nonce: nonce }); return [4 /*yield*/, web3.eth.estimateGas(tx)]; case 2: gas = _b.sent(); return [2 /*return*/, __assign({}, tx, { gas: web3.utils.toHex(gas) })]; } }); }); }; return Web3Handler; }()); exports.Web3Handler = Web3Handler;