Skip to content

Commit

Permalink
update maxFeePerGas calculate logic
Browse files Browse the repository at this point in the history
change feeHistory field name

fix getFeeBurnt parameter issue

fix raw rpc error

fix lint

op code

update version
  • Loading branch information
Pana committed Jun 18, 2024
1 parent c74ba6c commit 8809484
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 173 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-conflux-sdk",
"description": "JavaScript Conflux Software Development Kit",
"version": "2.4.3",
"version": "2.4.5",
"license": "LGPL-3.0",
"author": "[email protected]",
"repository": "https://github.com/Conflux-Chain/js-conflux-sdk.git",
Expand Down Expand Up @@ -52,12 +52,12 @@
"websocket": "^1.0.35"
},
"devDependencies": {
"@conflux-dev/jsonrpc-spec": "^0.2.1",
"@babel/core": "^7.8.4",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/runtime": "^7.8.4",
"@conflux-dev/jsdoc-tsimport-plugin": "^1.0.5",
"@conflux-dev/jsonrpc-spec": "^0.0.5",
"@geekberry/jsdoc-to-md": "0.0.8",
"@types/node": "^14.0.23",
"babel-plugin-lodash": "^3.3.4",
Expand Down
8 changes: 7 additions & 1 deletion src/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ const rlp = require('./util/rlp');
const format = require('./util/format');
const cfxFormat = require('./rpc/types/formatter');
const { AccessList } = require('./primitives/AccessList');
const { TXRLP_TYPE_PREFIX_2930, TXRLP_TYPE_PREFIX_1559, TRANSACTION_TYPE_LEGACY, TRANSACTION_TYPE_EIP2930, TRANSACTION_TYPE_EIP1559 } = require('./CONST');
const {
TXRLP_TYPE_PREFIX_2930,
TXRLP_TYPE_PREFIX_1559,
TRANSACTION_TYPE_LEGACY,
TRANSACTION_TYPE_EIP2930,
TRANSACTION_TYPE_EIP1559,
} = require('./CONST');

/**
* @typedef {import('./rpc/types/formatter').CallRequest} TransactionMeta
Expand Down
28 changes: 23 additions & 5 deletions src/rpc/cfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ class CFX extends RPCMethodFactory {
},
{
method: 'cfx_maxPriorityFeePerGas',
alias: 'maxPriorityFeePerGas',
responseFormatter: format.bigUInt,
},
{
method: 'cfx_getFeeBurnt',
alias: 'getFeeBurnt',
requestFormatters: [
format.epochNumberOrUndefined,
],
responseFormatter: format.bigUInt,
},
{
method: 'cfx_feeHistory',
alias: 'feeHistory',
requestFormatters: [
format.bigUIntHex,
format.epochNumber,
Expand Down Expand Up @@ -467,6 +467,10 @@ class CFX extends RPCMethodFactory {
options.epochHeight = await this.epochNumber();
}

if (options.gasPrice && (options.maxFeePerGas || options.maxPriorityFeePerGas)) {
throw new Error('`gasPrice` should not be set with `maxFeePerGas` or `maxPriorityFeePerGas`');
}

// auto detect transaction type
let baseFeePerGas;
if (options.type === undefined) {
Expand Down Expand Up @@ -510,18 +514,32 @@ class CFX extends RPCMethodFactory {
options.gasPrice = defaultGasPrice;
}
}
options.maxFeePerGas = undefined;
options.maxPriorityFeePerGas = undefined;
}
// auto fill maxPriorityFeePerGas and maxFeePerGas
if (options.type === CONST.TRANSACTION_TYPE_EIP1559) {
if (!options.maxPriorityFeePerGas || !options.maxFeePerGas) {
if (options.gasPrice) {
options.maxFeePerGas = options.gasPrice;
options.maxPriorityFeePerGas = options.gasPrice;
options.gasPrice = undefined;
}

if (!options.maxPriorityFeePerGas) {
options.maxPriorityFeePerGas = await this.maxPriorityFeePerGas();
}

if (!options.maxFeePerGas) {
if (!baseFeePerGas) {
const block = await this.getBlockByEpochNumber(options.epochHeight, false);
baseFeePerGas = block.baseFeePerGas;
}

options.maxFeePerGas = options.maxPriorityFeePerGas + (baseFeePerGas * BigInt(4)) / BigInt(3);
options.maxFeePerGas = options.maxPriorityFeePerGas + baseFeePerGas * BigInt(2);
}

if (options.maxFeePerGas < options.maxPriorityFeePerGas) {
throw new Error('`maxFeePerGas` should not be less than `maxPriorityFeePerGas`');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/rpc/types/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,13 @@ cfxFormat.wrapTransaction = format({

/**
* @typedef {Object} FeeHistory
* @property {BigInt} oldestBlock Lowest block number of returned range.
* @property {BigInt} oldestEpoch Lowest epoch number of returned range.
* @property {number[]} gasUsedRatio An array of block gas used ratios. These are calculated as the ratio of tx gasLimit sum and block gasLimit.
* @property {BigInt[]} baseFeePerGas An array of block base fees per gas. This includes the next block after the newest of the returned range, because this value can be derived from the newest block. Zeroes are returned for pre-EIP-1559 blocks.
* @property {BigInt[][]} reward A two-dimensional array of effective priority fees per gas at the requested block percentiles.
*/
cfxFormat.feeHistory = format({
oldestBlock: format.bigUInt,
oldestEpoch: format.bigUInt,
baseFeePerGas: [format.bigUInt],
reward: [[format.bigUInt]],
gasUsedRatio: format.any,
Expand Down
8 changes: 8 additions & 0 deletions src/rpc/types/rawFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ format.rawTransaction = format({
v: format.bigUIntHex,
status: format.bigUIntHex.$or(null),
transactionIndex: format.bigUIntHex.$or(null),
maxFeePerGas: format.bigUIntHex.$or(null),
maxPriorityFeePerGas: format.bigUIntHex.$or(null),
type: format.bigUIntHex.$or(null),
yParity: format.bigUIntHex.$or(null),
}, {
name: 'format.rawTransaction',
}).$or(null);
Expand All @@ -39,6 +43,9 @@ format.rawReceipt = format({
gasUsed: format.bigUIntHex,
gasFee: format.bigUIntHex,
storageCollateralized: format.bigUIntHex,
type: format.bigUIntHex.$or(null),
effectiveGasPrice: format.bigUIntHex.$or(null),
burntGasFee: format.bigUIntHex.$or(null),
storageReleased: [{
collaterals: format.bigUIntHex,
}],
Expand All @@ -60,6 +67,7 @@ format.rawLogs = format([format.rawLog]);
format.rawBlock = format({
epochNumber: format.bigUIntHex.$or(null),
blockNumber: format.bigUIntHex.$or(null),
baseFeePerGas: format.bigUIntHex.$or(null),
blame: format.bigUIntHex,
height: format.bigUIntHex,
size: format.bigUIntHex,
Expand Down
Loading

0 comments on commit 8809484

Please sign in to comment.