Tutorial 2 - Running jigs on Bitcoin Mainnet, Part 1

From RunWiki
Jump to: navigation, search

ALERT: This Page is Outdated and Waiting for Update The current way of testing the RUN library to create tokens and jigs is to open the devtools console at Runcraft.io, then head over to creating and loading jigs.

By completing the Tutorial 1 - Intro to jigs that run on Bitcoin: Mocknet and Web Console, you simulated transactions locally on your computer. In this tutorial, you’ll learn how to send transactions to the real Bitcoin Mainnet.

Getting started

First, open the[Homepage]

in the Firefox browser. You'll be coding up your jigs there.

Pro tip: We’ve also included run.js on our Docs page, so you can play in the Console while viewing the documentation.

Now, open the Web Console in Firefox by pressing Control-Shift-K on Windows or Command-Option-K on macOS.

Create a new empty instance of run by typing this code into the command prompt:

const run = new Run()


Press Return to submit the command:

If everything went correctly, you’ll see undefined as the result. That means there was no error and you’re ready to continue. 🙌

Understanding custody

When you create a new empty instance of Run, you are automatically assigned a random private key using the

The private key is constructed locally and is not shared with any server. The private key resides locally in your browser and explicitly not on the server. The wallet is non-custodial; no one can spend the money except for you.

Noting down your private key

The first thing you’ll want to do is write down your private key since there is no backup.

Exercise caution: No one will be able to help you recover your purse if you skip this step and close your browser.

To get your private key, type the following code:

run.purse.privkey


Press Return to submit it:

3 privkey.png

Copy the private key into your password manager. If you’re unsure about that, check out atutorial about how to manage your keys

with an app called, Enpass.

Note: Don’t store private keys unencrypted on your computer as notes or text files. Also, don’t share your private key with anyone. If someone gets your private key, they can spend your bitcoin.
== Finding your purse address ==

Your purse address is what you give people who want to send you bitcoin. You’re going to use your purse address to fund your Run instance so that you will be able to upload code and call methods on jigs.

To figure out what address you need to use, type the following code:

run.purse.address


Press Return to submit it. Console will display your bitcoin address.

Funding your RUN purse

You can fund this address using any BSV you have on any wallet, or simply ask a friend to send you a dollar of BSV. If you are looking for a wallet with an easy way to top-up using your credit card, we recommend you try HandCash.

To fund your purse, copy the purse address (which you can get with run.purse.address) and send some BSV using the wallet of your choice. We recommend just sending $1 so that you can pay for the mining fees when sending your transactions. Once you’ve sent a bit of money to the purse, you’re ready to upload the code for a jig. You can have at least 100 interactions with jigs for $1 USD (actually much more).

In the future, RUN will be integrated into wallets so that users can easily send jigs to each other. Also, companies could use a Pay Server to fund transaction fees, so users won’t need to use their own money at all! 🤩

Reusing your private key

Now the next time you want to start using run, you can reuse your funded private key easily like this:

const run = new Run({purse: "<my-private-key>"})

this will automatically reuse the previous purse, and you can check your purse's balance easily as well (in satoshis):

const satoshis = await run.purse.balance()

Understanding the commodity ledger

The code for a jig is stored in a Bitcoin transaction. It is optimized and stored as text in the memo field, known as the op_return. Miners charge transaction fees based on the number of bytes. Right now, the standard is one satoshi for two bytes. 1sat is the smallest unit of bitcoin, or 0.00000001 BSV.

The longer your code is, the more you’ll have to pay. But, don’t worry, even substantially long jig code might cost only one US cent (1¢):


4 ledger.jpg


Bitcoin is a commodity ledger. That means anyone can pay for space to record a transaction. A transaction could send bitcoin as money, but it could also represent other things including posting data or changing the ownership of a non-bitcoin asset like a digital pet or a land title.

It is important for the space in the commodity ledger to be valuable to prevent spam. However, everything that is worth recording for a fraction of a cent can be recorded and backed by the full strength of the network. The ledger is a neutral territory where all people, companies, and governments can collaborate together.

Ownership of jigs like creating, updating, or sending are all the kind of transactions that are important enough to pay the mining fee.

Using a block explorer

Open a new browser tab toWhatsOnChain

. To check the amount of bitcoin in your purse address, search for your address:

5 whatsonchain.png


Here’s what’s happening in that screenshot:

  1. Your address will be different than the one in the screenshot because each person has a unique purse.
  2. Look at the transactions to verify that you’ve got some money in your purse. If you have a transaction for something like 0.005 BSV, then you’re ready to post transactions.
  3. While the transaction has the status of Unconfirmed, the balance in the address will still show “0”. But don’t worry, you can spend unconfirmed money using RUN.

Writing code to the blockchain

Navigate to the first tab with the RUN page that has the open Console. Create a new jig by typing the following code:

Pro tip: You can create a new line in command prompt without executing code by pressing Shift-Return. Or for speed, you can copy and paste the following code in its entirety.
class Dragon extends Jig {
  setName(name) {
    this.name = name
  }
}
Dragon.metadata = { emoji: '🐉' }

Press Return to execute the code.

You defined a Dragon jig with a method to set the animal’s name and an icon. The code is called the class definition or the type of your jig.

Advanced programmers: We keep a best practice to support JavaScript 2015 (ES6) for maximum compatibility with Node.js. In the code above, you set the emoji metadata outside of the class because that's how static properties work in ES6.

The definition of the class is like the schematic or blueprint for the jig. Now that you’ve got the blueprint, you can give birth to a real instance.

Create an instance of the class by typing this code:

const dragon = new Dragon()


Now your dragon is living and breathing:

6 dragon.gif

By convention, the instance uses a lowercase first letter like dragon, and the type uses an uppercase first letter like Dragon.

Check the origin of the type:

Dragon.origin


Then check the origin of the instance:

dragon.origin


The origin is composed of two pieces, the transaction ID (before the underscore) and the output index (after the underscore).

Both the type and the instance have the same transaction ID, but a different output index. You can see the difference on a block explorer. Copy the transaction ID—the part before the underscore:

7 dragon-origin.png

Navigate to the WhatsOnChain tab and search for the transaction ID:

8 whatsonchain2.png

The output indexes start at zero, so they are: o0, o1, o2, o3, etc. In the code you’ve uploaded here, the definition of the Dragon type resides at o1 and the instance of dragon resides at o2.

The text displayed in the op_return at o0 contains all the information necessary to interact with the jig.

Updating a jig

The origin is the very first location of a jig. So, the origin is the place in the blockchain where you can find the original definition of a jig. Each time you update a jig, its location will change.

Navigate to the first tab with the RUN page that has the open Console. Type this code:


dragon.location


Verify that the current location of the instance is the same as its origin:

9 locations.png

Update the jig by setting the name of the dragon with this code in the Console:


dragon.setName('Empress')


Now that your dragon has a name, you can also check how the update affected its location:


dragon.location


Copy the new transaction ID (the part before the underscore). Search for it with the block explorer:

10 whatsonchain3.png

Notice how the op_return text at o0 is much shorter this time. That’s because the only information that is stored is the method call, set(name).

You’re looking at the state of a jig. The state consists of the jig definition and all the updates that have been applied to it. If you have the origin, you can step forward in transactions until you can’t go any farther. Using a block explorer, you can step transactions forwards and backwards. In this case, you’re at the current location and so you could step backwards in transactions until you get to the origin transaction.

In the screenshot above, locate the first input, i0, click transaction ID step backwards to the previous transaction.

Now you’re back at the origin transaction:

11 whatsonchain4.png


You can tell that you’re back at the origin transaction because the op_return data is much longer since it contains the full class definition.

Seeing your jig in the RUN Explorer

Navigate back to the first tab with the Console open.

Copy the entire location from your dragon, including its output index:

12 dragon-location.png

Open the RUN Explorer. You can get there by clicking in the top-right hand part of the [homepage.

13 explorer.png

Paste your location into the search field:

14 search-dragon.png

You see your jig, complete with its latest method call, and emoji metadata. Pretty cool, eh!

Where to go from here?

You learned how to upload code onto the live Bitcoin network and check out transactions with a block explorer.

Developing with RUN is accessible to everyone, even beginners. If you can write a few lines of JavaScript, you can unleash interactive objects that run on Bitcoin. Jigs can be something always-changing like a digital pet, or they can represent something as unchanging as a still photograph.

As a challenge, try importing the private key from the beginning of tutorial into Electrum SV. That way, you’ll be able to recover your funds and use the money left over from your transactions. Browse the Docs to figure out how you can specify a private key as a parameter when you create a new Run instance. If you succeed, you’ll be able to fund your purse once and then use the same purse over and over again.

Once you’ve given the challenge some thought, discover the solution in the next tutorial: Tutorial 3 - Running jigs on Bitcoin Mainnet, Part 2