Skip to content

Commit c52cfd3

Browse files
committed
Make mining client less gas-conservative
1 parent 5a220d2 commit c52cfd3

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

packages/reputation-miner/ReputationMinerClient.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,30 +232,47 @@ class ReputationMinerClient {
232232
this._adapter.log("🏁 Initialised");
233233
}
234234

235-
async updateGasEstimate(type) {
236-
if (this.chainId === 100){
237-
this._miner.gasPrice = ethers.utils.hexlify(1000000000);
238-
return;
239-
}
240-
// Get latest from ethGasStation
235+
async updateGasEstimate(_type) {
236+
let type = _type;
241237
const options = {
242-
uri: 'https://ethgasstation.info/json/ethgasAPI.json',
243238
headers: {
244-
'User-Agent': 'Request-Promise'
239+
'User-Agent': 'Request-Promise'
245240
},
246241
json: true // Automatically parses the JSON string in the response
247242
};
243+
let defaultGasPrice;
244+
let factor;
245+
246+
if (this.chainId === 100){
247+
options.uri = "https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle";
248+
defaultGasPrice = ethers.utils.hexlify(1000000000);
249+
factor = 1;
250+
// This oracle presents the information slightly differently from ethgasstation.
251+
if (_type === "safeLow") {
252+
type = "slow";
253+
}
254+
} else if (this.chainId === 1) {
255+
options.uri = "https://ethgasstation.info/json/ethgasAPI.json";
256+
defaultGasPrice = ethers.utils.hexlify(20000000000);
257+
factor = 10;
258+
} else {
259+
this._adapter.error(`Error during gas estimation: unknown chainid ${this.chainId}`);
260+
this._miner.gasPrice = ethers.utils.hexlify(20000000000);
261+
return;
262+
}
263+
264+
// Get latest from whichever oracle
248265
try {
249266
const gasEstimates = await request(options);
250267

251268
if (gasEstimates[type]){
252-
this._miner.gasPrice = ethers.utils.hexlify(gasEstimates[type] / 10 * 1e9);
269+
this._miner.gasPrice = ethers.utils.hexlify(gasEstimates[type] / factor * 1e9);
253270
} else {
254-
this._miner.gasPrice = ethers.utils.hexlify(20000000000);
271+
this._miner.gasPrice = defaultGasPrice;
255272
}
256273
} catch (err) {
257274
this._adapter.error(`Error during gas estimation: ${err}`);
258-
this._miner.gasPrice = ethers.utils.hexlify(20000000000);
275+
this._miner.gasPrice = defaultGasPrice;
259276
}
260277
}
261278

@@ -335,7 +352,7 @@ class ReputationMinerClient {
335352
if (canSubmit) {
336353
this._adapter.log("⏰ Looks like it's time to submit an entry to the current cycle");
337354
this.submissionIndex += 1;
338-
await this.updateGasEstimate('safeLow');
355+
await this.updateGasEstimate('average');
339356
await this.submitEntry(entryIndex);
340357
}
341358
}
@@ -380,7 +397,7 @@ class ReputationMinerClient {
380397
return;
381398
}
382399
}
383-
await this.updateGasEstimate('safeLow');
400+
await this.updateGasEstimate('average');
384401
await repCycle.invalidateHash(round, oppIndex, {"gasPrice": this._miner.gasPrice});
385402
this.endDoBlockChecks();
386403
return;
@@ -398,7 +415,7 @@ class ReputationMinerClient {
398415
);
399416
if (responsePossible) {
400417
// If so, invalidate them.
401-
await this.updateGasEstimate('safeLow');
418+
await this.updateGasEstimate('average');
402419
await repCycle.invalidateHash(round, oppIndex, {"gasPrice": this._miner.gasPrice});
403420
this.endDoBlockChecks();
404421
return;
@@ -466,7 +483,7 @@ class ReputationMinerClient {
466483
if (responsePossible){
467484
this.best12Submissions = []; // Clear the submissions
468485
this.submissionIndex = 0;
469-
await this.updateGasEstimate('safeLow');
486+
await this.updateGasEstimate('average');
470487
await this.confirmEntry();
471488
}
472489
}

packages/reputation-miner/bin/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
network,
2323
localPort,
2424
localProviderAddress,
25+
providerAddress,
2526
syncFrom,
2627
auto,
2728
oracle,
@@ -36,6 +37,7 @@ if ((!minerAddress && !privateKey) || !colonyNetworkAddress || !syncFrom) {
3637
process.exit();
3738
}
3839

40+
3941
const loader = new TruffleLoader({
4042
contractDir: path.resolve(__dirname, "..", "..", "..", "build", "contracts")
4143
});
@@ -48,7 +50,13 @@ if (network) {
4850
}
4951
provider = new ethers.providers.InfuraProvider(network);
5052
} else {
51-
provider = new ethers.providers.JsonRpcProvider(`http://${localProviderAddress || "localhost"}:${localPort || "8545"}`);
53+
let rpcEndpoint = providerAddress;
54+
55+
if (!rpcEndpoint) {
56+
rpcEndpoint = `http://${localProviderAddress || "localhost"}:${localPort || "8545"}`;
57+
}
58+
59+
provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
5260
}
5361

5462
let adapterObject;

0 commit comments

Comments
 (0)