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

Fixed testing logic for dynamic base fee #672

Merged
merged 6 commits into from
Jul 8, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"txUnitTest": "./node_modules/mocha/bin/mocha test/packages/caver.transaction/*.js && ./node_modules/mocha/bin/mocha test/createTest.js",
"etcUnitTest": "./node_modules/mocha/bin/mocha test/decodeFunctionCall.js && ./node_modules/mocha/bin/mocha test/contractError.js && ./node_modules/mocha/bin/mocha test/contractOverloading.js && ./node_modules/mocha/bin/mocha ./test/contract.setWallet.js && ./node_modules/mocha/bin/mocha ./test/setRequestManager.js && ./node_modules/mocha/bin/mocha test/reconnectTest.js && ./node_modules/mocha/bin/mocha test/hashMessage.js && ./node_modules/mocha/bin/mocha test/iban.* && ./node_modules/mocha/bin/mocha test/randomHex.js && ./node_modules/mocha/bin/mocha test/sha3.js && ./node_modules/mocha/bin/mocha test/toChecksumAddress.js && ./node_modules/mocha/bin/mocha test/default*&& ./node_modules/mocha/bin/mocha test/getNodeInfo.js && ./node_modules/mocha/bin/mocha test/getNetworkType.js && ./node_modules/mocha/bin/mocha test/isContractDeployment.js && ./node_modules/mocha/bin/mocha test/multiProviderTest.js && ./node_modules/mocha/bin/mocha test/unitMap.js && ./node_modules/mocha/bin/mocha test/invalidResponse.js && ./node_modules/mocha/bin/mocha test/createTest.js && ./node_modules/mocha/bin/mocha test/setProvider.js && ./node_modules/mocha/bin/mocha test/contractOverloading.js && ./node_modules/mocha/bin/mocha test/supportsSubscriptions.js && ./node_modules/mocha/bin/mocha test/packages/caver.abi.js",
"rpcTest": "./node_modules/mocha/bin/mocha test/packages/caver.rpc.js",
"txTest": "./node_modules/mocha/bin/mocha test/methodErrorHandling.js && ./node_modules/mocha/bin/mocha test/sendSignedTransaction.js && ./node_modules/mocha/bin/mocha test/estimateComputationCost.js && ./node_modules/mocha/bin/mocha test/getTransactionReceipt.js && ./node_modules/mocha/bin/mocha test/setNonceWithPendingTag.js && ./node_modules/mocha/bin/mocha test/getTransaction.js && ./node_modules/mocha/bin/mocha test/setContractOptions.js && ./node_modules/mocha/bin/mocha test/encodeContractDeploy.js && ./node_modules/mocha/bin/mocha test/accounts.signTransaction.js && ./node_modules/mocha/bin/mocha test/sendTransactionCallback.js && ./node_modules/mocha/bin/mocha test/signWithMultiSig.js && ./node_modules/mocha/bin/mocha test/transactionType/legacyTransaction.js && ./node_modules/mocha/bin/mocha test/transactionType/valueTransfer* && ./node_modules/mocha/bin/mocha test/transactionType/accountUpdate.js && ./node_modules/mocha/bin/mocha test/transactionType/contract* && ./node_modules/mocha/bin/mocha test/transactionType/cancelTransaction.js && ./node_modules/mocha/bin/mocha test/transactionType/feeDelegated*",
"txTest": "./node_modules/mocha/bin/mocha test/dynamicBaseFee.js && ./node_modules/mocha/bin/mocha test/methodErrorHandling.js && ./node_modules/mocha/bin/mocha test/sendSignedTransaction.js && ./node_modules/mocha/bin/mocha test/estimateComputationCost.js && ./node_modules/mocha/bin/mocha test/getTransactionReceipt.js && ./node_modules/mocha/bin/mocha test/setNonceWithPendingTag.js && ./node_modules/mocha/bin/mocha test/getTransaction.js && ./node_modules/mocha/bin/mocha test/setContractOptions.js && ./node_modules/mocha/bin/mocha test/encodeContractDeploy.js && ./node_modules/mocha/bin/mocha test/accounts.signTransaction.js && ./node_modules/mocha/bin/mocha test/sendTransactionCallback.js && ./node_modules/mocha/bin/mocha test/signWithMultiSig.js && ./node_modules/mocha/bin/mocha test/transactionType/*",
"etcTest": "./node_modules/mocha/bin/mocha ./test/packages/caver.contract.js && ./node_modules/mocha/bin/mocha ./test/tupleTest.js && ./node_modules/mocha/bin/mocha test/confirmationListener.js && ./node_modules/mocha/bin/mocha test/eventEmitter.js && ./node_modules/mocha/bin/mocha test/personal.js && ./node_modules/mocha/bin/mocha test/subscription.js && ./node_modules/mocha/bin/mocha test/contract.once.js && ./node_modules/mocha/bin/mocha test/ipfsTest.js",
"accountKeyTest": "./node_modules/mocha/bin/mocha test/scenarioTest/accountKeyPublic.js && ./node_modules/mocha/bin/mocha test/scenarioTest/accountKeyMultiSig.js && ./node_modules/mocha/bin/mocha test/scenarioTest/accountKeyRoleBased.js",
"kctTest": "./node_modules/mocha/bin/mocha test/packages/caver.klay.KIP7.js && ./node_modules/mocha/bin/mocha test/packages/caver.klay.KIP17.js && ./node_modules/mocha/bin/mocha test/packages/caver.kct.kip37.js && ./node_modules/mocha/bin/mocha test/createTest.js",
Expand Down
90 changes: 61 additions & 29 deletions packages/caver-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,25 @@ const buildSendRequestFunc = (defer, sendSignedTx, sendTxCallback) => (payload,
return method.requestManager.send(payload, sendTxCallback)
}

const buildSendFunc = (method, isSendTx) => async (...args) => {
const buildSendFunc = (method, isSendTx) => (...args) => {
const defer = utils.promiEvent(!isSendTx)
const payload = method.toPayload(args)

const sendTxCallback = buildSendTxCallbackFunc(defer, method, payload, isSendTx)
const sendSignedTx = buildSendSignedTxFunc(method, payload, sendTxCallback)
const sendRequest = buildSendRequestFunc(defer, sendSignedTx, sendTxCallback)

const isGasPriceInputMissing = isSendTx && _.isObject(payload.params[0]) && payload.params[0].gasPrice === undefined
// isSendTx can determine only for "send"Transaction request.
// For sign transaction request, we also need to fill up the optional values.
const isSignTx = method.name.includes('signTransaction')
let isGasPriceInputMissing = false
if ((isSendTx || isSignTx) && _.isObject(payload.params[0]) && payload.params[0].gasPrice === undefined) {
isGasPriceInputMissing = true
}

// The TxTypeEthereumDynamicFee transaction does not use the gasPrice field,
// so we need to check `maxPriorityFeePerGas` and `maxFeePerGas` field instead of `gasPrice`.
const isDynamicFeeTx = isSendTx && payload.params[0].type === TX_TYPE_STRING.TxTypeEthereumDynamicFee
const isDynamicFeeTx = (isSendTx || isSignTx) && payload.params[0].type === TX_TYPE_STRING.TxTypeEthereumDynamicFee
const filledDynamicGasFeeTx =
isDynamicFeeTx && payload.params[0].maxPriorityFeePerGas !== undefined && payload.params[0].maxFeePerGas !== undefined

Expand Down Expand Up @@ -490,42 +496,68 @@ const buildSendFunc = (method, isSendTx) => async (...args) => {
params: 0,
}).createFunction(method.requestManager)

const header = await getHeader('latest')
const bf = utils.hexToNumber(header.baseFeePerGas || '0x0')

// The baseFeePerGas is bigger than 0 means that Klaytn uses dynamic gas price.
if (bf > 0) {
if (!isDynamicFeeTx) {
payload.params[0].gasPrice = bf * 2
} else {
// If maxFeePerGas is undefined, set maxFeePerGas with `baseFee * 2`.
payload.params[0].maxFeePerGas = payload.params[0].maxFeePerGas || bf * 2
// If maxPriorityFeePerGas is undefined, call `klay_maxPriorityFeePerGas`.
if (payload.params[0].maxPriorityFeePerGas === undefined) {
const maxPriorityFeePerGas = await getMaxPriorityFeePerGas()
payload.params[0].maxPriorityFeePerGas = maxPriorityFeePerGas
getHeader('latest', (err, header) => {
// Get baseFee(`baseFeePerGas`) from block header
const baseFee = utils.hexToNumber(header.baseFeePerGas || '0x0')

// The baseFeePerGas is bigger than 0 means that Klaytn uses dynamic gas price.
if (baseFee > 0) {
if (!isDynamicFeeTx) {
payload.params[0].gasPrice = baseFee * 2
} else {
// If maxFeePerGas is undefined, set maxFeePerGas with `baseFee * 2`.
payload.params[0].maxFeePerGas = payload.params[0].maxFeePerGas || baseFee * 2
// If maxPriorityFeePerGas is undefined, call `klay_maxPriorityFeePerGas`.
if (payload.params[0].maxPriorityFeePerGas === undefined) {
return getMaxPriorityFeePerGas((e, maxPriorityFeePerGas) => {
payload.params[0].maxPriorityFeePerGas = maxPriorityFeePerGas
// Format gas price parameters(gasPrice, maxPriorityFeePerGas, maxFeePerGas)
formatGasParametersToHex(payload.params[0])
sendRequest(payload, method)
})
}
}
}
} else {
// If baseFeePerGas is not defined or 0, we need to use unit price for the `gasPrice` field.
const gp = await getGasPrice()
// The TxTypeEthereumDynamicFee transaction does not use the gasPrice field,
// so the gas price default is not set for TxTypeEthereumDynamicFee.
if (!isDynamicFeeTx) {
payload.params[0].gasPrice = payload.params[0].gasPrice || gp
} else {
payload.params[0].maxPriorityFeePerGas = payload.params[0].maxPriorityFeePerGas || gp
payload.params[0].maxFeePerGas = payload.params[0].maxFeePerGas || gp
// If baseFeePerGas is not defined or 0, we need to use unit price for the `gasPrice` field.
return getGasPrice((e, gp) => {
// The TxTypeEthereumDynamicFee transaction does not use the gasPrice field,
// so the gas price default is not set for TxTypeEthereumDynamicFee.
if (!isDynamicFeeTx) {
payload.params[0].gasPrice = payload.params[0].gasPrice || gp
} else {
payload.params[0].maxPriorityFeePerGas = payload.params[0].maxPriorityFeePerGas || gp
payload.params[0].maxFeePerGas = payload.params[0].maxFeePerGas || gp
}
// Format gas price parameters(gasPrice, maxPriorityFeePerGas, maxFeePerGas)
formatGasParametersToHex(payload.params[0])
sendRequest(payload, method)
})
}
}
// Format gas price parameters(gasPrice, maxPriorityFeePerGas, maxFeePerGas)
formatGasParametersToHex(payload.params[0])
sendRequest(payload, method)
})

sendRequest(payload, method)
/**
* attaching `.on('receipt')` is possible by returning defer.eventEmitter
*/
return defer.eventEmitter
}

// A function to change the format to a hex string after randomly filling the default gasPrice value
// with the API (personal_sendValueTransfer, personal_sendAccountUpdate) that does not use a transaction object.
function formatGasParametersToHex(txObject) {
if (txObject.gasPrice !== undefined && !utils.isHexStrict(txObject.gasPrice)) {
txObject.gasPrice = utils.toHex(txObject.gasPrice)
}
if (txObject.maxPriorityFeePerGas !== undefined && !utils.isHexStrict(txObject.maxPriorityFeePerGas)) {
txObject.maxPriorityFeePerGas = utils.toHex(txObject.maxPriorityFeePerGas)
}
if (txObject.maxFeePerGas !== undefined && !utils.isHexStrict(txObject.maxFeePerGas)) {
txObject.maxFeePerGas = utils.toHex(txObject.maxFeePerGas)
}
}

function buildCall() {
const method = this
const isSendTx =
Expand Down
Loading