@vechain/vebetterdao-contracts
Version:
Open-source repository that houses the smart contracts powering the decentralized VeBetterDAO on the VeChain Thor blockchain.
105 lines (104 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSeedAccounts = exports.getTestKeys = exports.getTestKey = exports.SeedStrategy = void 0;
const sdk_core_1 = require("@vechain/sdk-core");
const env_1 = require("./env");
var SeedStrategy;
(function (SeedStrategy) {
SeedStrategy[SeedStrategy["RANDOM"] = 0] = "RANDOM";
SeedStrategy[SeedStrategy["FIXED"] = 1] = "FIXED";
SeedStrategy[SeedStrategy["LINEAR"] = 2] = "LINEAR";
})(SeedStrategy || (exports.SeedStrategy = SeedStrategy = {}));
let hdnode = null;
const getHDNode = () => {
if (!hdnode) {
const mnemonic = (0, env_1.getMnemonic)(true); // Required for actual deployment/simulation
hdnode = sdk_core_1.HDKey.fromMnemonic(mnemonic.split(" "));
}
return hdnode;
};
const getTestKey = (index) => {
const pk = getHDNode().deriveChild(index);
if (!pk.privateKey) {
throw new Error("Private key not found");
}
return {
pk: pk.privateKey,
address: sdk_core_1.Address.ofPrivateKey(pk.privateKey),
};
};
exports.getTestKey = getTestKey;
const getTestKeys = (count) => {
const accounts = [];
for (let i = 0; i < count; i++) {
accounts.push((0, exports.getTestKey)(i));
}
return accounts;
};
exports.getTestKeys = getTestKeys;
/**
* 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 sdk_core_1.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
*/
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");
}
};
exports.getSeedAccounts = getSeedAccounts;
const getSeedAccountsFixed = (numAccounts, acctOffset) => {
const keys = (0, exports.getTestKeys)(numAccounts + acctOffset);
const seedAccounts = [];
keys.slice(acctOffset).forEach(key => {
seedAccounts.push({
key,
amount: sdk_core_1.VET.of(10000).wei,
});
});
return seedAccounts;
};
const getSeedAccountsRandom = (numAccounts, acctOffset) => {
const keys = (0, exports.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 = (0, exports.getTestKeys)(numAccounts + acctOffset);
const seedAccounts = [];
keys.slice(acctOffset).forEach((key, index) => {
seedAccounts.push({
key,
amount: sdk_core_1.VET.of(((index + 1) * 5).toFixed(2)).wei,
});
});
return seedAccounts;
};