@alchemy/aa-core
Version:
viem based SDK that enables interactions with ERC-4337 Smart Accounts. ABIs are based off the definitions generated in @account-abstraction/contracts
47 lines • 1.9 kB
JavaScript
import { keccak256, toHex } from "viem";
import { isMultiplier } from "./schema.js";
export const bigIntMax = (...args) => {
if (!args.length) {
throw new Error("bigIntMax requires at least one argument");
}
return args.reduce((m, c) => (m > c ? m : c));
};
export const bigIntMin = (...args) => {
if (!args.length) {
throw new Error("bigIntMin requires at least one argument");
}
return args.reduce((m, c) => (m < c ? m : c));
};
export const bigIntClamp = (value, lower, upper) => {
lower = lower != null ? BigInt(lower) : null;
upper = upper != null ? BigInt(upper) : null;
if (upper != null && lower != null && upper < lower) {
throw new Error(`invalid range: upper bound ${upper} is less than lower bound ${lower}`);
}
let ret = BigInt(value);
if (lower != null && lower > ret) {
ret = lower;
}
if (upper != null && upper < ret) {
ret = upper;
}
return ret;
};
export var RoundingMode;
(function (RoundingMode) {
RoundingMode[RoundingMode["ROUND_DOWN"] = 0] = "ROUND_DOWN";
RoundingMode[RoundingMode["ROUND_UP"] = 1] = "ROUND_UP";
})(RoundingMode || (RoundingMode = {}));
export const bigIntMultiply = (base, multiplier, roundingMode = RoundingMode.ROUND_UP) => {
if (!isMultiplier({ multiplier })) {
throw new Error("bigIntMultiply requires a multiplier validated number as the second argument");
}
const decimalPlaces = multiplier.toString().split(".")[1]?.length ?? 0;
const val = roundingMode === RoundingMode.ROUND_UP
? BigInt(base) * BigInt(multiplier * 10 ** decimalPlaces) +
BigInt(10 ** decimalPlaces - 1)
: BigInt(base) * BigInt(multiplier * 10 ** decimalPlaces);
return val / BigInt(10 ** decimalPlaces);
};
export const stringToIndex = (phrase) => BigInt(keccak256(toHex(phrase)));
//# sourceMappingURL=bigint.js.map