Skip to content

Commit 6d5fbe5

Browse files
authored
use latest for pending tag (#908)
* use latest for pending tag * update tests * type polish * fix
1 parent d1f2660 commit 6d5fbe5

3 files changed

Lines changed: 25 additions & 44 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const TRACE_METHODS = [
1313
'getBalance',
1414
'getTransactionCount',
1515
'getEvmTransactionCount',
16-
'getSubstrateNonce',
1716
'getCode',
1817
'call',
1918
'_ethCall',

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -758,36 +758,12 @@ export abstract class BaseProvider extends AbstractProvider {
758758
return minedNonce + pendingNonce;
759759
};
760760

761-
getSubstrateNonce = async (
762-
addressOrName: string | Promise<string>,
763-
blockTag?: BlockTag | Promise<BlockTag>
764-
): Promise<number> => {
765-
const resolvedBlockTag = await blockTag;
766-
767-
const address = await addressOrName;
768-
const [substrateAddress, blockHash] = await Promise.all([
769-
this.getSubstrateAddress(address),
770-
this._getBlockHash(blockTag),
771-
]);
772-
773-
if (resolvedBlockTag === 'pending') {
774-
const idx = await this.api.rpc.system.accountNextIndex(substrateAddress);
775-
return idx.toNumber();
776-
}
777-
778-
const accountInfo = await this.queryStorage('system.account', [substrateAddress], blockHash);
779-
780-
return accountInfo.nonce.toNumber();
781-
};
782-
783761
getCode = async (
784762
addressOrName: string | Promise<string>,
785763
_blockTag?: BlockTag | Promise<BlockTag> | Eip1898BlockTag
786764
): Promise<string> => {
787765
const blockTag = await this._ensureSafeModeBlockTagFinalization(await parseBlockTag(_blockTag));
788766

789-
if (blockTag === 'pending') return '0x';
790-
791767
const [address, blockHash] = await Promise.all([
792768
addressOrName,
793769
this._getBlockHash(blockTag),
@@ -944,7 +920,7 @@ export abstract class BaseProvider extends AbstractProvider {
944920
*/
945921
estimateGas = async (
946922
transaction: Deferrable<TransactionRequest>,
947-
blockTag?: BlockTag | Promise<BlockTag>
923+
blockTag?: BlockTag,
948924
): Promise<BigNumber> => {
949925
const blockHash = blockTag && blockTag !== 'latest'
950926
? await this._getBlockHash(blockTag)
@@ -1186,7 +1162,7 @@ export abstract class BaseProvider extends AbstractProvider {
11861162
};
11871163
};
11881164

1189-
getSubstrateAddress = async (addressOrName: string, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> => {
1165+
getSubstrateAddress = async (addressOrName: string, blockTag?: BlockTag): Promise<string> => {
11901166
const [address, blockHash] = await Promise.all([
11911167
addressOrName,
11921168
this._getBlockHash(blockTag),
@@ -1197,7 +1173,7 @@ export abstract class BaseProvider extends AbstractProvider {
11971173
return substrateAccount.isEmpty ? computeDefaultSubstrateAddress(address) : substrateAccount.toString();
11981174
};
11991175

1200-
getEvmAddress = async (substrateAddress: string, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> => {
1176+
getEvmAddress = async (substrateAddress: string, blockTag?: BlockTag): Promise<string> => {
12011177
const blockHash = await this._getBlockHash(blockTag);
12021178
const evmAddress = await this.queryStorage<Option<H160>>('evmAccounts.evmAddresses', [substrateAddress], blockHash);
12031179

@@ -1208,10 +1184,7 @@ export abstract class BaseProvider extends AbstractProvider {
12081184
addressOrName: string | Promise<string>,
12091185
_blockTag?: BlockTag | Promise<BlockTag> | Eip1898BlockTag
12101186
): Promise<Option<EvmAccountInfo>> => {
1211-
let blockTag = await this._ensureSafeModeBlockTagFinalization(await parseBlockTag(_blockTag));
1212-
if (blockTag === 'pending') {
1213-
blockTag = 'latest';
1214-
}
1187+
const blockTag = await this._ensureSafeModeBlockTagFinalization(await parseBlockTag(_blockTag));
12151188

12161189
const [address, blockHash] = await Promise.all([
12171190
addressOrName,
@@ -1502,9 +1475,7 @@ export abstract class BaseProvider extends AbstractProvider {
15021475

15031476
_getBlockNumber = async (blockTag: BlockTag): Promise<number> => {
15041477
switch (blockTag) {
1505-
case 'pending': {
1506-
return logger.throwError('pending tag not implemented', Logger.errors.UNSUPPORTED_OPERATION);
1507-
}
1478+
case 'pending':
15081479
case 'latest': {
15091480
return this.getBlockNumber();
15101481
}
@@ -1531,13 +1502,11 @@ export abstract class BaseProvider extends AbstractProvider {
15311502
}
15321503
};
15331504

1534-
_getBlockHash = async (_blockTag?: BlockTag | Promise<BlockTag>): Promise<string> => {
1535-
const blockTag = (await _blockTag) || 'latest';
1505+
_getBlockHash = async (_blockTag?: BlockTag): Promise<string> => {
1506+
const blockTag = _blockTag ?? 'latest';
15361507

15371508
switch (blockTag) {
1538-
case 'pending': {
1539-
return logger.throwError('pending tag not supported', Logger.errors.UNSUPPORTED_OPERATION);
1540-
}
1509+
case 'pending':
15411510
case 'latest': {
15421511
return this.safeMode ? this.finalizedBlockHash : this.bestBlockHash;
15431512
}
@@ -1638,7 +1607,7 @@ export abstract class BaseProvider extends AbstractProvider {
16381607
};
16391608

16401609
_getBlockHeader = async (blockTag?: BlockTag | Promise<BlockTag>): Promise<Header> => {
1641-
const blockHash = await this._getBlockHash(blockTag);
1610+
const blockHash = await this._getBlockHash(await blockTag);
16421611

16431612
try {
16441613
const header = await this.api.rpc.chain.getHeader(blockHash);

packages/eth-rpc-adapter/src/__tests__/e2e/endpoint.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,48 +1911,56 @@ describe('endpoint', () => {
19111911
});
19121912
});
19131913

1914-
describe('finalized blocktag', () => {
1914+
describe('finalized/safe/pending blocktag', () => {
19151915
/* ----------
19161916
latest block <=> finalized block in local setup
19171917
---------- */
19181918
it('eth_getTransactionCount', async () => {
19191919
const res = (await eth_getTransactionCount([ADDRESS_ALICE, 'latest'])).data.result;
19201920
const resF = (await eth_getTransactionCount([ADDRESS_ALICE, 'finalized'])).data.result;
19211921
const resS = (await eth_getTransactionCount([ADDRESS_ALICE, 'safe'])).data.result;
1922+
const resP = (await eth_getTransactionCount([ADDRESS_ALICE, 'pending'])).data.result;
19221923

19231924
expect(parseInt(res)).to.greaterThan(0);
19241925
expect(res).to.equal(resF);
19251926
expect(res).to.equal(resS);
1927+
expect(res).to.equal(resP); // no pending in local setup
19261928
});
19271929

19281930
it('eth_getCode', async () => {
19291931
const res = (await eth_getCode([DETERMINISTIC_SETUP_DEX_ADDRESS, 'latest'])).data.result;
19301932
const resF = (await eth_getCode([DETERMINISTIC_SETUP_DEX_ADDRESS, 'finalized'])).data.result;
19311933
const resS = (await eth_getCode([DETERMINISTIC_SETUP_DEX_ADDRESS, 'safe'])).data.result;
1934+
const resP = (await eth_getCode([DETERMINISTIC_SETUP_DEX_ADDRESS, 'pending'])).data.result;
19321935

19331936
expect(res).not.to.be.undefined;
19341937
expect(res).to.equal(resF);
19351938
expect(res).to.equal(resS);
1939+
expect(res).to.equal(resP);
19361940
});
19371941

19381942
it('eth_getBalance', async () => {
19391943
const res = (await eth_getBalance([ADDRESS_ALICE, 'latest'])).data.result;
19401944
const resF = (await eth_getBalance([ADDRESS_ALICE, 'finalized'])).data.result;
19411945
const resS = (await eth_getBalance([ADDRESS_ALICE, 'safe'])).data.result;
1946+
const resP = (await eth_getBalance([ADDRESS_ALICE, 'pending'])).data.result;
19421947

19431948
expect(parseInt(res)).to.greaterThan(0);
19441949
expect(res).to.equal(resF);
19451950
expect(res).to.equal(resS);
1951+
expect(res).to.equal(resP);
19461952
});
19471953

19481954
it('eth_getBlockByNumber', async () => {
19491955
const res = (await eth_getBlockByNumber(['latest', false])).data.result;
19501956
const resF = (await eth_getBlockByNumber(['finalized', false])).data.result;
19511957
const resS = (await eth_getBlockByNumber(['safe', false])).data.result;
1958+
const resP = (await eth_getBlockByNumber(['pending', false])).data.result;
19521959

19531960
expect(res).not.to.be.undefined;
19541961
expect(res).to.deep.equal(resF);
19551962
expect(res).to.deep.equal(resS);
1963+
expect(res).to.deep.equal(resP);
19561964
});
19571965

19581966
it('eth_isBlockFinalized', async () => {
@@ -1994,8 +2002,13 @@ describe('endpoint', () => {
19942002
const { gasLimit } = await estimateGas(tx);
19952003
const bbb = (gasLimit.toNumber() % 100000) / 100;
19962004

1997-
const { gasLimit: gasLimitWithBlockTag } = await estimateGas(tx, 'latest');
1998-
expect(gasLimitWithBlockTag.toBigInt()).to.equal(gasLimit.toBigInt());
2005+
/* ---------- should work with latest and pending tag ---------- */
2006+
const { gasLimit: gasLimitLatest } = await estimateGas(tx, 'latest');
2007+
const { gasLimit: gasLimitPending } = await estimateGas(tx, 'pending');
2008+
2009+
expect(gasLimitLatest.toBigInt()).to.equal(gasLimit.toBigInt());
2010+
expect(gasLimitPending.toBigInt()).to.equal(gasLimit.toBigInt());
2011+
/* -------------------------------------------------------------- */
19992012

20002013
// should be passing gasLimit instead of usedGas
20012014
expect(bbb).to.gt(GAS_MONSTER_GAS_REQUIRED / 30000);

0 commit comments

Comments
 (0)