浏览代码

Merge pull request #196 from metaplex-foundation/leo/umi-overview-rework

Reworked and Merged `Getting Started` and `Connecting to Umi` docs
Leonardo Donatacci 1 年之前
父节点
当前提交
7009e0722a

+ 3 - 2
src/components/products/umi/index.js

@@ -26,8 +26,9 @@ export const umi = {
           title: 'Introduction',
           links: [
             { title: 'Overview', href: '/umi' },
-            { title: 'Getting started', href: '/umi/getting-started' },
-            { title: 'Connecting to Umi', href: '/umi/connecting-to-umi' },
+            { 
+              title: 'Getting started', 
+              href: '/umi/getting-started' },
             {
               title: 'Metaplex Umi Plugins',
               href: '/umi/metaplex-umi-plugins',

+ 4 - 0
src/middleware.js

@@ -9,6 +9,10 @@ const redirectData = {
     "destination": "/umi/web3js-differences-and-adapters",
     "permanent": true
   },
+  "/umi/connecting-to-umi": {
+    "destination": "/umi/getting-started",
+    "permanent": true
+  },
 }
 
 export async function middleware(request) {

+ 0 - 111
src/pages/umi/connecting-to-umi.md

@@ -1,111 +0,0 @@
----
-title: Connecting to Umi
-metaTitle: Connecting to Umi | Umi
-description: Learn how hook up Umi to Node and front end clients such as React.
----
-
-## Connecting to an RPC
-
-Connecting Umi to an RPC is as simple as creating an umi instance and passing through your rpc end point as the first argument. It is recommended to use at least a free RPC end point from one of the many Solana RPC providers and not use the public endpoint of `https://api.mainnet-beta.solana.com` due to it's restrictions and limitations.
-
-Connecting Umi to devnet is as simple as swapping out the RPC end point for that of a Devnet endpoint.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-
-const umi = createUmi('https://api.mainnet-beta.solana.com')
-```
-
-## Registering Programs and Clients
-
-Sometimes Umi may require you to register programs/clients directly to it. To do this you can call the `.use()` method from your umi instance and pass in the client as the argument. In the below example we'll register the `mpl-token-metdata` client to UMI.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
-
-const umi = createUmi('https://api.mainnet-beta.solana.com').use(
-  mplTokenMetadata()
-)
-```
-
-You can chain `.use()` together to register multiple clients.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
-import { mplCandyMachine } from '@metaplex-foundation/mpl-candy-machine'
-
-const umi = createUmi('https://api.mainnet-beta.solana.com')
-  .use(mplTokenMetadata())
-  .use(mplCandyMachine())
-```
-
-## Connecting w/ a Secret Key
-
-To use Umi you'll need to register a wallet in order to send transactions. To use a file system wallet you can import the json stored private key and convert it to a keypair for use with Umi.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-import { keypairIdentity } from '@metaplex-foundation/umi'
-import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
-import { mplCandyMachine } from '@metaplex-foundation/mpl-candy-machine'
-
-// Create Umi Instance
-const umi = createUmi('https://api.mainnet-beta.solana.com')
-
-// Import your private key file and parse it.
-const wallet = './my-wallet.json'
-const secretKey = JSON.parse(fs.readFileSync(wallet, 'utf-8'))
-
-// Create a keypair from your private key
-const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secretKey))
-
-// Register it to the Umi client.
-umi.use(keypairIdentity(keypair))
-```
-
-## Connecting w/ Wallet Adapter
-
-Umi can connect to `solana-labs/wallet-adapter` directly to provide a seemless experiance for users on your front end. This prebuilt wallet UI is a great starting place for websites that are looking for user transactions and interactions. For this example we'll create a simple `useUmi` hook in React.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-import { walletAdapterIdentity } from '@metaplex-foundation/umi-signer-wallet-adapters'
-import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
-import { mplCandyMachine } from '@metaplex-foundation/mpl-candy-machine'
-import { useWallet } from '@solana/wallet-adapter-react'
-
-const useUmi = () => {
-  // Import useWallet hook
-  const wallet = useWallet()
-
-  // Create Umi instance
-  const umi = createUmi('https://api.mainnet-beta.solana.com')
-    .use(mplTokenMetadata())
-    .use(mplCandyMachine())
-    // Register Wallet Adapter to Umi
-    .use(walletAdapterIdentity(wallet))
-
-  return umi
-}
-
-export default useUmi
-```
-
-From here on you can import your `useUmi` hook into your components and use as needed.
-
-```ts
-// Import hook from where you saved it
-import { useUmi } from '@hooks/useUmi'
-
-// Your component
-const MyComponent = () => {
-  // Assign you hook to a const within component ready for use
-  const umi = useUmi()
-
-  return <div>...</div>
-}
-
-export default MyComponent
-```

+ 160 - 24
src/pages/umi/getting-started/index.md

@@ -4,40 +4,43 @@ metaTitle: Getting Started | Umi
 description: A Javascript Framework for Solana.
 ---
 
-# Installation
+## Umi Installation
 
-## For end-users
+To use Umi you need to install Umi and all the external plugins you'll want to use. Alternatively, if you don't need a specific plugin, you can install the default bundle that includes a set of plugins that's suitable for most use cases. 
 
-End-users using Umi to build applications need to install Umi and select the plugins they want to use. Alternatively, they can install the default bundle that includes a set of plugins that's suitable for most use cases. Note that, for now, the default bundle relies on web3.js for some of the interfaces so we have to install it as well.
+**Note**: since the default bundle relies on web3.js for some of the interfaces you'll need to install that package as well.
 
-```sh
-npm install \
-  @metaplex-foundation/umi \
-  @metaplex-foundation/umi-bundle-defaults \
-  @solana/web3.js
-```
+### Required Packages
 
-Then, you can create a new Umi instance using the `createUmi` function of the default bundle.
+{% packagesUsed packages=["umi", "umiDefaults", "@solana/web3.js"] type="npm" /%}
 
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
+To install them, use the following commands:
 
-const umi = createUmi('https://api.mainnet-beta.solana.com');
+```
+npm i @metaplex-foundation/umi 
 ```
 
-Then you want to tell Umi which wallet to use. This can either be a [keypair](/umi/connecting-to-umi#connecting-w-a-secret-key) or the [solana wallet adapter](/umi/connecting-to-umi#connecting-w-wallet-adapter).
+```
+npm i @metaplex-foundation/umi-bundle-defaults 
+```
 
-That's it, now pass your Umi instance around and add more plugins as needed.
+```
+npm i @solana/web3.js
+```
 
-## For library authors
+### For library authors
 
-Library authors that want to use Umi's interfaces to drastically reduce their dependencies only need to install the main Umi library. It is highly recommended to install it as a peer dependency to ensure the end-user does not end up with multiple versions of the Umi library.
+Library authors, that want to use Umi's interfaces to drastically reduce their dependencies, will only need to install the main Umi library. It is highly recommended to install it as a peer dependency to ensure the end-user does not end up with multiple versions of the Umi library using the following command:
 
-```sh
-npm install @metaplex-foundation/umi --save-peer
 ```
+npm i @metaplex-foundation/umi --save-peer
+```
+
+You can then use Umi's `Context` object or a subset of it to inject any interface you need in your functions.
+
+{% totem %}
 
-You can then use Umi's `Context` object or a subset of it to inject any interface you need in your functions. For instance:
+{% totem-accordion title="Example" %}
 
 ```ts
 import type { Context, PublicKey } from '@metaplex-foundation/umi';
@@ -53,10 +56,143 @@ export async function myFunction(
 }
 ```
 
-## For testing
+{% /totem-accordion %}
+
+{% /totem %}
+
+### For testing
 
 Also note that Umi comes with a testing bundle that can help both end-users and library authors to test their code. For instance, it includes a `MockStorage` implementation used for both the `UploaderInterface` and the `DownloaderInterface` so you can reliably test your code without having to rely on a real storage provider.
 
-```sh
-npm install @metaplex-foundation/umi @metaplex-foundation/umi-bundle-tests
-```
+```
+npm i @metaplex-foundation/umi
+```
+
+```
+npm i @metaplex-foundation/umi-bundle-tests
+```
+
+## Umi Basics
+
+In this section, we'll cover the essential steps to get started with Umi:
+- [Creating Umi and Connecting to an RPC](/umi/getting-started#connecting-to-an-rpc)
+- [Connecting a Wallet](/umi/getting-started#connecting-a-wallet)
+- [Registering Programs and Clients](/umi/getting-started#registering-programs-and-clients)
+
+### Connecting to an RPC
+
+Solana has different clusters (e.g., Mainnet-beta, Devnet, Testnet, ...) that serve various purposes, each with dedicated API nodes to handle RPC requests.
+
+Connecting Umi to a cluster of choice is is as simple as creating an umi instance since the RPC endpoint is passed as the first argument. 
+
+**Note**: If you're connecting to **Mainnet**, it's recommended to use a dedicated RPC endpoint from a Solana RPC provider instead of the public endpoint (`https://api.mainnet-beta.solana.com`) due to its limitations.
+
+To create a Umi instance, import the `createUmi` function and provide your RPC endpoint. Optionally, you can also specify the commitment level as the second argument.
+
+```ts
+import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
+
+const umi = createUmi('<RPC-Endpoint>', '<Commitment-Level>')
+```
+
+### Connecting a Wallet
+
+When setting up Umi, you'll need to use or generate a wallet in order to send transactions. To do so, you can **create a new wallet** for testing, **import an existing wallet** from the filesystem, or **use a walletAdapter** for web-based dApps.
+
+**Note**: The `walletAdapter` section provides only the code needed to connect it to Umi, assuming you've already installed and set up the `walletAdapter`. For a comprehensive guide, refer to [this](https://github.com/anza-xyz/wallet-adapter/blob/master/APP.md)
+
+{% totem %}
+
+{% totem-accordion title="From a New Wallet" %}
+
+```ts
+import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
+import { generateSigner, signerIdentity } from '@metaplex-foundation/umi'
+
+const umi = createUmi('https://api.devnet.solana.com')
+
+// Generate a new keypair signer.
+const signer = generateSigner(umi)
+
+// Tell Umi to use the new signer.
+umi.use(signerIdentity(signer))
+```
+
+{% /totem-accordion %}
+
+{% totem-accordion title="From an Existing Wallet saved using a File System" %}
+
+```ts
+import * as fs from "fs";
+import * as path from "path";
+import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
+import { createSignerFromKeypair, signerIdentity } from '@metaplex-foundation/umi'
+
+const umi = createUmi('https://api.devnet.solana.com')
+
+// Use fs to navigate the filesystem till you reach
+// the wallet you wish to use via relative pathing.
+const walletFile = fs.readFileSync(
+  path.join(__dirname, './keypair.json')
+)
+
+// Usually Keypairs are saved as Uint8Array, so you  
+// need to transform it into a usable keypair.  
+let keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(walletFile));
+
+// Before Umi can use this Keypair you need to generate 
+// a Signer type with it.  
+const signer = createSignerFromKeypair(umi, keypair);
+
+// Tell Umi to use the new signer.
+umi.use(signerIdentity(walletFile))
+```
+
+{% /totem-accordion %}
+
+{% totem-accordion title="From an Existing Wallet saved using Wallet Adapter" %}
+
+```ts
+import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
+import { walletAdapterIdentity } from '@metaplex-foundation/umi-signer-wallet-adapters'
+import { useWallet } from '@solana/wallet-adapter-react'
+
+const wallet = useWallet()
+
+const umi = createUmi('https://api.devnet.solana.com')
+
+// Register Wallet Adapter to Umi
+umi.use(walletAdapterIdentity(wallet))
+```
+
+{% /totem-accordion %}
+
+{% /totem %}
+
+**Note**: The **Umi** interface stores two instances of **Signer**: The **identity** using the app and the **payer** paying for transaction and storage fees. By default, the `signerIdentity` method will also update the **payer** attribute since, in most cases, the identity is also the payer. 
+
+If you want to learn more, go to the [Umi Context Interfaces Paragraph](/umi/interfaces#the-context-interface) 
+
+### Registering Programs and Clients
+
+In some cases, Umi requires you to specify the programs or clients you want to use (e.g, when minting a Core Asset, you'll need to tell Umi to use the `Core` program). You can do this by calling the `.use()` method on your Umi instance and passing in the client.
+
+Here’s how to register the `mpl-token-metadata` client with Umi:
+
+```ts
+import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
+
+const umi = createUmi('https://api.mainnet-beta.solana.com')
+  .use(mplTokenMetadata())
+```
+
+**Note**: You can also chain `.use()` calls to register multiple clients like this:
+
+```ts
+import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
+import { mplCandyMachine } from '@metaplex-foundation/mpl-candy-machine'
+
+const umi = createUmi('https://api.mainnet-beta.solana.com')
+  .use(mplTokenMetadata())
+  .use(mplCandyMachine())
+```

+ 0 - 42
src/pages/umi/getting-started/js.md

@@ -1,42 +0,0 @@
----
-title: Getting Started using JavaScript
-metaTitle: JavaScript SDK | Umi
-description: Get started with essential programs using JavaScript
----
-
-Metaplex provides a JavaScript library that can be used to interact with essential programs. Thanks to the [Umi framework](https://github.com/metaplex-foundation/umi), it ships without many opinionated dependencies and, thus, provides a lightweight library that can be used in any JavaScript project.
-
-To get started, you'll need to [install the Umi framework](https://github.com/metaplex-foundation/umi/blob/main/docs/installation.md) and the Toolbox JavaScript library.
-
-```sh
-npm install \
-  @metaplex-foundation/umi \
-  @metaplex-foundation/umi-bundle-defaults \
-  @solana/web3.js \
-  @metaplex-foundation/mpl-toolbox
-```
-
-Next, you may create your `Umi` instance and install the `mplToolbox` plugin like so.
-
-```ts
-import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
-import { mplToolbox } from '@metaplex-foundation/mpl-toolbox'
-
-// Use the RPC endpoint of your choice.
-const umi = createUmi('http://127.0.0.1:8899').use(mplToolbox())
-```
-
-That's it, you can now interact with essential programs by using [the various functions provided by the library](https://mpl-toolbox.typedoc.metaplex.com/) and passing your `Umi` instance to them. Here's an example of creating a new associated token account.
-
-```ts
-import { createAssociatedToken } from '@metaplex-foundation/mpl-toolbox'
-
-await createAssociatedToken(umi, { mint, owner }).sendAndConfirm(umi)
-```
-
-🔗 **Helpful Links:**
-
-- [Umi Framework](https://github.com/metaplex-foundation/umi)
-- [GitHub Repository](https://github.com/metaplex-foundation/mpl-toolbox)
-- [NPM Package](https://www.npmjs.com/package/@metaplex-foundation/mpl-toolbox)
-- [API References](https://mpl-toolbox.typedoc.metaplex.com/)