Skip to content

Commit

Permalink
op fluent cfx_call compatibility (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pana authored Sep 9, 2024
1 parent 5ef99c4 commit 803ad65
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion 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.9",
"version": "2.4.10",
"license": "LGPL-3.0",
"author": "[email protected]",
"repository": "https://github.com/Conflux-Chain/js-conflux-sdk.git",
Expand Down
36 changes: 26 additions & 10 deletions src/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -140,7 +140,7 @@ class Transaction {
* @return {Transaction}
*/
constructor({
type = 0,
type,
from,
nonce,
gasPrice,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions test/contract/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -145,8 +145,8 @@ test('contract.sendTransaction', async () => {
gas: '0x0',
storageLimit: '0x0',
chainId: '0x1',
type: '0x0',
accessList: null,
// type: '0x0',
// accessList: null,
});

call.mockRestore();
Expand Down
12 changes: 6 additions & 6 deletions test/contract/stringContractAbi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -146,8 +146,8 @@ test('contract.sendTransaction', async () => {
gas: '0x0',
storageLimit: '0x0',
chainId: '0x1',
type: '0x0',
accessList: null,
// type: '0x0',
// accessList: null,
});

call.mockRestore();
Expand Down

0 comments on commit 803ad65

Please sign in to comment.