JavaScript Integration Guide

Learn how to integrate Plasma with your JavaScript application.

Sections in this article

Install the Client

First, install the kyokan-plasma-client library via NPM:

$ npm install --save kyokan-plasma-client

This guide will be written in JavaScript, but the client itself is written in TypeScript and provides its own typings.

Instantiate the Client

Instantiating the Client is straightforward:

import Plasma from 'kyokan-plasma-client';

const web3 = new Web3('...');

const client = new Plasma({
	web3: web3, // a Web3 instance
	contractAddress: '0x123abc', // the address of the Plasma smart contract
	rootUrl: 'localhost:6545', // the URL to a Plasma node
	privateKey: '0x12345', // your private key
});

Note that whatever Web3 provider you use needs to have an unlocked account whose address belongs to the private key passed into the Plasma constructor if you want to be able to deposit, send, or exit.

Deposit And Send Some Funds

To deposit funds, use the deposit() method on the Plasma class like this:

// all values are in Wei
const valueToDeposit = '10000000000';
const depositReceipt = await client.deposit(valueToDeposit);

The depositReceipt returned by client.deposit includes a nonce value that can be used to spend your deposit in subsequent transactions:

const sender = '0x1234....';
const amount = '999';
const fee = '0';
await client.send(sender, amount, fee, depositReceipt.nonce);

To send funds that don’t come directly from a deposit, simply omit the nonce parameter. The client will pick the correct UTXOs and spend them for you.