|
| 1 | +// Safe math for unsigned 256-bit integers using native BigInt |
| 2 | +// Matches Go arc/safemath.go exactly |
| 3 | + |
| 4 | +const MAX_U256 = (1n << 256n) - 1n; |
| 5 | + |
| 6 | +export class SafeMathError extends Error { |
| 7 | + constructor(message) { super(message); this.name = 'SafeMathError'; } |
| 8 | +} |
| 9 | + |
| 10 | +export const ErrOverflow = () => new SafeMathError('arithmetic overflow'); |
| 11 | +export const ErrUnderflow = () => new SafeMathError('arithmetic underflow'); |
| 12 | +export const ErrDivisionByZero = () => new SafeMathError('division by zero'); |
| 13 | + |
| 14 | +// Clamp to U256 range [0, 2^256 - 1] |
| 15 | +function toU256(v) { |
| 16 | + v = BigInt(v); |
| 17 | + if (v < 0n) throw ErrUnderflow(); |
| 18 | + if (v > MAX_U256) throw ErrOverflow(); |
| 19 | + return v; |
| 20 | +} |
| 21 | + |
| 22 | +// SafeAdd returns a + b, throws on overflow |
| 23 | +export function safeAdd(a, b) { |
| 24 | + a = BigInt(a); b = BigInt(b); |
| 25 | + const result = a + b; |
| 26 | + if (result > MAX_U256) throw ErrOverflow(); |
| 27 | + return result; |
| 28 | +} |
| 29 | + |
| 30 | +// SafeSub returns a - b, throws on underflow |
| 31 | +export function safeSub(a, b) { |
| 32 | + a = BigInt(a); b = BigInt(b); |
| 33 | + if (b > a) throw ErrUnderflow(); |
| 34 | + return a - b; |
| 35 | +} |
| 36 | + |
| 37 | +// SafeMul returns a * b, throws on overflow |
| 38 | +export function safeMul(a, b) { |
| 39 | + a = BigInt(a); b = BigInt(b); |
| 40 | + const result = a * b; |
| 41 | + if (result > MAX_U256) throw ErrOverflow(); |
| 42 | + return result; |
| 43 | +} |
| 44 | + |
| 45 | +// SafeDiv returns a / b, throws on division by zero |
| 46 | +export function safeDiv(a, b) { |
| 47 | + a = BigInt(a); b = BigInt(b); |
| 48 | + if (b === 0n) throw ErrDivisionByZero(); |
| 49 | + return a / b; |
| 50 | +} |
| 51 | + |
| 52 | +// SafeMod returns a % b, throws on division by zero |
| 53 | +export function safeMod(a, b) { |
| 54 | + a = BigInt(a); b = BigInt(b); |
| 55 | + if (b === 0n) throw ErrDivisionByZero(); |
| 56 | + return a % b; |
| 57 | +} |
| 58 | + |
| 59 | +// MulDiv computes (a * b) / c with full precision intermediate result |
| 60 | +export function mulDiv(a, b, c) { |
| 61 | + a = BigInt(a); b = BigInt(b); c = BigInt(c); |
| 62 | + if (c === 0n) throw ErrDivisionByZero(); |
| 63 | + return (a * b) / c; |
| 64 | +} |
| 65 | + |
| 66 | +// ConvertToShares computes shares = (assets * totalShares) / totalAssets |
| 67 | +// Standard ERC-4626 conversion with rounding down |
| 68 | +export function convertToShares(assets, totalShares, totalAssets) { |
| 69 | + assets = BigInt(assets); |
| 70 | + totalShares = BigInt(totalShares); |
| 71 | + totalAssets = BigInt(totalAssets); |
| 72 | + if (totalAssets === 0n) return assets; // First deposit: 1:1 |
| 73 | + return mulDiv(assets, totalShares, totalAssets); |
| 74 | +} |
| 75 | + |
| 76 | +// ConvertToAssets computes assets = (shares * totalAssets) / totalShares |
| 77 | +// Standard ERC-4626 conversion with rounding down |
| 78 | +export function convertToAssets(shares, totalAssets, totalShares) { |
| 79 | + shares = BigInt(shares); |
| 80 | + totalAssets = BigInt(totalAssets); |
| 81 | + totalShares = BigInt(totalShares); |
| 82 | + if (totalShares === 0n) return shares; // No shares: 1:1 |
| 83 | + return mulDiv(shares, totalAssets, totalShares); |
| 84 | +} |
| 85 | + |
| 86 | +// Min returns the smaller of a and b |
| 87 | +export function min(a, b) { |
| 88 | + a = BigInt(a); b = BigInt(b); |
| 89 | + return a < b ? a : b; |
| 90 | +} |
| 91 | + |
| 92 | +// Max returns the larger of a and b |
| 93 | +export function max(a, b) { |
| 94 | + a = BigInt(a); b = BigInt(b); |
| 95 | + return a > b ? a : b; |
| 96 | +} |
| 97 | + |
| 98 | +export { MAX_U256 }; |
0 commit comments