Latest Radiant Node Release
1.3.0 Energy
Radiant Blockchain Wiki
Official Website
  • Radiant Blockchain
  • FAQs
  • Induction Proof System FAQs
  • Links
  • White Papers
    • Radiant: A Peer-to-Peer Digital Asset System
      • Radiant: un Système d'Actifs Numériques Pair-à-Pair
      • Radiant: 一种点对点数字资产系统
      • Radiant: un sistema de activos digitales electrónico peer-to-peer
      • रेडियंट: एक सहकर्मी से सहकर्मी डिजिटल संपत्ति प्रणाली
      • Radiant: Sistem Kasugihan Aset Digital Peer-to-Peer
      • Radiant: Sistem Aset Digital Peer-to-Peer
    • Radiant Blockchain System Design
  • Programming
    • Radiant Opcodes
    • Pay to Ref
    • Non-Fungible Tokens on Radiant
    • Token-Controlled Contracts on Radiant (Pay to Token)
    • Glyphs Protocol
    • RadiantScript
      • About RXD
      • Command Line Interface
      • Getting Started
      • Guides
        • Writing Covenants & Introspection
        • Syntax Highlighting
      • Language
        • Artifacts
        • Contract Structure
        • Examples
        • Global Functions & Operators
        • Global Variables
        • Language Grammar
        • Types
      • SDK
        • Examples
        • Contract Instantiation
        • Sending Transactions
      • Releases
        • Migration Notes
        • Release Notes
    • scryptlib
      • preimage
        • preimage - Chinese
      • rawstate
      • HashedMap
        • HashedMap - Chinese
      • Chained APIs
        • Chained APIs - Chinese
    • Scrypt Boilerplate
  • Guides
    • Docker
    • Electron Wallet
      • Restore
      • Create multisign
    • Radiant Node
    • Flux
Powered by GitBook
On this page
  • Installing the CashScript compiler
  • Installing the JavaScript SDK
  • Writing your first smart contract
  • Integrating into JavaScript

Was this helpful?

Edit on GitHub
Export as PDF
  1. Programming
  2. RadiantScript

Getting Started

PreviousCommand Line InterfaceNextGuides

Last updated 6 days ago

Was this helpful?

Installing the CashScript compiler

The command line CashScript compiler cashc can be installed from NPM.

npm install -g cashc

Installing the JavaScript SDK

The JavaScript SDK can be installed into your project with NPM.

npm install cashscript

Caution: CashScript only offers a JavaScript SDK, but CashScript contracts can be integrated into other languages as well. Because there are no ready-to-use SDKs available for them, this is considered advanced usage, and it is recommended to use the JavaScript SDK.

Writing your first smart contract

There are some examples available on the , that can be used to take inspiration from. Further examples of the JavaScript integration can be found on . A simple example is included below.

pragma cashscript ^0.7.0;

contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
    // Allow the recipient to claim their received money
    function transfer(sig recipientSig) {
        require(checkSig(recipientSig, recipient));
    }

    // Allow the sender to reclaim their sent money after the timeout is reached
    function timeout(sig senderSig) {
        require(checkSig(senderSig, sender));
        require(tx.time >= timeout);
    }
}

Integrating into JavaScript

While more detailed examples are available on GitHub, we show an integration of the TransferWithTimeout contract in a JavaScript project.

After compiling the contract file to an artifact JSON with cashc, it can be imported into the CashScript SDK.

cashc ./transfer_with_timeout.cash --output ./transfer_with_timeout.json
const { ElectrumNetworkProvider, Contract, SignatureTemplate } = require('cashscript');
const { alice, bob, alicePk, bobPk } = require('./keys');

async function run() {
  // Import the TransferWithTimeout JSON artifact
  const artifact = require('./transfer_with_timeout.json');

  // Initialise a network provider for network operations
  const provider = new ElectrumNetworkProvider('mainnet');

  // Instantiate a new TransferWithTimeout contract
  const contract = new Contract(artifact, [alicePk, bobPk, 600000], provider);

  // Call the transfer function with Bob's signature
  // i.e. Bob claims the money that Alice has sent him
  const transferDetails = await contract.functions
    .transfer(new SignatureTemplate(bob))
    .to('bitcoincash:qrhea03074073ff3zv9whh0nggxc7k03ssh8jv9mkx', 10000)
    .send();
  console.log(transferDetails);

  // Call the timeout function with Alice's signature
  // i.e. Alice recovers the money that Bob has not claimed
  const timeoutDetails = await contract.functions
    .timeout(new SignatureTemplate(alice))
    .to('bitcoincash:qqeht8vnwag20yv8dvtcrd4ujx09fwxwsqqqw93w88', 10000)
    .send();
  console.log(timeoutDetails);
}

Tip: Read more about the CashScript language syntax in the .

Tip: Read more about the JavaScript SDK in the .

Examples page
GitHub
Language Description
SDK documentation