0.0.1-beta.7 • Published 2 years ago

cava_web3_bridge v0.0.1-beta.7

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

CAVA web3 bridge

CAVA blockchain JavaScript API is simply bridged based on Web3.js

Basics

Requirements

You need to have Node.js 10.10.0+

Installation

Run the following commands in your project folder:

npm install cava_web3_bridge
cd ./node_modules/cava_web3_bridge // cd <YOUR_NODE_MODULES_LOCATION>/cava_web3_bridge
npm install
npm run rebuild

Creating an Instance

var CAVA_Web3_Bridge = require('cava_web3_bridge');

Connect to CAVA Blockchain

var cava_web3_bridge = new CAVA_Web3_Bridge(RPC_HOST)

CAVA_Web3_Bridge.initWeb3() has been deprecated

You can use CAVA Blockchain public rpc as the rpc host \ Mainnet\ https://cavachn.pro/rpc:8501 Testnet \ ganache-ci http://127.0.0.1:8545

Examples

Create account

var account = cava_web3_bridge.createAccount();
console.log(account);

Create account by privateKey

var private_key = "<YOUR PRIVATEKEY>";
var address_info =  cava_web3_bridge.privateKeyToAccount(privateKey);

Get CAVA balance

var address = "<ADDRESS>";
cava_web3_bridge.getBalance(address, function(error, balance){
	console.log(balance);
});

Send transaction

var private_key = "<YOUR PRIVATEKEY>";
var from = "<YOUR ADDRESS>";
var to = "<TO ADDRESS>";

var raw_tx = {
	"from": from,
	"to": to,
	"nonce": 104,
	"gasPrice": 10,
	"gasLimit": 100000,
	"value":1000000000000000000,
  	"data": "0x0"
}

cava_web3_bridge.sendRawTransaction(raw_tx,private_key,function(error, result){
	console.log("tx sent:", result);
});

Sign transaction

var private_key = "<YOUR PRIVATEKEY>";
var from = "<YOUR ADDRESS>";
var to = "<TO ADDRESS>";

var raw_tx = {
	"from": from,
	"to": to,
	"nonce": 104,
	"gasPrice": 10,
	"gasLimit": 100000,
	"value":1000000000000000000,
  	"data": "0x0"
}

cava_web3_bridge.signTransaction(raw_tx,private_key,function(result){
	console.log(result);
});

Send signed transaction

// eg: f86d428502540be400830186a0948f46586d7b9d28ab41eaed17feab7c0d403830f0880de0b6b3a7640000001ca08b2759a90cd6fd841cc827a688f7678a57ff0e4eb26412a3dc65e501a13ef398a0463c0bc41e9e3ec9780fd9fc071fa088958a4054593a7ab4fe85ac9d8a5306ae
var signed_data = "";
cava_web3_bridge.sendSignedTransaction(signed_data,function(error, hash){
	console.log("tx sent:", hash);
});

Send vote transaction

var private_key = "<YOUR PRIVATEKEY>";
var from = "<YOUR ADDRESS>";
var to = "<Rpresentative Coinbase ADDRESS>";

// Voting data: cava_web3.toHex("ufo:1:event:vote") = "0x75666f3a313a6576656e743a766f7465"
var raw_tx = {
	"from": from,
	"to": to,
	"nonce": 104,
	"gasPrice": 10,
	"gasLimit": 100000,
	"value":0,
  	"data": "0x75666f3a313a6576656e743a766f7465"
}

cava_web3_bridge.sendRawTransaction(raw_tx,private_key,function(error, result){
	console.log("tx sent:", result);
});

Deploy contract

// compiled solidity source code
var bytecode = "<Byte Code>";
var contract_abi = <YOUR CONTRACT ABI>;

var private_key = "<YOUR PRIVATEKEY>";
var from = "<YOUR ADDRESS>";

var raw_tx = {
	"from": from,
	"nonce": 99,
	"gasPrice": 10,
	"gasLimit": 3000000,
	"value":0,
}
var args = [];
cava_web3_bridge.deployContract(contract_abi, bytecode, args, raw_tx, private_key, function(error, hash){
	console.log(hash);
  // will also return the contract address if the contract was deployed
});

Call contract

Read data from contract

var contract_abi = <YOUR CONTRACT ABI>;
var contract_address = "<CONTRACT ADDRESS>";
var function_name = "<FUNCTION NAME>";
var args = [];
cava_web3_bridge.callAndReadContract(contract_address, contract_abi,function_name, args, function(error, result){
  console.log(result);
});

Write data to contract

var private_key = "<YOUR PRIVATEKEY>";
var from = "<YOUR ADDRESS>";
var contract_address = "<CONTRACT ADDRESS>";
var contract_abi = <YOUR CONTRACT ABI>;
var function_name = "<FUNCTION NAME>";
var args = [];

var raw_tx = {
	"from": from,
	"to": contract_address,
	"nonce": 99,
	"gasPrice": 10,
	"gasLimit": 1000000,
	"value":0
}

cava_web3_bridge.callAndWriteContract(raw_tx, private_key, contract_address, contract_abi, function_name, args, function(error,result){
   console.log(result);
});

Get transaction

var tx_hash = "<TX HASH>";
cava_web3_bridge.getTransaction(tx_hash, function(error,result){
	console.log(result);
});

Get transaction receipt

var tx_hash = "<TX HASH>";
cava_web3_bridge.getTransactionReceipt(tx_hash, function(error,result){
	console.log(result);
});

Get transaction count

// Get nonce, supports pending、latest
var address = "<ADDRESS>";
cava_web3_bridge.getTransactionCount(address, function(error, nonce){
	console.log(nonce);
});

cava_web3_bridge.getTransactionCount(address,"pending", function(error, nonce){
	console.log(nonce);
});

Get Block

Get block by block hash

var block_hash = "<BLOCK HASH>";
cava_web3_bridge.getBlock(block_hash,function(error, result){
	console.log(result);
});

Get block by block height

var block_height = <BLOCK Height>;
cava_web3_bridge.getBlock(block_height,function(error, result){
	console.log(result);
});

Contact

email: stvenyin@ttc.eco