From ef8353d80319e7dac4fa94f26c91fde1798f27e3 Mon Sep 17 00:00:00 2001 From: Cameron Manavian Date: Sun, 7 May 2023 22:59:13 -0700 Subject: [PATCH] rewrite using safeExec --- packages/providers/package.json | 1 + packages/providers/src/provider.test.ts | 167 ++++++++++++------------ packages/providers/src/provider.ts | 2 +- pnpm-lock.yaml | 3 + 4 files changed, 86 insertions(+), 87 deletions(-) diff --git a/packages/providers/package.json b/packages/providers/package.json index 19493721630..5bb6e9cfd3b 100644 --- a/packages/providers/package.json +++ b/packages/providers/package.json @@ -45,6 +45,7 @@ "lodash.clonedeep": "^4.5.0" }, "devDependencies": { + "@fuel-ts/utils": "workspace:*", "@graphql-codegen/cli": "^2.13.7", "@graphql-codegen/typescript": "^2.8.0", "@graphql-codegen/typescript-graphql-request": "^4.5.7", diff --git a/packages/providers/src/provider.test.ts b/packages/providers/src/provider.test.ts index 41e30493dc3..6013da6b821 100644 --- a/packages/providers/src/provider.test.ts +++ b/packages/providers/src/provider.test.ts @@ -6,6 +6,7 @@ import { randomBytes } from '@fuel-ts/keystore'; import { BN, bn } from '@fuel-ts/math'; import type { Receipt } from '@fuel-ts/transactions'; import { InputType, ReceiptType, TransactionType } from '@fuel-ts/transactions'; +import { safeExec } from '@fuel-ts/utils/test'; import * as GraphQL from 'graphql-request'; import Provider from './provider'; @@ -296,11 +297,10 @@ describe('Provider', () => { const provider = new Provider('http://127.0.0.1:4000/graphql'); const transactionRequest = new ScriptTransactionRequest({}); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - expect(provider.cache).toEqual(undefined); - } + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); + + expect(error).toBeTruthy(); + expect(provider.cache).toEqual(undefined); }); it('can cacheUtxo [will not cache inputs cache enabled + no coins]', async () => { @@ -318,12 +318,11 @@ describe('Provider', () => { inputs: [MessageInput], }); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - expect(provider.cache).toBeTruthy(); - expect(provider.cache?.getExcluded()).toStrictEqual([]); - } + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); + + expect(error).toBeTruthy(); + expect(provider.cache).toBeTruthy(); + expect(provider.cache?.getExcluded()).toStrictEqual([]); }); it('can cacheUtxo [will cache inputs cache enabled + coins]', async () => { @@ -373,16 +372,15 @@ describe('Provider', () => { inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC], }); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - const EXCLUDED = provider.cache?.getExcluded() || []; - expect(EXCLUDED.length).toEqual(3); - expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); - // clear cache - EXCLUDED.forEach((value) => provider.cache?.del(value)); - } + expect(error).toBeTruthy(); + const EXCLUDED = provider.cache?.getExcluded() || []; + expect(EXCLUDED.length).toEqual(3); + expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + + // clear cache + EXCLUDED.forEach((value) => provider.cache?.del(value)); }); it('can cacheUtxo [will cache inputs and also use in exclude list]', async () => { @@ -432,31 +430,30 @@ describe('Provider', () => { inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC], }); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - const EXCLUDED = provider.cache?.getExcluded() || []; - expect(EXCLUDED.length).toEqual(3); - expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); - - const owner = Address.fromRandom(); - const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] })); - // @ts-expect-error mock - provider.operations.getResourcesToSpend = resourcesToSpendMock; - await provider.getResourcesToSpend(owner, []); - - expect(resourcesToSpendMock).toHaveBeenCalledWith({ - owner: owner.toB256(), - queryPerAsset: [], - excludedIds: { - messages: [], - utxos: EXPECTED, - }, - }); - - // clear cache - EXCLUDED.forEach((value) => provider.cache?.del(value)); - } + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); + + expect(error).toBeTruthy(); + const EXCLUDED = provider.cache?.getExcluded() || []; + expect(EXCLUDED.length).toEqual(3); + expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + + const owner = Address.fromRandom(); + const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] })); + // @ts-expect-error mock + provider.operations.getResourcesToSpend = resourcesToSpendMock; + await provider.getResourcesToSpend(owner, []); + + expect(resourcesToSpendMock).toHaveBeenCalledWith({ + owner: owner.toB256(), + queryPerAsset: [], + excludedIds: { + messages: [], + utxos: EXPECTED, + }, + }); + + // clear cache + EXCLUDED.forEach((value) => provider.cache?.del(value)); }); it('can cacheUtxo [will cache inputs cache enabled + coins]', async () => { @@ -506,16 +503,15 @@ describe('Provider', () => { inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC], }); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - const EXCLUDED = provider.cache?.getExcluded() || []; - expect(EXCLUDED.length).toEqual(3); - expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); - // clear cache - EXCLUDED.forEach((value) => provider.cache?.del(value)); - } + expect(error).toBeTruthy(); + const EXCLUDED = provider.cache?.getExcluded() || []; + expect(EXCLUDED.length).toEqual(3); + expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + + // clear cache + EXCLUDED.forEach((value) => provider.cache?.del(value)); }); it('can cacheUtxo [will cache inputs and also merge/de-dupe in exclude list]', async () => { @@ -565,43 +561,42 @@ describe('Provider', () => { inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC], }); - try { - await provider.sendTransaction(transactionRequest); - } catch (e) { - const EXCLUDED = provider.cache?.getExcluded() || []; - expect(EXCLUDED.length).toEqual(3); - expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); - - const owner = Address.fromRandom(); - const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] })); - // @ts-expect-error mock - provider.operations.getResourcesToSpend = resourcesToSpendMock; - await provider.getResourcesToSpend(owner, [], { + const { error } = await safeExec(() => provider.sendTransaction(transactionRequest)); + + expect(error).toBeTruthy(); + const EXCLUDED = provider.cache?.getExcluded() || []; + expect(EXCLUDED.length).toEqual(3); + expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED); + + const owner = Address.fromRandom(); + const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] })); + // @ts-expect-error mock + provider.operations.getResourcesToSpend = resourcesToSpendMock; + await provider.getResourcesToSpend(owner, [], { + utxos: [ + '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503', + '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507', + '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508', + ], + }); + + expect(resourcesToSpendMock).toHaveBeenCalledWith({ + owner: owner.toB256(), + queryPerAsset: [], + excludedIds: { + messages: [], utxos: [ '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503', '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507', '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508', + EXPECTED[1], + EXPECTED[2], ], - }); - - expect(resourcesToSpendMock).toHaveBeenCalledWith({ - owner: owner.toB256(), - queryPerAsset: [], - excludedIds: { - messages: [], - utxos: [ - '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503', - '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507', - '0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508', - EXPECTED[1], - EXPECTED[2], - ], - }, - }); - - // clear cache - EXCLUDED.forEach((value) => provider.cache?.del(value)); - } + }, + }); + + // clear cache + EXCLUDED.forEach((value) => provider.cache?.del(value)); }); it('can getBlocks', async () => { diff --git a/packages/providers/src/provider.ts b/packages/providers/src/provider.ts index a104c024eb4..84c11419c8a 100644 --- a/packages/providers/src/provider.ts +++ b/packages/providers/src/provider.ts @@ -313,10 +313,10 @@ export default class Provider { transactionRequestLike: TransactionRequestLike ): Promise { const transactionRequest = transactionRequestify(transactionRequestLike); + this.#cacheInputs(transactionRequest.inputs); await this.estimateTxDependencies(transactionRequest); // #endregion Provider-sendTransaction - this.#cacheInputs(transactionRequest.inputs); const encodedTransaction = hexlify(transactionRequest.toTransactionBytes()); const { gasUsed, minGasPrice } = await this.getTransactionCost(transactionRequest, 0); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5dfdf4de105..08a9d5312e0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -690,6 +690,9 @@ importers: specifier: ^4.5.0 version: 4.5.0 devDependencies: + '@fuel-ts/utils': + specifier: workspace:* + version: link:../utils '@graphql-codegen/cli': specifier: ^2.13.7 version: 2.14.0(@babel/core@7.20.2)(@types/node@18.15.3)(graphql@16.6.0)(ts-node@10.9.1)(typescript@4.9.3)