Difference between revisions of "Creating and loading jigs"

From RunWiki
Jump to: navigation, search
Line 1: Line 1:
After you have [[Installing the_run_sdk#Setup|installed and setup Run]], you are ready to create and load jigs.
+
After you have simply opened the devtools console at Runcraft.io, or [[Installing the_run_sdk#Setup|installed and setup Run]], you are ready to create and load jigs !
  
 
Jigs are interactive objets on Bitcoin. You define a jig with a JavaScript class explaining what the jig can do. Each jig is unique and only the owner can update it, which is secured by bitcoin miners.
 
Jigs are interactive objets on Bitcoin. You define a jig with a JavaScript class explaining what the jig can do. Each jig is unique and only the owner can update it, which is secured by bitcoin miners.
Line 64: Line 64:
 
<code>jig.constructor.origin</code> = 1st transaction of the '''code''' of this jig<br>
 
<code>jig.constructor.origin</code> = 1st transaction of the '''code''' of this jig<br>
 
<br>
 
<br>
 
 
<p>Now that you understand the basics, let's [[Jigs|create some more interesting jigs]]</p>
 
<p>Now that you understand the basics, let's [[Jigs|create some more interesting jigs]]</p>
 
[[Category:Documentation]]
 
[[Category:Documentation]]

Revision as of 18:20, 26 September 2023

After you have simply opened the devtools console at Runcraft.io, or installed and setup Run, you are ready to create and load jigs !

Jigs are interactive objets on Bitcoin. You define a jig with a JavaScript class explaining what the jig can do. Each jig is unique and only the owner can update it, which is secured by bitcoin miners.

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.

Code

class SimpleStore extends Jig {
  set(value) {
    this.value = value
  }
}

const jig = new SimpleStore()

jig.set('Satoshi Nakamoto')

await jig.sync()

console.log(jig.owner)
console.log(jig.location)
console.log(jig.origin)

Loading Jigs

The simplest way to load your jigs is to call run.inventory.sync() and then access run.inventory.jigs. run.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.

Code

Loading all jigs

await run.inventory.sync()

const simpleStore = run.inventory.jigs.find(x => x instanceof SimpleStore)

simpleStore.set(123)

Loading a specific jig

const specificJig = await run.load(simpleStore.location)

specificJig.set('abc')

// Fast-forward the jig to its latest state
await specificJig.sync()

Remember this:

jig.origin = 1st transaction of this jig
jig.constructor.origin = 1st transaction of the code of this jig

Now that you understand the basics, let's create some more interesting jigs