@axiom-crypto/tools
Version:
Useful data, field, and byte manipulation tools for Axiom.
32 lines (31 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeRowHash = exports.encodeQueryRowV1 = exports.encodeQueryV1 = void 0;
const ethers_1 = require("ethers");
function encodeQueryV1(length, encodedQueries) {
const encodedQueryData = ethers_1.ethers.solidityPacked(["uint8", "uint32", "bytes[]"], [1, length, encodedQueries]);
return encodedQueryData;
}
exports.encodeQueryV1 = encodeQueryV1;
function encodeQueryRowV1(length, blockNumber, address, slot, value) {
const queryTypes = ["uint8", "uint32", "address", "uint256", "uint256"];
const queryData = [length, blockNumber, address, slot, value];
// Only encode the first `length + 1` elements
const encodedQuery = ethers_1.ethers.solidityPacked(queryTypes.slice(0, length + 1), queryData.slice(0, length + 1));
return encodedQuery;
}
exports.encodeQueryRowV1 = encodeQueryRowV1;
function encodeRowHash(blockNumber, address, slot) {
let length = 3;
const addressValue = address ?? ethers_1.ZeroAddress;
const slotValue = slot ?? ethers_1.ethers.toBeHex(0, 32);
if (slot === undefined) {
length = 2;
}
if (address === undefined) {
length = 1;
}
const packed = ethers_1.ethers.solidityPacked(["uint8", "uint32", "address", "uint256"], [length, blockNumber, addressValue, slotValue]);
return ethers_1.ethers.keccak256(packed);
}
exports.encodeRowHash = encodeRowHash;