Giter Club home page Giter Club logo

cashscript's Introduction

CashScript

Build Status Coverage Status NPM Version NPM Monthly Downloads NPM License

CashScript is a high level language enabling basic smart contract functionality on Bitcoin Cash. While these cash contracts are less powerful than Ethereum's smart contracts, CashScript was in many ways inspired by Ethereum's development ecosystem. Ethereum has always had one of the most accessible development ecosystems in terms of tooling, and with CashScript we want to bring that accessibility to Bitcoin Cash. Its full documentation can be found here


Attention: CashScript is in active development, and is currently in a beta phase. While CashScript is in beta stage, its APIs and usage is subject to change, so be sure to check the documentation. During the beta phase it is possible that the library still contains bugs, so it is recommended to only use the CashScript SDK on the testnet network.

This repository contains the code for the CashScript compiler & command line tool under packages/cashc. This repository also contains the code for the CashScript JavaScript SDK under packages/cashscript. This README includes the basic documentation for both of these packages, but their respective package directories go into a bit more detail.

The CashScript Language

CashScript is a high-level language that allows you to write Cash Contracts in a straightforward and familiar way. It is inspired by Ethereum's Solidity, but it is not the same, and cash contracts work very differently from Ethereum's smart contracts. See the Language documentation for a full reference of the language.

The CashScript Compiler

CashScript features a compiler as a standalone command line tool, called cashc. It can be installed through npm and used to compile .cash files into .json artifact files. These artifact files can be imported into the CashScript JavaScript SDK (or other SDKs in the future). Note that the CashScript SDK also has a function to import and compile .cash files directly, so it is not required to use the cashc command line tool.

For more information on cashc, refer to its README.

Installation

npm install -g cashc

Usage

Usage: cashc [options] [source_file]

Options:
  --output, -o  Specify a file to output the generated artifact.
                                                             [string] [required]
  --help        Show help                                              [boolean]
  --version     Show version number                                    [boolean]

The CashScript SDK

The main way to interact with cash contracts and integrate them into applications is using the CashScript SDK. This SDK allows you to compile .cash files or import .json artifact files, and convert them to Contract objects. These objects are used to create new contract instances. These instances are used to interact with the contracts using the functions that were implemented in the .cash file. For more information on the CashScript SDK, refer to its README or the full SDK documentation.

Note: The CashScript currently only supports NodeJS, as it uses some NodeJS-specific functionality (fs, path). We are working on making the library compatible with the browser as well as NodeJS, but this is currently not supported.

Installation

npm install cashscript

Usage

import { Contract, ... } from 'cashscript';
const { Contract, ... } = require('cashscript');

Using the CashScript SDK, you can import / compile existing cash contract files, create new instances of these contracts, and interact with these instances:

...
  // Compile the P2PKH Cash Contract
  const P2PKH: Contract = Contract.compile(path.join(__dirname, 'p2pkh.cash'), 'testnet');

  // Instantiate a new P2PKH contract with constructor arguments: { pkh: pkh }
  const instance: Instance = P2PKH.new(pkh);

  // Get contract balance & output address + balance
  const contractBalance: number = await instance.getBalance();
  console.log('contract address:', instance.address);
  console.log('contract balance:', contractBalance);

  // Call the spend function with the owner's signature
  // And use it to send 0. 000 100 00 BCH back to the contract's address
  const tx: TxnDetailsResult = await instance.functions.spend(pk, new Sig(keypair))
    .send(instance.address, 10000);
  console.log('transaction details:', tx);
...

Examples

If you want to see CashScript in action and check out its usage, there are several example contracts in the examples/ directory. The .cash files contain example contracts, and the .ts files contain example usage of the CashScript SDK to interact with these contracts.

The "Hello World" of cash contracts is defining the P2PKH pattern inside a cash contract, which can be found under examples/p2pkh.cash. Its usage can be found under examples/p2pkh.ts or examples/p2pkh-artifact.ts.

Running the examples

To run the examples, clone this repository and navigate to the examples/ directory. Since the examples depend on the SDK, be sure to run npm install or yarn inside the examples/ directory, which installs all required packages.

git clone [email protected]:Bitcoin-com/cashscript.git
cd cashscript/examples
yarn / npm install

All .ts files in the examples/ directory can then be executed with ts-node.

npm install -g ts-node
ts-node p2pkh.ts

All .js files can be executed with node.

node p2pkh.js

cashscript's People

Contributors

34r7h avatar cgcardona avatar dktapps avatar rkalis avatar

Watchers

 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.