Difference between revisions of "Jigs"
| Line 2: | Line 2: | ||
== Creating == | == Creating == | ||
| + | |||
| + | Let's create a new class of jigs called <code>Post</code> to represent comments on a message board. In JavaScript, your class initializer is called <code>constructor</code>, but for jigs, this method is called <code>init</code>. Think of them the same way. If <code>init</code> throws an exception, the jig will never be created, just like constructors. You create jigs with the <code>new</code> keyword, just as you would with normal JavaScript objects, and they get deployed onto the Bitcoin network. Pretty cool. | ||
=== Code === | === Code === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Post extends Jig { | |
| + | init(message) { | ||
| + | this.message = message | ||
| + | } | ||
| + | } | ||
| + | |||
| + | new Post('Hello, world') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 16: | Line 24: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class EditablePost extends Post { | |
| + | edit(message) { | ||
| + | this.message = message | ||
| + | } | ||
| + | } | ||
| + | |||
| + | const editablePost = new EditablePost('Hello, world') | ||
| + | |||
| + | editablePost.edit('Hello, BitCoin') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 26: | Line 42: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Dragon extends Jig { | |
| + | send(to) { | ||
| + | this.owner = to | ||
| + | } | ||
| + | } | ||
| + | |||
| + | new Dragon().send(address) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 32: | Line 54: | ||
=== Code === | === Code === | ||
| + | |||
| + | Wait for updates to complete | ||
| + | |||
| + | <syntaxhighlight lang="javascript"> | ||
| + | class LoyaltyCard extends Jig { | ||
| + | init() { this.stamps = 0 } | ||
| + | stamp() { this.stamps +=1 } | ||
| + | } | ||
| + | |||
| + | const card = new LoyaltyCard() | ||
| + | await card.sync() | ||
| + | |||
| + | card.stamp() | ||
| + | await card.sync() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | Sync a jig from its origin to its latest state | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | const card2 = await run.load(card.origin) | |
| + | |||
| + | await card2.sync() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 42: | Line 83: | ||
<syntaxhighlight lang="javascript"> | <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> | </syntaxhighlight> | ||
| Line 48: | Line 97: | ||
=== Code === | === Code === | ||
| + | |||
| + | Destroy a jig | ||
<syntaxhighlight lang="javascript"> | <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> | </syntaxhighlight> | ||
| Line 58: | Line 124: | ||
<syntaxhighlight lang="javascript"> | <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> | </syntaxhighlight> | ||
| Line 64: | Line 142: | ||
=== Code === | === Code === | ||
| + | Attaching an item | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Hat extends Jig { } | |
| + | |||
| + | class Person extends Jig { | ||
| + | wear(item) { | ||
| + | expect(item).toBeInstanceOf(Hat) | ||
| + | |||
| + | this.item = item | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Person.deps = { Hat, expect: Run.extra.expect } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 74: | Line 163: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class DinoPet extends Jig { } | |
| − | |||
| − | = | + | DinoPet.metadata = { |
| + | emoji: '🦖', | ||
| + | image: 'b://55e2c09672355e009e4727c2365fb61d12c69add91215ee3e9f50aa76c808536' | ||
| + | } | ||
| − | + | new DinoPet() | |
| − | |||
| − | |||
| − | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 23:00, 14 December 2022
Jigs are interactive objects on Bitcoin. You define a jig with a JavaScript class and that class determines exactly what the jig can do. All of your jigs are unique. Each one has an owner and only that owner can update the jig. How is that secured? Bitcoin! Let's explore how you create a jig.
Contents
Creating
Let's create a new class of jigs called Post to represent comments on a message board. In JavaScript, your class initializer is called constructor, but for jigs, this method is called init. Think of them the same way. If init throws an exception, the jig will never be created, just like constructors. You create jigs with the new keyword, just as you would with normal JavaScript objects, and they get deployed onto the Bitcoin network. Pretty cool.
Code
class Post extends Jig {
init(message) {
this.message = message
}
}
new Post('Hello, world')
Updating
Jigs are updated by calling methods. In fact, this is the only way to update jigs. Your jig class defines the ways that your jig instances may evolve, so be sure to think ahead. When you call a method, Run publishes a Bitcoin transaction with data in an op_return that includes the method name and its arguments. The state may be recomputed simply by playing back every update one-by-one. For more information about how it works, see How It Works.
Code
class EditablePost extends Post {
edit(message) {
this.message = message
}
}
const editablePost = new EditablePost('Hello, world')
editablePost.edit('Hello, BitCoin')
Sending
Jigs may be sent to someone else by changing the owner property of a jig in a method. You can set the owner to a Bitcoin address, a public key, or a custom Lock. The new owner will be able to update the jig starting in the next transaction.
Code
class Dragon extends Jig {
send(to) {
this.owner = to
}
}
new Dragon().send(address)
Syncing
Code
Wait for updates to complete
class LoyaltyCard extends Jig {
init() { this.stamps = 0 }
stamp() { this.stamps +=1 }
}
const card = new LoyaltyCard()
await card.sync()
card.stamp()
await card.sync()
Sync a jig from its origin to its latest state
const card2 = await run.load(card.origin)
await card2.sync()
Interactivity
Code
class Event extends Jig {
createTicket() { return new Ticket(this) }
}
class Ticket extends Jig {
init(event) { this.event = event }
}
Event.deps = { Ticket }
Destroying
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
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)
Checking Parameters
Code
Attaching an item
class Hat extends Jig { }
class Person extends Jig {
wear(item) {
expect(item).toBeInstanceOf(Hat)
this.item = item
}
}
Person.deps = { Hat, expect: Run.extra.expect }
Attaching Media
Code
class DinoPet extends Jig { }
DinoPet.metadata = {
emoji: '🦖',
image: 'b://55e2c09672355e009e4727c2365fb61d12c69add91215ee3e9f50aa76c808536'
}
new DinoPet()