UNPKG

@vechain/vebetterdao-contracts

Version:

Open-source repository that houses the smart contracts powering the decentralized VeBetterDAO on the VeChain Thor blockchain.

99 lines (98 loc) 3.22 kB
import { Address, HDKey, VET } from "@vechain/sdk-core"; import { getMnemonic } from "./env"; export var SeedStrategy; (function (SeedStrategy) { SeedStrategy[SeedStrategy["RANDOM"] = 0] = "RANDOM"; SeedStrategy[SeedStrategy["FIXED"] = 1] = "FIXED"; SeedStrategy[SeedStrategy["LINEAR"] = 2] = "LINEAR"; })(SeedStrategy || (SeedStrategy = {})); let hdnode = null; const getHDNode = () => { if (!hdnode) { const mnemonic = getMnemonic(true); // Required for actual deployment/simulation hdnode = HDKey.fromMnemonic(mnemonic.split(" ")); } return hdnode; }; export const getTestKey = (index) => { const pk = getHDNode().deriveChild(index); if (!pk.privateKey) { throw new Error("Private key not found"); } return { pk: pk.privateKey, address: Address.ofPrivateKey(pk.privateKey), }; }; export const getTestKeys = (count) => { const accounts = []; for (let i = 0; i < count; i++) { accounts.push(getTestKey(i)); } return accounts; }; /** * Generates a random starting balance for an account * Lower balances are favoured based on a log scale * @param min * @param max * @returns */ const getRandomStartingBalance = (min, max) => { const scale = Math.log(max) - Math.log(min); const random = Math.random() ** 6; // Raise to a power to skew towards smaller values. const result = Math.exp(Math.log(min) + scale * random); return VET.of(Math.floor(result)).wei; }; /** * Get seed accounts based on the strategy * @param strategy the strategy to use * @param numAccounts the number of accounts to generate * @param acctOffset the offset to start the account index * @returns a list of seed accounts */ export const getSeedAccounts = (strategy, numAccounts, acctOffset) => { switch (strategy) { case SeedStrategy.RANDOM: return getSeedAccountsRandom(numAccounts, acctOffset); case SeedStrategy.LINEAR: return getSeedAccountsLinear(numAccounts, acctOffset); case SeedStrategy.FIXED: return getSeedAccountsFixed(numAccounts, acctOffset); default: throw new Error("Unknown seed strategy"); } }; const getSeedAccountsFixed = (numAccounts, acctOffset) => { const keys = getTestKeys(numAccounts + acctOffset); const seedAccounts = []; keys.slice(acctOffset).forEach(key => { seedAccounts.push({ key, amount: VET.of(10000).wei, }); }); return seedAccounts; }; const getSeedAccountsRandom = (numAccounts, acctOffset) => { const keys = getTestKeys(numAccounts + acctOffset); const seedAccounts = []; keys.slice(acctOffset).forEach(key => { seedAccounts.push({ key, amount: getRandomStartingBalance(5, 1000), }); }); return seedAccounts; }; const getSeedAccountsLinear = (numAccounts, acctOffset) => { const keys = getTestKeys(numAccounts + acctOffset); const seedAccounts = []; keys.slice(acctOffset).forEach((key, index) => { seedAccounts.push({ key, amount: VET.of(((index + 1) * 5).toFixed(2)).wei, }); }); return seedAccounts; };