Skip to content

Commit 35f91d1

Browse files
committed
export eth gas decoder
1 parent 68063ac commit 35f91d1

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

packages/eth-providers/src/base-provider.ts

+27-46
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,10 @@ import {
5050
EMTPY_UNCLES,
5151
EMTPY_UNCLE_HASH,
5252
ERROR_PATTERN,
53-
GAS_LIMIT_CHUNK,
54-
GAS_MASK,
5553
LOCAL_MODE_MSG,
56-
MAX_GAS_LIMIT_CC,
5754
ONE_HUNDRED_GWEI,
5855
PROD_MODE_MSG,
5956
SAFE_MODE_WARNING_MSG,
60-
STORAGE_MASK,
61-
TEN_GWEI,
62-
U32_MAX,
6357
ZERO,
6458
ZERO_BLOCK_HASH,
6559
} from './consts';
@@ -75,8 +69,8 @@ import {
7569
checkEvmExecutionError,
7670
computeDefaultEvmAddress,
7771
computeDefaultSubstrateAddress,
72+
decodeEthGas,
7873
encodeGasLimit,
79-
ethToNativeDecimal,
8074
filterLog,
8175
filterLogByTopics,
8276
getAllReceiptsAtBlock,
@@ -1228,10 +1222,12 @@ export abstract class BaseProvider extends AbstractProvider {
12281222
accessList: AccessList;
12291223
v2: boolean;
12301224
} => {
1231-
let gasLimit = 0n;
1232-
let storageLimit = 0n;
1233-
let validUntil = 0n;
1234-
let tip = 0n;
1225+
let substrateParams: {
1226+
gasLimit: bigint,
1227+
storageLimit: bigint,
1228+
validUntil: bigint,
1229+
tip: bigint,
1230+
};
12351231
let v2 = false;
12361232

12371233
if (ethTx.type === 96) {
@@ -1241,10 +1237,12 @@ export abstract class BaseProvider extends AbstractProvider {
12411237
if (!ethTx.validUntil) return logger.throwError('expect validUntil');
12421238
if (!ethTx.tip) return logger.throwError('expect priorityFee (tip)');
12431239

1244-
gasLimit = ethTx.gasLimit.toBigInt();
1245-
storageLimit = BigInt(ethTx.storageLimit.toString());
1246-
validUntil = BigInt(ethTx.validUntil.toString());
1247-
tip = BigInt(ethTx.tip.toString());
1240+
substrateParams = {
1241+
gasLimit: ethTx.gasLimit.toBigInt(),
1242+
storageLimit: BigInt(ethTx.storageLimit.toString()),
1243+
validUntil: BigInt(ethTx.validUntil.toString()),
1244+
tip: BigInt(ethTx.tip.toString()),
1245+
};
12481246
} else if (
12491247
ethTx.type === undefined || // legacy
12501248
ethTx.type === null || // legacy
@@ -1253,48 +1251,34 @@ export abstract class BaseProvider extends AbstractProvider {
12531251
try {
12541252
const { storageDepositPerByte, txFeePerGas } = this._getGasConsts();
12551253

1256-
const params = calcSubstrateTransactionParams({
1254+
const { gasLimit, validUntil, storageLimit } = calcSubstrateTransactionParams({
12571255
txGasPrice: ethTx.maxFeePerGas || ethTx.gasPrice || '0',
12581256
txGasLimit: ethTx.gasLimit || '0',
12591257
storageByteDeposit: storageDepositPerByte,
12601258
txFeePerGas: txFeePerGas,
12611259
});
12621260

1263-
gasLimit = params.gasLimit.toBigInt();
1264-
validUntil = params.validUntil.toBigInt();
1265-
storageLimit = params.storageLimit.toBigInt();
1266-
tip = (ethTx.maxPriorityFeePerGas?.toBigInt() || 0n) * gasLimit;
1267-
1268-
if (gasLimit < 0n || validUntil < 0n || storageLimit < 0n) {
1261+
if (gasLimit.lt(0) || validUntil.lt(0) || storageLimit.lt(0)) {
12691262
throw new Error();
12701263
}
1264+
1265+
substrateParams = {
1266+
gasLimit: gasLimit.toBigInt(),
1267+
validUntil: validUntil.toBigInt(),
1268+
storageLimit: storageLimit.toBigInt(),
1269+
tip: 0n,
1270+
};
12711271
} catch (error) {
12721272
// v2
12731273
v2 = true;
12741274

12751275
if (!ethTx.gasLimit) return logger.throwError('expect gasLimit');
12761276
if (!ethTx.gasPrice) return logger.throwError('expect gasPrice');
12771277

1278-
const bbbcc = ethTx.gasLimit.mod(GAS_MASK);
1279-
const encodedGasLimit = bbbcc.div(STORAGE_MASK); // bbb
1280-
const encodedStorageLimit = bbbcc.mod(STORAGE_MASK); // cc
1281-
1282-
let gasPrice = ethTx.gasPrice;
1283-
const tipNumber = gasPrice.div(TEN_GWEI).sub(10);
1284-
if (tipNumber.gt(0)) {
1285-
gasPrice = gasPrice.sub(tipNumber.mul(TEN_GWEI));
1286-
const ethTip = gasPrice.mul(ethTx.gasLimit).mul(tipNumber).div(10);
1287-
tip = ethToNativeDecimal(ethTip).toBigInt();
1288-
}
1289-
1290-
validUntil = gasPrice.sub(ONE_HUNDRED_GWEI).toBigInt();
1291-
if (validUntil > U32_MAX) {
1292-
validUntil = U32_MAX;
1293-
}
1294-
gasLimit = encodedGasLimit.mul(GAS_LIMIT_CHUNK).toBigInt();
1295-
storageLimit = BigNumber.from(2)
1296-
.pow(encodedStorageLimit.gt(MAX_GAS_LIMIT_CC) ? MAX_GAS_LIMIT_CC : encodedStorageLimit)
1297-
.toBigInt();
1278+
substrateParams = decodeEthGas({
1279+
gasLimit: ethTx.gasLimit,
1280+
gasPrice: ethTx.gasPrice,
1281+
});
12981282
}
12991283
} else if (ethTx.type === 1 || ethTx.type === 2) {
13001284
return logger.throwError(
@@ -1308,10 +1292,7 @@ export abstract class BaseProvider extends AbstractProvider {
13081292
}
13091293

13101294
return {
1311-
gasLimit,
1312-
storageLimit,
1313-
validUntil,
1314-
tip,
1295+
...substrateParams,
13151296
accessList: ethTx.accessList ?? [],
13161297
v2,
13171298
};

packages/eth-providers/src/utils/transactionHelper.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { BigNumber, BigNumberish, Transaction } from 'ethers';
22
import { Deferrable, resolveProperties } from '@ethersproject/properties';
3-
import { GAS_LIMIT_CHUNK, GAS_MASK, STORAGE_MASK } from '../consts';
43
import { TransactionRequest } from '@ethersproject/abstract-provider';
54
import { accessListify } from '@ethersproject/transactions';
6-
import { formatter } from './receiptHelper';
75
import { hexlify } from '@ethersproject/bytes';
86

7+
import { GAS_LIMIT_CHUNK, GAS_MASK, MAX_GAS_LIMIT_CC, ONE_HUNDRED_GWEI, STORAGE_MASK, TEN_GWEI, U32_MAX } from '../consts';
8+
import { ethToNativeDecimal } from './utils';
9+
import { formatter } from './receiptHelper';
10+
911
type TxConsts = {
1012
storageByteDeposit: BigNumberish;
1113
txFeePerGas: BigNumberish;
@@ -144,3 +146,43 @@ export const encodeGasLimit = (
144146

145147
return aaaa00000.add(bbb00).add(cc); // aaaabbbcc
146148
};
149+
150+
export const decodeEthGas = ({
151+
gasPrice,
152+
gasLimit,
153+
}: {
154+
gasPrice: BigNumber,
155+
gasLimit: BigNumber,
156+
}) => {
157+
const bbbcc = gasLimit.mod(GAS_MASK);
158+
const encodedGasLimit = bbbcc.div(STORAGE_MASK); // bbb
159+
const encodedStorageLimit = bbbcc.mod(STORAGE_MASK); // cc
160+
161+
let tip = 0n;
162+
const tipNumber = gasPrice.div(TEN_GWEI).sub(10);
163+
if (tipNumber.gt(0)) {
164+
gasPrice = gasPrice.sub(tipNumber.mul(TEN_GWEI));
165+
const ethTip = gasPrice.mul(gasLimit).mul(tipNumber).div(10);
166+
tip = ethToNativeDecimal(ethTip).toBigInt();
167+
}
168+
169+
let validUntil = gasPrice.sub(ONE_HUNDRED_GWEI).toBigInt();
170+
if (validUntil > U32_MAX) {
171+
validUntil = U32_MAX;
172+
}
173+
const substrateGasLimit = encodedGasLimit.mul(GAS_LIMIT_CHUNK).toBigInt();
174+
const storageLimit = BigNumber.from(2)
175+
.pow(
176+
encodedStorageLimit.gt(MAX_GAS_LIMIT_CC)
177+
? MAX_GAS_LIMIT_CC
178+
: encodedStorageLimit
179+
)
180+
.toBigInt();
181+
182+
return {
183+
gasLimit: substrateGasLimit,
184+
storageLimit,
185+
tip,
186+
validUntil,
187+
};
188+
};

0 commit comments

Comments
 (0)