Skip to content

Commit

Permalink
fix: return merkleWhitelist as bigint (#574)
Browse files Browse the repository at this point in the history
Because of incorrect types we were actually returning strings
instead of bigints. This became an issue when adding those values together
as instead of adding VPs we were concatenating them.
  • Loading branch information
Sekhmet authored Aug 2, 2024
1 parent e1c930a commit 6bb2385
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 8 additions & 10 deletions packages/sx.js/src/strategies/evm/merkleWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Strategy, StrategyConfig } from '../../clients/evm/types';

type Entry = {
address: string;
votingPower: bigint;
votingPower: string;
};

function getProofForVoter(
Expand All @@ -29,14 +29,13 @@ export default function createMerkleWhitelist(): Strategy {
signerAddress: string,
metadata: Record<string, any> | null
): Promise<string> {
const tree = metadata?.tree;
const tree: Entry[] = metadata?.tree;

if (!tree) throw new Error('Invalid metadata. Missing tree');

const whitelist: [string, bigint][] = tree.map((entry: Entry) => [
entry.address,
entry.votingPower
]);
const whitelist = tree.map(
entry => [entry.address, BigInt(entry.votingPower)] as [string, bigint]
);
const merkleTree = StandardMerkleTree.of(whitelist, [
'address',
'uint96'
Expand All @@ -56,17 +55,16 @@ export default function createMerkleWhitelist(): Strategy {
voterAddress: string,
metadata: Record<string, any> | null
): Promise<bigint> {
const tree = metadata?.tree;
const tree: Entry[] = metadata?.tree;

if (!tree) throw new Error('Invalid metadata. Missing tree');

const match = tree.find(
(entry: Entry) =>
entry.address.toLowerCase() === voterAddress.toLowerCase()
entry => entry.address.toLowerCase() === voterAddress.toLowerCase()
);

if (match) {
return BigInt(match.votingPower.toString());
return BigInt(match.votingPower);
}

return 0n;
Expand Down
10 changes: 5 additions & 5 deletions packages/sx.js/src/strategies/starknet/merkleWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AddressType, generateMerkleProof, Leaf } from '../../utils/merkletree';
type Entry = {
type: AddressType;
address: string;
votingPower: bigint;
votingPower: string;
};

export default function createMerkleWhitelistStrategy(): Strategy {
Expand All @@ -22,12 +22,12 @@ export default function createMerkleWhitelistStrategy(): Strategy {
envelope: Envelope<Propose | Vote>,
clientConfig: ClientConfig
): Promise<string[]> {
const tree = metadata?.tree;
const tree: Entry[] = metadata?.tree;

if (!tree) throw new Error('Invalid metadata. Missing tree');

const leaves: Leaf[] = tree.map(
(entry: Entry) => new Leaf(entry.type, entry.address, entry.votingPower)
entry => new Leaf(entry.type, entry.address, BigInt(entry.votingPower))
);
const hashes = leaves.map(leaf => leaf.hash);
const voterIndex = leaves.findIndex(
Expand Down Expand Up @@ -61,12 +61,12 @@ export default function createMerkleWhitelistStrategy(): Strategy {
params: string[],
clientConfig: ClientConfig
): Promise<bigint> {
const tree = metadata?.tree;
const tree: Entry[] = metadata?.tree;

if (!tree) throw new Error('Invalid metadata. Missing tree');

const leaves: Leaf[] = tree.map(
(entry: Entry) => new Leaf(entry.type, entry.address, entry.votingPower)
entry => new Leaf(entry.type, entry.address, BigInt(entry.votingPower))
);
const voter = leaves.find(
leaf =>
Expand Down

0 comments on commit 6bb2385

Please sign in to comment.