@dydxfoundation/governance
Version:
dYdX governance smart contracts
43 lines (42 loc) • 1.69 kB
JavaScript
;
// Based on https://github.com/Uniswap/merkle-distributor/blob/c3255bfa2b684594ecd562cacd7664b0f18330bf/src/balance-tree.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
const lodash_1 = __importDefault(require("lodash"));
const merkle_tree_1 = __importDefault(require("./merkle-tree"));
class BalanceTree extends merkle_tree_1.default {
constructor(balances) {
super(lodash_1.default.map(balances, (amount, account) => {
return BalanceTree.toNode(account, amount);
}));
}
static verifyProof(account, amount, proof, root) {
let pair = BalanceTree.toNode(account, amount);
for (const item of proof) {
pair = merkle_tree_1.default.combinedHash(pair, item);
}
return pair.equals(root);
}
static toNode(account, amount) {
const amountBN = ethers_1.BigNumber.from(amount);
return Buffer.from(
// Equivalent to keccak256(abi.encodePacked(account, amount))
ethers_1.utils.solidityKeccak256(['address', 'uint256'], [account, amountBN]).substr(2), 'hex');
}
/**
* @notice Returns the hex bytes32 values of the proof.
*/
getProof(account, amount) {
return this._getProof(BalanceTree.toNode(account, amount));
}
/**
* @notice Returns the hex bytes32 values of the proof.
*/
getHexProof(account, amount) {
return this._getHexProof(BalanceTree.toNode(account, amount));
}
}
exports.default = BalanceTree;