UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

806 lines 51.2 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 __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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataClient = void 0; var viem_1 = require("viem"); var constants_1 = require("../constants"); var errors_1 = require("../errors"); var subgraph_1 = require("../subgraph"); var constants_2 = require("../subgraph/constants"); var split_1 = require("../subgraph/split"); var waterfall_1 = require("../subgraph/waterfall"); var vesting_1 = require("../subgraph/vesting"); var swapper_1 = require("../subgraph/swapper"); var pass_through_wallet_1 = require("../subgraph/pass-through-wallet"); var liquid_1 = require("../subgraph/liquid"); var user_1 = require("../subgraph/user"); var utils_1 = require("../utils"); var DataClient = /** @class */ (function () { function DataClient(_a) { var publicClient = _a.publicClient, publicClients = _a.publicClients, ensPublicClient = _a.ensPublicClient, apiConfig = _a.apiConfig, _b = _a.includeEnsNames, includeEnsNames = _b === void 0 ? false : _b; var _c, _d; if (includeEnsNames && !publicClient && !(publicClients === null || publicClients === void 0 ? void 0 : publicClients[1]) && !ensPublicClient) throw new errors_1.InvalidConfigError('Must include a mainnet public client if includeEnsNames is set to true'); this._ensPublicClient = (_d = (_c = publicClients === null || publicClients === void 0 ? void 0 : publicClients[1]) !== null && _c !== void 0 ? _c : ensPublicClient) !== null && _d !== void 0 ? _d : publicClient; this._publicClient = publicClient; this._publicClients = publicClients; this._includeEnsNames = includeEnsNames; this._graphqlClient = (0, subgraph_1.getGraphqlClient)(apiConfig); } DataClient.prototype._requirePublicClient = function (chainId) { this._getPublicClient(chainId); }; DataClient.prototype._getPublicClient = function (chainId) { if (this._publicClients && this._publicClients[chainId]) { return this._publicClients[chainId]; } if (!this._publicClient) throw new errors_1.MissingPublicClientError("Public client required on chain ".concat(chainId, " to perform this action, please update your call to the constructor")); return this._publicClient; }; DataClient.prototype._makeGqlRequest = function (query, variables) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this._graphqlClient) { throw new errors_1.UnsupportedSubgraphChainIdError(); } if ((variables === null || variables === void 0 ? void 0 : variables.chainId) && typeof variables.chainId === 'string') { if (!constants_1.SPLITS_SUBGRAPH_CHAIN_IDS.includes(Number(variables.chainId))) { throw new errors_1.UnsupportedSubgraphChainIdError(); } } return [4 /*yield*/, this._graphqlClient .query(query, variables) .toPromise()]; case 1: response = _a.sent(); if (response.error) { throw response.error; } return [2 /*return*/, response.data]; } }); }); }; DataClient.prototype._loadAccount = function (accountId, chainId) { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._makeGqlRequest(subgraph_1.ACCOUNT_QUERY, { accountId: accountId.toLowerCase(), chainId: chainId.toString(), })]; case 1: result = _a.sent(); if (!result.account) return [2 /*return*/]; return [2 /*return*/, (0, subgraph_1.formatGqlAccount)(result.account)]; } }); }); }; DataClient.prototype._loadFullAccount = function (accountId, chainId) { return __awaiter(this, void 0, void 0, function () { var result, response, relatedAccounts, allPassThroughWallets; var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v; return __generator(this, function (_w) { switch (_w.label) { case 0: return [4 /*yield*/, this._makeGqlRequest(subgraph_1.FULL_ACCOUNT_QUERY, { accountId: accountId.toLowerCase(), chainId: chainId.toString(), relatedAccountsLimit: constants_2.MAX_RELATED_ACCOUNTS, })]; case 1: result = _w.sent(); response = {}; relatedAccounts = result.relatedAccounts; if (result.account) { response.upstreamSplits = (_b = (_a = relatedAccounts.upstreamSplits) === null || _a === void 0 ? void 0 : _a.map(function (gqlSplit) { return (0, split_1.formatGqlSplit)(gqlSplit); })) !== null && _b !== void 0 ? _b : []; response.upstreamWaterfalls = (_d = (_c = relatedAccounts.upstreamWaterfalls) === null || _c === void 0 ? void 0 : _c.map(function (waterfallModule) { return (0, waterfall_1.formatGqlWaterfallModule)(waterfallModule); })) !== null && _d !== void 0 ? _d : []; response.upstreamVesting = (_f = (_e = relatedAccounts.upstreamVesting) === null || _e === void 0 ? void 0 : _e.map(function (vestingModule) { return (0, vesting_1.formatGqlVestingModule)(vestingModule); })) !== null && _f !== void 0 ? _f : []; response.upstreamSwappers = (_h = (_g = relatedAccounts.upstreamSwappers) === null || _g === void 0 ? void 0 : _g.map(function (swapper) { return (0, swapper_1.formatGqlSwapper)(swapper); })) !== null && _h !== void 0 ? _h : []; response.upstreamPassThroughWallets = (_k = (_j = relatedAccounts.upstreamPassThroughWallets) === null || _j === void 0 ? void 0 : _j.map(function (passThroughWallet) { return (0, pass_through_wallet_1.formatGqlPassThroughWallet)(passThroughWallet); })) !== null && _k !== void 0 ? _k : []; response.upstreamLiquidSplits = (_m = (_l = relatedAccounts.upstreamLiquidSplits) === null || _l === void 0 ? void 0 : _l.map(function (gqlLiquidSplit) { return (0, liquid_1.formatGqlLiquidSplit)(gqlLiquidSplit); })) !== null && _m !== void 0 ? _m : []; if (response.upstreamLiquidSplits.length > 0) { response.upstreamSplits = response.upstreamSplits.concat(relatedAccounts.upstreamLiquidSplits.map(function (gqlLiquidSplit) { return (0, split_1.formatGqlSplit)(gqlLiquidSplit.split); })); } response.controllingSplits = (_p = (_o = relatedAccounts.controllingSplits) === null || _o === void 0 ? void 0 : _o.map(function (split) { return (0, split_1.formatGqlSplit)(split); })) !== null && _p !== void 0 ? _p : []; response.pendingControlSplits = (_r = (_q = relatedAccounts.pendingControlSplits) === null || _q === void 0 ? void 0 : _q.map(function (split) { return (0, split_1.formatGqlSplit)(split); })) !== null && _r !== void 0 ? _r : []; response.ownedSwappers = (_t = (_s = relatedAccounts.ownedSwappers) === null || _s === void 0 ? void 0 : _s.map(function (swapper) { return (0, swapper_1.formatGqlSwapper)(swapper); })) !== null && _t !== void 0 ? _t : []; response.ownedPassThroughWallets = (_v = (_u = relatedAccounts.ownedPassThroughWallets) === null || _u === void 0 ? void 0 : _u.map(function (passThroughWallet) { return (0, pass_through_wallet_1.formatGqlPassThroughWallet)(passThroughWallet); })) !== null && _v !== void 0 ? _v : []; response.account = (0, subgraph_1.formatFullGqlAccount)(result.account, response.upstreamSplits, response.upstreamVesting, response.upstreamWaterfalls, response.upstreamLiquidSplits, response.upstreamSwappers, response.upstreamPassThroughWallets, relatedAccounts.controllingSplits, relatedAccounts.pendingControlSplits, relatedAccounts.ownedSwappers, relatedAccounts.ownedPassThroughWallets); allPassThroughWallets = __spreadArray([], __read(relatedAccounts.ownedPassThroughWallets), false); if (result.account.__typename === 'PassThroughWallet') { allPassThroughWallets.push(result.account); } if (result.account.__typename === 'LiquidSplit') { // Not really an upstream split, but it's just used for loading. Should probably update that name. Maybe related splits? response.upstreamSplits.push((0, split_1.formatGqlSplit)(result.account.split)); } else if (result.account.__typename === 'WaterfallModule') { result.account.tranches.map(function (gqlWaterfallTranche) { var _a; if (gqlWaterfallTranche.recipient.__typename === 'Split') { response.upstreamSplits = (_a = response.upstreamSplits) !== null && _a !== void 0 ? _a : []; response.upstreamSplits.push((0, split_1.formatGqlSplit)(gqlWaterfallTranche.recipient)); } }); } if (allPassThroughWallets.length > 0) { allPassThroughWallets.map(function (gqlPassThroughWallet) { var _a; if (gqlPassThroughWallet.passThroughAccount.__typename === 'Split') { response.upstreamSplits = (_a = response.upstreamSplits) !== null && _a !== void 0 ? _a : []; response.upstreamSplits.push((0, split_1.formatGqlSplit)(gqlPassThroughWallet.passThroughAccount)); gqlPassThroughWallet.passThroughAccount.recipients.map(function (gqlRecipient) { var _a; if (gqlRecipient.account.__typename === 'Swapper') { response.upstreamSwappers = (_a = response.upstreamSwappers) !== null && _a !== void 0 ? _a : []; response.upstreamSwappers.push((0, swapper_1.formatGqlSwapper)(gqlRecipient.account)); } }); } }); } } return [2 /*return*/, response]; } }); }); }; DataClient.prototype._getUserBalancesByContract = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response, contractEarnings; var chainId = _b.chainId, userAddress = _b.userAddress, contractAddresses = _b.contractAddresses; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this._loadAccount(userAddress, chainId)]; case 1: response = _c.sent(); if (!response) throw new errors_1.AccountNotFoundError('user', userAddress, chainId); contractEarnings = (0, user_1.formatContractEarnings)(response.contractEarnings, contractAddresses); return [2 /*return*/, { contractEarnings: contractEarnings, }]; } }); }); }; DataClient.prototype._getAccountBalances = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionPublicClient, response, withdrawn, distributed, splitmainBalances, warehouseBalances, tokenList, balances, transferredErc20Tokens, customTokens, fullTokenList, allTokens, filteredBalances; var _c, _d, _e; var chainId = _b.chainId, accountAddress = _b.accountAddress, includeActiveBalances = _b.includeActiveBalances, erc20TokenList = _b.erc20TokenList; return __generator(this, function (_f) { switch (_f.label) { case 0: functionPublicClient = this._getPublicClient(chainId); return [4 /*yield*/, this._loadAccount(accountAddress, chainId)]; case 1: response = _f.sent(); if (!response) throw new errors_1.AccountNotFoundError('account', accountAddress, chainId); withdrawn = response.type === 'user' ? (0, user_1.formatAccountBalances)(response.withdrawn) : {}; distributed = (0, user_1.formatAccountBalances)(response.distributions); if (!includeActiveBalances) { return [2 /*return*/, { withdrawn: withdrawn, distributed: distributed, }]; } splitmainBalances = (0, user_1.formatAccountBalances)(response.splitmainBalances); warehouseBalances = (0, user_1.formatAccountBalances)(response.warehouseBalances); if (response.type === 'user') { return [2 /*return*/, { withdrawn: withdrawn, distributed: distributed, activeBalances: (0, utils_1.mergeFormattedTokenBalances)([ splitmainBalances, warehouseBalances, ]), }]; } // Need to fetch current balance. Handle alchemy/infura with logs, and all other rpc's with token list if (!functionPublicClient) throw new errors_1.MissingPublicClientError('Public client required to get active balances. Please update your call to the client constructor, or set includeActiveBalances to false'); if (((_c = functionPublicClient.chain) === null || _c === void 0 ? void 0 : _c.id) !== chainId) { throw new errors_1.InvalidArgumentError("Public client is set to chain id ".concat((_d = functionPublicClient.chain) === null || _d === void 0 ? void 0 : _d.id, ", but active balances are being fetched on chain ").concat(chainId, ". Active balances can only be fetched on the same chain as the public client.")); } tokenList = erc20TokenList !== null && erc20TokenList !== void 0 ? erc20TokenList : []; if (!(erc20TokenList === undefined && (0, utils_1.isAlchemyPublicClient)(functionPublicClient))) return [3 /*break*/, 3]; return [4 /*yield*/, (0, utils_1.fetchContractBalancesWithAlchemy)(chainId, accountAddress, functionPublicClient)]; case 2: // If no token list passed in and we're using alchemy, fetch all balances with alchemy's custom api balances = _f.sent(); return [3 /*break*/, 7]; case 3: if (!(erc20TokenList === undefined)) return [3 /*break*/, 5]; // If no token list passed in, make sure the public client supports logs and then fetch all erc20 tokens if (!(0, utils_1.isLogsPublicClient)(functionPublicClient)) throw new errors_1.InvalidArgumentError('Token list required if public client is not alchemy or infura'); return [4 /*yield*/, (0, utils_1.fetchERC20TransferredTokens)(chainId, functionPublicClient, accountAddress)]; case 4: transferredErc20Tokens = _f.sent(); tokenList.push.apply(tokenList, __spreadArray([], __read(transferredErc20Tokens), false)); _f.label = 5; case 5: customTokens = (_e = Object.keys(distributed)) !== null && _e !== void 0 ? _e : []; fullTokenList = Array.from(new Set(__spreadArray([viem_1.zeroAddress], __read(tokenList), false).concat(Object.keys(splitmainBalances)) .concat(Object.keys(warehouseBalances)) .concat(customTokens) .map(function (token) { return (0, viem_1.getAddress)(token); }))); return [4 /*yield*/, (0, utils_1.fetchActiveBalances)(chainId, accountAddress, functionPublicClient, fullTokenList)]; case 6: balances = _f.sent(); _f.label = 7; case 7: allTokens = Array.from(new Set(Object.keys(balances) .concat(Object.keys(splitmainBalances)) .concat(Object.keys(warehouseBalances)))); filteredBalances = allTokens.reduce(function (acc, token) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; var splitmainBalanceAmount = (_b = (_a = splitmainBalances[token]) === null || _a === void 0 ? void 0 : _a.rawAmount) !== null && _b !== void 0 ? _b : BigInt(0); var warehouseBalanceAmount = (_d = (_c = warehouseBalances[token]) === null || _c === void 0 ? void 0 : _c.rawAmount) !== null && _d !== void 0 ? _d : BigInt(0); var contractBalanceAmount = (_f = (_e = balances[token]) === null || _e === void 0 ? void 0 : _e.rawAmount) !== null && _f !== void 0 ? _f : BigInt(0); // SplitMain leaves a balance of 1 for gas efficiency in internal balances. // Splits leave a balance of 1 (for erc20) for gas efficiency var tokenBalance = (splitmainBalanceAmount > BigInt(1) ? splitmainBalanceAmount : BigInt(0)) + (warehouseBalanceAmount > BigInt(1) ? warehouseBalanceAmount : BigInt(0)) + (contractBalanceAmount > BigInt(1) ? contractBalanceAmount : BigInt(0)); var symbol = (_k = (_h = (_g = splitmainBalances[token]) === null || _g === void 0 ? void 0 : _g.symbol) !== null && _h !== void 0 ? _h : (_j = warehouseBalances[token]) === null || _j === void 0 ? void 0 : _j.symbol) !== null && _k !== void 0 ? _k : (_l = balances[token]) === null || _l === void 0 ? void 0 : _l.symbol; var decimals = (_q = (_o = (_m = splitmainBalances[token]) === null || _m === void 0 ? void 0 : _m.decimals) !== null && _o !== void 0 ? _o : (_p = warehouseBalances[token]) === null || _p === void 0 ? void 0 : _p.decimals) !== null && _q !== void 0 ? _q : (_r = balances[token]) === null || _r === void 0 ? void 0 : _r.decimals; var formattedAmount = (0, utils_1.fromBigIntToTokenValue)(tokenBalance, decimals); if (tokenBalance > BigInt(0)) acc[token] = { rawAmount: tokenBalance, formattedAmount: formattedAmount, symbol: symbol, decimals: decimals, }; return acc; }, {}); return [2 /*return*/, { withdrawn: withdrawn, distributed: distributed, activeBalances: filteredBalances, }]; } }); }); }; DataClient.prototype.getContractEarnings = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var _c, distributed, activeBalances; var chainId = _b.chainId, contractAddress = _b.contractAddress, _d = _b.includeActiveBalances, includeActiveBalances = _d === void 0 ? true : _d, erc20TokenList = _b.erc20TokenList; return __generator(this, function (_e) { switch (_e.label) { case 0: (0, utils_1.validateAddress)(contractAddress); if (includeActiveBalances) this._requirePublicClient(chainId); return [4 /*yield*/, this._getAccountBalances({ chainId: chainId, accountAddress: (0, viem_1.getAddress)(contractAddress), includeActiveBalances: includeActiveBalances, erc20TokenList: erc20TokenList, })]; case 1: _c = _e.sent(), distributed = _c.distributed, activeBalances = _c.activeBalances; if (!includeActiveBalances) return [2 /*return*/, { distributed: distributed }]; return [2 /*return*/, { distributed: distributed, activeBalances: activeBalances }]; } }); }); }; DataClient.prototype.getSplitMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, splitAddress = _b.splitAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(splitAddress); return [4 /*yield*/, this._loadAccount(splitAddress, chainId)]; case 1: response = _c.sent(); if (!response || (response.type !== 'split' && response.type !== 'splitV2' && response.type !== 'splitV2o1')) throw new errors_1.AccountNotFoundError('split', splitAddress, chainId); return [4 /*yield*/, this.formatSplit(response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; DataClient.prototype.getAccountMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, accountAddress = _b.accountAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(accountAddress); this._requirePublicClient(chainId); return [4 /*yield*/, this._loadAccount(accountAddress, chainId)]; case 1: response = _c.sent(); if (!response) throw new errors_1.AccountNotFoundError('account', accountAddress, chainId); return [4 /*yield*/, this._formatAccount(chainId, response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; // Graphql read actions DataClient.prototype.getRelatedSplits = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response, _c, receivingFrom, controlling, pendingControl; var _this = this; var chainId = _b.chainId, address = _b.address; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, utils_1.validateAddress)(address); return [4 /*yield*/, this._loadFullAccount(address, chainId)]; case 1: response = _d.sent(); return [4 /*yield*/, Promise.all([ Promise.all(response.upstreamSplits ? response.upstreamSplits.map(function (recipient) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.formatSplit(recipient)]; }); }); }) : []), Promise.all(response.controllingSplits ? response.controllingSplits.map(function (recipient) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.formatSplit(recipient)]; }); }); }) : []), Promise.all(response.pendingControlSplits ? response.pendingControlSplits.map(function (recipient) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.formatSplit(recipient)]; }); }); }) : []), ])]; case 2: _c = __read.apply(void 0, [_d.sent(), 3]), receivingFrom = _c[0], controlling = _c[1], pendingControl = _c[2]; return [2 /*return*/, { receivingFrom: receivingFrom, controlling: controlling, pendingControl: pendingControl, }]; } }); }); }; DataClient.prototype.getSplitEarnings = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var _c, distributed, activeBalances; var chainId = _b.chainId, splitAddress = _b.splitAddress, _d = _b.includeActiveBalances, includeActiveBalances = _d === void 0 ? true : _d, erc20TokenList = _b.erc20TokenList; return __generator(this, function (_e) { switch (_e.label) { case 0: (0, utils_1.validateAddress)(splitAddress); if (includeActiveBalances) this._requirePublicClient(chainId); return [4 /*yield*/, this._getAccountBalances({ chainId: chainId, accountAddress: (0, viem_1.getAddress)(splitAddress), includeActiveBalances: includeActiveBalances, erc20TokenList: erc20TokenList, })]; case 1: _c = _e.sent(), distributed = _c.distributed, activeBalances = _c.activeBalances; if (!includeActiveBalances) return [2 /*return*/, { distributed: distributed }]; return [2 /*return*/, { distributed: distributed, activeBalances: activeBalances }]; } }); }); }; DataClient.prototype.getUserEarnings = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var _c, withdrawn, activeBalances; var chainId = _b.chainId, userAddress = _b.userAddress; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, utils_1.validateAddress)(userAddress); return [4 /*yield*/, this._getAccountBalances({ chainId: chainId, accountAddress: (0, viem_1.getAddress)(userAddress), includeActiveBalances: true, })]; case 1: _c = _d.sent(), withdrawn = _c.withdrawn, activeBalances = _c.activeBalances; if (!activeBalances) throw new Error('Missing active balances'); return [2 /*return*/, { withdrawn: withdrawn, activeBalances: activeBalances }]; } }); }); }; DataClient.prototype.getUserEarningsByContract = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var contractEarnings, _c, withdrawn, activeBalances; var chainId = _b.chainId, userAddress = _b.userAddress, contractAddresses = _b.contractAddresses; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, utils_1.validateAddress)(userAddress); if (contractAddresses) { contractAddresses.map(function (contractAddress) { return (0, utils_1.validateAddress)(contractAddress); }); } return [4 /*yield*/, this._getUserBalancesByContract({ chainId: chainId, userAddress: userAddress, contractAddresses: contractAddresses, })]; case 1: contractEarnings = (_d.sent()).contractEarnings; _c = __read(Object.values(contractEarnings).reduce(function (acc, _a) { var contractWithdrawn = _a.withdrawn, contractActiveBalances = _a.activeBalances; Object.keys(contractWithdrawn).map(function (tokenId) { if (!acc[0][tokenId]) acc[0][tokenId] = { symbol: contractWithdrawn[tokenId].symbol, decimals: contractWithdrawn[tokenId].decimals, rawAmount: BigInt(0), formattedAmount: '0', }; var rawAmount = acc[0][tokenId].rawAmount + contractWithdrawn[tokenId].rawAmount; var formattedAmount = (0, utils_1.fromBigIntToTokenValue)(rawAmount, contractWithdrawn[tokenId].decimals); acc[0][tokenId].rawAmount = rawAmount; acc[0][tokenId].formattedAmount = formattedAmount; }); Object.keys(contractActiveBalances).map(function (tokenId) { if (!acc[1][tokenId]) acc[1][tokenId] = { symbol: contractActiveBalances[tokenId].symbol, decimals: contractActiveBalances[tokenId].decimals, rawAmount: BigInt(0), formattedAmount: '0', }; var rawAmount = acc[1][tokenId].rawAmount + contractActiveBalances[tokenId].rawAmount; var formattedAmount = (0, utils_1.fromBigIntToTokenValue)(rawAmount, contractActiveBalances[tokenId].decimals); acc[1][tokenId].rawAmount = rawAmount; acc[1][tokenId].formattedAmount = formattedAmount; }); return acc; }, [{}, {}]), 2), withdrawn = _c[0], activeBalances = _c[1]; return [2 /*return*/, { withdrawn: withdrawn, activeBalances: activeBalances, earningsByContract: contractEarnings, }]; } }); }); }; DataClient.prototype.getLiquidSplitMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, liquidSplitAddress = _b.liquidSplitAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(liquidSplitAddress); return [4 /*yield*/, this._loadAccount(liquidSplitAddress, chainId)]; case 1: response = _c.sent(); if (!response || response.type !== 'liquidSplit') throw new errors_1.AccountNotFoundError('liquid split', liquidSplitAddress, chainId); return [4 /*yield*/, this.formatLiquidSplit(chainId, response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; DataClient.prototype.getSwapperMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, swapperAddress = _b.swapperAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(swapperAddress); return [4 /*yield*/, this._loadAccount(swapperAddress, chainId)]; case 1: response = _c.sent(); if (!response || response.type !== 'swapper') throw new errors_1.AccountNotFoundError('swapper', swapperAddress, chainId); return [4 /*yield*/, this.formatSwapper(response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; DataClient.prototype.getVestingMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, vestingModuleAddress = _b.vestingModuleAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(vestingModuleAddress); return [4 /*yield*/, this._loadAccount(vestingModuleAddress, chainId)]; case 1: response = _c.sent(); if (!response || response.type !== 'vesting') throw new errors_1.AccountNotFoundError('vesting module', vestingModuleAddress, chainId); return [4 /*yield*/, this.formatVestingModule(chainId, response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; DataClient.prototype.getWaterfallMetadata = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var response; var chainId = _b.chainId, waterfallModuleAddress = _b.waterfallModuleAddress; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, utils_1.validateAddress)(waterfallModuleAddress); return [4 /*yield*/, this._loadAccount(waterfallModuleAddress, chainId)]; case 1: response = _c.sent(); if (!response || response.type != 'waterfall') throw new errors_1.AccountNotFoundError('waterfall module', waterfallModuleAddress, chainId); return [4 /*yield*/, this.formatWaterfallModule(chainId, response)]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; // Helper functions DataClient.prototype._formatAccount = function (chainId, gqlAccount) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!gqlAccount) return [2 /*return*/]; if (!(gqlAccount.type === 'split')) return [3 /*break*/, 2]; return [4 /*yield*/, this.formatSplit(gqlAccount)]; case 1: return [2 /*return*/, _a.sent()]; case 2: if (!(gqlAccount.type === 'waterfall')) return [3 /*break*/, 4]; return [4 /*yield*/, this.formatWaterfallModule(chainId, gqlAccount)]; case 3: return [2 /*return*/, _a.sent()]; case 4: if (!(gqlAccount.type === 'liquidSplit')) return [3 /*break*/, 6]; return [4 /*yield*/, this.formatLiquidSplit(chainId, gqlAccount)]; case 5: return [2 /*return*/, _a.sent()]; case 6: if (!(gqlAccount.type === 'swapper')) return [3 /*break*/, 8]; return [4 /*yield*/, this.formatSwapper(gqlAccount)]; case 7: return [2 /*return*/, _a.sent()]; case 8: return [2 /*return*/]; } }); }); }; DataClient.prototype.formatWaterfallModule = function (chainId, gqlWaterfallModule) { return __awaiter(this, void 0, void 0, function () { var publicClient, tokenData, waterfallModule, ensRecipients; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this._requirePublicClient(chainId); publicClient = this._getPublicClient(chainId); return [4 /*yield*/, (0, utils_1.getTokenData)(chainId, (0, viem_1.getAddress)(gqlWaterfallModule.token), publicClient)]; case 1: tokenData = _b.sent(); waterfallModule = (0, waterfall_1.protectedFormatWaterfallModule)(gqlWaterfallModule, tokenData.symbol, tokenData.decimals); if (!this._includeEnsNames) return [3 /*break*/, 3]; ensRecipients = waterfallModule.tranches .map(function (tranche) { return tranche.recipient; }) .concat(waterfallModule.nonWaterfallRecipient ? [waterfallModule.nonWaterfallRecipient] : []); return [4 /*yield*/, (0, utils_1.addEnsNames)((_a = this._ensPublicClient) !== null && _a !== void 0 ? _a : publicClient, ensRecipients)]; case 2: _b.sent(); _b.label = 3; case 3: return [2 /*return*/, waterfallModule]; } }); }); }; DataClient.prototype.formatVestingModule = function (chainId, gqlVestingModule) { return __awaiter(this, void 0, void 0, function () { var publicClient, tokenIds, tokenData, vestingModule; var _this = this; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: this._requirePublicClient(chainId); publicClient = this._getPublicClient(chainId); tokenIds = Array.from(new Set((_b = (_a = gqlVestingModule.streams) === null || _a === void 0 ? void 0 : _a.map(function (stream) { return stream.token; })) !== null && _b !== void 0 ? _b : [])); tokenData = {}; return [4 /*yield*/, Promise.all(tokenIds.map(function (token) { return __awaiter(_this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, utils_1.getTokenData)(chainId, (0, viem_1.getAddress)(token), publicClient)]; case 1: result = _a.sent(); tokenData[token] = result; return [2 /*return*/]; } }); }); }))]; case 1: _d.sent(); vestingModule = (0, vesting_1.protectedFormatVestingModule)(gqlVestingModule, tokenData); if (!this._includeEnsNames) return [3 /*break*/, 3]; return [4 /*yield*/, (0, utils_1.addEnsNames)((_c = this._ensPublicClient) !== null && _c !== void 0 ? _c : publicClient, [ vestingModule.beneficiary, ])]; case 2: _d.sent(); _d.label = 3; case 3: return [2 /*return*/, vestingModule]; } }); }); }; DataClient.prototype.formatSwapper = function (gqlSwapper) { return __awaiter(this, void 0, void 0, function () { var swapper, ensRecipients; return __generator(this, function (_a) { switch (_a.label) { case 0: swapper = (0, swapper_1.protectedFormatSwapper)(gqlSwapper); if (!this._includeEnsNames) return [3 /*break*/, 2]; if (!this._ensPublicClient) throw new Error(); ensRecipients = [swapper.beneficiary].concat(swapper.owner ? [swapper.owner] : []); return [4 /*yield*/, (0, utils_1.addEnsNames)(this._ensPublicClient, ensRecipients)]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, swapper]; } }); }); }; DataClient.prototype.formatLiquidSplit = function (chainId, gqlLiquidSplit) { return __awaiter(this, void 0, void 0, function () { var liquidSplit; return __generator(this, function (_a) { switch (_a.label) { case 0: this._requirePublicClient(chainId); liquidSplit = (0, liquid_1.protectedFormatLiquidSplit)(gqlLiquidSplit); if (!this._includeEnsNames) return [3 /*break*/, 2]; return [4 /*yield*/, (0, utils_1.addEnsNames)(this._ensPublicClient, liquidSplit.holders.map(function (holder) { return holder.recipient; }))]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, liquidSplit]; } }); }); }; DataClient.prototype.formatSplit = function (gqlSplit) { return __awaiter(this, void 0, void