Smart contracts
Smart contracts on Obyte are expressions that evaluate to true or false, the money stored on the contract can be spent only when it evaluates to true.
Last updated
Was this helpful?
Smart contracts on Obyte are expressions that evaluate to true or false, the money stored on the contract can be spent only when it evaluates to true.
Last updated
Was this helpful?
The smart contract language is declarative, meaning that it expresses what conditions must be met to allow movement of money, rather than how the decisions are made. This makes it easy to see that the implementation of the contract matches its intent, and hard to make mistakes (which cannot be undone in distributed ledgers).
However, the language is not as powerful as Ethereum’s Solidity, it is not Turing-complete, it doesn’t allow to code any program, rather it is a domain specific language for money on the distributed ledger. They are more like for Bitcoin. If you are looking to build more complex dApps that are as capable as Ethereum smart-contracts, then you need to use .
Money on Obyte is stored on addresses. Address is just a hash (plus checksum) of an , and the address definition is an expression in the Obyte smart contract language that evaluates to either true
or false
.
When you want to create a new smart contract with a user, your sequence is usually as follows:
you ask the user to send his payment address (it will be included in the contract)
you define a new contract using the user's address and your address as parties of the contract
you pay your share to the contract
at the same time, you send a specially formatted payment request (different from the payment request above) to the user to request his share. You start waiting for the user's payment
the user views the contract definition in his wallet and agrees to pay
you receive the payment notification and wait for it to get confirmed
after the payment is confirmed, the contract is fully funded
We'll discuss creating and offering contracts below. These two steps are similar to what happens in the GUI wallet when a user designs a new contract.
Create a JSON object that defines the contract:
Create another object that describes the positions of your and user addresses in the above definition:
The keys of this object are r
(from "root") followed by indexes into arrays in the definition's or
and and
conditions. Since the conditions can be nested, there can be many indexes, they are separated by dot.
Then you create the smart contract address:
If the address was successfully created, it was also already automatically sent to the user, so the user's wallet will know it.
To request the user to pay his share to the contract, create the below Javascript object objPaymentRequest
which contains both the payment request and the definition of the contract, encode the object in base64, and send it over to the user:
The user's wallet will parse this message, display the definition of the contract in a user-readable form, and offer the user to pay the requested amount. Your payment-waiting code will be called when the payment is seen on the DAG.
Since smart-contracts are private until they have been spent, users might loose them if they probably didn't backup their wallet or restored from seed words. Some attestation bots lock user's reward for months or years into smart-contracts and it would be more user-friendly if users could still restore the smart-contract even after they have lost it. Since all parties (2 or more) have the definition of the smart-contract in their wallet, one could ask to resend it from the other. With chat bots, this could be done like this:
Example of vesting contracts: , , . Examples with oracles: , . Examples with tokens: , .
More definition examples can be seen on .