Skip to content

Commit

Permalink
feat(Provider): add "getMessageStatus" method (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight authored Sep 26, 2023
1 parent 30d1d8e commit c3c8da6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-sheep-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": minor
---

add method "getMessageStatus" to provider
9 changes: 9 additions & 0 deletions packages/providers/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,3 +60,10 @@ export type MessageProof = {
amount: BN;
data: string;
};

/**
* Message Status
*/
export type MessageStatus = {
state: GqlMessageState;
};
6 changes: 6 additions & 0 deletions packages/providers/src/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<MessageStatus> {
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.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/providers/test/__snapshots__/provider.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ exports[`Provider can getMessageProof with all data 1`] = `
"sender": "fuel108z5yxd9eygf08j7fsnj3hck87n9fg07qwzru6h4nk4zc07dgt4q5t8nka",
}
`;

exports[`Provider can getMessageStatus 1`] = `
{
"state": "SPENT",
}
`;
1 change: 1 addition & 0 deletions packages/providers/test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './messageProof';
export * from './messageStatus';
1 change: 1 addition & 0 deletions packages/providers/test/fixtures/messageStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const messageStatusResponse = { data: { messageStatus: { state: 'SPENT' } } };
17 changes: 16 additions & 1 deletion packages/providers/test/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c3c8da6

Please sign in to comment.