diff --git a/.changeset/filthy-bacteria-multiply.md b/.changeset/filthy-bacteria-multiply.md new file mode 100644 index 00000000000..45e22cf4fb3 --- /dev/null +++ b/.changeset/filthy-bacteria-multiply.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/account": patch +--- + +chore: merge `chain` and `nodeInfo` queries into one diff --git a/packages/account/src/providers/operations.graphql b/packages/account/src/providers/operations.graphql index 2d66b625732..4e1154c96c1 100644 --- a/packages/account/src/providers/operations.graphql +++ b/packages/account/src/providers/operations.graphql @@ -617,6 +617,15 @@ query getChain { } } +query getChainAndNodeInfo { + chain { + ...chainInfoFragment + } + nodeInfo { + ...nodeInfoFragment + } +} + query getTransaction($transactionId: TransactionId!) { transaction(id: $transactionId) { ...transactionFragment diff --git a/packages/account/src/providers/provider.test.ts b/packages/account/src/providers/provider.test.ts index 52af88effb5..76975eb60f4 100644 --- a/packages/account/src/providers/provider.test.ts +++ b/packages/account/src/providers/provider.test.ts @@ -1067,36 +1067,31 @@ describe('Provider', () => { const { provider } = launched; expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1); - expect(spyFetchChain).toHaveBeenCalledTimes(1); - expect(spyFetchNode).toHaveBeenCalledTimes(1); provider.getChain(); provider.getNode(); expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1); - expect(spyFetchChain).toHaveBeenCalledTimes(1); - expect(spyFetchNode).toHaveBeenCalledTimes(1); + expect(spyFetchChain).toHaveBeenCalledTimes(0); + expect(spyFetchNode).toHaveBeenCalledTimes(0); }); it('should ensure fetchChainAndNodeInfo always fetch new data', async () => { Provider.clearChainAndNodeCaches(); const spyFetchChainAndNodeInfo = vi.spyOn(Provider.prototype, 'fetchChainAndNodeInfo'); - const spyFetchChain = vi.spyOn(Provider.prototype, 'fetchChain'); - const spyFetchNode = vi.spyOn(Provider.prototype, 'fetchNode'); using launched = await setupTestProviderAndWallets(); - const { provider } = launched; expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1); - expect(spyFetchChain).toHaveBeenCalledTimes(1); - expect(spyFetchNode).toHaveBeenCalledTimes(1); + + const { provider } = launched; + + const spyOperation = vi.spyOn(provider.operations, 'getChainAndNodeInfo'); await provider.fetchChainAndNodeInfo(); - expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(2); - expect(spyFetchChain).toHaveBeenCalledTimes(2); - expect(spyFetchNode).toHaveBeenCalledTimes(2); + expect(spyOperation).toHaveBeenCalledTimes(1); }); it('should ensure getGasConfig return essential gas related data', async () => { @@ -1204,50 +1199,6 @@ Supported fuel-core version: ${mock.supportedVersion}.` ); }); - it('should ensure fuel node version warning is shown before chain incompatibility error', async () => { - const { FUEL_CORE } = versions; - const [major, minor, patch] = FUEL_CORE.split('.'); - const majorMismatch = major === '0' ? 1 : parseInt(patch, 10) - 1; - - const mock = { - isMajorSupported: false, - isMinorSupported: true, - isPatchSupported: true, - supportedVersion: `${majorMismatch}.${minor}.${patch}`, - }; - - if (mock.supportedVersion === FUEL_CORE) { - throw new Error(); - } - - const spy = vi.spyOn(fuelTsVersionsMod, 'checkFuelCoreVersionCompatibility'); - spy.mockImplementationOnce(() => mock); - - const consoleWarnSpy = vi.spyOn(console, 'warn'); - - const graphQLDummyError = `Unknown field "height" on type "Block". - Unknown field "version" on type "ScriptParameters". - Unknown field "version" on type "ConsensusParameters".`; - - const fuelError = new FuelError(ErrorCode.INVALID_REQUEST, graphQLDummyError); - - const fetchChainSpy = vi - .spyOn(Provider.prototype, 'fetchChain') - .mockImplementationOnce(async () => Promise.reject(fuelError)); - - await expectToThrowFuelError(() => setupTestProviderAndWallets(), fuelError); - - expect(consoleWarnSpy).toHaveBeenCalledOnce(); - expect(consoleWarnSpy).toHaveBeenCalledWith( - `The Fuel Node that you are trying to connect to is using fuel-core version ${FUEL_CORE}, -which is not supported by the version of the TS SDK that you are using. -Things may not work as expected. -Supported fuel-core version: ${mock.supportedVersion}.` - ); - - expect(fetchChainSpy).toHaveBeenCalledOnce(); - }); - it('An invalid subscription request throws a FuelError and does not hold the test runner (closes all handles)', async () => { using launched = await setupTestProviderAndWallets(); const { provider } = launched; diff --git a/packages/account/src/providers/provider.ts b/packages/account/src/providers/provider.ts index 586b62000c0..af936e4eb0d 100644 --- a/packages/account/src/providers/provider.ts +++ b/packages/account/src/providers/provider.ts @@ -616,13 +616,26 @@ export default class Provider { * @returns A promise that resolves to the Chain and NodeInfo. */ async fetchChainAndNodeInfo() { - const nodeInfo = await this.fetchNode(); - Provider.ensureClientVersionIsSupported(nodeInfo); - const chain = await this.fetchChain(); + const { nodeInfo, chain } = await this.operations.getChainAndNodeInfo(); + + const processedNodeInfo: NodeInfo = { + maxDepth: bn(nodeInfo.maxDepth), + maxTx: bn(nodeInfo.maxTx), + nodeVersion: nodeInfo.nodeVersion, + utxoValidation: nodeInfo.utxoValidation, + vmBacktrace: nodeInfo.vmBacktrace, + }; + + Provider.ensureClientVersionIsSupported(processedNodeInfo); + + const processedChain = processGqlChain(chain); + + Provider.chainInfoCache[this.urlWithoutAuth] = processedChain; + Provider.nodeInfoCache[this.urlWithoutAuth] = processedNodeInfo; return { - chain, - nodeInfo, + chain: processedChain, + nodeInfo: processedNodeInfo, }; }