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

update code examples #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions documentation/src/01-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: Introduction
hide_title: true
---

# Introduction
## Introduction

## What is Filecoin.js
### What is Filecoin.js

The filecoin.js library aims to be a complete library for interacting with local or remote Filecoin nodes.

Expand Down
25 changes: 12 additions & 13 deletions documentation/src/02-installing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@ title: Adding Filecoin.js
hide_title: true
---

# Adding Filecoin.js
## Adding Filecoin.js

In this section, we will show how to add Filecoin.js library to your web application.

## NodeJS
### Node.js

#### Step 1: Install Filecoin.js

**Step 1:** Install Filecoin.js using [yarn](https://classic.yarnpkg.com/en/package/jest)
```shell
$ npm install filecoin.js
// or
$ yarn add filecoin.js
```
or [npm](https://www.npmjs.com/)
```shell
$ npm install filecoin.js
```

**Step 2:** Import module using node.js require or ES6 synthax
#### Step 2: Import module using node.js require or ES6 syntax

```javascript
const { } = require("filecoin.js");
const filecoin = require("filecoin.js");
// or
import { } from "filecoin.js";
import * as filecoin from "filecoin.js";
```

## Browser
### Browser

For using Filecoin.js library in broswer, include the core library in your HTML file and you're ready to go!
For using Filecoin.js library in browser, include the core library in your HTML file and you're ready to go!

```html
<script type="text/javascript" src="https://unpkg.com/filecoin.js"></script>
<script type="text/javascript" src="https://unpkg.com/filecoin.js" />
```
57 changes: 39 additions & 18 deletions documentation/src/03-guides-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,58 @@ title: Guides example
hide_title: true
---

# Guides example
## Guides example

Node JavaScript/TypeScript:
Before getting into coding, you are going to have to start a [Lotus](https://lotus.filecoin.io/lotus/install/prerequisites/) node. To get started quickly, you can use a [hosted node by Glif](https://api.node.glif.io/). In this example, we can use a RPC endpoint for calibration net:

```javascript
import { HttpJsonRpcConnector, LotusClient } from 'filecoin.js';
```shell
https://api.calibration.node.glif.io/rpc/v0
```

(async () => {
### Node.js and front-end frameworks

const httpConnector = new HttpJsonRpcConnector({ url: __LOTUS_HTTP_RPC_ENDPOINT__, token: __LOTUS_AUTH_TOKEN__ });
You can use ES6 style and/or use it in your Typescript.

```javascript
import { HttpJsonRpcConnector, LotusClient } from 'filecoin.js';

(async() => {
const httpConnector = new HttpJsonRpcConnector('https://api.calibration.node.glif.io/rpc/v0');
const lotusClient = new LotusClient(httpConnector);
const version = await lotusClient.common.version();
console.log(version);

})().then().catch();
console.log(`Hello, Filecoin.js v${version}`);
})();
```

Browser:
#### Node.js polyfill

> ⚠️ Because many Node.js dependencies are not
> compatible on the front end, if you faced an issue like
> [this](https://github.com/filecoin-shipyard/filecoin.js/issues/57),
> chances are you will have to add a [polyfills](https://www.npmjs.com/package/vite-plugin-node-polyfills) as a dependency.

### Browser

Just drop Filecoin.js into the `<script>` tag and you're ready to go.

```html
<script type="text/javascript" src="https://unpkg.com/filecoin.js"></script>
<script type="text/javascript" src="https://unpkg.com/filecoin.js" />
<script type="text/javascript">
(async () => {
(async () => {
const httpConnector = new FilecoinJs.HttpJsonRpcConnector('https://api.calibration.node.glif.io/rpc/v0');

const httpConnector = new FilecoinJs.HttpJsonRpcConnector({ url: __LOTUS_HTTP_RPC_ENDPOINT__, token: __LOTUS_AUTH_TOKEN__ });
const lotusClient = new FilecoinJs.LotusClient(httpConnector);
const version = await lotusClient.common.version();
console.log(version);
})();
</script>
```

const lotusClient = new FilecoinJs.LotusClient(httpConnector);
const version = await lotusClient.common.version();
console.log(version);
If you run your own node, you will have to provide an argument of type [`JsonRpcConnectionOptions`](https://filecoin-shipyard.github.io/filecoin.js/docs/api/filecoin.js.httpjsonrpcconnector._constructor_) to [`HttpJsonRpcConnector`](https://filecoin-shipyard.github.io/filecoin.js/docs/api/filecoin.js.httpjsonrpcconnector) constructor.

})().then().catch();
</script>
```js
const httpConnector = new HttpJsonRpcConnector({
url: __LOTUS_HTTP_RPC_ENDPOINT__,
token: __LOTUS_AUTH_TOKEN__,
});
```
9 changes: 7 additions & 2 deletions documentation/src/08-setup-mnemonic-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ hide_title: true
---

# Setup mnemonic provider

Node JavaScript/TypeScript:

```javascript
import { HttpJsonRpcConnector, MnemonicWalletProvider } from 'filecoin.js';
import {
HttpJsonRpcConnector,
LotusClient,
MnemonicWalletProvider,
} from 'filecoin.js';

(async () => {

Expand All @@ -27,7 +32,7 @@ import { HttpJsonRpcConnector, MnemonicWalletProvider } from 'filecoin.js';
console.log(myAddress);
// f1zx43cf6qb6rd...

})().then().catch();
})();
```

Browser:
Expand Down
Loading