@0xsplits/splits-sdk
Version:
SDK for the 0xSplits protocol
239 lines • 12.4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
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.sleep = exports.hashSplitV1 = exports.hashSplitV2 = exports.getAccountsAndPercentAllocations = exports.getSplitType = exports.getValidatedSplitV2Config = exports.MAX_V2_DISTRIBUTION_INCENTIVE = exports.getAddressAndAllocationFromRecipients = exports.getRecoupTranchesAndSizes = exports.getTrancheRecipientsAndSizes = exports.getNftCountsFromPercents = exports.getRecipientSortedAddressesAndAllocations = void 0;
var viem_1 = require("viem");
var constants_1 = require("../constants");
var types_1 = require("../types");
var numbers_1 = require("./numbers");
var tokens_1 = require("./tokens");
var errors_1 = require("../errors");
__exportStar(require("./ens"), exports);
__exportStar(require("./numbers"), exports);
__exportStar(require("./swapper"), exports);
__exportStar(require("./validation"), exports);
__exportStar(require("./balances"), exports);
__exportStar(require("./requests"), exports);
__exportStar(require("./tokens"), exports);
var getRecipientSortedAddressesAndAllocations = function (recipients) {
var accounts = [];
var percentAllocations = [];
recipients
.sort(function (a, b) {
if (a.address.toLowerCase() > b.address.toLowerCase())
return 1;
return -1;
})
.map(function (value) {
accounts.push((0, viem_1.getAddress)(value.address));
percentAllocations.push((0, numbers_1.getBigIntFromPercent)(value.percentAllocation));
});
return [accounts, percentAllocations];
};
exports.getRecipientSortedAddressesAndAllocations = getRecipientSortedAddressesAndAllocations;
var getNftCountsFromPercents = function (percentAllocations) {
return percentAllocations.map(function (p) {
return Number((p * BigInt(constants_1.LIQUID_SPLIT_NFT_COUNT)) / constants_1.PERCENTAGE_SCALE);
});
};
exports.getNftCountsFromPercents = getNftCountsFromPercents;
var getTrancheRecipientsAndSizes = function (chainId, token, tranches, publicClient) { return __awaiter(void 0, void 0, void 0, function () {
var recipients, sizes, tokenData, trancheSum;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
recipients = [];
sizes = [];
return [4 /*yield*/, (0, tokens_1.getTokenData)(chainId, token, publicClient)];
case 1:
tokenData = _a.sent();
trancheSum = BigInt(0);
tranches.forEach(function (tranche) {
recipients.push((0, viem_1.getAddress)(tranche.recipient));
if (tranche.size) {
trancheSum =
trancheSum + (0, numbers_1.getBigIntTokenValue)(tranche.size, tokenData.decimals);
sizes.push(trancheSum);
}
});
return [2 /*return*/, [recipients, sizes]];
}
});
}); };
exports.getTrancheRecipientsAndSizes = getTrancheRecipientsAndSizes;
var getRecoupTranchesAndSizes = function (chainId, token, tranches, publicClient) { return __awaiter(void 0, void 0, void 0, function () {
var recoupTranches, sizes, tokenData, trancheSum;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
recoupTranches = [];
sizes = [];
return [4 /*yield*/, (0, tokens_1.getTokenData)(chainId, token, publicClient)];
case 1:
tokenData = _a.sent();
trancheSum = BigInt(0);
tranches.forEach(function (tranche) {
var _a;
if (typeof tranche.recipient === 'string') {
recoupTranches.push([
[tranche.recipient],
[constants_1.PERCENTAGE_SCALE],
viem_1.zeroAddress,
BigInt(0),
]);
}
else {
var _b = __read((0, exports.getRecipientSortedAddressesAndAllocations)(tranche.recipient.recipients), 2), addresses = _b[0], percentAllocations = _b[1];
var distributorFee = (0, numbers_1.getBigIntFromPercent)(tranche.recipient.distributorFeePercent);
recoupTranches.push([
addresses,
percentAllocations,
(_a = tranche.recipient.controller) !== null && _a !== void 0 ? _a : viem_1.zeroAddress,
distributorFee,
]);
}
if (tranche.size) {
trancheSum =
trancheSum + (0, numbers_1.getBigIntTokenValue)(tranche.size, tokenData.decimals);
sizes.push(trancheSum);
}
});
return [2 /*return*/, [recoupTranches, sizes]];
}
});
}); };
exports.getRecoupTranchesAndSizes = getRecoupTranchesAndSizes;
var getAddressAndAllocationFromRecipients = function (recipients) {
return {
recipientAddresses: recipients.map(function (recipient) { return recipient.address; }),
recipientAllocations: recipients.map(function (recipient) {
return (0, numbers_1.getBigIntFromPercent)(recipient.percentAllocation);
}),
};
};
exports.getAddressAndAllocationFromRecipients = getAddressAndAllocationFromRecipients;
exports.MAX_V2_DISTRIBUTION_INCENTIVE = 6.5535;
var getValidatedSplitV2Config = function (recipients, distributorFeePercent, totalAllocationPercent) {
var _a = (0, exports.getAddressAndAllocationFromRecipients)(recipients), recipientAddresses = _a.recipientAddresses, recipientAllocations = _a.recipientAllocations;
if (distributorFeePercent > exports.MAX_V2_DISTRIBUTION_INCENTIVE)
throw new errors_1.InvalidDistributorFeePercentErrorV2(distributorFeePercent);
var distributionIncentive = (0, numbers_1.getNumberFromPercent)(distributorFeePercent);
var calculatedTotalAllocation = recipientAllocations.reduce(function (a, b) { return a + b; });
if (totalAllocationPercent &&
(0, numbers_1.getBigIntFromPercent)(totalAllocationPercent) !== calculatedTotalAllocation)
throw new errors_1.InvalidTotalAllocation(totalAllocationPercent);
else if (!totalAllocationPercent &&
calculatedTotalAllocation !== constants_1.PERCENTAGE_SCALE)
throw new errors_1.InvalidTotalAllocation();
return {
recipientAddresses: recipientAddresses,
recipientAllocations: recipientAllocations,
distributionIncentive: distributionIncentive,
totalAllocation: calculatedTotalAllocation,
};
};
exports.getValidatedSplitV2Config = getValidatedSplitV2Config;
var getSplitType = function (chainId, factoryAddress) {
if ((0, viem_1.getAddress)(factoryAddress) ===
(0, constants_1.getSplitV2FactoryAddress)(chainId, types_1.SplitV2Type.Pull))
return types_1.SplitV2Type.Pull;
return types_1.SplitV2Type.Push;
};
exports.getSplitType = getSplitType;
var getAccountsAndPercentAllocations = function (recipients, shouldSort) {
if (shouldSort === void 0) { shouldSort = false; }
var accounts = [];
var percentAllocations = [];
var recipientsCopy = recipients.slice();
if (shouldSort) {
recipientsCopy.sort(function (a, b) {
if (a.address.toLowerCase() > b.address.toLowerCase())
return 1;
return -1;
});
}
recipientsCopy.forEach(function (recipient) {
accounts.push(recipient.address);
percentAllocations.push(recipient.ownership);
});
return [accounts, percentAllocations];
};
exports.getAccountsAndPercentAllocations = getAccountsAndPercentAllocations;
var hashSplitV2 = function (accounts, percentAllocations, totalAllocations, distributorFee) {
return (0, viem_1.keccak256)((0, viem_1.encodePacked)(['address[]', 'uint256[]', 'uint256', 'uint16'], [accounts, percentAllocations, totalAllocations, distributorFee]));
};
exports.hashSplitV2 = hashSplitV2;
var hashSplitV1 = function (accounts, percentAllocations, distributorFee) {
return (0, viem_1.keccak256)((0, viem_1.encodePacked)(['address[]', 'uint32[]', 'uint32'], [accounts, percentAllocations, distributorFee]));
};
exports.hashSplitV1 = hashSplitV1;
var sleep = function (timeMs) {
return new Promise(function (resolve) { return setTimeout(resolve, timeMs); });
};
exports.sleep = sleep;
//# sourceMappingURL=index.js.map