Skip to content

Instantly share code, notes, and snippets.

@tinybike
Created August 19, 2015 23:59
Show Gist options
  • Select an option

  • Save tinybike/b8d092090494a9f886f7 to your computer and use it in GitHub Desktop.

Select an option

Save tinybike/b8d092090494a9f886f7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var assert = require("assert");
// note: btc.total and btc.balance in BTC, not satoshis!
function calculateRepPercentage(btc, eth, exchangeRate) {
return (eth.balance*exchangeRate + btc.balance) / (eth.total*exchangeRate + btc.total);
}
var test = function (t) {
assert(calculateRepPercentage(t.btc, t.eth, t.exchangeRate), t.repPercentage);
};
test({
btc: { total: 10, balance: 10 },
eth: { total: 0, balance: 0 },
exchangeRate: 1,
repPercentage: 1
});
test({
btc: { total: 0, balance: 0 },
eth: { total: 10, balance: 10 },
exchangeRate: 1,
repPercentage: 1
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 0, balance: 0 },
exchangeRate: 1,
repPercentage: 0.5
});
test({
btc: { total: 0, balance: 0 },
eth: { total: 10, balance: 5 },
exchangeRate: 1,
repPercentage: 0.5
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 10, balance: 5 },
exchangeRate: 1,
repPercentage: 0.5
});
test({
btc: { total: 10, balance: 0 },
eth: { total: 10, balance: 5 },
exchangeRate: 1,
repPercentage: 0.25
});
test({
btc: { total: 10, balance: 0 },
eth: { total: 10, balance: 5 },
exchangeRate: 1,
repPercentage: 0.25
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 10, balance: 0 },
exchangeRate: 1,
repPercentage: 0.25
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 10, balance: 5 },
exchangeRate: 0.5,
repPercentage: 0.5
});
test({
btc: { total: 10, balance: 0 },
eth: { total: 10, balance: 5 },
exchangeRate: 0.5,
repPercentage: 1/6
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 10, balance: 0 },
exchangeRate: 0.5,
repPercentage: 1/3
});
test({
btc: { total: 10, balance: 0 },
eth: { total: 10, balance: 2.5 },
exchangeRate: 0.5,
repPercentage: 1/12
});
test({
btc: { total: 10, balance: 2.5 },
eth: { total: 10, balance: 0 },
exchangeRate: 0.5,
repPercentage: 1/6
});
test({
btc: { total: 10, balance: 2.5 },
eth: { total: 10, balance: 5 },
exchangeRate: 0.5,
repPercentage: 1/3
});
test({
btc: { total: 10, balance: 5 },
eth: { total: 10, balance: 2.5 },
exchangeRate: 0.5,
repPercentage: 5/12
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment