UNPKG

thorchain.js

Version:

Thorchain.js is a libary that lets you interact with mutiple blockchains perform swaps, etc

106 lines (105 loc) 4.44 kB
"use strict"; /* YOU WILL FIND ALOT OF TS-IGNORE's THROUGHOUT THIS PROJECT. YOU CAN FIX THEM IF YOU WANT I DONT REALLY CARE */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.client = void 0; const xchain_client_1 = require("@xchainjs/xchain-client"); const xchain_util_1 = require("@xchainjs/xchain-util"); const types_1 = require("./types"); /* All chains */ const thorchain_1 = require("../chains/thorchain"); const bitcoin_1 = require("../chains/bitcoin"); class client { constructor({ network = xchain_client_1.Network.Mainnet, phrase = "", }) { this.chains = types_1.Chains; this.getChainClient = (chain) => { if (chain === xchain_util_1.THORChain) return this.thor; if (chain === xchain_util_1.BTCChain) return this.btc; // if (chain === BCHChain) return this.bch return null; }; this.getAddressByChain = (chain) => __awaiter(this, void 0, void 0, function* () { const chainClient = this.getChainClient(chain); if (!chainClient) throw new Error("invalid chain"); try { const wallet = chainClient.getAddress; return wallet; } catch (error) { return Promise.reject(error); } }); this.getBalanceByChain = (chain, address) => __awaiter(this, void 0, void 0, function* () { const chainClient = this.getChainClient(chain); if (!chainClient) throw new Error("invalid chain"); try { if (address) { const balance = yield chainClient.getBalance(address); return balance; } else { const balance = yield chainClient.getBalance(); return balance; } } catch (error) { return Promise.reject(error); } }); this.getFees = (chain) => { const chainClient = this.getChainClient(chain); if (!chainClient) throw new Error("invalid chain"); return chainClient.getClient().getFees(); }; this.transfer = (chain, transferObj) => __awaiter(this, void 0, void 0, function* () { const chainClient = this.getChainClient(chain); if (!chainClient) throw new Error("invalid chain"); try { const tx = yield chainClient.transfer(transferObj.amount, transferObj.recipient, transferObj.memo); return tx; } catch (error) { return Promise.reject(error); } }); this.network = network; this.phrase = phrase; // create chain clients this.thor = new thorchain_1.ThorChain({ network, phrase, chainIds: { testnet: "thorchain-testnet-v2", stagenet: "thorchain-stagenet-v2", mainnet: "thorchain-mainnet-v1", }, }); this.btc = new bitcoin_1.Bitcoin({ network, phrase }); // this.bch = new BchChain({ network, phrase }) } assetToBase(amount) { if (isNaN(amount)) { return new Error("Amount is not a number"); } // @ts-ignore return (0, xchain_util_1.assetToBase)((0, xchain_util_1.assetAmount)(amount, 8)); } baseToAsset(amount) { return (0, xchain_util_1.baseToAsset)(amount); } } exports.client = client;