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

@pratikpc/fabric-contract-client

v0.0.3

Published

Write easy to read and maintain Chaincode Clients using TypeScript and Node.JS

Downloads

2

Readme

Fabric Contract Client

Contract's should be written in a way that is clean, simple and easy to maintain.
For this, I was a big fan of Convector
But given Convector is not likely to be maintained or support Fabric 2.0, I needed to find and maintain a replacement which allows me to write cleaner code.

I noticed that the fabric-contract-api exists to write Chaincode but it has no client support.

This leverages your fabric-contract-api Chaincodes to extend and support them for usage at the Client end thus helping you write cleaner and easy to maintain code.

Usage

Include the library first

import ContractClient from '@pratikpc/fabric-contract-client';

You need to connect to the network first. This code remains same and is fairly simple.
ChainCodeContract is the type of the contract that you have deployed.

  1. My ChainCodeContract's actual name is CarContract
  2. So you don't even need to know the actual name.
  3. The extraction is handled by the code.
  4. As long as you annotate via Info parameter which you should be doing anyways
  5. If not, you can pass the name of the contract as the 3rd parameter
// Use Library
const client = ContractClient(ChainCodeContract, network);
// Modern way to communicate with the library
const time = await client.returnCurrentTime();
console.log(
    'Chaincode test result: Time is ',
    time
);

// See how simple passing args can be
console.log(
    `Chaincode says ${await client.sayHi('Pratik')}`
);

console.log('Transaction has been submitted');

Intellisense

When using Typescript or JavaScript, does this support Intellisense?

Yes of course it does.

Intellisense working

Typescript Type Checks

Yes of course.

Older way

For those interested how it looked before this API was around

const contract =
    network.getContract('chaincode');
console.log(
    'Using traditional approach to query Hyperledger Fabric',
    Number(
        (
            await contract.evaluateTransaction(
                'returnCurrentTime'
            )
        ).toString()
    )
);
console.log(
    'Using traditional approach to say hi to Hyperledger Fabric',
    (
        await contract.evaluateTransaction(
            'sayHi',
            'Pratik'
        )
    ).toString()
);

This code:-

  1. Readability is less
  2. Expects every argument to be a String unlike ours
  3. Needs to convert Output from String unlike ours
    Ours uses the value of your return argument
  4. Needs you to know whether a transaction is submit or evaluate
  5. Requires you to remember the name of the Chaincode. Mine extracts it from the Info annotation.

Support for executing new and old way in the same code

Yes this support exists!

What's the catch?

The catch is that we expect you to write the code with proper and corect annotations

@Info({
    title: 'CarContract',
    DeployedChainCodeName: 'DeployedCarContract',
    description: 'A sample Car Contract'
})
export default class CarContract extends Contract {
    @Transaction(false)
    @Returns('boolean')
    // eslint-disable-next-line class-methods-use-this
    public async carExists(
        ctx: Context,
        carId: string
    ): Promise<boolean> {
        const data: Uint8Array = await ctx.stub.getState(
            carId
        );
        return !!data && data.length > 0;
    }
}

Contact me

You can create an issue here or contact me via LinkedIn