Code Monkey home page Code Monkey logo

js-conflux-sdk's Introduction

js-conflux-sdk

npm npm

JavaScript Conflux Software Development Kit

1.0 Key features

  1. Add cfx.wallet to manage multiple accounts, which has replace v0.13's cfx.Account.
  2. Add cfx.InternalContract(name) to interact with Conflux internal contracts.
  3. Add websocket provider, support pub/sub
  4. Provide Drip to easily convert unit between CFX, Drip, Gdrip

Check the complete changelog

Installation

npm install js-conflux-sdk

Quick Usage

Nodejs

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

async function main() {
  // initialize a Conflux object
  const conflux = new Conflux({
    url: 'http://test.confluxrpc.org',
    logger: console, // for debug
  });
  // get balance
  const balance = await conflux.getBalance('0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b');
  // Note you should console.log a JSBI's string representation, directly log a JSBI is very ugly.
  console.log(balance.toString()); // 10098788868004995614504
}

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 conflux = new window.Conflux.Conflux({
    url: 'http://test.confluxrpc.org',
    logger: console,
  });
</script>

Documentation

Examples

Send transaction simple

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

async function main() {
  const conflux = new Conflux({ url: 'http://test.confluxrpc.org' });
  const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
  
  const receipt = await conflux.sendTransaction({
    from: account.address, // sender address which added into conflux.wallet
    to: ADDRESS, // receiver address
    value: Drip.fromCFX(0.1), // 0.1 CFX = 100000000000000000 Drip
  }).executed(); // wait till transaction executed and get receipt

  console.log(receipt); // outcomeStatus 0 means success
  /*
    {
      "index": 0,
      "epochNumber": 784649,
      "outcomeStatus": 0,
      "gasUsed": "21000",
      "gasFee": "21000000000000",
      "blockHash": "0xfa7c6d9d0c8ae436f1c9c785a316ac6cc4db16286eede3dd3d5c6a5a22ad5f9e",
      "contractCreated": null,
      "from": "0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b",
      "logs": [],
      "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
      "stateRoot": "0x166cbcbfd747505d91237ccd8f849fd6882ad0e6d8b9923ca3c44891cf2b8753",
      "to": "0x1ead8630345121d19ee3604128e5dc54b36e8ea6",
      "transactionHash": "0x50cd13d5f97dd867d4ca65e24eb642f6444c07d6af8143018c558df456f11e63"
    }
  */
}

main();

Send transaction complete

For the detail explanation of these fields check official doc.

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

async function main() {
  const conflux = new Conflux({ url: 'http://test.confluxrpc.org' });
  const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet

  const estimate = await conflux.estimateGasAndCollateral({ to, value });
  const status = await conflux.getStatus();
  
  const txHash = await conflux.sendTransaction({
    from: account.address, // or just `from: account` 
    to: ADDRESS,
    value: Drip.fromGDrip(100), // 100 GDrip = 100000000000 Drip
    gas: estimate.gasUsed,
    storageLimit: estimate.storageCollateralized,
    chainId: status.chainId,
    data: null,
    nonce: await conflux.getNextNonce(ADDRESS),
    gasPrice: await conflux.getGasPrice(),
    epochHeight: await conflux.getEpochNumber(),  
  });
  console.log(txHash);
  
  // you might need wait minute here...
  await new Promise(resolve => setTimeout(resolve, 30*1000));
  
  const transaction = await conflux.getTransactionByHash(txHash);
  console.log(transaction);
  
  const receipt = await conflux.getTransactionReceipt(txHash);
  console.log(receipt);
}

main();

Deploy contract

const { Conflux } = require('js-conflux-sdk');
const { abi, bytecode } = MINI_ERC20; // see https://github.com/Conflux-Chain/js-conflux-sdk/tree/master/example/contract/miniERC20.json

async function main() {
  const conflux = new Conflux({ url: 'http://test.confluxrpc.org' });
  const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
  const contract = conflux.Contract({ abi, bytecode });

  const receipt = await contract.constructor('MiniERC20', 18, 'MC', 10000)
    .sendTransaction({ from: account })
    .executed();
  console.log(receipt);
  /*
  {
    "index": 0,
    "epochNumber": 318456,
    "outcomeStatus": 0,
    "gasUsed": "1054531",
    "gasFee": "1054531000000000",
    "blockHash": "0x4a8b07e2694e358af075f7a9e96e78842b77ac2d511e2ab33f6acfff34a5846c",
    "contractCreated": "0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97",
    "from": "0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b",
    "logs": [],
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "stateRoot": "0x0940d4870e25bae1e7a5e5d7c19411b41922c025aa3de61aea2be17759673b1a",
    "to": null,
    "transactionHash": "0x6f55e67b486b5ef0c658c6d50cb5b89a2a2ddfecc1a1f2e414bbbefe36ef8dd5"
  }
  */

  // create contract address "0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97"
}

main();

Call contract

const { Conflux } = require('js-conflux-sdk');
const { abi } = MINI_ERC20; // see https://github.com/Conflux-Chain/js-conflux-sdk/tree/master/example/contract/miniERC20.json

async function main() {
  const conflux = new Conflux({ url: 'http://test.confluxrpc.org' });
  const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
  const contract = conflux.Contract({ abi, address: '0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97' });

  const name = await contract.name(); // call method without arguments
  console.log(name); // MiniERC20
  // user can set options by `contract.name().call({ from: account, ... })`

  const balance = await contract.balanceOf(account.address); // call method with arguments
  console.log(balance); // "10000" JSBI

  const txHash = await contract.transfer(ADDRESS, 10).sendTransaction({ from: account }); // send method transaction
  console.log(txHash); // 0xb31eb095b62bed1ef6fee6b7b4ee43d4127e4b42411e95f761b1fdab89780f1a
  // user can set options by `contract.transfer(ADDRESS, 10).sendTransaction({ from: account, gasPrice: <number>, ... })`
}

main();

Change log

see

Conflux Docs

  1. Official developer documentation
  2. RPC
  3. Subscribtion
  4. Conflux Portal

TODO

  • use native BigInt in node.js, use JSBI in browser.
  • TODO support CIP 23

js-conflux-sdk's People

Contributors

aaronisme avatar dependabot[bot] avatar geekberry avatar pana avatar resodo avatar yqrashawn 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.