Difference between revisions of "Jigs (advanced)"

From RunWiki
Jump to: navigation, search
(Created page with "Now that you understand the basic of Jigs, we can explore more complex ways to use them.")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Now that you understand the [[Jigs|basic of Jigs]], we can explore more complex ways to use them.
 
Now that you understand the [[Jigs|basic of Jigs]], we can explore more complex ways to use them.
 +
 +
== Interactivity ==
 +
 +
Jigs are designed to interact together. Jigs may create other jigs, call methods on other jigs, read properties on other jigs, and even store other jigs inside them. These are just a few of the types of interactions that Run supports.
 +
 +
Calling <code>new</code> to construct a jig within a method of another jig will create a new output in the transaction. Sometimes you will create new jigs that are of the same class, but other times you will want to create jigs that are of a different class. Because Jig code runs in a sandbox without access to other code, you set the <code>deps</code> property on your jig class to tell Run about any other classes you use. These dependencies will be made available as globals to your jig.
 +
 +
Jigs may store other jigs as properties too. Think of these as standard JavaScript object references that you may read or write. However, if stored jigs are updated by calling methods, then their owners must also sign the Bitcoin transaction that updates their state. For more information about when owners need to approve, see the [https://run.network/docs/#how-it-works-ownership-rules Ownership Rules].
 +
 +
=== Code ===
 +
 +
<syntaxhighlight lang="javascript">
 +
class Event extends Jig {
 +
  createTicket() { return new Ticket(this) }
 +
}
 +
 +
class Ticket extends Jig {
 +
  init(event) { this.event = event }
 +
}
 +
 +
Event.deps = { Ticket }
 +
</syntaxhighlight>
 +
 +
 +
 +
== Destroying ==
 +
 +
Not all jigs will live forever. You may need to clean up old jigs in your inventory, and or you may need to have one jig consume another jig to make an update. Calling <code>destroy()</code> on a jig creates a transaction that removes the jig's output and makes it un-updatable. To do this, a jig may be destroyed. When a jig is destroyed, it no longer has an unspent output on the blockchain, and it will not appear in your inventory.
 +
 +
Destroyed jigs still have unique locations that identify their final state. You can even still load their final state using <code>run.load()</code>. However, destroyed jigs are marked as being destroyed with a <code>null</code> owner and they are forever locked in their final state.
 +
 +
By default, every jig has a destroy method. If you wish to prevent a jig from being destroyed, override the destroy method and throw an error in it. You can call destroy from outside the jig or from another method. You can also override <code>destroy()</code> to put the jig into its final state.
 +
 +
 +
=== Code ===
 +
 +
Destroy a jig
 +
 +
<syntaxhighlight lang="javascript">
 +
giftCard.destroy()
 +
</syntaxhighlight>
 +
 +
Override destroy to finalize a jig
 +
 +
<syntaxhighlight lang="javascript">
 +
class GiftCard extends Jig {
 +
  init(value) {
 +
    this.value = value
 +
  }
 +
 +
  destroy() {
 +
    super.destroy()
 +
    this.value = 0
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 +
 +
 +
== Backing ==
 +
 +
Jigs may be backed by bitcoins. When a jig is backed, it means that the output associated with that jig has a non-dust value in it. Backed jigs let users send money between each other and provide a baseline value for items. To back a jig, set the <code>satoshis</code> property to a number in a method. Your purse will automatically deposit that amount into the jig. When the <code>satoshis</code> property is later decreased, those Bitcoins will be withdrawn to the active purse.
 +
 +
It is important to remember that backed jigs may be melted at any time by spending the jig output outside of Run. This will destroy the jig, and the owner will keep the satoshis.
 +
 +
<pre>
 +
🛈 Note: All bitcoin outputs must have an amount at least the dust limit to be accepted by the network. Run will choose the greater of your jig's satoshis property and this dust limit when building your jig's transaction output.
 +
</pre>
 +
 +
=== Code ===
 +
 +
<syntaxhighlight lang="javascript">
 +
class Tip extends Jig {
 +
  init(message, pubkey, amount) {
 +
    this.message = message
 +
    this.owner = pubkey
 +
    this.satoshis = amount
 +
  }
 +
 +
  withdraw() {
 +
    this.satoshis = 0
 +
  }
 +
}
 +
 +
new Tip('I like your videos', pubkey, 100000)
 +
</syntaxhighlight>
 +
 +
 +
== Attaching Media ==
 +
 +
You may optionally set an icon onto a jig using an emoji, a custom image, or both. These icons will show in explorers, exchanges, and wallets, whenever your jig is displayed. To attach an icon, you set the metadata property on your jig class and then this icon will apply to all instances of that class. You can also set rich media like audio and 3D models too.
 +
 +
'''Setting an emoji:''' To add an emoji, set the <code>emoji</code> field on <code>metadata</code> to a single emoji character. You can find an appropriate emoji on [https://emojipedia.org/ Emojipedia].
 +
 +
'''Setting an image:''' To set an image, first upload your image on-chain in the B:// protocol format. [https://run.network/docs/#tools-easy-b Easy-B] is an excellent tool for this. The image data you uploaded can then be referenced using a string that starts with b:// and includes the transaction ID. Currently Run supports SVG, PNG, and GIF formats. SVG is recommended in most cases because it scales up better on large displays and loads quicker due to its smaller file size.
 +
 +
'''Setting audio:''' To add an audio file, set a <code>b://</code> protocol uri into the <code>audio</code> field in the <code>metadata</code> object similar to images.
 +
 +
'''Setting a 3d model''': To add a 3d model, set a <code>b://</code> protocol URI to the <code>glbModel</code> field in the <code>metadata</code> object similar to images.
 +
 +
For more information, see the section on [https://run.network/docs/#advanced-usage-standard-metadata Standard Metadata.]
 +
 +
<pre>
 +
🛈 Note: Individual jig instances may also have their own icons. To do this, you should set the metadata property on the instance rather than the class either in its constructor or via another function.
 +
</pre>
 +
 +
=== Code ===
 +
 +
<syntaxhighlight lang="javascript">
 +
class DinoPet extends Jig { }
 +
 +
DinoPet.metadata = {
 +
  emoji: '🦖',
 +
  image: 'b://55e2c09672355e009e4727c2365fb61d12c69add91215ee3e9f50aa76c808536'
 +
}
 +
 +
new DinoPet()
 +
</syntaxhighlight>
 +
 +
Now that you are an expert on jigs, let's go meta by exploring [[Code|Code]]
 +
 +
[[Category:run-doc]]

Latest revision as of 20:18, 27 March 2023

Now that you understand the basic of Jigs, we can explore more complex ways to use them.

Interactivity

Jigs are designed to interact together. Jigs may create other jigs, call methods on other jigs, read properties on other jigs, and even store other jigs inside them. These are just a few of the types of interactions that Run supports.

Calling new to construct a jig within a method of another jig will create a new output in the transaction. Sometimes you will create new jigs that are of the same class, but other times you will want to create jigs that are of a different class. Because Jig code runs in a sandbox without access to other code, you set the deps property on your jig class to tell Run about any other classes you use. These dependencies will be made available as globals to your jig.

Jigs may store other jigs as properties too. Think of these as standard JavaScript object references that you may read or write. However, if stored jigs are updated by calling methods, then their owners must also sign the Bitcoin transaction that updates their state. For more information about when owners need to approve, see the Ownership Rules.

Code

class Event extends Jig {
  createTicket() { return new Ticket(this) }
}

class Ticket extends Jig {
  init(event) { this.event = event }
}

Event.deps = { Ticket }


Destroying

Not all jigs will live forever. You may need to clean up old jigs in your inventory, and or you may need to have one jig consume another jig to make an update. Calling destroy() on a jig creates a transaction that removes the jig's output and makes it un-updatable. To do this, a jig may be destroyed. When a jig is destroyed, it no longer has an unspent output on the blockchain, and it will not appear in your inventory.

Destroyed jigs still have unique locations that identify their final state. You can even still load their final state using run.load(). However, destroyed jigs are marked as being destroyed with a null owner and they are forever locked in their final state.

By default, every jig has a destroy method. If you wish to prevent a jig from being destroyed, override the destroy method and throw an error in it. You can call destroy from outside the jig or from another method. You can also override destroy() to put the jig into its final state.


Code

Destroy a jig

giftCard.destroy()

Override destroy to finalize a jig

class GiftCard extends Jig {
  init(value) {
    this.value = value
  }

  destroy() {
    super.destroy()
    this.value = 0
  }
}


Backing

Jigs may be backed by bitcoins. When a jig is backed, it means that the output associated with that jig has a non-dust value in it. Backed jigs let users send money between each other and provide a baseline value for items. To back a jig, set the satoshis property to a number in a method. Your purse will automatically deposit that amount into the jig. When the satoshis property is later decreased, those Bitcoins will be withdrawn to the active purse.

It is important to remember that backed jigs may be melted at any time by spending the jig output outside of Run. This will destroy the jig, and the owner will keep the satoshis.

🛈 Note: All bitcoin outputs must have an amount at least the dust limit to be accepted by the network. Run will choose the greater of your jig's satoshis property and this dust limit when building your jig's transaction output.

Code

class Tip extends Jig {
  init(message, pubkey, amount) {
    this.message = message
    this.owner = pubkey
    this.satoshis = amount
  }

  withdraw() {
    this.satoshis = 0
  }
}

new Tip('I like your videos', pubkey, 100000)


Attaching Media

You may optionally set an icon onto a jig using an emoji, a custom image, or both. These icons will show in explorers, exchanges, and wallets, whenever your jig is displayed. To attach an icon, you set the metadata property on your jig class and then this icon will apply to all instances of that class. You can also set rich media like audio and 3D models too.

Setting an emoji: To add an emoji, set the emoji field on metadata to a single emoji character. You can find an appropriate emoji on Emojipedia.

Setting an image: To set an image, first upload your image on-chain in the B:// protocol format. Easy-B is an excellent tool for this. The image data you uploaded can then be referenced using a string that starts with b:// and includes the transaction ID. Currently Run supports SVG, PNG, and GIF formats. SVG is recommended in most cases because it scales up better on large displays and loads quicker due to its smaller file size.

Setting audio: To add an audio file, set a b:// protocol uri into the audio field in the metadata object similar to images.

Setting a 3d model: To add a 3d model, set a b:// protocol URI to the glbModel field in the metadata object similar to images.

For more information, see the section on Standard Metadata.

🛈 Note: Individual jig instances may also have their own icons. To do this, you should set the metadata property on the instance rather than the class either in its constructor or via another function.

Code

class DinoPet extends Jig { }

DinoPet.metadata = {
  emoji: '🦖',
  image: 'b://55e2c09672355e009e4727c2365fb61d12c69add91215ee3e9f50aa76c808536'
}

new DinoPet()

Now that you are an expert on jigs, let's go meta by exploring Code