0.2.3 • Published 2 years ago

@timestope-official/core v0.2.3

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

@timestope-official/core

This package provides a collection of apis to interact with Feechain blockchain.

Installation

npm install @timestope-official/core

Usage

Create a Feechain instance connecting to testnet

const { Feechain } = require('@timestope-official/core');
const {
  ChainID,
  ChainType,
  hexToNumber,
  numberToHex,
  fromWei,
  Units,
  Unit,
} = require('@timestope-official/utils');

const fch = new Feechain(
    'https://api.s0.asadal.timestope.net/',
    {
        chainType: ChainType.Feechain,
        chainId: ChainID.FchTestnet,
    },
);

Getting balance of account fee103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7

fch.blockchain
  .getBalance({ address: 'fee103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7' })
  .then((response) => {
    console.log('balance in FEEs: ' + fromWei(hexToNumber(response.result), Units.fee));
  });

Getting the latest block number

fch.blockchain.getBlockNumber().then((response) => {
  console.log('current block number: ' + hexToNumber(response.result));
});

Getting the block using block hash

fch.blockchain
  .getBlockByHash({
    blockHash: '0x08c46ae7249362a7d1f602d44c5a81f33ebdab6a7dcb6068f99610b57911aafd',
  })
  .then((response) => {
    console.log(response.result);
  });

Getting the block using block number

fch.blockchain
  .getBlockByNumber({
    blockNumber: numberToHex(422635),
  })
  .then((response) => {
    console.log(response.result);
  });

Getting the transaction using hash

fch.blockchain
  .getTransactionByHash({
    txnHash: '0x56c73eb993b18dc04baacec5c2e9d1292a090f6a978a4a1c461db5255fcbc831',
  })
  .then((response) => {
    console.log(response.result);
  });

Getting the transaction receipt

fch.blockchain
  .getTransactionReceipt({
    txnHash: '0x56c73eb993b18dc04baacec5c2e9d1292a090f6a978a4a1c461db5255fcbc831',
  })
  .then((response) => {
    console.log(response.result);
  });

Getting the cross-shard transaction receipt

fch.blockchain
  .getCxReceiptByHash({
    txnHash: '0xcd36a90ff5d5373285c2896ba7bbcd3f5324263c0cb8ecfb7cad2f5fc2fbdbda',
    shardID: 1,
  })
  .then((value) => {
    console.log(value.result);
  });

Getting the deployed smart contract code

fch.blockchain
  .getCode({
    address: '0x08AE1abFE01aEA60a47663bCe0794eCCD5763c19',
    blockNumber: 'latest',
  })
  .then((response) => {
    console.log(response.result);
  });

Getting the transaction count of an account

fch.blockchain
  .getTransactionCount({
    address: 'fee1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy',
  })
  .then((response) => {
    console.log(hexToNumber(response.result));
  });

Getting the shard structure and details

fch.blockchain.getShardingStructure().then((response) => {
  console.log(response.result);
});

Transferring funds using sendTransaction

// key corresponds to fee103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7, only has testnet balance
fch.wallet.addByPrivateKey('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

async function transfer() {
  const txn = fch.transactions.newTx({
    to: 'fee166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
    value: new Unit(1).asFee().toWei(),
    // gas limit, you can use string
    gasLimit: '21000',
    // send token from shardID
    shardID: 0,
    // send token to toShardID
    toShardID: 0,
    // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
    gasPrice: new fch.utils.Unit('1').asGwei().toWei(),
  });

  // sign the transaction use wallet;
  const signedTxn = await fch.wallet.signTransaction(txn);
  const txnHash = await fch.blockchain.sendTransaction(signedTxn);
  console.log(txnHash.result);
}

transfer();