Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: implement pagination for Account methods #2408

Open
wants to merge 70 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
defe281
add pageInfo to return of getCoins and getMessages
Torres-ssf May 28, 2024
718055b
refact getCoins on Provider class
Torres-ssf May 28, 2024
21364ce
adjusting getCoins on Account class
Torres-ssf May 28, 2024
638481a
refact getMessages on Provider class
Torres-ssf May 28, 2024
f0f3411
adjusting getMessages on Account class
Torres-ssf May 28, 2024
57e2f0a
fixing test suites
Torres-ssf May 28, 2024
abf3b71
add messages to state config
Torres-ssf May 28, 2024
b6b34ca
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf May 28, 2024
f718939
add pageInfo to return of getBalances
Torres-ssf May 28, 2024
4850e28
refact provider getBalances
Torres-ssf May 28, 2024
94636b5
adjusting tests
Torres-ssf May 28, 2024
3847ec2
add changeset
Torres-ssf May 28, 2024
5368078
linting
Torres-ssf May 28, 2024
e4ae168
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf May 29, 2024
4c8c261
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 6, 2024
869d410
Merge branch 'master' into st/feat/implement-pagination-get-coins
arboleya Jun 6, 2024
9b3c91d
add import
Torres-ssf Jun 10, 2024
9a3840c
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 10, 2024
1e79dde
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 13, 2024
2dddd38
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 13, 2024
c159171
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 17, 2024
44b5ef0
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 18, 2024
c66ba2c
add new doc page pagination
Torres-ssf Jun 18, 2024
3dce4b4
set up pagination page at provider section
Torres-ssf Jun 18, 2024
7be96e5
update param for getBlocks
Torres-ssf Jun 18, 2024
e1bf612
refact querying the chain doc page
Torres-ssf Jun 18, 2024
c8ad228
fix links
Torres-ssf Jun 18, 2024
aa0f6c7
add nonces to spell check words
Torres-ssf Jun 18, 2024
830fa46
fix test case
Torres-ssf Jun 18, 2024
b2238b0
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 19, 2024
7837d8e
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 19, 2024
3da88b2
add tests to account test suite
Torres-ssf Jun 20, 2024
e45e3ef
improve tests on provider test suite
Torres-ssf Jun 20, 2024
8caf981
remove pagination from getBalances
Torres-ssf Jun 20, 2024
b905970
edit code comment
Torres-ssf Jun 20, 2024
6a947b4
fix doc page
Torres-ssf Jun 20, 2024
435c7c8
set limit for paginated items
Torres-ssf Jun 20, 2024
8d849a5
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 20, 2024
1c88a6b
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 20, 2024
0088738
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 23, 2024
13bebc7
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 24, 2024
9928ce6
adjust code snippet
Torres-ssf Jun 24, 2024
999d357
add graphQL link to doc page
Torres-ssf Jun 24, 2024
7fec40b
made getBlocks support pagination
Torres-ssf Jun 24, 2024
14d3a26
Merge branch 'master' into st/feat/implement-pagination-get-coins
maschad Jun 26, 2024
dd5308b
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 28, 2024
d0e8b58
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jun 29, 2024
c485bc5
remove additional messages added to state config
Torres-ssf Jun 30, 2024
0f705a4
refact provider validatePaginationArgs method
Torres-ssf Jun 30, 2024
95a1bcd
using validatePaginationArgs on paginated queries
Torres-ssf Jun 30, 2024
b7f9393
update tests for paginated methods
Torres-ssf Jun 30, 2024
8a4cf16
update tests for account paginated methods
Torres-ssf Jun 30, 2024
b67eac3
Merge branch 'st/feat/implement-pagination-get-coins' of github.com:F…
Torres-ssf Jun 30, 2024
c465c10
update changeset
Torres-ssf Jul 1, 2024
0776e6f
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 1, 2024
d5c8429
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 1, 2024
e294b55
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 2, 2024
90aa82c
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 2, 2024
1ee4bb8
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 3, 2024
5f76ffe
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 3, 2024
dbbf85b
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 4, 2024
2926c83
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 4, 2024
6f35d20
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 4, 2024
b1d0e5e
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
b64cb79
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
2527d17
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
649a906
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
c696338
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
9d13959
Merge branch 'master' into st/feat/implement-pagination-get-coins
maschad Jul 5, 2024
c1cb5e9
Merge branch 'master' into st/feat/implement-pagination-get-coins
Torres-ssf Jul 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-shoes-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": minor
---

feat!: implement pagination for `Account` methods
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Getting started', () => {
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider);

// Perform a balance check.
const balances = await wallet.getBalances();
const { balances } = await wallet.getBalances();
Torres-ssf marked this conversation as resolved.
Show resolved Hide resolved
// [{ assetId: '0x..', amount: bn(..) }, ..]
// #endregion connecting-to-the-testnet

Expand Down
106 changes: 106 additions & 0 deletions apps/docs-snippets/src/guide/provider/pagination.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import type { GqlPageInfo } from '@fuel-ts/account/dist/providers/__generated__/operations';
import type { CursorPaginationArgs } from 'fuels';
import { FUEL_NETWORK_URL, Provider, Wallet } from 'fuels';

/**
* @group node
*/
describe('querying the chain', () => {
it('pagination snippet test 1', () => {
// #region pagination-1
const paginationArgs: CursorPaginationArgs = {
after: 'cursor',
first: 10,
before: 'cursor',
last: 10,
};
// #endregion pagination-1

// #region pagination-2
const pageInfo: GqlPageInfo = {
endCursor: 'cursor',
hasNextPage: true,
startCursor: 'cursor',
hasPreviousPage: true,
};
// #endregion pagination-2

expect(paginationArgs).toBeDefined();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this pattern throughout the pagination test suite but I'm not sure what the benefit of these assertions are, given that an initialized object will always be defined.

In the context of them being passed to provider.getCoins() I think an assertion that the pageInfo returned is the logical consequence of a particular paginationArgs suffices, but otherwise just asserting that a GqlPageInfo object or CursorPaginationArgs is defined doesn't offer any value imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maschad The tests within the doc-snippets dir exist to be used as code snippets only. We should not rely on those tests to validate our implementation

The changes made within this PR are being tested here and here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that, I elaborated a more in my final review comment , but I am suggesting that these snippet regions be incorporated into a larger test such as the ones you linked, rather than having an individual test just for the initialization of an object.

expect(pageInfo).toBeDefined();
});

it('pagination snippet test 2', async () => {
// #region pagination-3
// #import { Provider, CursorPaginationArgs, FUEL_NETWORK_URL, Wallet };

const provider = await Provider.create(FUEL_NETWORK_URL);
const baseAssetId = provider.getBaseAssetId();
const myWallet = Wallet.generate({ provider });

let paginationArgs: CursorPaginationArgs = {
first: 10, // It will return only the first 10 coins
};

const { coins, pageInfo } = await provider.getCoins(
myWallet.address,
baseAssetId,
paginationArgs
);

if (pageInfo.hasNextPage) {
paginationArgs = {
after: pageInfo.endCursor,
first: 10,
};
// The coins array will include the next 10 coins after the last one in the previous array
await provider.getCoins(myWallet.address, baseAssetId, paginationArgs);
}
// #endregion pagination-3

// #region pagination-4
if (pageInfo.hasPreviousPage) {
paginationArgs = {
before: pageInfo.startCursor,
last: 10,
};

// It will includes the previous 10 coins before the first one in the previous array
await provider.getCoins(myWallet.address, baseAssetId, paginationArgs);
}
// #endregion pagination-4

expect(paginationArgs).toBeDefined();
expect(coins).toBeDefined();
expect(pageInfo).toBeDefined();
});

it('pagination snippet test 3', () => {
// #region pagination-5
const paginationArgs = { after: 'cursor', first: 10 };
// #endregion pagination-5

expect(paginationArgs).toBeDefined();
});
it('pagination snippet test 4', () => {
// #region pagination-6
const paginationArgs = { before: 'cursor', last: 10 };
// #endregion pagination-6

expect(paginationArgs).toBeDefined();
});

it('pagination snippet test 5', async () => {
// #region pagination-7
// #import { Provider, FUEL_NETWORK_URL, Wallet };

const provider = await Provider.create(FUEL_NETWORK_URL);
const myWallet = Wallet.generate({ provider });

// It will return the first 100 coins of the base asset
const { coins, pageInfo } = await provider.getCoins(myWallet.address);
// #endregion pagination-7

expect(coins).toBeDefined();
expect(pageInfo).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion apps/docs-snippets/src/guide/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Provider', () => {
const wallet = WalletUnlocked.generate({ provider });

// Get the balances of the wallet (this will be empty until we have assets)
const balances = await wallet.getBalances();
const { balances } = await wallet.getBalances();
// []
// #endregion provider-definition

Expand Down
123 changes: 69 additions & 54 deletions apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { FUEL_NETWORK_URL, Provider, ScriptTransactionRequest, bn } from 'fuels';
import { FUEL_NETWORK_URL, Provider, ScriptTransactionRequest } from 'fuels';
import { generateTestWallet } from 'fuels/test-utils';

/**
* @group node
*/
describe('querying the chain', () => {
it('query coins', async () => {
// #region wallet-query
// #region get-coins-1
// #import { Provider, FUEL_NETWORK_URL, generateTestWallet };

const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
const baseAssetId = provider.getBaseAssetId();
Expand All @@ -17,40 +18,33 @@ describe('querying the chain', () => {
[100, assetIdA],
]);

// get single coin
const coin = await wallet.getCoins(baseAssetId);
// [{ amount: bn(42), assetId: baseAssetId }]
// fetches up to 100 coins from baseAssetId
const { coins, pageInfo } = await provider.getCoins(wallet.address, baseAssetId);
// [
// { amount: bn(42), assetId: baseAssetId },
// ...
// ]

// get all coins
const coins = await wallet.getCoins();
// fetches up to 100 coins from all assets
await provider.getCoins(wallet.address);
// [
maschad marked this conversation as resolved.
Show resolved Hide resolved
// { amount: bn(42), assetId: baseAssetId }
// { amount: bn(100), assetId: assetIdA }
// ...
// ]
// #endregion wallet-query

expect(coin.length).toEqual(1);
expect(coin).toEqual([
expect.objectContaining({
assetId: baseAssetId,
amount: bn(42),
}),
]);
expect(coins).toEqual([
expect.objectContaining({
assetId: baseAssetId,
amount: bn(42),
}),
expect.objectContaining({
assetId: assetIdA,
amount: bn(100),
}),
]);
// #endregion get-coins-1

// #region get-coins-2
await wallet.getCoins(baseAssetId);
// #endregion get-coins-2

expect(coins).toBeDefined();
expect(pageInfo).toBeDefined();
});

it('get balances', async () => {
// #region wallet-get-balances
// #import { Provider, FUEL_NETWORK_URL, generateTestWallet };
it('get spendable resources', async () => {
// #region get-spendable-resources-1
// #import { Provider, FUEL_NETWORK_URL, generateTestWallet, ScriptTransactionRequest };

const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
Expand All @@ -61,21 +55,38 @@ describe('querying the chain', () => {
[100, assetIdA],
]);

const walletBalances = await wallet.getBalances();
// [
// { amount: bn(42), assetId: baseAssetId }
// { amount: bn(100), assetId: assetIdA }
// ]
// #endregion wallet-get-balances
const quantities: CoinQuantityLike[] = [
{ amount: 32, assetId: baseAssetId, max: 42 },
{ amount: 50, assetId: assetIdA },
];

expect(walletBalances).toEqual([
{ assetId: assetIdA, amount: bn(100) },
{ assetId: baseAssetId, amount: bn(42) },
]);
const utxoId = '0x00000000000000000000000000000000000000000000000000000000000000010001';
const messageNonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0';
const excludedIds: ExcludeResourcesOption = {
utxos: [utxoId],
messages: [messageNonce],
};

const spendableResources = await provider.getResourcesToSpend(
wallet.address,
quantities,
excludedIds
);

const tx = new ScriptTransactionRequest();
tx.addResources(spendableResources);
// #endregion get-spendable-resources-1

// #region get-spendable-resources-2
await wallet.getResourcesToSpend(spendableResources, excludedIds);
// #endregion get-spendable-resources-2
maschad marked this conversation as resolved.
Show resolved Hide resolved

expect(spendableResources).toBeDefined();
});
it('get spendable resources', async () => {
// #region wallet-get-spendable-resources
// #import { Provider, FUEL_NETWORK_URL, generateTestWallet, ScriptTransactionRequest };

it('get balances', async () => {
// #region get-balances-1
// #import { Provider, FUEL_NETWORK_URL, generateTestWallet };

const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
Expand All @@ -86,43 +97,47 @@ describe('querying the chain', () => {
[100, assetIdA],
]);

const spendableResources = await wallet.getResourcesToSpend([
{ amount: 32, assetId: baseAssetId, max: 42 },
{ amount: 50, assetId: assetIdA },
]);
const { balances } = await provider.getBalances(wallet.address);
// [
// { amount: bn(42), assetId: baseAssetId } // total amount of baseAssetId
// { amount: bn(100), assetId: assetIdA } // total amount of assetIdA
// ]
// #endregion get-balances-1

const tx = new ScriptTransactionRequest();
tx.addResources(spendableResources);
// #endregion wallet-get-spendable-resources
// #region get-balances-2
await wallet.getBalances();
// #endregion get-balances-2

expect(spendableResources[0].amount).toEqual(bn(42));
expect(spendableResources[1].amount).toEqual(bn(100));
expect(balances).toBeDefined();
});

it('can getBlocks', async () => {
// #region Provider-get-blocks
// #import { Provider, FUEL_NETWORK_URL };

const provider = await Provider.create(FUEL_NETWORK_URL);

// Force-producing some blocks to make sure that 10 blocks exist
await provider.produceBlocks(10);
const blocks = await provider.getBlocks({

const { blocks } = await provider.getBlocks({
last: 10,
});
// #endregion Provider-get-blocks
expect(blocks.length).toBe(10);
});

it('can getMessageByNonce', async () => {
// #region getMessageByNonce
// #region get-message-by-nonce-1
// #import { FUEL_NETWORK_URL, Provider };

const provider = await Provider.create(FUEL_NETWORK_URL);

const nonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0';
const message = await provider.getMessageByNonce(nonce);
// #endregion get-message-by-nonce-1

expect(message).toBeDefined();
expect(message?.nonce).toEqual(nonce);
// #endregion getMessageByNonce
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('launching a test node', () => {
wallets: [wallet],
} = launched;

const coins = await wallet.getCoins(assets[0].value);
const { coins } = await wallet.getCoins(assets[0].value);
// #endregion asset-ids
expect(coins[0].assetId).toEqual(assets[0].value);
});
Expand All @@ -211,7 +211,9 @@ describe('launching a test node', () => {
wallets: [wallet],
} = launched;

const [message] = await wallet.getMessages();
const {
messages: [message],
} = await wallet.getMessages();
// message.nonce === testMessage.nonce
// #endregion test-messages

Expand Down Expand Up @@ -242,7 +244,9 @@ describe('launching a test node', () => {

recipient.provider = provider;

const [message] = await recipient.getMessages();
const {
messages: [message],
} = await recipient.getMessages();
// message.nonce === testMessage.nonce
// #endregion test-messages-chain

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BigNumberish, CoinQuantity, WalletUnlocked } from 'fuels';
import type { BigNumberish, WalletUnlocked } from 'fuels';
import { Provider, Wallet, FUEL_NETWORK_URL } from 'fuels';

/**
Expand Down Expand Up @@ -30,7 +30,7 @@ describe(__filename, () => {
// #region wallet-check-balances
// #context import { CoinQuantity } from 'fuels';

const balances: CoinQuantity[] = await myWallet.getBalances();
const { balances } = await myWallet.getBalances();
// #endregion wallet-check-balances

expect(balances).toEqual([]);
Expand Down
8 changes: 4 additions & 4 deletions apps/docs-snippets/src/guide/wallets/test-wallets.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CoinQuantity, WalletUnlocked } from 'fuels';
import type { WalletUnlocked } from 'fuels';
import { FUEL_NETWORK_URL, Provider, bn } from 'fuels';
import { generateTestWallet } from 'fuels/test-utils';

Expand Down Expand Up @@ -31,9 +31,9 @@ describe(__filename, () => {
const walletC = await generateTestWallet(provider);

// retrieve balances of wallets
const walletABalances: CoinQuantity[] = await walletA.getBalances();
const walletBBalances = await walletB.getBalances();
const walletCBalances = await walletC.getBalances();
const { balances: walletABalances } = await walletA.getBalances();
const { balances: walletBBalances } = await walletB.getBalances();
const { balances: walletCBalances } = await walletC.getBalances();

expect(walletABalances).toEqual([{ assetId: baseAssetId, amount: bn(42) }]);
expect(walletBBalances).toEqual([
Expand Down
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export default defineConfig({
text: 'Provider Options',
link: '/guide/provider/provider-options',
},
{
text: 'Pagination',
link: '/guide/provider/pagination',
},
{
text: 'Querying the Chain',
link: '/guide/provider/querying-the-chain',
Expand Down
Loading
Loading