Git Product home page Git Product logo

filecoin-client-tutorial's Introduction

Filecoin Client Tutorial

A simple tutorial for starting an express server and storing data on the Filecoin Network in under 5 minutes.

Satisfy dependency requirements

  • Make sure you have homebrew.
  • Make sure you run xcode-select -p, if the command does not return a response, run xcode-select --install
  • Make sure you run brew install node
  • Make sure you run brew install go

Setup Docker

Terminal

You will need to run three simultaneous terminal windows:

Setup Lotus DevNet

In Terminal window 1:

  • Clone the Lotus DevNet repository: [email protected]:textileio/lotus-devnet.git
  • Run docker run --name texdevnet -e TEXLOTUSDEVNESPEED=1500 -p 1234:7777 textile/lotus-devnet

Setup Powergate

In Terminal window 2:

  • Clone the Powergate repository: [email protected]:textileio/powergate.git
  • cd powergate
  • Build and install the CLI: make build-pow
  • Build the Powergate server: make build-powd
  • cd docker
  • make devnet

Install and run

In Terminal window 3:

Run these commands to start the client locally.

git clone [email protected]:filecoin-project/filecoin-client-tutorial.git
cd filecoin-client-tutorial
npm install

Create a server.js file

The main API you will interact with is the Filecoin File System (FFS).

//import express server
import express from "express";

import fs from "fs"
import { ffs, createPow } from "@textile/powergate-client"

const pow = createPow({ host:"http://0.0.0.0:6002" })
const server = express();

server.listen(8080, async () => {

  //create a new FFS instance.
  const { token } = await pow.ffs.create()
  console.log({token});

  //set the auth token that the Powergate client to use.
  pow.setToken(token)

  // cache data in IPFS in preparation to store it using FFS
  const { cid } = await pow.ffs.addToHot(buffer)
  console.log({cid});
  const buffer = fs.readFileSync(`dog.jpg`)

  // store the data in FFS using the default storage configuration
  const { jobId } = await pow.ffs.pushConfig(cid)
  console.log({jobId});

  // watch the FFS job status to see the storage process progressing
  const cancel = pow.ffs.watchJobs((job) => {
    console.log({job})
    if (job.status === ffs.JobStatus.CANCELED) {
      console.log("job canceled")
    } else if (job.status === ffs.JobStatus.FAILED) {
      console.log("job failed")
    } else if (job.status === ffs.JobStatus.SUCCESS) {
      console.log("job success!")
    }
  }, jobId);
});
  • Run node .

Go to the Filecoin Client to see the full end to end application.

References

filecoin-client-tutorial's People

Contributors

jasonleyser avatar snario avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.