Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Trivial changes for dynamic base fee #664

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/caver-klay/caver-klay-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ const Accounts = function Accounts(...args) {
delete this.BatchRequest
delete this.extend

const _klaytnCall = [rpc.getChainId, rpc.getGasPrice, rpc.getTransactionCount]
const _klaytnCall = [rpc.getChainId, rpc.getGasPrice, rpc.getTransactionCount, rpc.getHeader]
// attach methods to this._klaytnCall
this._klaytnCall = {}
_.each(_klaytnCall, function(method) {
Expand Down Expand Up @@ -393,6 +393,27 @@ Accounts.prototype._getRoleKey = function _getRoleKey(tx, account) {
return key
}

/**
* _getSuggestedGasPrice returns suggested gas price.
* This function will be used to set gasPrice field if that is omitted.
* Before common architecture does not support newly added transaction types.
*
* @method _getSuggestedGasPrice
* @return {string}
*/
Accounts.prototype._getSuggestedGasPrice = async function _getSuggestedGasPrice() {
const header = await this._klaytnCall.getHeader('latest')
const bf = utils.hexToNumber(header.baseFeePerGas || '0x0')

// In before common architecture, ethereum typed transactions are not supported.
// So just depends on baseFeePerGas field in the header,
// return `baseFee * 2` or `gasPrice`.
if (bf > 0) return bf * 2

const gasPrice = await this._klaytnCall.getGasPrice()
return gasPrice
}

/**
* create function creates random account with entropy.
*
Expand Down Expand Up @@ -888,7 +909,7 @@ Accounts.prototype.signTransaction = function signTransaction() {
// Otherwise, get the missing info from the Klaytn Node
return Promise.all([
isNot(tx.chainId) ? _this._klaytnCall.getChainId() : tx.chainId,
isNot(tx.gasPrice) ? _this._klaytnCall.getGasPrice() : tx.gasPrice,
isNot(tx.gasPrice) ? _this._getSuggestedGasPrice() : tx.gasPrice,
isNot(tx.nonce) ? _this._klaytnCall.getTransactionCount(tx.from, 'pending') : tx.nonce,
]).then(function(args) {
if (isNot(args[0]) || isNot(args[1]) || isNot(args[2])) {
Expand Down
18 changes: 18 additions & 0 deletions packages/caver-rpc/src/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ const Net = function Net(...args) {
call: 'net_peerCountByType',
params: 0,
}),
/**
* Returns the current klaytn protocol version.
*
* @memberof Net
* @method getVersion
* @instance
*
* @example
* const result = await caver.rpc.net.getVersion()
*
* @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second.
* @return {Promise<string>} The klaytn protocol version.
*/
new Method({
name: 'getVersion',
call: 'net_version',
params: 0,
}),
]

netMethods.forEach(function(method) {
Expand Down
2 changes: 1 addition & 1 deletion test/packages/caver.transaction/ethereumDynamicFee.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ describe('TxTypeEthereumDynamicFee', () => {
})

context('ethereumDynamicFee.fillTransaction', () => {
it('CAVERJS-UNIT-TRANSACTION-547: fillTransaction should call klay_getMaxPriorityFeePerGas to fill maxPriorityFeePerGas when maxPriorityFeePerGas is undefined', async () => {
it('CAVERJS-UNIT-TRANSACTION-547: fillTransaction should call klay_maxPriorityFeePerGas to fill maxPriorityFeePerGas when maxPriorityFeePerGas is undefined', async () => {
transactionObj.nonce = '0x3a'
delete transactionObj.maxPriorityFeePerGas
const tx = caver.transaction.ethereumDynamicFee.create(transactionObj)
Expand Down
1 change: 1 addition & 0 deletions types/packages/caver-core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface TransactionReceipt {
blockNumber: string
codeFormat?: string
chainId?: string
effectiveGasPrice?: string
feePayer?: string
feePayerSignatures?: SignatureForRPC[]
feeRatio?: string
Expand Down