Skip to content

Commit

Permalink
Merge branch '2-snapshot-taker' of github.com:LiskHQ/lisk-token-claim…
Browse files Browse the repository at this point in the history
… into 5-merkle-tree-backend
  • Loading branch information
Phanco committed Feb 6, 2024
2 parents 07f915f + 6be97ad commit ad40428
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
16 changes: 5 additions & 11 deletions packages/tree-builder/src/applications/example/create_accounts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as fs from 'fs';
import { address } from '@liskhq/lisk-cryptography';
import { Account, ExampleKey } from '../../interface';
import { randomBalanceBeddows } from '../../utils';

// 1 LSK = 10^8 Beddows
const LSK_MULTIPLIER = 10 ** 8;

// Balances are random between 0 - <RANDOM_RANGE>
const RANDOM_RANGE = 10000;
// Random Balance in Beddows between 0 - 2 ** 8 * (RANDOM_BYTES_RANGE)
const RANDOM_BYTES_RANGE = 5;

// Multisig Accounts
// For each account it will use the address of the index as account holder,
Expand Down Expand Up @@ -41,8 +39,6 @@ const multiSigs = [
},
];

const randomBalance = (range: number): number => Number((range * Math.random()).toFixed(8));

export function createAccounts(numberOfAccounts = 54) {
const keyPairs = JSON.parse(
fs.readFileSync('../../data/example/key-pairs.json', 'utf-8'),
Expand All @@ -60,8 +56,7 @@ export function createAccounts(numberOfAccounts = 54) {
// Regular Accounts
for (let index = 0; index < numberOfAccounts - multiSigs.length; index++) {
const account = sortedKeyPairs[index];
const balance = randomBalance(RANDOM_RANGE);
const balanceBeddows = Math.round(balance * LSK_MULTIPLIER);
const balanceBeddows = randomBalanceBeddows(RANDOM_BYTES_RANGE);

results.push({
lskAddress: account.address,
Expand All @@ -71,8 +66,7 @@ export function createAccounts(numberOfAccounts = 54) {

for (const multiSig of multiSigs) {
const account = sortedKeyPairs[results.length];
const balance = randomBalance(RANDOM_RANGE);
const balanceBeddows = Math.round(balance * LSK_MULTIPLIER).toString();
const balanceBeddows = randomBalanceBeddows(RANDOM_BYTES_RANGE);

results.push({
lskAddress: account.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { ExampleKey } from '../../interface';
const initialPath = "m/44'/134'";

export async function createKeyPairs(amount = 100) {
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const keys: ExampleKey[] = [];
for (let i = 0; i < amount; i++) {
const keyPath = `${initialPath}/${i}'`;
const privateKey = await ed.getPrivateKeyFromPhraseAndPath('lisk', keyPath);
const privateKey = await ed.getPrivateKeyFromPhraseAndPath(mnemonic, keyPath);
keys.push({
address: address.getLisk32AddressFromAddress(address.getAddressFromPrivateKey(privateKey)),
keyPath,
Expand Down
3 changes: 0 additions & 3 deletions packages/tree-builder/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { computeStorePrefix } from './utils';

// 1 LSK = 10^8 Beddows
export const LSK_MULTIPLIER = 10 ** 8;

// Each leaf will be encoded in the following order:
// LSK_ADDRESS_IN_HEX: bytes20
// BALANCE_IN_BEDDOWS: uint64
Expand Down
5 changes: 5 additions & 0 deletions packages/tree-builder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ export function getTotalBalance(balance: UserBalance): bigint {
balance.lockedBalances.reduce((acc, cur) => acc + cur.amount, BigInt(0))
);
}

// When bytes = 8, max value = 2 ** (8 * 8) - 1
export function randomBalanceBeddows(maxBytes = 8): string {
return BigInt(append0x(utils.getRandomBytes(maxBytes))).toString();
}
7 changes: 3 additions & 4 deletions packages/tree-builder/test/applications/build_tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { StandardMerkleTree } from '@openzeppelin/merkle-tree';
import { defaultAbiCoder } from '@ethersproject/abi';
import { Account, ExampleKey } from '../../src/interface';
import { createPayload, buildTree } from '../../src/applications/generate-merkle-tree/build_tree';
import { LEAF_ENCODING, LSK_MULTIPLIER } from '../../src/constants';
import { LEAF_ENCODING } from '../../src/constants';
import { createKeyPairs } from '../../src/applications/example/create_key_pairs';
import { append0x } from '../../src/utils';
import { append0x, randomBalanceBeddows } from '../../src/utils';

describe('buildTree', () => {
let accounts: Account[];
Expand All @@ -24,12 +24,11 @@ describe('buildTree', () => {

// Create 5 accounts on the fly, they are all Multisig such that all fields are filled
accounts = keyPairsSorted.slice(0, 5).map(key => {
const balance = Number((10000 * Math.random()).toFixed(8));
const numberOfMandatoryKeys = Math.floor(5 * Math.random()) + 1;
const numberOfOptionalKeys = Math.floor(5 * Math.random());
return {
lskAddress: key.address,
balanceBeddows: Math.floor(balance * LSK_MULTIPLIER).toString(),
balanceBeddows: randomBalanceBeddows(),
numberOfSignatures: numberOfMandatoryKeys + numberOfOptionalKeys,
mandatoryKeys: keyPairsSorted.slice(0, numberOfMandatoryKeys).map(key => key.publicKey),
optionalKeys: keyPairsSorted
Expand Down

0 comments on commit ad40428

Please sign in to comment.