Getting started with Obyte Autonomous Agents

in #obyte5 years ago (edited)

obyte-autonomous-agents.jpg

In the Obyte ecosystem we have had smart contracts and chat bots for a long time: smart contracts provide a way to lock and unlock funds based on immutable predefined conditions enforced by the Obyte ledger, while chat bots bring dynamics and complex user interactions. While smart contracts are evaluated by every node and are decentralized, chat bots are external users of the system and they are not decentralized. Obyte didn't have a decentralized programmable reactive component... until now!

Autonomous Agents

Autonomous Agents (AAs) are a recent addition to the Obyte ecosystem. AAs are programs that live on the Obyte ledger and execute in a decentralized fashion when they are triggered. Indeed, they are not running constantly by themselves, they have to be activated by users, or as a matter of fact other AAs, who interact with AAs by sending transactions to them. Such trigger transaction is typically a simple payment but they can also carry data elements for example.

AAs are program codes written in a so called Oscript language and are stored and executed by every full node in the network. Given the unique properties of AAs, they are deterministic and evaluate the same on every node independently. A typical flow is the following:

  • the user posts a transaction addressed to the AA
  • when the transaction becomes stable, the AA program code is executed on all nodes with the transaction created by the user as input
  • the AA computes the result and outputs one or more messages bundled in a transaction
  • the output of the AA becomes stable and the interaction is considered completed

The output of the AA can be many things, it can be a payment back to the user, to other users or AAs, it can be a definition of a new asset or data posted in a data feed etc, but one of the most important is a state change output. Each AA can store its state in state variables that can be read and updated in consecutive executions.

Hello World

Let's create the classic Hello World application as an autonomous agent. The simplest way is to use the web based Oscript Editor (testnet).
The AA program code is a JSON object which can include executable Oscript statements in the value parts of the JSON object. The mandatory object is the messages, which declares the output of the AA. Let's just start with a simple code which returns the Hello World! string to the caller eg. the user wallet.

{
    messages: [
        {
            app: 'state',
            state: `{
                response['message'] = 'Hello World!';   
            }`
        }
    ]
}

Above we instruct the AA to produce a state output with a response variable called message. The response is informational to the user that triggered the AA and is not stored on the Obyte ledger. Here is what the user can see in the payment history tab:

HelloWorld-01.png

There is a couple of things we can observe here:

  • we sent a payment to RQFA6V47KAHRUPYKH6VSJLILNXIXKZ3F which is the address of the Hello World AA and is simply the hash of AA's definition
  • we sent 10000 bytes, which is the minimum amount an AA can be triggered with and is chosen to make spams more costly, it is also called as "bounce fee" which is charged when the AA is triggered with invalid input
  • The Response unit is empty since the we only had a state message that does not result in a new Obyte transaction
  • The Response contains the variables returned using the response array
  • Bounced? No states that the AA executed normally

Now let's improve our Hello World AA by returning some of the money the user sent to trigger the AA. We can do it by adding an additional payment message:

{
    messages: [
        {
            app: 'payment',
            payload: {
                asset: 'base',
                outputs: [{address: "{trigger.address}"}]
            }
        },
        {
            app: 'state',
            state: `{
                response['message'] = 'Hello World!';   
            }`
        }
    ]
}

Have a look at the payment message:

  • it declares that the payment is made in the base asset which is the native currency of the Obyte platform called bytes.
  • it also declares to send back the whole balance of the AA to the caller address. Note the special value of the address: {trigger.address}. This is an Oscript expression that evaluates to the address of the triggering unit.

Deploying this script results in a new AA address (XWSU74B7OMNIUNEXCVOKKJXXYR67BCFW), which is expected since the code has changed and so the hash of the AA definition. If we now trigger this AA, we will see the AA returning some of the money, basically the original 10000 bytes minus posting fees charged by the Obyte protocol:

HelloWorld-02.png

And here we can see the Response unit is now filled with the unit hash that points to the output of the AA which is actually the returned payment transaction:

HelloWorld-03.png

Further reading and other resources

Sort:  


Congratulations @pmiklos!
You raised your level and are now a Minnow!

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64029.44
ETH 3157.04
USDT 1.00
SBD 4.02