From 803ad658f8c5f57e4e9653885d7d2db9076c2eb4 Mon Sep 17 00:00:00 2001 From: Pana Date: Mon, 9 Sep 2024 10:25:44 +0800 Subject: [PATCH] op fluent cfx_call compatibility (#274) --- package.json | 2 +- src/Transaction.js | 36 ++++++++++++++++++------- test/contract/contract.test.js | 8 +++--- test/contract/stringContractAbi.test.js | 12 ++++----- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 419f963..2763876 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "js-conflux-sdk", "description": "JavaScript Conflux Software Development Kit", - "version": "2.4.9", + "version": "2.4.10", "license": "LGPL-3.0", "author": "pan.wang@confluxnetwork.org", "repository": "https://github.com/Conflux-Chain/js-conflux-sdk.git", diff --git a/src/Transaction.js b/src/Transaction.js index f281fe1..edee3e7 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -68,7 +68,7 @@ class Transaction { const formatedMeta = Transaction.formatTxMeta({ nonce, gas, to, value, storageLimit, epochHeight, chainId, data, r, s, v }); const tx = new Transaction({ - type: 0, + type: TRANSACTION_TYPE_LEGACY, gasPrice: format.bigIntFromBuffer(gasPrice), ...formatedMeta, }); @@ -86,7 +86,7 @@ class Transaction { const formatedMeta = Transaction.formatTxMeta({ nonce, gas, to, value, storageLimit, epochHeight, chainId, data, r, s, v }); const tx = new Transaction({ - type: 1, + type: TRANSACTION_TYPE_EIP2930, gasPrice: format.bigIntFromBuffer(gasPrice), accessList, ...formatedMeta, @@ -106,7 +106,7 @@ class Transaction { const formatedMeta = Transaction.formatTxMeta({ nonce, gas, to, value, storageLimit, epochHeight, chainId, data, r, s, v }); const tx = new Transaction({ - type: 2, + type: TRANSACTION_TYPE_EIP1559, maxPriorityFeePerGas: format.bigIntFromBuffer(maxPriorityFeePerGas), maxFeePerGas: format.bigIntFromBuffer(maxFeePerGas), accessList, @@ -140,7 +140,7 @@ class Transaction { * @return {Transaction} */ constructor({ - type = 0, + type, from, nonce, gasPrice, @@ -172,7 +172,7 @@ class Transaction { this.v = v; this.r = r; this.s = s; - this.accessList = accessList ? new AccessList(accessList) : null; + this.accessList = accessList ? new AccessList(accessList) : undefined; this.maxPriorityFeePerGas = maxPriorityFeePerGas; this.maxFeePerGas = maxFeePerGas; } @@ -226,11 +226,27 @@ class Transaction { return format.publicKey(publicKey); } + /** + * Infer the transaction type from the fields. + * @returns {number} Transaction type + */ + txType() { + if (this.type !== undefined) { + return this.type; + } else if (this.maxPriorityFeePerGas !== undefined && this.maxFeePerGas !== undefined) { + return TRANSACTION_TYPE_EIP1559; + } else if (this.accessList !== undefined) { + return TRANSACTION_TYPE_EIP2930; + } else { + return TRANSACTION_TYPE_LEGACY; + } + } + typePrefix() { let prefix = Buffer.from([]); - if (this.type === 1) { + if (this.txType() === TRANSACTION_TYPE_EIP2930) { prefix = TXRLP_TYPE_PREFIX_2930; - } else if (this.type === 2) { + } else if (this.txType() === TRANSACTION_TYPE_EIP1559) { prefix = TXRLP_TYPE_PREFIX_1559; } return prefix; @@ -248,20 +264,20 @@ class Transaction { */ encode(includeSignature) { let raw; - if (this.type === TRANSACTION_TYPE_LEGACY) { // legacy transaction + if (this.txType() === TRANSACTION_TYPE_LEGACY) { // legacy transaction const { nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data, v, r, s } = cfxFormat.signTx(this); raw = includeSignature ? [[nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data], v, r, s] : [nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data]; - } else if (this.type === TRANSACTION_TYPE_EIP2930) { // 2930 transaction + } else if (this.txType() === TRANSACTION_TYPE_EIP2930) { // 2930 transaction const { nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data, v, r, s } = cfxFormat.signTx(this); const accessList = this.encodeAccessList(); raw = includeSignature ? [[nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data, accessList], v, r, s] : [nonce, gasPrice, gas, to, value, storageLimit, epochHeight, chainId, data, accessList]; - } else if (this.type === TRANSACTION_TYPE_EIP1559) { // 1559 transaction + } else if (this.txType() === TRANSACTION_TYPE_EIP1559) { // 1559 transaction const { nonce, maxPriorityFeePerGas, maxFeePerGas, gas, to, value, storageLimit, epochHeight, chainId, data, v, r, s } = cfxFormat.sign1559Tx(this); const accessList = this.encodeAccessList(); diff --git a/test/contract/contract.test.js b/test/contract/contract.test.js index a13c29e..43ffe9e 100644 --- a/test/contract/contract.test.js +++ b/test/contract/contract.test.js @@ -125,8 +125,8 @@ test('contract.estimateGasAndCollateral', async () => { expect(call).toHaveBeenLastCalledWith('cfx_estimateGasAndCollateral', { to: address, data: '0x06661abd', - type: '0x0', - accessList: null, + // type: '0x0', + // accessList: null, }, undefined); call.mockRestore(); @@ -145,8 +145,8 @@ test('contract.sendTransaction', async () => { gas: '0x0', storageLimit: '0x0', chainId: '0x1', - type: '0x0', - accessList: null, + // type: '0x0', + // accessList: null, }); call.mockRestore(); diff --git a/test/contract/stringContractAbi.test.js b/test/contract/stringContractAbi.test.js index e7c5a83..af9d0ad 100644 --- a/test/contract/stringContractAbi.test.js +++ b/test/contract/stringContractAbi.test.js @@ -63,8 +63,8 @@ test('contract.call', async () => { expect(call).toHaveBeenLastCalledWith('cfx_call', { to: address, data: '0x06661abd', - type: '0x0', - accessList: null, + // type: '0x0', + // accessList: null, }, undefined); const error = new Error(); @@ -126,8 +126,8 @@ test('contract.estimateGasAndCollateral', async () => { expect(call).toHaveBeenLastCalledWith('cfx_estimateGasAndCollateral', { to: address, data: '0x06661abd', - type: '0x0', - accessList: null, + // type: '0x0', + // accessList: null, }, undefined); call.mockRestore(); @@ -146,8 +146,8 @@ test('contract.sendTransaction', async () => { gas: '0x0', storageLimit: '0x0', chainId: '0x1', - type: '0x0', - accessList: null, + // type: '0x0', + // accessList: null, }); call.mockRestore();