Skip to content

Commit

Permalink
docs: migrate other half of the wallet snippets (#3365)
Browse files Browse the repository at this point in the history
* docs: convert old wallet snippets

* docs: split snippets

* docs: update snippets

* docs: ensure test is node only

* docs: add node group at the bottom

* remove old import

Co-authored-by: Peter Smith <[email protected]>

* remove old import

Co-authored-by: Peter Smith <[email protected]>

* remove space

Co-authored-by: Peter Smith <[email protected]>

* adjust imports in snippet

Co-authored-by: Peter Smith <[email protected]>

* remove old import

Co-authored-by: Peter Smith <[email protected]>

* docs: remove old import

* docs: update snippet test suffix

* docs: move snippets into wallet guide

* docs: update snippets comment

Co-authored-by: Daniel Bate <[email protected]>

* docs: remove unnecessary full examples

* linting fixes

* docs: add try/catch to snippet

* docs: add defaultConnectors + remove unused snippet

* dep: patch temp issue with knip

* docs: update to use mocked deps

---------

Co-authored-by: Peter Smith <[email protected]>
Co-authored-by: Daniel Bate <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent 8c77765 commit 63a34f6
Show file tree
Hide file tree
Showing 26 changed files with 451 additions and 501 deletions.
1 change: 1 addition & 0 deletions .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bun",
"@/sway-api/*",
"@fuel-ts/*",
"@fuels/connectors",
"@internal/fuel-core",
"@internal/forc",
"@types/jest",
Expand Down
78 changes: 0 additions & 78 deletions apps/docs-snippets/src/guide/wallets/access.test.ts

This file was deleted.

This file was deleted.

45 changes: 0 additions & 45 deletions apps/docs-snippets/src/guide/wallets/checking-balances.test.ts

This file was deleted.

This file was deleted.

45 changes: 45 additions & 0 deletions apps/docs-snippets2/src/wallets/access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// #region full
// #region wallets
import type { WalletLocked, WalletUnlocked } from 'fuels';
import { Provider, Wallet } from 'fuels';

import { LOCAL_NETWORK_URL } from '../env';

// We can use the `generate` to create a new unlocked wallet.
const provider = await Provider.create(LOCAL_NETWORK_URL);
const myWallet: WalletUnlocked = Wallet.generate({ provider });

// or use an Address to create a wallet
const someWallet: WalletLocked = Wallet.fromAddress(myWallet.address, provider);
// #endregion wallets

const wallet: WalletUnlocked = Wallet.generate({ provider });
const PRIVATE_KEY = wallet.privateKey;

// Lock an existing wallet
const lockedWallet: WalletLocked = Wallet.fromAddress(
myWallet.address,
provider
);

// Unlock an existing wallet
const someUnlockedWallet: WalletUnlocked = lockedWallet.unlock(PRIVATE_KEY);

const unlockedWallet: WalletUnlocked = Wallet.generate({ provider });
const newlyLockedWallet = unlockedWallet.lock();

// You can create a wallet, without a provider
let unlockedWalletWithoutProvider: WalletUnlocked = Wallet.generate();
unlockedWalletWithoutProvider = Wallet.fromPrivateKey(
unlockedWalletWithoutProvider.privateKey
);

// All non-provider dependent methods are available
unlockedWalletWithoutProvider.lock();

// All provider dependent methods will throw
await expect(() => unlockedWalletWithoutProvider.getCoins()).rejects.toThrow(
/Provider not set/
);
// #endregion full
11 changes: 11 additions & 0 deletions apps/docs-snippets2/src/wallets/checking-balances-two.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// #region checking-balances-2
import { Provider, Wallet } from 'fuels';

import { WALLET_PVT_KEY_2, LOCAL_NETWORK_URL } from '../env';

const provider = await Provider.create(LOCAL_NETWORK_URL);
const myOtherWallet = Wallet.fromPrivateKey(WALLET_PVT_KEY_2, provider);

const { balances } = await myOtherWallet.getBalances();
// #endregion checking-balances-2
console.log('balances:', balances);
15 changes: 15 additions & 0 deletions apps/docs-snippets2/src/wallets/checking-balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// #region checking-balances-1
import type { BN } from 'fuels';
import { Provider, Wallet } from 'fuels';

import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env';

const provider = await Provider.create(LOCAL_NETWORK_URL);

const myWallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

// The returned amount is a BigNumber
const balance: BN = await myWallet.getBalance(provider.getBaseAssetId());

// #endregion checking-balances-1
console.log('balance', balance);
Loading

0 comments on commit 63a34f6

Please sign in to comment.