npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xjshutu/js-conflux-sdk

v0.13.6-consortium.1.0.3

Published

JavaScript Conflux Software Development Kit

Downloads

15

Readme

js-conflux-sdk

npm npm

JavaScript Conflux Software Development Kit

Installation

npm install js-conflux-sdk

Quick Usage

Nodejs

const { Conflux } = require('js-conflux-sdk');

async function main() {
  // initalize a Conflux object
  const cfx = new Conflux({
    url: 'http://testnet-jsonrpc.conflux-chain.org:12537',
    defaultGasPrice: 100,
    defaultGas: 1000000,
    logger: console,
  });
  // get balance
  const balance = await cfx.getBalance('0xbbd9e9be525ab967e633bcdaeac8bd5723ed4d6b');
  console.log(balance); // 937499420597305000n
}

main();

require deep nested file/dir

const util = require('js-conflux-sdk/src/util');

Frontend

umd

import { Conflux } from 'js-conflux-sdk/dist/js-conflux-sdk.umd.min.js';

or if your bundler supports the browser field in package.json

import { Conflux } from 'js-conflux-sdk';

or

<script type="text/javascript" src="node_modules/js-conflux-sdk/dist/js-conflux-sdk.umd.min.js"></script>
<script type="text/javascript">
  const cfx = new window.Conflux.Conflux({
    url: 'http://testnet-jsonrpc.conflux-chain.org:12537',
    defaultGasPrice: 100,
    defaultGas: 1000000,
    logger: console,
  });
</script>

Complete usage

Send an transaction

If you want send a transaction to conflux network, you can use the SDK's sendTransaction method, for example:

const account = cfx.Account(PRIVATE_KEY); // create account instance
const txHash = await cfx.sendTransaction({
    from: account, // from account instance and will by sign by account.privateKey
    to: "0x-another-address", // accept address string or account instance
    value: util.unit.fromCFXToDrip(0.125), // use unit to transfer from CFX to Drip
});
const tx = await cfx.getTransactionByHash(txHash);  // status 0x0 means success
const txReceipt = await cfx.getTransactionReceipt(txHash);  // outcomeStatus 0x0 means success

That's it, it's so easy. Besides from, to, value there are other fields you can set and should know:

  • gas
  • gasPrice
  • epochHeight
  • chainId
  • data
  • storageLimit
  • nonce

For the detail explanation of these fields check official doc.

You should pay more attention to storageLimit, it's a conflux special tx parameter, if you transaction execute failed or stucked in tx pool, you can try this parameter.

If you have encounter problem when sending transaction, you should check nonce, gas, storageLimit and your balance. After adjust your parameter you can send a new transaction with same(or correct) nonce and higher gasPrice.

Advanced usage of sendTransaction

The SDK's sendTransaction and sendRawTransaction method have several advance method will be helpful.

let txParameters = {
  from: account,
  to: "0x-a-address",
  value: "0x100"
};
const txHash = await cfx.sendTransaction(txParameters);  // send the tx and return a hash
const tx = await cfx.sendTransaction(txParameters).get();  // will also get the tx by hash
const tx = await cfx.sendTransaction(txParameters).mined();  // wait tx mined and return the tx
const receipt = await cfx.sendTransaction(txParameters).executed();  // wait tx executed and return receipt
const receipt = await cfx.sendTransaction(txParameters).confirmed();  // wait tx confirmed and return receipt

Deploy or interact with contract

If you use this SDK, probability you want to deploy a contract or interact with a contract. When deploy a contract you need contract's bytecode and abi

// create contract instance
const contract = cfx.Contract({
  abi: USER_YOUR_CONTRACT_ABI,
  bytecode: USER_YOUR_CONTRACT_BYTECODE,
});
// deploy the contract, and get `contractCreated`
const receipt = await contract.constructor()  // fill the parameter to your constructor
  .sendTransaction({ from: account })  // you can set other tx parameters such as: gas, gasPrice, storageLimit and so on (leave `to` empty)
  .confirmed();
console.log(receipt); // contract address is receipt.contractCreated

With a deployed contract's address and abi, you can interact with it: query or change contract state.

const contract = cfx.Contract({
    abi: YOUR_CONTRACT_ABI,
    address: 'YOUR-CONTRACT-ADDRESS',
});
// query contract state
let ret = await contract.get(); // `get` is a method of your contract
console.log(ret.toString()); 
// change contract state by send a transaction
const receipt = await contract.inc(1).sendTransaction({  // `inc` is also your contract's method 
  from: account,  // you can set other tx parameter such as `gas`, `gasPrice`, `storageLimit` and so on
}).confirmed();

Document

Change log

see