UNPKG

@bloom-trade/finance-connector

Version:

Is a package entended to be used with multiple web3 and web2 providers to interact with blockchains.

168 lines (167 loc) 9.03 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProviderConnectorImpl = void 0; const connector_1 = require("../connector"); const axios_1 = __importDefault(require("axios")); const utils_1 = require("../../utils"); const web3_1 = __importDefault(require("web3")); class ProviderConnectorImpl extends connector_1.ProviderConnector { getBalance() { return __awaiter(this, void 0, void 0, function* () { const apiKey = this._credentials.apiKey; const balance = []; const contracts = (0, utils_1.getSupportedContracts)(); if (!this.addresses) { return []; } for (const address of this.addresses) { if (!balance.find((e) => { if (e.detail.find((e) => e.address === address) && e.asset === 'ETH') { return e; } })) { const { data } = yield axios_1.default.get(`${this._baseurl}?module=account&action=balance&address=${address}&tag=latest&apikey=${apiKey}`); //TODO: Wei convertion const web3 = new web3_1.default(web3_1.default.givenProvider || 'ws://localhost:8545'); balance.push({ asset: 'ETH', description: 'Ethereum', balance: web3.utils.fromWei(data.result, 'ether'), detail: [ { address, provider: this._provider.id, chain: this.chain, balance: web3.utils.fromWei(data.result, 'ether'), }, ], }); } for (const contract of contracts) { const { data } = yield axios_1.default.get(`${this._baseurl}?module=account&action=tokenbalance&contractaddress=${(0, utils_1.getAssetDataByChain)(contract, this.chain, this._provider) .address}&address=${address}&tag=latest&apikey=${apiKey}`); if (data.result !== '0') { let retrievedBalance; if ((0, utils_1.getAssetDataByChain)(contract, this.chain, this._provider) .decimalPosition === 18) { retrievedBalance = (0, utils_1.weiToEth)(data.result); } else { retrievedBalance = (0, utils_1.convertToken)(data.result, (0, utils_1.getAssetDataByChain)(contract, this.chain, this._provider) .decimalPosition); } balance.push({ asset: contract.token, description: (0, utils_1.getDescription)(contract.token), balance: retrievedBalance, detail: [ { address, provider: this._provider.id, chain: this.chain, balance: retrievedBalance, }, ], }); } continue; } } return balance; //staff }); } getTransactionHistory(filters) { return __awaiter(this, void 0, void 0, function* () { const apiKey = this._credentials.apiKey; const contracts = (0, utils_1.getSupportedContracts)(); if (!this.addresses) { return []; } const transactions = []; let startingBlock = 0; for (const address of this.addresses) { if (filters.from !== 'beginning' && filters.from.selfCustodialProviders && filters.from.selfCustodialProviders.length > 0 && filters.from.selfCustodialProviders.filter((provider) => provider.chain === this.chain).length > 0) { //Check if eth is included otherwise use from block 0 const ethIncluded = filters.from.selfCustodialProviders.find((e) => e.chain === 'eth'); startingBlock = ethIncluded.block; } const { data: { result: ethTransactions }, } = yield axios_1.default.get(`${this._baseurl}?module=account&action=txlist&address=${address}&startblock=${startingBlock}&endblock=99999999&tag=latest&apikey=${apiKey}`); for (const e of ethTransactions) { if (e.value !== '0' && e.from) { const lowerCaseAddress = address.toLowerCase(); const lowerCaseFrom = e.from.toLowerCase(); const obj = { asset: 'eth', from: e.from, to: e.to, amount: (0, utils_1.weiToEth)(e.value), type: lowerCaseAddress != lowerCaseFrom ? 'in' : 'out', status: e.isError === '0' ? 'completed' : 'failed', timestamp: parseInt(e.timeStamp) * 1000, provider: this._provider.id, chain: this.chain, block: e.blockNumber, }; if (lowerCaseAddress == lowerCaseFrom) { Object.assign(obj, { gas: e.gas, gasPrice: e.gasPrice, gasUsed: e.gasUsed, }); } transactions.push(obj); } } for (const contract of contracts) { const { data: { result: ERC20Transactions }, } = yield axios_1.default.get(`${this._baseurl}?module=account&action=tokentx&contractaddress=${(0, utils_1.getAssetDataByChain)(contract, this.chain, this._provider) .address}&address=${address}&startblock=${startingBlock}&endblock=99999999&tag=latest&apikey=${apiKey}`); for (const e of ERC20Transactions) { if (e instanceof Object && e.value !== '0') { const lowerCaseAddress = address.toLowerCase(); const lowerCaseFrom = e.from.toLowerCase(); const obj = { asset: contract.token.toLowerCase(), from: e.from, to: e.to, amount: (0, utils_1.convertToken)(e.value, (0, utils_1.getAssetDataByChain)(contract, this.chain, this._provider).decimalPosition), type: lowerCaseAddress !== lowerCaseFrom ? 'in' : 'out', status: lowerCaseAddress !== lowerCaseFrom ? 'in' : 'out', timestamp: parseInt(e.timeStamp) * 1000, provider: this._provider.id, chain: this.chain, block: e.blockNumber, }; if (lowerCaseAddress === lowerCaseFrom) { Object.assign(obj, { gas: e.gas, gasPrice: e.gasPrice, gasUsed: e.gasUsed, }); } transactions.push(obj); } } } } return transactions; }); } } exports.ProviderConnectorImpl = ProviderConnectorImpl;