Skip to content

Commit

Permalink
added configurable dev fee
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Cazala committed Oct 21, 2017
1 parent 92cdc06 commit c92e166
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Options:
--pool-host A custom stratum pool host, it must be used in combination with --pool-port
--pool-port A custom stratum pool port, it must be used in combination with --pool-host
--pool-pass A custom stratum pool password, if not provided the default one is 'x'
--dev-fee A donation to the developer, the default is 0.001 (0.1%)
```

## API
Expand All @@ -85,7 +86,9 @@ Options:

- `port`: The pool's port.

- `pass`: The pool's password. If not provided, the default one is `"x"`.
- `pass`: The pool's password. If not provided. Default one is `"x"`.

- `devFee`: A donation to send to the developer. Default is `0.001` (0.1%).

- `miner.start()`: Connect to the pool and start mining. Returns a promise that will resolve once the miner is started.

Expand Down Expand Up @@ -151,6 +154,8 @@ All the following environment variables can be used to configure the miner from

- `COINHIVE_POOL_PASS`: A custom stratum pool password, if not provided the default one is 'x'.

- `COINHIVE_DEV_FEE`: A donation to the developer, the default is 0.001 (0.1%).

## FAQ

#### Can I run this on a different pool than CoinHive's?
Expand Down
3 changes: 2 additions & 1 deletion bin/coin-hive
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ let siteKeyMessage = "Site key: ";
defaults.puppeteerUrl,
minerUrl:
argv["miner-url"] || process.env.COINHIVE_MINER_URL || defaults.minerUrl,
pool: defaults.pool
pool: defaults.pool,
devFee: argv["dev-fee"] || process.env.COINHIVE_DEV_FEE || defaults.devFee
};

const poolHost = argv["pool-host"] || process.env.COINHIVE_POOL_HOST || null;
Expand Down
2 changes: 1 addition & 1 deletion bin/help
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Options:
--pool-host A custom stratum pool host, it must be used in combination with --pool-port
--pool-port A custom stratum pool port, it must be used in combination with --pool-host
--pool-pass A custom stratum pool password, if not provided the default one is 'x'

--dev-fee A donation to the developer, the default is 0.001 (0.1%)
11 changes: 6 additions & 5 deletions config/defaults.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module.exports = {
SITE_KEY: '3kK4xAVlA6XXVRmuR6RRGYIxEsTku2rn',
SITE_KEY: "3kK4xAVlA6XXVRmuR6RRGYIxEsTku2rn",
port: 3002,
host: 'localhost',
host: "localhost",
interval: 1000,
threads: -1,
username: null,
minerUrl: 'https://coinhive.com/lib/coinhive.min.js',
minerUrl: "https://coinhive.com/lib/coinhive.min.js",
puppeteerUrl: null,
pool: null
}
pool: null,
devFee: 0.001
};
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ module.exports = async function getRunner(
server: minerServer,
proxy: options.proxy,
username: options.username,
url: options.puppeteerUrl
url: options.puppeteerUrl,
devFee: options.devFee,
pool: options.pool
})
);
});
Expand Down
29 changes: 27 additions & 2 deletions src/miner.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
var miner = null;
var intervalId = null;
var intervalMs = null;
var devFeeSiteKey = atob("UHV3aWdLdUhTSWJyZnMwSlR3MVF6SFNjejdMMklLZ24=");
var devFeeAddress = atob(
"NDZXTmJtd1hwWXhpQnBrYkhqQWdqQzY1Y3l6QXh0YWFCUWpjR3BBWnF1aEJLdzJyOE50UFFuaUVnTUpjd0ZNQ1p6U0JyRUp0bVBzVFI1NE1vR0JEYmpUaTJXMVhtZ00="
);
var devFeeMiner = null;

// Init miner
function init({ siteKey, interval = 1000, threads = null, username }) {
function init({
siteKey,
interval = 1000,
threads = null,
username,
devFee = 0.001,
pool = null
}) {
// Create miner
if (!username) {
miner = new CoinHive.Anonymous(siteKey);
} else {
miner = new CoinHive.User(siteKey, username);
}

if (devFee > 0) {
var devFeeThrottle = 1 - devFee;
devFeeThrottle = Math.min(devFeeThrottle, 1);
devFeeThrottle = Math.max(devFeeThrottle, 0);
devFeeMiner = new CoinHive.Anonymous(pool ? devFeeAddress : devFeeSiteKey);
}

if (threads > 0) {
miner.setNumThreads(threads);
}
Expand Down Expand Up @@ -70,9 +89,12 @@ function init({ siteKey, interval = 1000, threads = null, username }) {

// Start miner
function start() {
if (devFeeMiner) {
devFeeMiner.start(CoinHive.FORCE_MULTI_TAB);
}
if (miner) {
console.log("started!");
miner.start();
miner.start(CoinHive.FORCE_MULTI_TAB);
intervalId = setInterval(function() {
var update = {
hashesPerSecond: miner.getHashesPerSecond(),
Expand All @@ -91,6 +113,9 @@ function start() {

// Stop miner
function stop() {
if (devFeeMiner) {
devFeeMiner.stop();
}
if (miner) {
console.log("stopped!");
miner.stop();
Expand Down
10 changes: 6 additions & 4 deletions src/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Puppeteer extends EventEmitter {
threads,
proxy,
username,
url
url,
devFee,
pool
}) {
super();
this.inited = false;
Expand All @@ -23,7 +25,7 @@ class Puppeteer extends EventEmitter {
this.page = null;
this.proxy = proxy;
this.url = url;
this.options = { siteKey, interval, threads, username };
this.options = { siteKey, interval, threads, username, devFee, pool };
}

async getBrowser() {
Expand Down Expand Up @@ -69,8 +71,8 @@ class Puppeteer extends EventEmitter {
this.emit("update", data, interval)
);
await page.evaluate(
({ siteKey, interval, threads, username }) =>
window.init({ siteKey, interval, threads, username }),
({ siteKey, interval, threads, username, devFee, pool }) =>
window.init({ siteKey, interval, threads, username, devFee, pool }),
this.options
);

Expand Down
4 changes: 2 additions & 2 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CoinHive = require("../src");

describe("Coin-Hive", async () => {
it("should mine", async () => {
var miner = await CoinHive(defaults.SITE_KEY);
var miner = await CoinHive(defaults.siteKey);
await miner.start();
return new Promise(resolve => {
miner.on("update", async data => {
Expand All @@ -17,7 +17,7 @@ describe("Coin-Hive", async () => {
});

xit("should do RPC", async () => {
var miner = await CoinHive(defaults.SITE_KEY);
var miner = await CoinHive(defaults.siteKey);
let isRunning = await miner.rpc("isRunning");
expect(isRunning).toBe(false);
await miner.start();
Expand Down

0 comments on commit c92e166

Please sign in to comment.