Skip to content

Commit

Permalink
added rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Cazala committed Sep 16, 2017
1 parent e39a86f commit d34cf6d
Show file tree
Hide file tree
Showing 8 changed files with 658 additions and 68 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
61 changes: 57 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var CoinHive = require('coin-hive');
// Listen on events
miner.on('found', () => console.log('Found!'))
miner.on('accepted', () => console.log('Accepted!'))
miner.on('update', (data) => console.log(`
Hashes per second: ${data.hashesPerSecond}
Total hashes: ${data.totalHashes}
Accepted hashes: ${data.acceptedHashes}
miner.on('update', data => console.log(`
Hashes per second: ${data.hashesPerSecond}
Total hashes: ${data.totalHashes}
Accepted hashes: ${data.acceptedHashes}
`));

// Stop miner
Expand All @@ -40,7 +40,60 @@ Accepted hashes: ${data.acceptedHashes}
coin-hive <site-key>
```

## API

- `CoinHive(siteKey)`: Returns a promise of a `Miner` instance.

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

- `miner.stop()`: Stop mining and disconnect from the pool. Returns a promise that will resolve once the miner is stopped.

- `miner.on(event, callback)`: Specify a callback for an event. The event types are:

- `open`: The connection to our mining pool was opened. Usually happens shortly after miner.start() was called.

- `authed`: The miner successfully authed with the mining pool and the siteKey was verified. Usually happens right after open.

- `close`: The connection to the pool was closed. Usually happens when miner.stop() was called.

- `error`: An error occured. In case of a connection error, the miner will automatically try to reconnect to the pool.

- `job`: A new mining job was received from the pool.

- `found`: A hash meeting the pool's difficulty (currently 256) was found and will be send to the pool.

- `accepted`: A hash that was sent to the pool was accepted.

- `miner.rpc(methodName, argsArray)`: This method allows to interact with the Coin-Hive miner instance. It returns a Promise that resolves the the value of the remote method that was called. The miner intance API can be [found here](https://coin-hive.com/documentation/miner#miner-is-running). Here's an example:

```js
var miner = await CoinHive('SITE_KEY');
await miner.rpc('isRunning'); // false
await miner.start();
await miner.rpc('isRunning'); // true
await miner.rpc('getThrottle'); // 0
await miner.rpc('setThrottle', [0.5]);
await miner.rpc('getThrottle'); // 0.5
```

## ENVIRONMENT VARIABLES

All the following environment variables can be used to configure the miner from the outside:

- `SITE_KEY`: Coin-Hive's Site Key

- `INTERVAL`: The interval on which the miner reports an update

- `PORT`: The port that will be used to launch the server, and where puppeteer will point to

- `HOST`: The host that will be used to launch the server, and where puppeteer will point to

- `PUPPETEER_URL`: In case you don't want to point puppeteer to the local server, you can use this to make it point somewhere else where the miner is served (ie: `PUPPETEER_URL=http://coin-hive.herokuapp.com`)

## Requisites

+ Node v8+

## Disclaimer

I have nothing to do with `coin-hive.com`
13 changes: 11 additions & 2 deletions bin/coin-hive
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env node
'use strict';
const CoinHive = require('../src');
const defaults = require('../config/defaults');
console.log('Initializing...');

(async () => {

const siteKey = process.argv[2];
const interval = process.argv[3] || defaults.INTERVAL;
const port = process.argv[4] || defaults.PORT;
const host = process.argv[5] || defaults.HOST;

const help = `Usage: 'coin-hive <site-key> [, <interval>, <port>, <host>]'
- <site-key>: You CoinHive Site Key
Expand All @@ -21,10 +26,14 @@ console.log('Initializing...');
break;
default: {

var miner = await CoinHive(siteKey);
var miner = await CoinHive(siteKey, interval, port, host);
await miner.start();

console.log('Initialized!');
console.log('Initialized!\n');
console.log('Site Key:', siteKey);
console.log('Interval:', interval);
console.log('Port:', port);
console.log('Host:', host, '\n');

miner.on('found', () => console.log('Found!'))
miner.on('accepted', () => console.log('Accepted!'))
Expand Down
Loading

0 comments on commit d34cf6d

Please sign in to comment.