UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

369 lines 19.6 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 __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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplatesClient = void 0; var viem_1 = require("viem"); var base_1 = require("./base"); var constants_1 = require("../constants"); var recoupFactory_1 = require("../constants/abi/recoupFactory"); var diversifierFactory_1 = require("../constants/abi/diversifierFactory"); var errors_1 = require("../errors"); var mixin_1 = require("./mixin"); var utils_1 = require("../utils"); var validation_1 = require("../utils/validation"); var TemplatesTransactions = /** @class */ (function (_super) { __extends(TemplatesTransactions, _super); function TemplatesTransactions(transactionClientArgs) { return _super.call(this, __assign({ supportedChainIds: constants_1.TEMPLATES_CHAIN_IDS }, transactionClientArgs)) || this; } TemplatesTransactions.prototype._createRecoupTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, publicClient, _c, recoupTranches, trancheSizes, formattedNonWaterfallRecipientTrancheIndex, result; var token = _b.token, tranches = _b.tranches, _d = _b.nonWaterfallRecipientAddress, nonWaterfallRecipientAddress = _d === void 0 ? viem_1.zeroAddress : _d, _e = _b.nonWaterfallRecipientTrancheIndex, nonWaterfallRecipientTrancheIndex = _e === void 0 ? undefined : _e, chainId = _b.chainId, _f = _b.transactionOverrides, transactionOverrides = _f === void 0 ? {} : _f; return __generator(this, function (_g) { switch (_g.label) { case 0: (0, validation_1.validateAddress)(token); (0, validation_1.validateAddress)(nonWaterfallRecipientAddress); (0, validation_1.validateRecoupTranches)(tranches); (0, validation_1.validateRecoupNonWaterfallRecipient)(tranches.length, nonWaterfallRecipientAddress, nonWaterfallRecipientTrancheIndex); if (this._shouldRequireWalletClient) this._requireWalletClient(); functionChainId = this._getFunctionChainId(chainId); publicClient = this._getPublicClient(functionChainId); return [4 /*yield*/, (0, utils_1.getRecoupTranchesAndSizes)(functionChainId, (0, viem_1.getAddress)(token), tranches, publicClient)]; case 1: _c = __read.apply(void 0, [_g.sent(), 2]), recoupTranches = _c[0], trancheSizes = _c[1]; formattedNonWaterfallRecipientTrancheIndex = nonWaterfallRecipientTrancheIndex === undefined ? recoupTranches.length : nonWaterfallRecipientTrancheIndex; return [4 /*yield*/, this._executeContractFunction({ contractAddress: (0, constants_1.getRecoupAddress)(functionChainId), contractAbi: recoupFactory_1.recoupFactoryAbi, functionName: 'createRecoup', functionArgs: [ token, nonWaterfallRecipientAddress, formattedNonWaterfallRecipientTrancheIndex, recoupTranches, trancheSizes, ], transactionOverrides: transactionOverrides, })]; case 2: result = _g.sent(); return [2 /*return*/, result]; } }); }); }; TemplatesTransactions.prototype._createDiversifierTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, diversifierRecipients, formattedOracleParams, result; var owner = _b.owner, _c = _b.paused, paused = _c === void 0 ? false : _c, oracleParams = _b.oracleParams, recipients = _b.recipients, chainId = _b.chainId, _d = _b.transactionOverrides, transactionOverrides = _d === void 0 ? {} : _d; return __generator(this, function (_e) { switch (_e.label) { case 0: (0, validation_1.validateAddress)(owner); (0, validation_1.validateOracleParams)(oracleParams); (0, validation_1.validateDiversifierRecipients)(recipients); if (this._shouldRequireWalletClient) this._requireWalletClient(); functionChainId = this._getFunctionChainId(chainId); if (!constants_1.DIVERSIFIER_CHAIN_IDS.includes(functionChainId)) throw new errors_1.UnsupportedChainIdError(functionChainId, constants_1.DIVERSIFIER_CHAIN_IDS); diversifierRecipients = (0, utils_1.getDiversifierRecipients)(recipients); formattedOracleParams = (0, utils_1.getFormattedOracleParams)(oracleParams); return [4 /*yield*/, this._executeContractFunction({ contractAddress: (0, constants_1.getDiversifierFactoryAddress)(functionChainId), contractAbi: diversifierFactory_1.diversifierFactoryAbi, functionName: 'createDiversifier', functionArgs: [ [owner, paused, formattedOracleParams, diversifierRecipients], ], transactionOverrides: transactionOverrides, })]; case 1: result = _e.sent(); return [2 /*return*/, result]; } }); }); }; return TemplatesTransactions; }(base_1.BaseTransactions)); // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging var TemplatesClient = /** @class */ (function (_super) { __extends(TemplatesClient, _super); function TemplatesClient(clientArgs) { var _this = _super.call(this, __assign({ transactionType: constants_1.TransactionType.Transaction }, clientArgs)) || this; _this.eventTopics = { // TODO: add others here? create waterfall, create split, etc. createRecoup: [ (0, viem_1.encodeEventTopics)({ abi: recoupFactory_1.recoupFactoryAbi, eventName: 'CreateRecoup', })[0], ], createDiversifier: [ (0, viem_1.encodeEventTopics)({ abi: diversifierFactory_1.diversifierFactoryAbi, eventName: 'CreateDiversifier', })[0], ], }; _this.callData = new TemplatesCallData(clientArgs); _this.estimateGas = new TemplatesGasEstimates(clientArgs); return _this; } // Write actions TemplatesClient.prototype._submitCreateRecoupTransaction = function (createRecoupArgs) { return __awaiter(this, void 0, void 0, function () { var txHash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createRecoupTransaction(createRecoupArgs)]; case 1: txHash = _a.sent(); if (!this._isContractTransaction(txHash)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: txHash }]; } }); }); }; TemplatesClient.prototype.createRecoup = function (createRecoupArgs) { return __awaiter(this, void 0, void 0, function () { var txHash, events, event, log; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._submitCreateRecoupTransaction(createRecoupArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: this.eventTopics.createRecoup, })]; case 2: events = _a.sent(); event = events.length > 0 ? events[0] : undefined; if (event) { log = (0, viem_1.decodeEventLog)({ abi: recoupFactory_1.recoupFactoryAbi, data: event.data, topics: event.topics, }); return [2 /*return*/, { waterfallModuleAddress: log.args.waterfallModule, event: event, }]; } throw new errors_1.TransactionFailedError(); } }); }); }; TemplatesClient.prototype._submitCreateDiversifierTransaction = function (createDiversifierArgs) { return __awaiter(this, void 0, void 0, function () { var txHash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createDiversifierTransaction(createDiversifierArgs)]; case 1: txHash = _a.sent(); if (!this._isContractTransaction(txHash)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: txHash }]; } }); }); }; TemplatesClient.prototype.createDiversifier = function (createDiversifierArgs) { return __awaiter(this, void 0, void 0, function () { var txHash, events, event, log; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._submitCreateDiversifierTransaction(createDiversifierArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: this.eventTopics.createDiversifier, })]; case 2: events = _a.sent(); event = events.length > 0 ? events[0] : undefined; if (event) { log = (0, viem_1.decodeEventLog)({ abi: diversifierFactory_1.diversifierFactoryAbi, data: event.data, topics: event.topics, }); return [2 /*return*/, { passThroughWalletAddress: log.args.diversifier, event: event, }]; } throw new errors_1.TransactionFailedError(); } }); }); }; return TemplatesClient; }(TemplatesTransactions)); exports.TemplatesClient = TemplatesClient; (0, mixin_1.applyMixins)(TemplatesClient, [base_1.BaseClientMixin]); // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging var TemplatesGasEstimates = /** @class */ (function (_super) { __extends(TemplatesGasEstimates, _super); function TemplatesGasEstimates(clientArgs) { return _super.call(this, __assign({ transactionType: constants_1.TransactionType.GasEstimate }, clientArgs)) || this; } TemplatesGasEstimates.prototype.createRecoup = function (createRecoupArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createRecoupTransaction(createRecoupArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; TemplatesGasEstimates.prototype.createDiversifier = function (createDiversifierArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createDiversifierTransaction(createDiversifierArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; return TemplatesGasEstimates; }(TemplatesTransactions)); (0, mixin_1.applyMixins)(TemplatesGasEstimates, [base_1.BaseGasEstimatesMixin]); var TemplatesCallData = /** @class */ (function (_super) { __extends(TemplatesCallData, _super); function TemplatesCallData(clientArgs) { return _super.call(this, __assign({ transactionType: constants_1.TransactionType.CallData }, clientArgs)) || this; } TemplatesCallData.prototype.createRecoup = function (createRecoupArgs) { return __awaiter(this, void 0, void 0, function () { var callData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createRecoupTransaction(createRecoupArgs)]; case 1: callData = _a.sent(); if (!this._isCallData(callData)) throw new Error('Invalid response'); return [2 /*return*/, callData]; } }); }); }; TemplatesCallData.prototype.createDiversifier = function (createDiversifierArgs) { return __awaiter(this, void 0, void 0, function () { var callData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createDiversifierTransaction(createDiversifierArgs)]; case 1: callData = _a.sent(); if (!this._isCallData(callData)) throw new Error('Invalid response'); return [2 /*return*/, callData]; } }); }); }; return TemplatesCallData; }(TemplatesTransactions)); //# sourceMappingURL=templates.js.map