UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

364 lines 19.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __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; }; return __assign.apply(this, arguments); }; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseGasEstimatesMixin = exports.BaseClientMixin = exports.BaseTransactions = void 0; var viem_1 = require("viem"); var constants_1 = require("../constants"); var multicall_1 = require("../constants/abi/multicall"); var errors_1 = require("../errors"); var data_1 = require("./data"); var BaseClient = /** @class */ (function () { function BaseClient(_a) { var chainId = _a.chainId, publicClient = _a.publicClient, publicClients = _a.publicClients, ensPublicClient = _a.ensPublicClient, walletClient = _a.walletClient, apiConfig = _a.apiConfig, supportedChainIds = _a.supportedChainIds, _b = _a.includeEnsNames, includeEnsNames = _b === void 0 ? false : _b; var _c, _d; if (includeEnsNames && !publicClient && !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._chainId = chainId; this._walletClient = walletClient; this._includeEnsNames = includeEnsNames; this._apiConfig = apiConfig; this._supportedChainIds = supportedChainIds; if (apiConfig) { this._dataClient = new data_1.DataClient({ publicClient: publicClient, publicClients: publicClients, ensPublicClient: ensPublicClient, apiConfig: apiConfig, includeEnsNames: includeEnsNames, }); } } BaseClient.prototype._requireDataClient = function () { if (!this._dataClient) throw new errors_1.MissingDataClientError('API config required to perform this action, please update your call to the constructor'); }; BaseClient.prototype._requirePublicClient = function (chainId) { this._getPublicClient(chainId); }; BaseClient.prototype._requireWalletClient = function () { var _a; if (!this._walletClient) throw new errors_1.MissingWalletClientError('Wallet client required to perform this action, please update your call to the constructor'); if (!this._walletClient.account) throw new errors_1.MissingWalletClientError('Wallet client must have an account attached to it to perform this action, please update your wallet client passed into the constructor'); var chainId = (_a = this._walletClient.chain) === null || _a === void 0 ? void 0 : _a.id; if (!chainId) throw new Error('Wallet client must have a chain attached to it'); if (!this._supportedChainIds.includes(chainId)) throw new errors_1.UnsupportedChainIdError(chainId, this._supportedChainIds); this._requirePublicClient(chainId); }; BaseClient.prototype._getPublicClient = function (chainId) { var _a, _b; if (!this._supportedChainIds.includes(chainId)) throw new errors_1.UnsupportedChainIdError(chainId, this._supportedChainIds); 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")); if (((_a = this._publicClient.chain) === null || _a === void 0 ? void 0 : _a.id) !== chainId) { throw new errors_1.MissingPublicClientError("Public client is for chain ".concat((_b = this._publicClient.chain) === null || _b === void 0 ? void 0 : _b.id, ", but attempting to use it on chain ").concat(chainId)); } return this._publicClient; }; return BaseClient; }()); var BaseTransactions = /** @class */ (function (_super) { __extends(BaseTransactions, _super); function BaseTransactions(_a) { var _this = this; var transactionType = _a.transactionType, baseClientArgs = __rest(_a, ["transactionType"]); _this = _super.call(this, baseClientArgs) || this; _this._transactionType = transactionType; _this._shouldRequireWalletClient = [ constants_1.TransactionType.GasEstimate, constants_1.TransactionType.Transaction, ].includes(transactionType); return _this; } BaseTransactions.prototype._executeContractFunction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var publicClient, gasEstimate, calldata, publicClient, request, txHash; var _c, _d; var contractAddress = _b.contractAddress, contractAbi = _b.contractAbi, functionName = _b.functionName, functionArgs = _b.functionArgs, transactionOverrides = _b.transactionOverrides, value = _b.value; return __generator(this, function (_e) { switch (_e.label) { case 0: if (this._shouldRequireWalletClient) { this._requireWalletClient(); } if (!(this._transactionType === constants_1.TransactionType.GasEstimate)) return [3 /*break*/, 2]; if (!((_c = this._walletClient) === null || _c === void 0 ? void 0 : _c.account)) throw new Error(); publicClient = this._getPublicClient(this._walletClient.chain.id); return [4 /*yield*/, publicClient.estimateContractGas(__assign({ address: contractAddress, abi: contractAbi, functionName: functionName, account: this._walletClient.account, args: functionArgs !== null && functionArgs !== void 0 ? functionArgs : [], value: value }, transactionOverrides))]; case 1: gasEstimate = _e.sent(); return [2 /*return*/, gasEstimate]; case 2: if (!(this._transactionType === constants_1.TransactionType.CallData)) return [3 /*break*/, 3]; calldata = (0, viem_1.encodeFunctionData)({ abi: contractAbi, functionName: functionName, args: functionArgs !== null && functionArgs !== void 0 ? functionArgs : [], }); return [2 /*return*/, { address: contractAddress, data: calldata, value: value, }]; case 3: if (!(this._transactionType === constants_1.TransactionType.Transaction)) return [3 /*break*/, 6]; if (!((_d = this._walletClient) === null || _d === void 0 ? void 0 : _d.account)) throw new Error(); publicClient = this._getPublicClient(this._walletClient.chain.id); return [4 /*yield*/, publicClient.simulateContract(__assign({ address: contractAddress, abi: contractAbi, functionName: functionName, account: this._walletClient.account, args: functionArgs !== null && functionArgs !== void 0 ? functionArgs : [], value: value }, transactionOverrides))]; case 4: request = (_e.sent()).request; return [4 /*yield*/, this._walletClient.writeContract(request)]; case 5: txHash = _e.sent(); return [2 /*return*/, txHash]; case 6: throw new Error("Unknown transaction type: ".concat(this._transactionType)); } }); }); }; BaseTransactions.prototype._isContractTransaction = function (txHash) { return typeof txHash === 'string'; }; BaseTransactions.prototype._isBigInt = function (gasEstimate) { return typeof gasEstimate === 'bigint'; }; BaseTransactions.prototype._isCallData = function (callData) { if (callData instanceof BigInt) return false; if (typeof callData === 'string') return false; return true; }; BaseTransactions.prototype._getFunctionChainId = function (argumentChainId) { if (this._shouldRequireWalletClient) { if (argumentChainId !== undefined && this._walletClient.chain.id !== argumentChainId) { throw new errors_1.InvalidArgumentError("Passed in chain id ".concat(argumentChainId, " does not match walletClient chain id: ").concat(this._walletClient.chain.id, ".")); } return this._walletClient.chain.id; } return this._getReadOnlyFunctionChainId(argumentChainId); }; // Ignore wallet client here BaseTransactions.prototype._getReadOnlyFunctionChainId = function (argumentChainId) { var functionChainId = argumentChainId !== null && argumentChainId !== void 0 ? argumentChainId : this._chainId; if (!functionChainId) throw new errors_1.InvalidArgumentError('Please pass in the chainId you are using'); return functionChainId; }; BaseTransactions.prototype._multicallTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var callRequests, result; var calls = _b.calls, _c = _b.transactionOverrides, transactionOverrides = _c === void 0 ? {} : _c; return __generator(this, function (_d) { switch (_d.label) { case 0: this._requireWalletClient(); if (!this._walletClient) throw new Error(); callRequests = calls.map(function (call) { return { target: call.address, callData: call.data, }; }); return [4 /*yield*/, this._executeContractFunction({ contractAddress: constants_1.MULTICALL_3_ADDRESS, contractAbi: multicall_1.multicallAbi, functionName: 'aggregate', functionArgs: [callRequests], transactionOverrides: transactionOverrides, })]; case 1: result = _d.sent(); return [2 /*return*/, result]; } }); }); }; return BaseTransactions; }(BaseClient)); exports.BaseTransactions = BaseTransactions; var BaseClientMixin = /** @class */ (function (_super) { __extends(BaseClientMixin, _super); function BaseClientMixin() { return _super !== null && _super.apply(this, arguments) || this; } BaseClientMixin.prototype.getTransactionEvents = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var chainId, publicClient, transaction, events; var _c; var txHash = _b.txHash, eventTopics = _b.eventTopics, includeAll = _b.includeAll; return __generator(this, function (_d) { switch (_d.label) { case 0: this._requireWalletClient(); chainId = this._walletClient.chain.id; publicClient = this._getPublicClient(chainId); return [4 /*yield*/, publicClient.waitForTransactionReceipt({ hash: txHash, })]; case 1: transaction = _d.sent(); if (transaction.status === 'success') { events = (_c = transaction.logs) === null || _c === void 0 ? void 0 : _c.filter(function (log) { if (includeAll) return true; if (log.topics[0]) return eventTopics.includes(log.topics[0]); return false; }); return [2 /*return*/, events]; } return [2 /*return*/, []]; } }); }); }; BaseClientMixin.prototype._submitMulticallTransaction = function (multicallArgs) { return __awaiter(this, void 0, void 0, function () { var multicallResult; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._multicallTransaction(multicallArgs)]; case 1: multicallResult = _a.sent(); if (!this._isContractTransaction(multicallResult)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: multicallResult }]; } }); }); }; BaseClientMixin.prototype.multicall = function (multicallArgs) { return __awaiter(this, void 0, void 0, function () { var txHash, events; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._submitMulticallTransaction(multicallArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: [], includeAll: true, })]; case 2: events = _a.sent(); return [2 /*return*/, { events: events }]; } }); }); }; return BaseClientMixin; }(BaseTransactions)); exports.BaseClientMixin = BaseClientMixin; var BaseGasEstimatesMixin = /** @class */ (function (_super) { __extends(BaseGasEstimatesMixin, _super); function BaseGasEstimatesMixin() { return _super !== null && _super.apply(this, arguments) || this; } BaseGasEstimatesMixin.prototype.multicall = function (multicallArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._multicallTransaction(multicallArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; return BaseGasEstimatesMixin; }(BaseTransactions)); exports.BaseGasEstimatesMixin = BaseGasEstimatesMixin; //# sourceMappingURL=base.js.map