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

cpchain-typescript-sdk

v0.1.13

Published

The SDK for developing on CPChain with TypeScript(JavaScript)

Downloads

11

Readme

CPChain TypeScript SDK

This is the SDK for developing on CPChain with TypeScript(JavaScript).

Getting Started


npm install cpchain-typescript-sdk

Example code:


import cpc from 'cpchain-typescript-sdk'

// Create a wallet
const wallet = cpc.wallets.createWallet()

console.log(wallet.address)

// Check current blocknumber on testnet
const provider = cpc.providers.createJsonRpcProvider('https://testnet.cpchain.io')
provider.getBlockNumber().then(num => {
  console.log(num)
})

// Get balance of your wallet
provider.getBalance(wallet.address).then(balance => {
  console.log('Balance:', cpc.utils.formatCPC(balance))
})

API

Wallets

Create Wallet

You can create a new wallet as below:


import cpc from 'cpchain-typescript-sdk'

// Create a wallet
const wallet = cpc.wallets.createWallet()

console.log(wallet.address)

Generate Keystore


const password = '123456'

wallet.encrypt(password).then(encryptedJson => {
    console.log(encryptedJson)
})

Sign and Send Transactions


const wallet = // ...
const provider = createJsonRpcProvider('https://civilian.testnet.cpchain.io')

const transaction = {
    type: 0,
    to: address,
    value: utils.parseCPC('1'),
    nonce: await provider.getTransactionCount(address), // get Nonce
    gas: 300000,
    gasPrice: await provider.getGasPrice(), // get current Gas price
    chainId: 41
}
// 使用 cpchain/chain 生成的交易哈希
const rawTx = await wallet.signTransaction(transaction)

// submit tx
const response = await provider.sendTransaction(rawTx)
const receipt = await response.wait()

console.log(receipt.status)

Import Wallet

You can import a wallet by private key or mnemonic or keystore:


// private key
const wallet1 = wallets.createWallet(privateKey)

// mnemonic
const mnemonic = 'uniform hole fabric shock potato such rough provide nasty second dirt waste'
const wallet2 = wallets.fromMnemonic(mnemonic)

// keystore
const wallet3 = wallets.fromEcryptedJson(encryptedJson)

Providers

JsonRPCProviders


// Create a provider
const provider = createJsonRpcProvider('https://civilian.cpchain.io')

// Access chain data
const number = await provider.getBlockNumber()
const balance = await provider.getBalance('0xfe8c03415df612dc0e8c866283a4ed40277fa48b')

console.log(utils.formatCPC(balance))

Contracts

We wrote a test contract, please check it at fixtures/contracts/example. Below is the code of the example contract:


pragma solidity ^0.4.24;

contract Example {
    event ModifyName(string name, uint256 blockNumber);
    string name;
    address owner;

    constructor() public {
        name = "world";
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function greet() public view returns (string) {
        return string(abi.encodePacked("Hello, ", name));
    }

    function modify(string newName) public payable {
        name = newName;
        emit ModifyName(name, block.number);
    }

    function collectBack() onlyOwner public {
        msg.sender.transfer(address(this).balance);
    }
}

Note: If you want to develop smart contract on CPChain, please use this tool to help you create project: cpchain-cli.

Deploy Contracts


// const wallet = ...
// const provider = ...

const account = wallet.connect(provider)

const bytecode = fs.readFileSync('fixtures/contracts/example/Example_sol_Example.bin').toString()
const abi = JSON.parse(fs.readFileSync('fixtures/contracts/example/Example_sol_Example.abi').toString())

const exampleContractFactory = new contract.ContractFactory(abi, bytecode, account)

const exampleContract = await exampleContractFactory.deploy()

console.log(exampleContract.address)

Call Methods


// call view methods
console.log(await exampleContract.greet())
// output: hello, world!

// call payable methods
const tx = await exampleContract.modify('cpchain')

await tx.wait()

console.log(await exampleContract.greet())
// output: hello, cpchain!

// call payable methods with sending CPC
const tx2 = await exampleContract.modify('cpchain', {
    value: utils.parseCPC('1')
})
await tx2.wait()
console.log(await getBalance(provider, exampleContract.address))

// collect CPC back to sender
await (await exampleContract.collectBack()).wait()
console.log(await getBalance(provider, exampleContract.address))

Listen and Query Events


// listen events
exampleContract.on('ModifyName', (name: string, blockNumber: any, event: any) => {
    console.log(name, blockNumber, event)
})

// get history events
const filterFrom = exampleContract.filters.ModifyName()
const currentBlock = await provider.getBlockNumber()
const historicalEvents = await exampleContract.queryFilter(filterFrom, currentBlock - 10, currentBlock)

console.log(historicalEvents)

Utils


// convert Wei to CPC
utils.formatCPC(1)

// convert CPC to Wei
utils.parseCPC('1')

Test

All tests wirte in test.


npm test

Build and Uses in Browser

You can build this SDK then uses in the browser.


npm run build

The bundle.min.js will be generated in dist floder. Import this file by script tag. Then there will be a global object CPChain which is accessible in your front-end code.