0.2.0 • Published 6 years ago

blcf-web3-ts v0.2.0

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

blcf-web3-ts    [Gitter

A web3 adaptor for BlueCefa Chain RESTful API.

Table of contents

Install

npm install --save blcf-web3-ts
npm install --save-exact web3@1.0.0-beta.37   # Web3 is needed as dependency.

Usage

// ES6 style
import { blcf } from "blcf-web3-ts";
const Web3 = require("web3");		// Recommend using require() instead of import here

const web3 = blcf(new Web3(), "http://localhost:8669");

web3.eth.getBlock("latest").then(res => console.log(res));
// Best block info will be displayed

If you would like to write code in ES5, check below for the initialization code.

// ES5 style
const blcf = require("blcf-web3-ts").blcf;
const Web3 = require("web3");

const web3 = blcf(new Web3(), "http://localhost:8669");

web3.eth.getBlock("latest").then(res => console.log(res));
// Best block info will be displayed

Web3 method supported

web3 instance
├── eth
│   ├── getBlockNumber
│   ├── getBalance
│   ├── getStorageAt
│   ├── getCode
│   ├── getBlock
│   ├── getTransaction
│   ├── getTransactionReceipt
│   ├── sendTransaction
│   ├── sendSignedTransaction
│   ├── call
│   ├── estimateGas
│   ├── getPastLogs
│   ├── subscribe
│   ├── clearSubscriptions
│   ├── getEnergy
│   ├── getChainTag
│   ├── getBlockRef
│   ├── accounts
│   └── Contract
│       ├── Constructor(new Contract())
│       ├── clone
│       ├── deploy
│       ├── methods
│       ├── methods.myMethod.call
│       ├── methods.myMethod.send
│       ├── methods.myMethod.estimateGas
│       ├── methods.myMethod.encodeABI
│       ├── events
│       ├── once
│       ├── events.myEvent
│       ├── events.allEvents
│       └── getPastEvents
└── utils

Send Transaction

In BlueCefa official implementation , the client DOES NOT neither manage user's private-key/keyStore nor use private key to sign a Transaction. Unfortunately, blcf-web3-ts can not directly perform eth_sendTransaction but there is another way to sign a transaction.

In web3.js accounts, it gives the opportunity to add your private-key, stored in your runtime context (In Node.js context, it's stored in memory while in Browser context, it's stored in memory/local storage), to accounts module. When you are trying to send a transaction, the module will check the private key associated with from field. Once the private key and from have been matched, the module will sign the transaction. The APIs that follows the mechanism are:

  • web3.eth.sendTransaction()
  • contract.deploy.send()
  • contract.methods.myMethod.send()

Documentation

install docsify

npm i docsify-cli -g

start docsify serve

docsify serve docs

Play with multi-clause

  1. blcf-devkit.jssupports multi-clause and sign transaction
  2. send signed transaction using sendSignedTransaction

FAQ

blcf_web3_adaptor vs blcf-web3-ts

BlueCefa only supports RESTful API other than ethereum's JSON-RPC, so we developed two component for the developers familiar with ethereum's develop kits. blcf_web3_adaptor is a standalone program that accepts JSON-RPC calls and transforms the requests to BlueCefa's RESTful API and blcf-web3-ts is an extended web3 that sends the request directly to BlueCefa's RESTful API.So,

  • If you are writing smart contract using truffle/remix-ide or other tools that uses original web3, you should use blcf_web3_adaptor.

  • If you are writing some application or scripts executing in Node.js or Browser environment, you should use blcf-web3-ts.

Multi party payment protocol or sponsored contract

Method not supported

The RESTful API of BlueCefa is different with Ethereum's JSON-RPC, therefore, there are some methods in web3 are not supported by blcf-web3-ts, feel free to open an issue discuss the features.

There is a possibility that when you trying to call sendTransaction or send functions, blcf-web3-ts will return Method not supported under version 0.3.1, due to account module will check the private key associated with from field. After upgrade to version 0.3.1 or newer, blcf-web3-ts will show The private key corresponding to from filed can't be found in local eth.accounts.wallet to make an error more specific.

Subscriptions support

Need blcf@v1.0.0 and later to work with subscription module.

Notes

  • There are three special block number in Ethereum: earliest,latest,pending. In BlueCefa Chain, we introduced best block and there is no pending block, so they will be replaced with 0 (aka genesis), best, best

Compatibility

Currently, blcf-web3-ts is compatible with >= web3@1.0.0-beta.1 and <= web3@1.0.0-beta.37.

License

This project is licensed under the MIT license, Copyright (c) 2017 VeChain Foundation. For more information see LICENSE.md.

The MIT License

Copyright (c) 2017 VeChain Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.