Coder Social home page Coder Social logo

fabcotech / rchain-web Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 338 KB

General purpose javascript/typescript library to interact with the RChain blockchain from the browser.

Home Page: https://github.com/fabcotech/rchain-web

License: MIT License

JavaScript 7.39% TypeScript 92.61%
rchain rnode http

rchain-web's Introduction

rchain-web

Up to date with rnode 0.12.0

rchain-web is a general purpose javascript/typescript library to interact with the RChain blockchain from the browser, it is the alter ego of rchain-toolkit that has been built for nodeJS.

Warning: This library does not use keccak256 and/or blake2. It is not able to generate hashes and signature necessary for deploying rholang. In order to do that you can use nodeJS rchain-toolkit, or metamask (see example).

Examples

Instanciate RChainWeb

You will need an instance of RChainWeb for all the HTTP operations.

const rchainWeb = new RChainWeb.http({
  readOnlyHost: "https://observer.bm.testnet.rchain.coop",
  validatorHost: "https://node0.bm.testnet.rchain.coop",
});

deploying with metamask

const main = async () => {
  const vab = await rchainWeb.validAfterBlockNumber();
  const deployData = {
    term: `new x in { x!("hello") }`,
    timestamp,
    validAfterBlockNumber: vab,
    phloLimit: 100000000,
    phloPrice: 1,
  };

  const dds = RChainWeb.utils.getDeployDataToSign(deployData);

  const sig = await window.ethereum
    .request({
      method: "personal_sign",
      params: [
        [...dds],
        METAMASK_ETH_ADDRESS // ETH address of metamask account
      ],
    })

  const optionsDeploy = {
    data: deployData,
    deployer: PUBLIC_KEY, // RChain public key
    signature: RChainWeb.utils.encodeBase16(
      RChainWeb.utils.decodeBase16(sig)
    ),
    sigAlgorithm: "secp256k1:eth",
  };

  const result = await rchainWeb
    .deploy(
      optionsDeploy,
      120000 * 10 // will try to get value on x during 20 minutes
    )
    .then((a) => {
      resolve(a);
    })

  console.log(result)
}
main();

explore deploy

const main = async () => {
  const result = await rchainWeb
    .exploreDeploy(
      {
        term: `new x in { x!("hello") }`
      }
    );

  const value = RChainWeb.utils.rhoValToJs(result.expr[0])
}
main();

explore deploys (many at a time)

const main = async () => {
  const result = await rchainWeb
    .exploreDeploys(
      [
        `new x in { x!("hello") }`,
        `new x in { x!("world") }`,
      ]
    );

  const value1 = RChainWeb.utils.rhoValToJs(results[0].expr[0])
  const value2 = RChainWeb.utils.rhoValToJs(results[1].expr[0])
}
main();

prepare deploy

const main = async () => {
  const pd = await rchainWeb
    .prepareDeploy(
      {
        deployer: "abcdef", // RChain public key
        timestamp: new Date().getTim(),
        nameQty: 1,
      }
    );

  console.log(pd)
}
main();

data at name

const main = async () => {
  const result = await rchainWeb
    .dataAtName(
      {
        name: {
          UnforgPrivate: { data: pd.names[0] },
        },
        depth: 3
      }
    );

  console.log(RChainWeb.utils.rhoValToJs(result.exprs[0].expr))
}
main();

List of methods

Utils

  • RChainWeb.utils.rhoValToJs
  • RChainWeb.utils.rhoExprToVar
  • RChainWeb.utils.decodePar
  • RChainWeb.utils.getDeployDataToSign
  • RChainWeb.utils.decodeBase16
  • RChainWeb.utils.encodeBase16

HTTP (after instanciated)

  • RChainWeb.http.dataAtName
  • RChainWeb.http.deploy
  • RChainWeb.http.exploreDeploy
  • RChainWeb.http.exploreDeploys
  • RChainWeb.http.prepareDeploy
  • RChainWeb.http.validAfterBlockNumber
  • RChainWeb.http.blocks

rchain-web's People

Contributors

fabcotech avatar

Watchers

 avatar  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.