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!: fetch and cache chain info on Provider when initialized #1181

Merged
merged 74 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
99a1580
first draft
Dhaiwat10 Aug 9, 2023
204ad9b
second draft
Dhaiwat10 Aug 9, 2023
04cd2a4
add unit tests for connect and invalidCache methods
Dhaiwat10 Aug 9, 2023
b67baaa
Update demo.test.ts
Dhaiwat10 Aug 9, 2023
beb9320
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Aug 9, 2023
3118cbe
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Aug 11, 2023
84a4963
modify the `Provider` constructor, require `provider for all wallets
Dhaiwat10 Aug 11, 2023
de8ea13
change `new Provider` to `Provider.connect` everywhere
Dhaiwat10 Aug 11, 2023
54c57af
update some source and tests with new fn signatures
Dhaiwat10 Aug 14, 2023
f0e6197
fix more tests
Dhaiwat10 Aug 14, 2023
f772507
fix more tests
Dhaiwat10 Aug 16, 2023
23dfe13
fix more tests
Dhaiwat10 Aug 16, 2023
4f04391
Update wallet-spec.ts
Dhaiwat10 Aug 16, 2023
291714a
fix more tests
Dhaiwat10 Aug 18, 2023
f4cc727
fix more test
Dhaiwat10 Aug 18, 2023
6d3cb2b
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Aug 21, 2023
3438a77
fix tests
Dhaiwat10 Aug 21, 2023
afb8d76
add changeset
Dhaiwat10 Aug 21, 2023
d142ce1
add back a test
Dhaiwat10 Aug 22, 2023
4fc6ba3
minor refactor
Dhaiwat10 Aug 22, 2023
2e72503
fix some failing tests
Dhaiwat10 Aug 22, 2023
76d44c0
some refactors and renaming
Dhaiwat10 Aug 22, 2023
1692179
update docs
Dhaiwat10 Aug 22, 2023
ccd5d5f
Update provider.ts
Dhaiwat10 Aug 22, 2023
06b5830
Update provider.ts
Dhaiwat10 Aug 22, 2023
5b872ce
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Aug 22, 2023
38a7f34
remove a test that i added by mistake when resolving merge conflicts
Dhaiwat10 Aug 31, 2023
5b465a0
fix `ProviderCustom` class in failing tests
Dhaiwat10 Sep 6, 2023
3210083
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 6, 2023
0d9ef4d
Update pnpm-lock.yaml
Dhaiwat10 Sep 6, 2023
7aba465
Update base-invocation-scope.ts
Dhaiwat10 Sep 6, 2023
2303b9b
fix merge conflicts
Dhaiwat10 Sep 6, 2023
395d33a
fix failing tests
Dhaiwat10 Sep 6, 2023
93f8543
update testnet url
Dhaiwat10 Sep 6, 2023
8fabdd3
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 6, 2023
3dbe395
Update contract.test.ts
Dhaiwat10 Sep 6, 2023
9ff5e9c
cache `chainInfo` instead of only `consensusParams`
Dhaiwat10 Sep 6, 2023
447110a
update changset
Dhaiwat10 Sep 7, 2023
40fdfad
unskip a test
Dhaiwat10 Sep 11, 2023
6808dd8
style changes
Dhaiwat10 Sep 11, 2023
8a833f2
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 11, 2023
b4a4aad
use `FuelError` for errors
Dhaiwat10 Sep 11, 2023
3150c72
fix lint error
Dhaiwat10 Sep 11, 2023
09def59
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 13, 2023
d7f7479
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 14, 2023
71434cc
fix merge conflicts
Dhaiwat10 Sep 14, 2023
6c24577
fix docs and remove unnecessary awaits
Dhaiwat10 Sep 14, 2023
d7b8fd6
rename to `refreshChainInfoCache`
Dhaiwat10 Sep 14, 2023
99541a0
optimize cached `chainInfo` behaviour
Dhaiwat10 Sep 14, 2023
5b0f1ea
make sure the cache is empty for the refetch test
Dhaiwat10 Sep 14, 2023
9a49a27
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 18, 2023
3f64596
remove `chainInfo` param from constructor
Dhaiwat10 Sep 18, 2023
f120ab1
rename `connect` to `create`
Dhaiwat10 Sep 18, 2023
d1a00e7
rename `updateUrl` to `switchUrl`
Dhaiwat10 Sep 18, 2023
9de85e5
get rid of `refreshChainInfoCache` - not needed
Dhaiwat10 Sep 19, 2023
74e5176
refactor `getChain`, introduce `fetchChain`
Dhaiwat10 Sep 19, 2023
508dfbd
fix up a test
Dhaiwat10 Sep 19, 2023
507c7bb
add `getGasConfig` helper, purge `getNodeInfo`
Dhaiwat10 Sep 19, 2023
316267c
make constructor private, and disable/skip problematic tests for now
Dhaiwat10 Sep 19, 2023
6b111e8
Merge branch 'master' into dp/provider-consensus-params-cache
Dhaiwat10 Sep 19, 2023
a4a0db6
fix missing doc link
Dhaiwat10 Sep 19, 2023
8cea631
refactor!: remove `chainId` from the `Predicate` constructor (#1270)
Dhaiwat10 Sep 19, 2023
6211639
remove static from fetchChain and fetchNode
Torres-ssf Sep 19, 2023
d8b6894
simplify fetchChain and fetchNode
Torres-ssf Sep 19, 2023
6ce3d6b
made Provider constructor protected
Torres-ssf Sep 19, 2023
eb0e80a
uncomment test
Torres-ssf Sep 19, 2023
3531cd3
unskip and uncomment tests on accont.test file
Torres-ssf Sep 19, 2023
3b2d38d
simplify variable assignment
Torres-ssf Sep 19, 2023
425a2d7
adding some mocks
Torres-ssf Sep 19, 2023
e12de10
unskip test case on provider.test file
Torres-ssf Sep 19, 2023
488cd4b
unskip and uncomment tests on wallet.test file
Torres-ssf Sep 19, 2023
04c8390
add method fetchChainAndNodeInfo to Provider class
Torres-ssf Sep 19, 2023
43f6d6a
DRY
arboleya Sep 19, 2023
974c936
Replacing hardcoded addresses
arboleya Sep 19, 2023
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
19 changes: 19 additions & 0 deletions .changeset/fluffy-yaks-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"docs": minor
"@fuel-ts/docs-snippets": minor
"@fuel-ts/abi-coder": minor
"@fuel-ts/predicate": minor
"@fuel-ts/program": minor
"@fuel-ts/providers": minor
"@fuel-ts/script": minor
"@fuel-ts/wallet": minor
"@fuel-ts/wallet-manager": minor
---

`chainInfo` is now fetched and cached on all `Provider`s when they are initialized. With this release, you now need to initialize a `Provider` like so:

```ts
const provider = await Provider.create(url);
```

For the full list of breaking-changes, please see [this PR](https://github.com/FuelLabs/fuels-ts/pull/1181).
14 changes: 14 additions & 0 deletions .changeset/gold-planets-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"docs": minor
"@fuel-ts/docs-snippets": minor
"@fuel-ts/abi-coder": minor
"@fuel-ts/errors": minor
"@fuel-ts/predicate": minor
"@fuel-ts/program": minor
"@fuel-ts/providers": minor
"@fuel-ts/script": minor
"@fuel-ts/wallet": minor
"@fuel-ts/wallet-manager": minor
---

Remove `chainId` from the `Predicate` constructor. You don't need to pass in `chainId` anymore since you are passing in a `provider` already.
8 changes: 4 additions & 4 deletions apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import bytecode from './generated-types/DemoContractAbi.hex';

describe('ExampleContract', () => {
it('should return the input', async () => {
const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);

// Deploy
Expand All @@ -30,7 +30,7 @@ describe('ExampleContract', () => {
});

it('deployContract method', async () => {
const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);

// Deploy
Expand All @@ -48,7 +48,7 @@ describe('ExampleContract', () => {
// #endregion Testing-with-jest-ts

it('should throw when simulating via contract factory with wallet with no resources', async () => {
const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);
const fundedWallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

Expand All @@ -62,7 +62,7 @@ it('should throw when simulating via contract factory with wallet with no resour
});

it('should throw when dry running via contract factory with wallet with no resources', async () => {
const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);
const fundedWallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

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

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';
Expand All @@ -11,8 +11,11 @@ describe(__filename, () => {
deployedContract = await createAndDeployContractFromProject(SnippetProjectEnum.RETURN_CONTEXT);
});

it('should successfully update contract instance wallet', () => {
const newWallet = WalletUnlocked.generate();
it('should successfully update contract instance wallet', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const newWallet = WalletUnlocked.generate({
provider,
});

expect(deployedContract.account?.address).not.toBe(newWallet.address);

Expand All @@ -23,7 +26,10 @@ describe(__filename, () => {
expect(deployedContract.account.address).toBe(newWallet.address);
});

// TODO: We are skipping and commenting out this test because the constructor for `Provider` is private now.
/*
it('should successfully update contract instance provider', () => {
// use the `chainInfo` from the deployed contract's provider to create a new dummy provider
const newProvider = new Provider('http://provider:9999');

expect(deployedContract.provider?.url).not.toBe(newProvider.url);
Expand All @@ -34,4 +40,5 @@ describe(__filename, () => {

expect(deployedContract.provider.url).toBe(newProvider.url);
});
*/
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Contract } from 'fuels';
import { Wallet, BN, BaseAssetId } from 'fuels';
import { Wallet, BN, BaseAssetId, Provider, FUEL_NETWORK_URL } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';
Expand All @@ -18,7 +18,11 @@ describe(__filename, () => {
const amountToForward = 40;
const amountToTransfer = 10;

const recipient = Wallet.generate();
const provider = await Provider.create(FUEL_NETWORK_URL);

const recipient = Wallet.generate({
provider,
});

await contract.functions
.transfer(amountToTransfer, BaseAssetId, recipient.address.toB256())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe(__filename, () => {
// #region contract-setup-1
// #context const PRIVATE_KEY = "..."

const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);

const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider);
// #endregion contract-setup-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ describe(__filename, () => {
const amountToForward = 40;
const amountToTransfer = 10;

const someAddress = Wallet.generate().address.toB256();
const provider = contract.provider;

const someAddress = Wallet.generate({
provider,
}).address.toB256();

// #region simulate-transactions-1
const { gasUsed } = await contract.functions
Expand Down
5 changes: 2 additions & 3 deletions apps/docs-snippets/src/guide/predicates/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ describe(__filename, () => {
// #region predicate-index-2
// #context import { Predicate, Provider, FUEL_NETWORK_URL } from 'fuels';

const provider = new Provider(FUEL_NETWORK_URL);
const chainId = await provider.getChainId();
const predicate = new Predicate(binary, chainId, jsonAbi, provider);
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicate = new Predicate(binary, provider, jsonAbi);
// #endregion predicate-index-2

expect(predicate).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ describe(__filename, () => {
const newWhitelistedAddress = getRandomB256();

const configurable = { WHITELISTED: newWhitelistedAddress };
const chainId = await wallet.provider.getChainId();
// instantiate predicate with configurable constants
const predicate = new Predicate(bin, chainId, abi, wallet.provider, configurable);
const predicate = new Predicate(bin, wallet.provider, abi, configurable);

// set predicate data to be the same as the configurable constant
predicate.setData(configurable.WHITELISTED);
Expand All @@ -31,7 +30,9 @@ describe(__filename, () => {

await tx1.waitForResult();

const destinationWallet = WalletUnlocked.generate();
const destinationWallet = WalletUnlocked.generate({
provider: wallet.provider,
});

const amountToTransfer = 100;

Expand All @@ -48,8 +49,7 @@ describe(__filename, () => {

it('should successfully tranfer to default whitelisted address', async () => {
// #region predicate-with-configurable-constants-3
const chainId = await wallet.provider.getChainId();
const predicate = new Predicate(bin, chainId, abi, wallet.provider);
const predicate = new Predicate(bin, wallet.provider, abi);

// set predicate data to be the same as the configurable constant
predicate.setData('0xa703b26833939dabc41d3fcaefa00e62cee8e1ac46db37e0fa5d4c9fe30b4132');
Expand All @@ -59,7 +59,9 @@ describe(__filename, () => {

await tx1.waitForResult();

const destinationWallet = WalletUnlocked.generate();
const destinationWallet = WalletUnlocked.generate({
provider: wallet.provider,
});

const amountToTransfer = 100;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ describe(__filename, () => {

it('should successfully use predicate to spend assets', async () => {
// #region send-and-spend-funds-from-predicates-2
const provider = new Provider(FUEL_NETWORK_URL);
const chainId = await provider.getChainId();
const predicate = new Predicate(bin, chainId, abi, provider);
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicate = new Predicate(bin, provider, abi);
// #endregion send-and-spend-funds-from-predicates-2

// #region send-and-spend-funds-from-predicates-3
Expand All @@ -41,7 +40,9 @@ describe(__filename, () => {
// #endregion send-and-spend-funds-from-predicates-4

// #region send-and-spend-funds-from-predicates-5
const receiverWallet = WalletUnlocked.generate();
const receiverWallet = WalletUnlocked.generate({
provider,
});

const tx2 = await predicate.transfer(receiverWallet.address, amountToPredicate - 100);

Expand All @@ -50,9 +51,8 @@ describe(__filename, () => {
});

it('should fail when trying to spend predicates entire amount', async () => {
const predicateOwner = WalletUnlocked.generate();
const chainId = await predicateOwner.provider.getChainId();
const predicate = new Predicate(bin, chainId, abi, predicateOwner.provider);
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicate = new Predicate(bin, provider, abi);

const amountToPredicate = 100;

Expand All @@ -62,7 +62,9 @@ describe(__filename, () => {

const predicateBalance = new BN(await predicate.getBalance()).toNumber();

const receiverWallet = WalletUnlocked.generate();
const receiverWallet = WalletUnlocked.generate({
provider,
});

predicate.setData('0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4');

Expand All @@ -78,17 +80,21 @@ describe(__filename, () => {
});

it('should fail when set wrong input data for predicate', async () => {
const predicateOwner = WalletUnlocked.generate();
const chainId = await predicateOwner.provider.getChainId();
const predicate = new Predicate(bin, chainId, abi, predicateOwner.provider);
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicateOwner = WalletUnlocked.generate({
provider,
});
const predicate = new Predicate(bin, predicateOwner.provider, abi);

const amountToPredicate = 1_000;

const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate);

await tx.waitForResult();

const receiverWallet = WalletUnlocked.generate();
const receiverWallet = WalletUnlocked.generate({
provider,
});

predicate.setData(getRandomB256());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FUEL_NETWORK_URL, Provider, fromTai64ToUnix } from 'fuels';

test('produceBlocks with custom timestamp docs snippet', async () => {
const provider = new Provider(FUEL_NETWORK_URL);
const provider = await Provider.create(FUEL_NETWORK_URL);
const latestBlock = await provider.getBlock('latest');
if (!latestBlock) {
throw new Error('No latest block');
Expand Down
9 changes: 6 additions & 3 deletions apps/docs-snippets/src/guide/types/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Wallet } from 'fuels';
import { Address, FUEL_NETWORK_URL, Provider, Wallet } from 'fuels';

describe(__filename, () => {
it('should successfully create new address from bech32 string', () => {
Expand All @@ -12,9 +12,12 @@ describe(__filename, () => {
// #endregion address-2
});

it('should successfully generate new address instance from public key', () => {
it('should successfully generate new address instance from public key', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
// #region address-3
const wallet = Wallet.generate();
const wallet = Wallet.generate({
provider,
});

const address = Address.fromPublicKey(wallet.publicKey);

Expand Down
6 changes: 5 additions & 1 deletion apps/docs-snippets/src/guide/types/bits512.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ describe(__filename, () => {
// #context }
// #endregion bits512-1

const provider = contract.provider;

// #region bits512-2
// #context import { Wallet } from 'fuels';

const wallet = Wallet.generate();
const wallet = Wallet.generate({
provider,
});

// #context console.log(walllet.publicKey);

Expand Down
9 changes: 6 additions & 3 deletions apps/docs-snippets/src/guide/types/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../project

describe(__filename, () => {
const { abiContents: abi } = getSnippetProjectArtifacts(SnippetProjectEnum.ECHO_VALUES);
let provider: Provider;

beforeAll(async () => {
provider = await Provider.create(FUEL_NETWORK_URL);
});

it('should successfully convert between b256 and bytes32', () => {
// #region conversion-1
Expand All @@ -38,8 +43,6 @@ describe(__filename, () => {

const address = Address.fromRandom();

const provider = new Provider(FUEL_NETWORK_URL);

const contractLike = new Contract(address, abi, provider);

expect(address.equals(<Address>contractLike.id)).toBeTruthy();
Expand All @@ -50,7 +53,7 @@ describe(__filename, () => {
// #region conversion-3
const address = Address.fromRandom();

const wallet: WalletLocked = Wallet.fromAddress(address);
const wallet: WalletLocked = Wallet.fromAddress(address, provider);

// we use the cast here to cast from `AbstractAddress` to `Address`
expect(address.equals(<Address>wallet.address)).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { WalletManager } from '@fuel-ts/wallet-manager';
import { Wallet } from 'fuels';
import { FUEL_NETWORK_URL, Provider, Wallet } from 'fuels';

describe(__filename, () => {
it('instantiates the WalletManager', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
// #region getting-started-with-wallet-manager-1
const walletManager = new WalletManager();
// #endregion getting-started-with-wallet-manager-1
Expand All @@ -14,14 +15,17 @@ describe(__filename, () => {
// #endregion getting-started-with-wallet-manager-2

// #region getting-started-with-wallet-manager-3
const myWallet = Wallet.generate();
const myWallet = Wallet.generate({
provider,
});

const privateKey = myWallet.privateKey;

await walletManager.addVault({
type: 'privateKey',
secret: privateKey,
title: 'My first private key vault',
provider,
});
// #endregion getting-started-with-wallet-manager-3

Expand All @@ -30,6 +34,7 @@ describe(__filename, () => {
type: 'privateKey',
secret: privateKey,
title: 'My second private key vault',
provider,
});
// #endregion getting-started-with-wallet-manager-4

Expand Down
Loading
Loading