Skip to content

Instantly share code, notes, and snippets.

@uzyn
Last active July 19, 2021 21:05
Show Gist options
  • Save uzyn/f9766cfd69ff6a586a0877b6cbae11c0 to your computer and use it in GitHub Desktop.
Save uzyn/f9766cfd69ff6a586a0877b6cbae11c0 to your computer and use it in GitHub Desktop.
Freeze staking Monte Carlo simulation
// Monte carlo simulation of long term staking
// Nodes distribution, regular, 5y, 10y
const CYCLES = 100000; // Increase this to run more simulations
const NODES = [1000, 1000, 1000]; // regular, 5y, 10y
const TARGET = 0.0012; // 0.12% of guesses win
// const COINAGE_EVERY = [24, 9, 5]; // 6 hours, 2h15m, 1h15m
const COINAGE_EVERY = [24, 24, 24]; // consistent
const SUBNODES = [2, 3, 4];
console.log('CYCLES', CYCLES);
console.log('NODES', NODES);
console.log('TARGET', TARGET);
console.log('COINAGE_EVERY', COINAGE_EVERY);
console.log();
const wins = [0, 0, 0];
const attempts = []; // successive failure attempts
// init
NODES.forEach((count, type) => {
for (let node = 0; node < count; ++node) {
for (let subnode = 0; subnode < SUBNODES[type]; ++subnode) {
attempts[`${type}-${node}-${subnode}`] = 0;
}
}
});
for (let i = 0; i < CYCLES; ++i) {
NODES.forEach((count, type) => {
for (let node = 0; node < count; ++node) {
for (let subnode = 0; subnode < SUBNODES[type]; ++subnode) {
const subnodeKey = `${type}-${node}-${subnode}`;
const target = (Math.floor(attempts[subnodeKey] / COINAGE_EVERY[type]) + 1) * TARGET;
if (Math.random() <= target) {
attempts[subnodeKey] = 0;
++wins[type];
} else {
++attempts[subnodeKey];
}
}
}
});
}
console.log('Findings:', wins);
const rates = [0, 0, 0];
// Analyze results
wins.forEach((count, type) => {
const pct = (count / (NODES[type] * CYCLES)) * 100;
rates[type] = pct;
const multiple = pct / rates[0];
console.log(`Type ${type}: ${pct.toFixed(3)}% success rate. (${multiple.toFixed(3)}x)`);
});
$ node freeze-staking-montecarlo.js
CYCLES 100000
NODES [ 1000, 1000, 1000 ]
TARGET 0.0012
COINAGE_EVERY [ 24, 24, 24 ]
Findings: [ 1206911, 1810558, 2413457 ]
Type 0: 1.207% success rate. (1.000x)
Type 1: 1.811% success rate. (1.500x)
Type 2: 2.413% success rate. (2.000x)
$ node freeze-staking-montecarlo.js
CYCLES 10000
NODES [ 1000, 1000, 1000 ]
TARGET 0.0012
COINAGE_EVERY [ 24, 24, 24 ]
Findings: [ 119901, 180127, 240000 ]
Type 0: 1.199% success rate. (1.000x)
Type 1: 1.801% success rate. (1.502x)
Type 2: 2.400% success rate. (2.002x)
$ node freeze-staking-montecarlo.js
CYCLES 1000
NODES [ 1000, 1000, 1000 ]
TARGET 0.0001
COINAGE_EVERY [ 24, 24, 24 ]
Findings: [ 2595, 3881, 5169 ]
Type 0: 0.260% success rate. (1.000x)
Type 1: 0.388% success rate. (1.496x)
Type 2: 0.517% success rate. (1.992x)
$ node freeze-staking-montecarlo.js
CYCLES 10000
NODES [ 1000, 1000, 1000 ]
TARGET 0.0001
COINAGE_EVERY [ 24, 24, 24 ]
Findings: [ 32318, 48721, 64973 ]
Type 0: 0.323% success rate. (1.000x)
Type 1: 0.487% success rate. (1.508x)
Type 2: 0.650% success rate. (2.010x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment