UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

542 lines 27 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.VestingClient = void 0; var viem_1 = require("viem"); var base_1 = require("./base"); var constants_1 = require("../constants"); var vestingFactory_1 = require("../constants/abi/vestingFactory"); var vesting_1 = require("../constants/abi/vesting"); var errors_1 = require("../errors"); var mixin_1 = require("./mixin"); var validation_1 = require("../utils/validation"); var VestingTransactions = /** @class */ (function (_super) { __extends(VestingTransactions, _super); function VestingTransactions(transactionClientArgs) { return _super.call(this, __assign({ supportedChainIds: constants_1.VESTING_CHAIN_IDS }, transactionClientArgs)) || this; } VestingTransactions.prototype._createVestingModuleTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, result; var beneficiary = _b.beneficiary, vestingPeriodSeconds = _b.vestingPeriodSeconds, chainId = _b.chainId, _c = _b.transactionOverrides, transactionOverrides = _c === void 0 ? {} : _c; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, validation_1.validateAddress)(beneficiary); (0, validation_1.validateVestingPeriod)(vestingPeriodSeconds); if (this._shouldRequireWalletClient) this._requireWalletClient(); functionChainId = this._getFunctionChainId(chainId); return [4 /*yield*/, this._executeContractFunction({ contractAddress: (0, constants_1.getVestingFactoryAddress)(functionChainId), contractAbi: vestingFactory_1.vestingFactoryAbi, functionName: 'createVestingModule', functionArgs: [beneficiary, vestingPeriodSeconds], transactionOverrides: transactionOverrides, })]; case 1: result = _d.sent(); return [2 /*return*/, result]; } }); }); }; VestingTransactions.prototype._startVestTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var result; var vestingModuleAddress = _b.vestingModuleAddress, tokens = _b.tokens, _c = _b.transactionOverrides, transactionOverrides = _c === void 0 ? {} : _c; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); tokens.map(function (token) { return (0, validation_1.validateAddress)(token); }); if (this._shouldRequireWalletClient) this._requireWalletClient(); return [4 /*yield*/, this._executeContractFunction({ contractAddress: (0, viem_1.getAddress)(vestingModuleAddress), contractAbi: vesting_1.vestingAbi, functionName: 'createVestingStreams', functionArgs: [tokens], transactionOverrides: transactionOverrides, })]; case 1: result = _d.sent(); return [2 /*return*/, result]; } }); }); }; VestingTransactions.prototype._releaseVestedFundsTransaction = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var result; var vestingModuleAddress = _b.vestingModuleAddress, streamIds = _b.streamIds, _c = _b.transactionOverrides, transactionOverrides = _c === void 0 ? {} : _c; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); if (this._shouldRequireWalletClient) this._requireWalletClient(); return [4 /*yield*/, this._executeContractFunction({ contractAddress: (0, viem_1.getAddress)(vestingModuleAddress), contractAbi: vesting_1.vestingAbi, functionName: 'releaseFromVesting', functionArgs: [streamIds], transactionOverrides: transactionOverrides, })]; case 1: result = _d.sent(); return [2 /*return*/, result]; } }); }); }; VestingTransactions.prototype._getVestingContract = function (vestingModule, chainId) { var publicClient = this._getPublicClient(chainId); return (0, viem_1.getContract)({ address: (0, viem_1.getAddress)(vestingModule), abi: vesting_1.vestingAbi, client: publicClient, }); }; VestingTransactions.prototype._getVestingFactoryContract = function (chainId) { var publicClient = this._getPublicClient(chainId); return (0, viem_1.getContract)({ address: (0, constants_1.getVestingFactoryAddress)(chainId), abi: vestingFactory_1.vestingFactoryAbi, client: publicClient, }); }; return VestingTransactions; }(base_1.BaseTransactions)); // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging var VestingClient = /** @class */ (function (_super) { __extends(VestingClient, _super); function VestingClient(clientArgs) { var _this = _super.call(this, __assign({ transactionType: constants_1.TransactionType.Transaction }, clientArgs)) || this; _this.eventTopics = { createVestingModule: [ (0, viem_1.encodeEventTopics)({ abi: vestingFactory_1.vestingFactoryAbi, eventName: 'CreateVestingModule', })[0], ], startVest: [ (0, viem_1.encodeEventTopics)({ abi: vesting_1.vestingAbi, eventName: 'CreateVestingStream', })[0], ], releaseVestedFunds: [ (0, viem_1.encodeEventTopics)({ abi: vesting_1.vestingAbi, eventName: 'ReleaseFromVestingStream', })[0], ], }; _this.callData = new VestingCallData(clientArgs); _this.estimateGas = new VestingGasEstimates(clientArgs); return _this; } // Write actions VestingClient.prototype._submitCreateVestingModuleTransaction = function (createVestingArgs) { return __awaiter(this, void 0, void 0, function () { var txHash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createVestingModuleTransaction(createVestingArgs)]; case 1: txHash = _a.sent(); if (!this._isContractTransaction(txHash)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: txHash }]; } }); }); }; VestingClient.prototype.createVestingModule = function (createVestingArgs) { 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._submitCreateVestingModuleTransaction(createVestingArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: this.eventTopics.createVestingModule, })]; case 2: events = _a.sent(); event = events.length > 0 ? events[0] : undefined; if (event) { log = (0, viem_1.decodeEventLog)({ abi: vestingFactory_1.vestingFactoryAbi, data: event.data, topics: event.topics, }); return [2 /*return*/, { vestingModuleAddress: log.args.vestingModule, event: event, }]; } throw new errors_1.TransactionFailedError(); } }); }); }; VestingClient.prototype._submitStartVestTransaction = function (startVestArgs) { return __awaiter(this, void 0, void 0, function () { var txHash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._startVestTransaction(startVestArgs)]; case 1: txHash = _a.sent(); if (!this._isContractTransaction(txHash)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: txHash }]; } }); }); }; VestingClient.prototype.startVest = function (startVestArgs) { 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._submitStartVestTransaction(startVestArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: this.eventTopics.startVest, })]; case 2: events = _a.sent(); return [2 /*return*/, { events: events }]; } }); }); }; VestingClient.prototype._submitReleaseVestedFundsTransaction = function (releaseFundsArgs) { return __awaiter(this, void 0, void 0, function () { var txHash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._releaseVestedFundsTransaction(releaseFundsArgs)]; case 1: txHash = _a.sent(); if (!this._isContractTransaction(txHash)) throw new Error('Invalid response'); return [2 /*return*/, { txHash: txHash }]; } }); }); }; VestingClient.prototype.releaseVestedFunds = function (releaseFundsArgs) { 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._submitReleaseVestedFundsTransaction(releaseFundsArgs)]; case 1: txHash = (_a.sent()).txHash; return [4 /*yield*/, this.getTransactionEvents({ txHash: txHash, eventTopics: this.eventTopics.releaseVestedFunds, })]; case 2: events = _a.sent(); return [2 /*return*/, { events: events }]; } }); }); }; // Read actions VestingClient.prototype.predictVestingModuleAddress = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, vestingModuleFactoryContract, _c, address, exists; var beneficiary = _b.beneficiary, vestingPeriodSeconds = _b.vestingPeriodSeconds, chainId = _b.chainId; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, validation_1.validateAddress)(beneficiary); (0, validation_1.validateVestingPeriod)(vestingPeriodSeconds); functionChainId = this._getReadOnlyFunctionChainId(chainId); vestingModuleFactoryContract = this._getVestingFactoryContract(functionChainId); return [4 /*yield*/, vestingModuleFactoryContract.read.predictVestingModuleAddress([ (0, viem_1.getAddress)(beneficiary), BigInt(vestingPeriodSeconds), ])]; case 1: _c = __read.apply(void 0, [_d.sent(), 2]), address = _c[0], exists = _c[1]; return [2 /*return*/, { address: address, exists: exists, }]; } }); }); }; VestingClient.prototype.getBeneficiary = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, vestingContract, beneficiary; var vestingModuleAddress = _b.vestingModuleAddress, chainId = _b.chainId; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); functionChainId = this._getReadOnlyFunctionChainId(chainId); vestingContract = this._getVestingContract(vestingModuleAddress, functionChainId); return [4 /*yield*/, vestingContract.read.beneficiary()]; case 1: beneficiary = _c.sent(); return [2 /*return*/, { beneficiary: beneficiary }]; } }); }); }; VestingClient.prototype.getVestingPeriod = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, vestingContract, vestingPeriod; var vestingModuleAddress = _b.vestingModuleAddress, chainId = _b.chainId; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); functionChainId = this._getReadOnlyFunctionChainId(chainId); vestingContract = this._getVestingContract(vestingModuleAddress, functionChainId); return [4 /*yield*/, vestingContract.read.vestingPeriod()]; case 1: vestingPeriod = _c.sent(); return [2 /*return*/, { vestingPeriod: vestingPeriod }]; } }); }); }; VestingClient.prototype.getVestedAmount = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, vestingContract, amount; var vestingModuleAddress = _b.vestingModuleAddress, streamId = _b.streamId, chainId = _b.chainId; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); functionChainId = this._getReadOnlyFunctionChainId(chainId); vestingContract = this._getVestingContract(vestingModuleAddress, functionChainId); return [4 /*yield*/, vestingContract.read.vested([BigInt(streamId)])]; case 1: amount = _c.sent(); return [2 /*return*/, { amount: amount }]; } }); }); }; VestingClient.prototype.getVestedAndUnreleasedAmount = function (_a) { return __awaiter(this, arguments, void 0, function (_b) { var functionChainId, vestingContract, amount; var vestingModuleAddress = _b.vestingModuleAddress, streamId = _b.streamId, chainId = _b.chainId; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, validation_1.validateAddress)(vestingModuleAddress); functionChainId = this._getReadOnlyFunctionChainId(chainId); vestingContract = this._getVestingContract(vestingModuleAddress, functionChainId); return [4 /*yield*/, vestingContract.read.vestedAndUnreleased([ BigInt(streamId), ])]; case 1: amount = _c.sent(); return [2 /*return*/, { amount: amount }]; } }); }); }; return VestingClient; }(VestingTransactions)); exports.VestingClient = VestingClient; (0, mixin_1.applyMixins)(VestingClient, [base_1.BaseClientMixin]); // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging var VestingGasEstimates = /** @class */ (function (_super) { __extends(VestingGasEstimates, _super); function VestingGasEstimates(clientArgs) { return _super.call(this, __assign({ transactionType: constants_1.TransactionType.GasEstimate }, clientArgs)) || this; } VestingGasEstimates.prototype.createVestingModule = function (createVestingArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createVestingModuleTransaction(createVestingArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; VestingGasEstimates.prototype.startVest = function (startVestArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._startVestTransaction(startVestArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; VestingGasEstimates.prototype.releaseVestedFunds = function (releaseFundsArgs) { return __awaiter(this, void 0, void 0, function () { var gasEstimate; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._releaseVestedFundsTransaction(releaseFundsArgs)]; case 1: gasEstimate = _a.sent(); if (!this._isBigInt(gasEstimate)) throw new Error('Invalid response'); return [2 /*return*/, gasEstimate]; } }); }); }; return VestingGasEstimates; }(VestingTransactions)); (0, mixin_1.applyMixins)(VestingGasEstimates, [base_1.BaseGasEstimatesMixin]); var VestingCallData = /** @class */ (function (_super) { __extends(VestingCallData, _super); function VestingCallData(clientArgs) { return _super.call(this, __assign({ transactionType: constants_1.TransactionType.CallData }, clientArgs)) || this; } VestingCallData.prototype.createVestingModule = function (createVestingArgs) { return __awaiter(this, void 0, void 0, function () { var callData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._createVestingModuleTransaction(createVestingArgs)]; case 1: callData = _a.sent(); if (!this._isCallData(callData)) throw new Error('Invalid response'); return [2 /*return*/, callData]; } }); }); }; VestingCallData.prototype.startVest = function (startVestArgs) { return __awaiter(this, void 0, void 0, function () { var callData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._startVestTransaction(startVestArgs)]; case 1: callData = _a.sent(); if (!this._isCallData(callData)) throw new Error('Invalid response'); return [2 /*return*/, callData]; } }); }); }; VestingCallData.prototype.releaseVestedFunds = function (releaseFundsArgs) { return __awaiter(this, void 0, void 0, function () { var callData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._releaseVestedFundsTransaction(releaseFundsArgs)]; case 1: callData = _a.sent(); if (!this._isCallData(callData)) throw new Error('Invalid response'); return [2 /*return*/, callData]; } }); }); }; return VestingCallData; }(VestingTransactions)); //# sourceMappingURL=vesting.js.map