Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM RPC #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions config.example.js
Expand Up @@ -48,5 +48,12 @@ module.exports = {
type: 'ERC20'
}
}
},
ARBNode: {
chain: 'ARB',
host: 'localhost',
rpcPort: '8546',
protocol: 'http',
isEVM: true
}
};
1 change: 1 addition & 0 deletions docker-compose.yml
Expand Up @@ -138,6 +138,7 @@ services:
-b 2
-g 20000000000
-p 8545
-a 20

rippled:
networks:
Expand Down
7 changes: 5 additions & 2 deletions lib/eth/EthRpc.js
Expand Up @@ -3,13 +3,15 @@ const promptly = require('promptly');
const ethers = require('ethers');
const util = require('util');
const EventEmitter = require('events');
const chainConfig = require('./chains');

const passwordPromptAsync = util.promisify(promptly.password);
const setTimeoutAsync = util.promisify(setTimeout);

class EthRPC {
constructor(config) {
this.config = config;
this.chain = this.config.chain;
this.web3 = this.getWeb3(this.config);
this.account = this.config.account;
this.emitter = new EventEmitter();
Expand Down Expand Up @@ -206,7 +208,7 @@ class EthRPC {
* Estimates the fee.
* @param {Object} params - The parameters for estimating the fee.
* @param {Map} params.cache - The cache of block fees.
* @param {number} params.defaultEstimate - The RPC estimate of the fee.
* @param {number} params.defaultEstimate - The default fee value.
* @param {number} [params.percentile=25] - The percentile to use for the estimation.
* @param {Function} params.getFees - The function to get the fees from the blocks.
* @returns {Promise<number>} The estimated fee.
Expand Down Expand Up @@ -266,7 +268,8 @@ class EthRPC {
* @returns {Promise<number>} The estimated maximum priority fee.
*/
async estimateMaxPriorityFee({ percentile = 25 }) {
const defaultEstimate = parseInt(this.web3.utils.toWei('2.5', 'gwei'));
const minimumFee = chainConfig[this.chain] ? chainConfig[this.chain].priorityFee : 2.5;
const defaultEstimate = parseInt(this.web3.utils.toWei(String(minimumFee), 'gwei'));
const getFees = (_txs) => _txs
.filter((tx) => !!tx.maxPriorityFeePerGas)
.map((tx) => parseInt(tx.maxPriorityFeePerGas));
Expand Down
19 changes: 19 additions & 0 deletions lib/eth/chains.js
@@ -0,0 +1,19 @@
const chainConfig = {
ETH: {
priorityFee: 1 // in gwei
},
MATIC: {
priorityFee: 30
},
OP: {
priorityFee: 1
},
ARB: {
priorityFee: 0 // transactions are processed FIFO, no priorityFee fee is necessary for Arbitrum transactions
},
BASE: {
priorityFee: 1
}
};

module.exports = chainConfig;
25 changes: 21 additions & 4 deletions lib/index.js
Expand Up @@ -6,7 +6,7 @@ const RpcClasses = {
DOGE: require('./doge/DogeRpc'),
LTC: require('./ltc/LtcRpc'),
LNBTC: require('./lnd/LndRpc'),
MATIC: require('./matic/MaticRpc'),
MATIC: require('./matic/MaticRpc'), // keeping for backwards compatibility
};

const TokenClasses = {
Expand Down Expand Up @@ -39,9 +39,25 @@ const TokenClasses = {
};

class CryptoRpcProvider {

/**
* Constructor for CryptoRpcProvider class.
* @param {Object} config - The configuration object.
* @param {string} config.chain - The chain to connect to.
* @param {boolean} config.isEVM - Optional flag indicating if the chain is EVM compatible.
* @param {string} config.host - The host address for RPC connection.
* @param {number} config.port - The port for RPC connection.
* @param {string} config.rpcPort - The port for RPC connection (alternative).
* @param {string} config.user - The username for RPC connection.
* @param {string} config.rpcUser - The username for RPC connection (alternative).
* @param {string} config.pass - The password for RPC connection.
* @param {string} config.rpcPass - The password for RPC connection (alternative).
* @param {string} config.protocol - The protocol for RPC connection.
* @param {Object} config.tokens - Optional tokens configuration.
*/
constructor(config) {
this.chain = config.chain;
if (!RpcClasses[this.chain]) {
if (!RpcClasses[this.chain] && !config.isEVM) {
throw new Error('Invalid chain specified');
}
this.config = Object.assign({}, config, {
Expand All @@ -51,12 +67,13 @@ class CryptoRpcProvider {
pass: config.pass || config.rpcPass,
protocol: config.protocol
});
const rpcChain = !config.isEVM ? this.chain : 'ETH';
this.rpcs = {
[this.chain]: new RpcClasses[this.chain](this.config)
[this.chain]: new RpcClasses[rpcChain](this.config)
};
if (config.tokens) {
Object.entries(config.tokens).forEach(([token, tokenConfig]) => {
const TokenClass = TokenClasses[this.chain][tokenConfig.type];
const TokenClass = TokenClasses[rpcChain][tokenConfig.type];
const configForToken = Object.assign(tokenConfig, this.config);
this.rpcs[token] = new TokenClass(configForToken);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/eth.js
Expand Up @@ -81,7 +81,7 @@ describe('ETH Tests', function() {
sinon.spy(ethRPC.web3.eth, 'getBlock');
let maxFee = await ethRPC.estimateMaxFee({ percentile: 15 });
assert.isDefined(maxFee);
expect(maxFee).to.be.equal(2654455240);
expect(maxFee).to.be.equal(1154455240);
expect(ethRPC.web3.eth.getBlock.callCount).to.be.lt(10);
expect(ethRPC.emitter.emit.callCount).to.equal(0);
});
Expand All @@ -91,7 +91,7 @@ describe('ETH Tests', function() {
const maxPriorityFee = await ethRPC.estimateMaxPriorityFee({});
assert.isDefined(maxPriorityFee);
expect(maxPriorityFee).to.be.gt(0);
expect(maxPriorityFee).to.be.equal(2500000000);
expect(maxPriorityFee).to.be.equal(1000000000);
expect(ethRPC.blockMaxPriorityFeeCache.set.callCount).to.equal(0);
});

Expand Down