From c3c8da60b07b5b47c2a1c193b09106cae52f35f2 Mon Sep 17 00:00:00 2001 From: "Luiz Gomes - LuizAsFight.eth" <8636507+LuizAsFight@users.noreply.github.com> Date: Tue, 26 Sep 2023 18:14:32 -0300 Subject: [PATCH] feat(Provider): add "getMessageStatus" method (#1297) --- .changeset/shaggy-sheep-suffer.md | 5 +++++ packages/providers/src/message.ts | 9 +++++++++ packages/providers/src/operations.graphql | 6 ++++++ packages/providers/src/provider.ts | 16 +++++++++++++++- .../test/__snapshots__/provider.test.ts.snap | 6 ++++++ packages/providers/test/fixtures/index.ts | 1 + .../providers/test/fixtures/messageStatus.ts | 1 + packages/providers/test/provider.test.ts | 17 ++++++++++++++++- 8 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 .changeset/shaggy-sheep-suffer.md create mode 100644 packages/providers/test/fixtures/messageStatus.ts diff --git a/.changeset/shaggy-sheep-suffer.md b/.changeset/shaggy-sheep-suffer.md new file mode 100644 index 00000000000..37fea311377 --- /dev/null +++ b/.changeset/shaggy-sheep-suffer.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/providers": minor +--- + +add method "getMessageStatus" to provider diff --git a/packages/providers/src/message.ts b/packages/providers/src/message.ts index 97be882a5a7..89792a1085c 100644 --- a/packages/providers/src/message.ts +++ b/packages/providers/src/message.ts @@ -2,6 +2,8 @@ import type { BytesLike } from '@ethersproject/bytes'; import type { AbstractAddress } from '@fuel-ts/interfaces'; import type { BN } from '@fuel-ts/math'; +import type { GqlMessageState } from './__generated__/operations'; + // #region Message-shape /** * A Fuel message @@ -58,3 +60,10 @@ export type MessageProof = { amount: BN; data: string; }; + +/** + * Message Status + */ +export type MessageStatus = { + state: GqlMessageState; +}; diff --git a/packages/providers/src/operations.graphql b/packages/providers/src/operations.graphql index 79887f814d2..5e14a6fdeaf 100644 --- a/packages/providers/src/operations.graphql +++ b/packages/providers/src/operations.graphql @@ -446,6 +446,12 @@ query getMessageProof( } } +query getMessageStatus($nonce: Nonce!) { + messageStatus(nonce: $nonce) { + state + } +} + mutation dryRun($encodedTransaction: HexString!, $utxoValidation: Boolean) { dryRun(tx: $encodedTransaction, utxoValidation: $utxoValidation) { ...receiptFragment diff --git a/packages/providers/src/provider.ts b/packages/providers/src/provider.ts index 6bf1b743b53..e9c6e83a5c9 100644 --- a/packages/providers/src/provider.ts +++ b/packages/providers/src/provider.ts @@ -28,7 +28,7 @@ import type { Coin } from './coin'; import type { CoinQuantity, CoinQuantityLike } from './coin-quantity'; import { coinQuantityfy } from './coin-quantity'; import { MemoryCache } from './memory-cache'; -import type { Message, MessageCoin, MessageProof } from './message'; +import type { Message, MessageCoin, MessageProof, MessageStatus } from './message'; import type { ExcludeResourcesOption, Resource } from './resource'; import type { TransactionRequestLike, @@ -1202,6 +1202,20 @@ export default class Provider { }; } + /** + * Returns Message Proof for given transaction id and the message id from MessageOut receipt. + * + * @param nonce - The nonce of the message to get status from. + * @returns A promise that resolves to the message status + */ + async getMessageStatus( + /** The nonce of the message to get status from */ + nonce: string + ): Promise { + const result = await this.operations.getMessageStatus({ nonce }); + return result.messageStatus; + } + /** * Lets you produce blocks with custom timestamps and the block number of the last block produced. * diff --git a/packages/providers/test/__snapshots__/provider.test.ts.snap b/packages/providers/test/__snapshots__/provider.test.ts.snap index 4bc4c4edefb..3da9da65df1 100644 --- a/packages/providers/test/__snapshots__/provider.test.ts.snap +++ b/packages/providers/test/__snapshots__/provider.test.ts.snap @@ -49,3 +49,9 @@ exports[`Provider can getMessageProof with all data 1`] = ` "sender": "fuel108z5yxd9eygf08j7fsnj3hck87n9fg07qwzru6h4nk4zc07dgt4q5t8nka", } `; + +exports[`Provider can getMessageStatus 1`] = ` +{ + "state": "SPENT", +} +`; diff --git a/packages/providers/test/fixtures/index.ts b/packages/providers/test/fixtures/index.ts index 523cbe86e6d..20399b4163a 100644 --- a/packages/providers/test/fixtures/index.ts +++ b/packages/providers/test/fixtures/index.ts @@ -1 +1,2 @@ export * from './messageProof'; +export * from './messageStatus'; diff --git a/packages/providers/test/fixtures/messageStatus.ts b/packages/providers/test/fixtures/messageStatus.ts new file mode 100644 index 00000000000..c5adcc0491d --- /dev/null +++ b/packages/providers/test/fixtures/messageStatus.ts @@ -0,0 +1 @@ +export const messageStatusResponse = { data: { messageStatus: { state: 'SPENT' } } }; diff --git a/packages/providers/test/provider.test.ts b/packages/providers/test/provider.test.ts index aa3c68280c1..b24a267cb59 100644 --- a/packages/providers/test/provider.test.ts +++ b/packages/providers/test/provider.test.ts @@ -18,7 +18,7 @@ import type { import { ScriptTransactionRequest } from '../src/transaction-request'; import { fromTai64ToUnix, fromUnixToTai64 } from '../src/utils'; -import { messageProofResponse } from './fixtures'; +import { messageProofResponse, messageStatusResponse } from './fixtures'; afterEach(() => { jest.restoreAllMocks(); @@ -653,6 +653,21 @@ describe('Provider', () => { expect(messageProof).toMatchSnapshot(); }); + it('can getMessageStatus', async () => { + // Create a mock provider to return the message proof + // It test mainly types and converstions + const provider = await Provider.create(FUEL_NETWORK_URL, { + fetch: async (url, options) => { + const messageStatus = JSON.stringify(messageStatusResponse); + return Promise.resolve(new Response(messageStatus, options)); + }, + }); + const messageStatus = await provider.getMessageStatus( + '0x0000000000000000000000000000000000000000000000000000000000000008' + ); + expect(messageStatus).toMatchSnapshot(); + }); + it('default timeout is undefined', async () => { const provider = await Provider.create(FUEL_NETWORK_URL); expect(provider.options.timeout).toBeUndefined();