Skip to content

Commit

Permalink
feat: add getMessageByNonce to Provider (#2174)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Smith <peter@blueoceancomputing.co.uk>
Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent bbd31a1 commit 4aca0b8
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-plants-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: add `getMessageByNonce` to `Provider`
14 changes: 14 additions & 0 deletions apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,18 @@ describe('querying the chain', () => {
// #endregion Provider-get-blocks
expect(blocks.length).toBe(10);
});

it('can getMessageByNonce', async () => {
// #region getMessageByNonce
// #import { FUEL_NETWORK_URL, Provider };

const provider = await Provider.create(FUEL_NETWORK_URL);

const nonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0';
const message = await provider.getMessageByNonce(nonce);

expect(message).toBeDefined();
expect(message?.nonce).toEqual(nonce);
// #endregion getMessageByNonce
});
});
6 changes: 6 additions & 0 deletions apps/docs/src/guide/provider/querying-the-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ This method returns all the blocks from the blockchain that match the given quer

<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#Provider-get-blocks{ts:line-numbers}

## Get a message by its nonce

You can use the `getMessageByNonce` method to retrieve a message by its nonce.

<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#getMessageByNonce{ts:line-numbers}

<!-- TODO: fix these examples to not reference hardcoded values after #1356 which introduces message generation tools
### Get messages
Expand Down
1 change: 1 addition & 0 deletions packages/account/src/providers/fuel-core-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ type Query {
commitBlockHeight: U32
): MessageProof
messageStatus(nonce: Nonce!): MessageStatus!
message(nonce: Nonce!): Message
}

type Receipt {
Expand Down
6 changes: 6 additions & 0 deletions packages/account/src/providers/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,12 @@ mutation produceBlocks(
)
}

query getMessageByNonce($nonce: Nonce!) {
message(nonce: $nonce) {
...messageFragment
}
}

subscription submitAndAwait($encodedTransaction: HexString!) {
submitAndAwait(tx: $encodedTransaction) {
...transactionStatusSubscriptionFragment
Expand Down
10 changes: 10 additions & 0 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,4 +1471,14 @@ describe('Provider', () => {
message: 'The operation was aborted due to timeout',
});
});

test('getMessageByNonce', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);

const nonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0';
const message = await provider.getMessageByNonce(nonce);

expect(message).toBeDefined();
expect(message?.nonce).toEqual(nonce);
});
});
17 changes: 17 additions & 0 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
GqlDryRunSuccessStatusFragmentFragment,
GqlGasCosts,
GqlGetBlocksQueryVariables,
GqlMessage,
} from './__generated__/operations';
import type { Coin } from './coin';
import type { CoinQuantity, CoinQuantityLike } from './coin-quantity';
Expand Down Expand Up @@ -1635,4 +1636,20 @@ export default class Provider {
async getTransactionResponse(transactionId: string): Promise<TransactionResponse> {
return new TransactionResponse(transactionId, this);
}

/**
* Returns Message for given nonce.
*
* @param nonce - The nonce of the message to retrieve.
* @returns A promise that resolves to the Message object.
*/
async getMessageByNonce(nonce: string): Promise<GqlMessage | null> {
const { message } = await this.operations.getMessageByNonce({ nonce });

if (!message) {
return null;
}

return message;
}
}
8 changes: 8 additions & 0 deletions packages/utils/src/utils/defaultSnapshots/stateConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@
"amount": 12704439083013451934,
"data": "",
"da_height": 0
},
{
"sender": "0x22cae5308938e8b4caf217b6464884f6331eff05e81468df8ccd08126effc8d0",
"recipient": "0x8d2af98a4198732a46bf65d87a73427dd7608acaad2414585d8ccdd6f59c437b",
"nonce": "0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0",
"amount": 18446744073709551615,
"data": "",
"da_height": 0
}
],
"contracts": [],
Expand Down
6 changes: 3 additions & 3 deletions scripts/run-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

mkdir -p .fuel-core/db

cp ./packages/utils/src/utils/chainConfig.json .fuel-core/configs/chainConfig.json
cp ./packages/utils/src/utils/metadata.json .fuel-core/configs/metadata.json
cp ./packages/utils/src/utils/stateConfig.json .fuel-core/configs/stateConfig.json
cp ./packages/utils/src/utils/defaultSnapshots/chainConfig.json .fuel-core/configs/chainConfig.json
cp ./packages/utils/src/utils/defaultSnapshots/metadata.json .fuel-core/configs/metadata.json
cp ./packages/utils/src/utils/defaultSnapshots/stateConfig.json .fuel-core/configs/stateConfig.json

pnpm fuels-core run \
--db-path .fuel-core/db \
Expand Down

0 comments on commit 4aca0b8

Please sign in to comment.