Difference between revisions of "Installing the run sdk"

From RunWiki
Jump to: navigation, search
Line 2: Line 2:
  
 
Another solution is to download the SDK as a zip file from [https://run.network/ https://run.network/]. When writing this the latest version is [https://github.com/runonbitcoin/sdk/archive/refs/tags/v0.6.37.zip version 0.6.37].
 
Another solution is to download the SDK as a zip file from [https://run.network/ https://run.network/]. When writing this the latest version is [https://github.com/runonbitcoin/sdk/archive/refs/tags/v0.6.37.zip version 0.6.37].
 +
 
= Getting Started =
 
= Getting Started =
 +
 
(note the following section was originally a simple copy/paster from [https://run.network/docs/#getting-started the official doc])
 
(note the following section was originally a simple copy/paster from [https://run.network/docs/#getting-started the official doc])
 +
 
== Installation ==
 
== Installation ==
 +
 
<p>If you're new to Run, let the&nbsp;</p>[https://run.network/lessons tutorial series]<p>&nbsp;guide your journey to get acquainted. You can write code without installing anything. The web browser Console will be your playground.</p><p>The details written here in the Docs have example code in the sidebar on the right-hand side. Anything you read in paragraphs, you can preview in-action over there. ⇥</p><p>Run works everywhere including all major browsers, on desktop and mobile, as well as Node.js 10+ on servers. The Run SDK is written in JavaScript ES6 and uses the&nbsp;</p>[https://github.com/moneybutton/bsv bsv library]<p>&nbsp;to build and sign transactions. To get started:</p>
 
<p>If you're new to Run, let the&nbsp;</p>[https://run.network/lessons tutorial series]<p>&nbsp;guide your journey to get acquainted. You can write code without installing anything. The web browser Console will be your playground.</p><p>The details written here in the Docs have example code in the sidebar on the right-hand side. Anything you read in paragraphs, you can preview in-action over there. ⇥</p><p>Run works everywhere including all major browsers, on desktop and mobile, as well as Node.js 10+ on servers. The Run SDK is written in JavaScript ES6 and uses the&nbsp;</p>[https://github.com/moneybutton/bsv bsv library]<p>&nbsp;to build and sign transactions. To get started:</p>
 
*For a webpage: Add&nbsp;<code>bsv.browser.min.js</code>&nbsp;and&nbsp;<code>run.browser.min.js</code>&nbsp;to the&nbsp;<code>&lt;head&gt;</code>&nbsp;tag.
 
*For a webpage: Add&nbsp;<code>bsv.browser.min.js</code>&nbsp;and&nbsp;<code>run.browser.min.js</code>&nbsp;to the&nbsp;<code>&lt;head&gt;</code>&nbsp;tag.
 
*For Node.js: Run&nbsp;<code>npm install run-sdk</code>&nbsp;to install both the run and bsv libraries
 
*For Node.js: Run&nbsp;<code>npm install run-sdk</code>&nbsp;to install both the run and bsv libraries
 
<p>And that's it. All your code and jigs will be saved on-chain and Run will use public APIs to interact with the Bitcoin network. You don't need to deploy any servers to use Run. All the logic works client-side.</p>
 
<p>And that's it. All your code and jigs will be saved on-chain and Run will use public APIs to interact with the Bitcoin network. You don't need to deploy any servers to use Run. All the logic works client-side.</p>
 +
 
== Setup ==
 
== Setup ==
A <code>Run</code> instance manages your communication with the Bitcoin network. The default network is <code>main</code> (Mainnet), but for development and testing, we recommend <code>mock</code>. Mock is an in-memory simulation blockchain that does not require funds to use. We like to call it the ''mockchain''. For more configuration options, see [https://run.network/docs/#api-reference-run API Reference: Run]
 
  
.
+
A <code>Run</code> instance manages your communication with the Bitcoin network. The default network is <code>main</code> (Mainnet), but for development and testing, we recommend <code>mock</code>. Mock is an in-memory simulation blockchain that does not require funds to use. We like to call it the ''mockchain''. For more configuration options, see [https://run.network/docs/#api-reference-run API Reference: Run].
 +
 
 
== Creating Jigs ==
 
== Creating Jigs ==
 +
 
<p>Let's begin with a basic jig that stores a value in a variable. Create a jig called&nbsp;<code>SimpleStore</code>. By extending from&nbsp;<code>Jig</code>, the instances of your class will automatically sync to the blockchain. Every jig has an&nbsp;<code>owner</code>. Any code may read jigs but only its&nbsp;<code>owner</code>&nbsp;can update it. The&nbsp;<code>owner</code>&nbsp;is typically a Bitcoin address, and the private key is required to update it.</p><p>In addition to&nbsp;<code>owner</code>, every jig has a&nbsp;<code>location</code>. A jig's&nbsp;<code>location</code>&nbsp;is the pairing of a Bitcoin transaction ID and an output index, and it represents a particular state in time of an object or class on Bitcoin. When you check the&nbsp;<code>location</code>&nbsp;property of a jig, you get its current location. If you wish to get the location where the jig was first deployed, that is called its&nbsp;<code>origin</code>. The&nbsp;<code>origin</code>&nbsp;is unique and will not change, however&nbsp;<code>location</code>&nbsp;changes with every update. After a method call, your jig will have a new&nbsp;<code>location</code>&nbsp;pointing to a Bitcoin transaction containing the last method call.</p>
 
<p>Let's begin with a basic jig that stores a value in a variable. Create a jig called&nbsp;<code>SimpleStore</code>. By extending from&nbsp;<code>Jig</code>, the instances of your class will automatically sync to the blockchain. Every jig has an&nbsp;<code>owner</code>. Any code may read jigs but only its&nbsp;<code>owner</code>&nbsp;can update it. The&nbsp;<code>owner</code>&nbsp;is typically a Bitcoin address, and the private key is required to update it.</p><p>In addition to&nbsp;<code>owner</code>, every jig has a&nbsp;<code>location</code>. A jig's&nbsp;<code>location</code>&nbsp;is the pairing of a Bitcoin transaction ID and an output index, and it represents a particular state in time of an object or class on Bitcoin. When you check the&nbsp;<code>location</code>&nbsp;property of a jig, you get its current location. If you wish to get the location where the jig was first deployed, that is called its&nbsp;<code>origin</code>. The&nbsp;<code>origin</code>&nbsp;is unique and will not change, however&nbsp;<code>location</code>&nbsp;changes with every update. After a method call, your jig will have a new&nbsp;<code>location</code>&nbsp;pointing to a Bitcoin transaction containing the last method call.</p>
 +
 
== Loading Jigs ==
 
== Loading Jigs ==
 +
 
<p>The simplest way to load your jigs is to call&nbsp;<code>run.inventory.sync()</code>&nbsp;and then access&nbsp;<code>run.inventory.jigs</code>.&nbsp;<code>run.inventory.sync()</code>&nbsp;will find and load all objects owned by&nbsp;<code>run.owner</code>&nbsp;and place them in the&nbsp;<code>jigs</code>&nbsp;array. Once loaded, you may call methods and use them normally. Alternatively, you may wish to load a particular jig or load a jig in a historical state. To do either, pass the&nbsp;<code>location</code>&nbsp;of the jig you wish to load into&nbsp;<code>run.load()</code>.</p><p>If you've loaded a historical location so that your jig is in a previous state, you'll need to first catch up to the latest state before you'll be allowed to make a method call.&nbsp;<code>sync()</code>&nbsp;will handily fast-forward a jig to its latest state on the blockchain&nbsp;''without''&nbsp;triggering a Bitcoin transaction. In the example on the sidebar, if&nbsp;<code>specificJig</code>&nbsp;was in a historical state, you would call&nbsp;<code>specificJig.sync()</code>&nbsp;and then call&nbsp;<code>specificJig.set('abc')</code>. When you accidentally try to update a jig without the jig being in its latest state, Run will safely abort before publishing a Bitcoin transaction and inform you with an error. In that case, you'll just need to add the preceding&nbsp;<code>sync()</code>&nbsp;call and execute your code again. The best practice is to write code in such a way that jigs are always up-to-date in their latest state. Run manages the heavy lifting for you.</p><p><code>sync()</code>&nbsp;also acts as a debugging tool since it surfaces any errors your jigs have after you've made changes to them. If you notice your app acting funny, a well-placed preceding&nbsp;<code>specificJig.sync()</code>&nbsp;can help you uncover the error. You may also call&nbsp;<code>sync()</code>&nbsp;on your&nbsp;<code>Run</code>&nbsp;instance, like this:&nbsp;<code>run.sync()</code>. That'll help you search out any errors from your entire app in&nbsp;''all''&nbsp;of the jigs you own.</p>
 
<p>The simplest way to load your jigs is to call&nbsp;<code>run.inventory.sync()</code>&nbsp;and then access&nbsp;<code>run.inventory.jigs</code>.&nbsp;<code>run.inventory.sync()</code>&nbsp;will find and load all objects owned by&nbsp;<code>run.owner</code>&nbsp;and place them in the&nbsp;<code>jigs</code>&nbsp;array. Once loaded, you may call methods and use them normally. Alternatively, you may wish to load a particular jig or load a jig in a historical state. To do either, pass the&nbsp;<code>location</code>&nbsp;of the jig you wish to load into&nbsp;<code>run.load()</code>.</p><p>If you've loaded a historical location so that your jig is in a previous state, you'll need to first catch up to the latest state before you'll be allowed to make a method call.&nbsp;<code>sync()</code>&nbsp;will handily fast-forward a jig to its latest state on the blockchain&nbsp;''without''&nbsp;triggering a Bitcoin transaction. In the example on the sidebar, if&nbsp;<code>specificJig</code>&nbsp;was in a historical state, you would call&nbsp;<code>specificJig.sync()</code>&nbsp;and then call&nbsp;<code>specificJig.set('abc')</code>. When you accidentally try to update a jig without the jig being in its latest state, Run will safely abort before publishing a Bitcoin transaction and inform you with an error. In that case, you'll just need to add the preceding&nbsp;<code>sync()</code>&nbsp;call and execute your code again. The best practice is to write code in such a way that jigs are always up-to-date in their latest state. Run manages the heavy lifting for you.</p><p><code>sync()</code>&nbsp;also acts as a debugging tool since it surfaces any errors your jigs have after you've made changes to them. If you notice your app acting funny, a well-placed preceding&nbsp;<code>specificJig.sync()</code>&nbsp;can help you uncover the error. You may also call&nbsp;<code>sync()</code>&nbsp;on your&nbsp;<code>Run</code>&nbsp;instance, like this:&nbsp;<code>run.sync()</code>. That'll help you search out any errors from your entire app in&nbsp;''all''&nbsp;of the jigs you own.</p>
= Jigs =
 

Revision as of 00:24, 14 December 2022

The easiest way to get the latest version is to run npm install run-sdk

Another solution is to download the SDK as a zip file from https://run.network/. When writing this the latest version is version 0.6.37.

Getting Started

(note the following section was originally a simple copy/paster from the official doc)

Installation

If you're new to Run, let the 

tutorial series

 guide your journey to get acquainted. You can write code without installing anything. The web browser Console will be your playground.

The details written here in the Docs have example code in the sidebar on the right-hand side. Anything you read in paragraphs, you can preview in-action over there. ⇥

Run works everywhere including all major browsers, on desktop and mobile, as well as Node.js 10+ on servers. The Run SDK is written in JavaScript ES6 and uses the 

bsv library

 to build and sign transactions. To get started:

  • For a webpage: Add bsv.browser.min.js and run.browser.min.js to the <head> tag.
  • For Node.js: Run npm install run-sdk to install both the run and bsv libraries

And that's it. All your code and jigs will be saved on-chain and Run will use public APIs to interact with the Bitcoin network. You don't need to deploy any servers to use Run. All the logic works client-side.

Setup

A Run instance manages your communication with the Bitcoin network. The default network is main (Mainnet), but for development and testing, we recommend mock. Mock is an in-memory simulation blockchain that does not require funds to use. We like to call it the mockchain. For more configuration options, see API Reference: Run.

Creating Jigs

Let's begin with a basic jig that stores a value in a variable. Create a jig called SimpleStore. By extending from Jig, the instances of your class will automatically sync to the blockchain. Every jig has an owner. Any code may read jigs but only its owner can update it. The owner is typically a Bitcoin address, and the private key is required to update it.

In addition to owner, every jig has a location. A jig's location is the pairing of a Bitcoin transaction ID and an output index, and it represents a particular state in time of an object or class on Bitcoin. When you check the location property of a jig, you get its current location. If you wish to get the location where the jig was first deployed, that is called its origin. The origin is unique and will not change, however location changes with every update. After a method call, your jig will have a new location pointing to a Bitcoin transaction containing the last method call.

Loading Jigs

The simplest way to load your jigs is to call run.inventory.sync() and then access run.inventory.jigsrun.inventory.sync() will find and load all objects owned by run.owner and place them in the jigs array. Once loaded, you may call methods and use them normally. Alternatively, you may wish to load a particular jig or load a jig in a historical state. To do either, pass the location of the jig you wish to load into run.load().

If you've loaded a historical location so that your jig is in a previous state, you'll need to first catch up to the latest state before you'll be allowed to make a method call. sync() will handily fast-forward a jig to its latest state on the blockchain without triggering a Bitcoin transaction. In the example on the sidebar, if specificJig was in a historical state, you would call specificJig.sync() and then call specificJig.set('abc'). When you accidentally try to update a jig without the jig being in its latest state, Run will safely abort before publishing a Bitcoin transaction and inform you with an error. In that case, you'll just need to add the preceding sync() call and execute your code again. The best practice is to write code in such a way that jigs are always up-to-date in their latest state. Run manages the heavy lifting for you.

sync() also acts as a debugging tool since it surfaces any errors your jigs have after you've made changes to them. If you notice your app acting funny, a well-placed preceding specificJig.sync() can help you uncover the error. You may also call sync() on your Run instance, like this: run.sync(). That'll help you search out any errors from your entire app in all of the jigs you own.