Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes-mysten committed Jul 25, 2023
1 parent 6d5939e commit 4478400
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
21 changes: 13 additions & 8 deletions sdk/docs/pages/typescript-sdk/connect.mdx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Connecting to Sui Network

The Sui TypeScript SDK provides a SuiClient class that you can use to connect to a network's JSON-RPC server. Use the `SuiClient` for all JSON-RPC operations.
The Sui TypeScript SDK provides a SuiClient class that you can use to connect to a network's
JSON-RPC server. Use the `SuiClient` for all JSON-RPC operations.

## Network locations

The following table lists the locations for the Sui network and the default local network settings.

| Network | Full node | faucet |
| --- | --- | --- |
| local | `http://127.0.0.1:9000` | `http://127.0.0.1:9123/gas` |
| Devnet | `https://fullnode.devnet.sui.io:443` | `https://faucet.devnet.sui.io/gas` |
| Network | Full node | faucet |
| ------- | ------------------------------------- | ----------------------------------- |
| local | `http://127.0.0.1:9000` | `http://127.0.0.1:9123/gas` |
| Devnet | `https://fullnode.devnet.sui.io:443` | `https://faucet.devnet.sui.io/gas` |
| Testnet | `https://fullnode.testnet.sui.io:443` | `https://faucet.testnet.sui.io/gas` |
| Mainnet | `https://fullnode.mainnet.sui.io:443` | `null` |
| Mainnet | `https://fullnode.mainnet.sui.io:443` | `null` |

## Using SuiClient to connect to a Sui network

To establish a connection to a network, import `SuiClient` from `@mysten/sui.js/client` and pass the relevant URL to the `url` parameter. The following example establishes a connection to Testnet and requests SUI from that network's faucet.
To establish a connection to a network, import `SuiClient` from `@mysten/sui.js/client` and pass the
relevant URL to the `url` parameter. The following example establishes a connection to Testnet and
requests SUI from that network's faucet.

```typescript
import { SuiClient } from '@mysten/sui.js/client';
Expand All @@ -32,4 +35,6 @@ await suiClient.getCoins({
});
```

For local development, you can run `cargo run --bin sui-test-validator` to spin up a local network with a local validator, a Full node, and a faucet server. Refer to [the Local Network guide](https://docs.sui.io/build/sui-local-network) for more information.
For local development, you can run `cargo run --bin sui-test-validator` to spin up a local network
with a local validator, a Full node, and a faucet server. Refer to
[the Local Network guide](https://docs.sui.io/build/sui-local-network) for more information.
75 changes: 48 additions & 27 deletions sdk/docs/pages/typescript-sdk/hello-sui.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@

import { Callout } from 'nextra/components';

This basic example introduces you to the Sui TypeScript SDK. The Node.js example mints SUI on a Sui network and then queries the address to get a sum for the owned SUI. You don't need to use an IDE to complete the example, but one like Microsoft Visual Studio Code helps centralize more advanced projects.
This basic example introduces you to the Sui TypeScript SDK. The Node.js example mints SUI on a Sui
network and then queries the address to get a sum for the owned SUI. You don't need to use an IDE to
complete the example, but one like Microsoft Visual Studio Code helps centralize more advanced
projects.

## Before you begin

You need an address on a Sui development network (Devnet, Testnet, local). If you don't already have an address, use the [Sui Client CLI](https://docs.sui.io/build/cli-client) or the [Sui Wallet browser extension](https://docs.mystenlabs.com/wallet) to create one.
You need an address on a Sui development network (Devnet, Testnet, local). If you don't already have
an address, use the [Sui Client CLI](https://docs.sui.io/build/cli-client) or the
[Sui Wallet browser extension](https://docs.mystenlabs.com/wallet) to create one.

You also need [Node.js](https://nodejs.org/en/download/current) and a package manager like [pnpm](https://pnpm.io/installation) to follow this example, so install them on your system if you haven't already.
You also need [Node.js](https://nodejs.org/en/download/current) and a package manager like
[pnpm](https://pnpm.io/installation) to follow this example, so install them on your system if you
haven't already.

## Start a project

Using a Terminal or Console, create a folder on your system (`hello-sui` in this example) and make it the working directory.
Using a Terminal or Console, create a folder on your system (`hello-sui` in this example) and make
it the working directory.

```sh
mkdir hello-sui
cd hello-sui
```

When you use a package manager to install the necessary packages, it downloads the modules to your `node_modules` folder and adds the references to your `package.json` file, creating the file if it doesn't already exist. For this example, you need only the Sui TypeScript SDK:
When you use a package manager to install the necessary packages, it downloads the modules to your
`node_modules` folder and adds the references to your `package.json` file, creating the file if it
doesn't already exist. For this example, you need only the Sui TypeScript SDK:

```sh npm2yarn
npm i -D @mysten/sui.js
```

Your `package.json` file now has a *dependencies* section with `@mysten/sui.js` listed with the package version number.
Your `package.json` file now has a _dependencies_ section with `@mysten/sui.js` listed with the
package version number.

```json
"dependencies": {
Expand All @@ -35,9 +46,10 @@ Your `package.json` file now has a *dependencies* section with `@mysten/sui.js`

## Get some SUI for your account

Instead of a 'Hello World' output to your console, this example introduces some SUI to your wallet address. You must be on Devnet, Testnet, or a local network to use a faucet for minting SUI.
Instead of a 'Hello World' output to your console, this example introduces some SUI to your wallet
address. You must be on Devnet, Testnet, or a local network to use a faucet for minting SUI.

Create a new `index.js` file in the root of your project with the following code.
Create a new `index.js` file in the root of your project with the following code.

```js
import { requestSuiFromFaucetV0, getFaucetHost } from '@mysten/sui.js/faucet';
Expand All @@ -48,34 +60,38 @@ const SUI_NETWORK = 'https://fullnode.devnet.sui.io:443/';
const MY_ADDRESS = '<YOUR_SUI_ADDRESS>';

// Create a new SuiClient object pointing to the network you want to use.
const suiClient = new SuiClient({url: SUI_NETWORK})
const suiClient = new SuiClient({ url: SUI_NETWORK });

// Function to process the JSON that getCoins provides and then return number of SUI.
const balance = (coins) => {
let bal = 0;
coins.data.map((coin) => bal += Number(coin.balance));
return bal/1000000000;
}
let bal = 0;
coins.data.map((coin) => (bal += Number(coin.balance)));
return bal / 1000000000;
};

// Store the JSON representation for the SUI the address owns before using faucet.
const coinsBefore = await suiClient.getCoins({
owner: MY_ADDRESS
})
owner: MY_ADDRESS,
});

const statusId = await requestSuiFromFaucetV0({
// Use getFaucetHost to make sure you're using correct faucet address.
// You can also just use the address (see Connecting to Sui Network topic for values).
host: getFaucetHost('testnet'),
recipient: MY_ADDRESS
})
// Use getFaucetHost to make sure you're using correct faucet address.
// You can also just use the address (see Connecting to Sui Network topic for values).
host: getFaucetHost('testnet'),
recipient: MY_ADDRESS,
});

// Store the JSON representation for the SUI the address owns after using faucet.
const coinsAfter = await suiClient.getCoins({
owner: MY_ADDRESS
})
owner: MY_ADDRESS,
});

// Output result to console.
console.log(`Balance before faucet: ${balance(coinsBefore)} SUI. Balance after: ${balance(coinsAfter)} SUI. Hello, SUI!`);
console.log(
`Balance before faucet: ${balance(coinsBefore)} SUI. Balance after: ${balance(
coinsAfter,
)} SUI. Hello, SUI!`,
);
```

Save the file, then use Node.js to run it in your Console or Terminal:
Expand All @@ -84,10 +100,15 @@ Save the file, then use Node.js to run it in your Console or Terminal:
node index.js
```

The code imports the `requestSuiFromFaucetV0` function from the SDK and calls it to mint SUI for the provided address. The code also imports `SuiClient` to create a new client on the Sui network that it uses to query the address and output the amount of SUI the address owns before and after using the faucet. You can check the total SUI for your address using the Sui Wallet or Sui Client CLI.
The code imports the `requestSuiFromFaucetV0` function from the SDK and calls it to mint SUI for the
provided address. The code also imports `SuiClient` to create a new client on the Sui network that
it uses to query the address and output the amount of SUI the address owns before and after using
the faucet. You can check the total SUI for your address using the Sui Wallet or Sui Client CLI.

<Callout type="note">
Faucets on Devnet and Testnet are rate limited. If you run the script too many times, you surpass the limit and must wait to successfully run it again.
Faucets on Devnet and Testnet are rate limited. If you run the script too many times, you surpass
the limit and must wait to successfully run it again.
</Callout>

You can also use the [Sui Client CLI](https://docs.sui.io/build/cli-client) to perform client calls on a Sui network.
You can also use the [Sui Client CLI](https://docs.sui.io/build/cli-client) to perform client calls
on a Sui network.
18 changes: 14 additions & 4 deletions sdk/docs/pages/typescript-sdk/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ To use the Sui TypeScript SDK in your project, run the following command in your
npm i -D @mysten/sui.js
```

## Experimental tag for use with a local Sui network
## Experimental tag for use with a local Sui network

Projects developing against one of the on-chain Sui networks (Devnet, Testnet, Mainnet) should use the base SDK published in the NPM registry (previous section) because the code aligns with the relevant JSON-RPC. If your developing against a [local network](https://docs.sui.io/testnet/build/sui-local-network) built from the `main` branch of the Sui monorepo, however, you should use the `experimental`-tagged SDK package as it contains the latest features (or a local build detailed in the section that follows).
Projects developing against one of the on-chain Sui networks (Devnet, Testnet, Mainnet) should use
the base SDK published in the NPM registry (previous section) because the code aligns with the
relevant JSON-RPC. If your developing against a
[local network](https://docs.sui.io/testnet/build/sui-local-network) built from the `main` branch of
the Sui monorepo, however, you should use the `experimental`-tagged SDK package as it contains the
latest features (or a local build detailed in the section that follows).

```sh npm2yarn
npm i -D @mysten/sui.js@experimental
```

## Install from local build

To build the SDK from the [Sui monorepo](https://github.com/MystenLabs/sui), you must use [pnpm](https://pnpm.io/). With pnpm installed, run the following command from the `sui` root directory:
To build the SDK from the [Sui monorepo](https://github.com/MystenLabs/sui), you must use
[pnpm](https://pnpm.io/). With pnpm installed, run the following command from the `sui` root
directory:

```bash
# Install all dependencies
Expand All @@ -29,7 +36,10 @@ pnpm install
pnpm sdk build
```

With the SDK built, you can import the library from your `sui` project. To do so, use a path to the `sui/sdk/typescript` directory that is relative to your project. For example, if you created a folder `my-sui-project` at the same level as `sui`, use the following to import the locally built Sui Typescript package:
With the SDK built, you can import the library from your `sui` project. To do so, use a path to the
`sui/sdk/typescript` directory that is relative to your project. For example, if you created a
folder `my-sui-project` at the same level as `sui`, use the following to import the locally built
Sui Typescript package:

```bash
pnpm add ../sui/sdk/typescript
Expand Down

0 comments on commit 4478400

Please sign in to comment.