@dydxfoundation/governance
Version:
dYdX governance smart contracts
33 lines (32 loc) • 1.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripHexPrefix = exports.concatHex = exports.asUintHex = exports.asBytes32 = void 0;
const ethers_1 = require("ethers");
function asBytes32(value) {
const rawHex = stripHexPrefix(ethers_1.BigNumber.from(value).toHexString());
return `0x${rawHex.padStart(64, '0')}`;
}
exports.asBytes32 = asBytes32;
function asUintHex(value, bits) {
if (bits % 8 !== 0) {
throw new Error(`asUintHex: Expected bits to be a multiple of 8 but got ${bits}`);
}
const hexChars = bits / 4;
const rawHex = stripHexPrefix(ethers_1.BigNumber.from(value).toHexString());
if (rawHex.length > hexChars) {
throw new Error(`asUintHex: Value ${value} is out of range for ${bits} bits`);
}
return `0x${rawHex.padStart(hexChars, '0')}`;
}
exports.asUintHex = asUintHex;
function concatHex(hexValues) {
return `0x${hexValues.map(stripHexPrefix).join('')}`;
}
exports.concatHex = concatHex;
function stripHexPrefix(hexValue) {
if (hexValue.startsWith('0x')) {
return hexValue.slice(2);
}
return hexValue;
}
exports.stripHexPrefix = stripHexPrefix;