Skip to content

Commit 5c82a5f

Browse files
authored
Improve docs (#245)
1 parent e41dcac commit 5c82a5f

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Contract Infrastructure
3+
---
4+
5+
When creating a smart contract application on Bitcoin Cash you'll need to investigate whether you need surrounding contract infrastructure.
6+
Below we'll discuss the 2 main types of contract infrastructure you might run into: the need to store contract details and the need for a transaction server.
7+
8+
## Storing Contract Details
9+
10+
Because of the `pay-to-scripthash` (`P2SH`) standard for smart contracts on BCH, the details of the script are hidden after creating a contract UTXO. This means you need to store the full contract script to ensure you can spend from your smart contract later.
11+
12+
:::caution
13+
It is the responsibility of smart contract services/apps to keep track of the contract details making up the `P2SH` contract, as user-wallets are currently not smart enough to do this automatically.
14+
:::
15+
16+
When users are allowed to provide their own `constructor` arguments when creating a BCH smart contract, each contract creation will have a unique smart contract address. Only the constructor arguments in the contract bytecode are variable, the rest of the bytecode for a contract is static. This static part is derived from the CashScript source file, so storing the contract source file or artifact both has the same effect.
17+
18+
:::note
19+
To construct the full contract script you need both the `constructor` arguments and static contract bytecode (either the contract source file or the `Artifact`) to be available.
20+
:::
21+
22+
23+
### Off-chain storage
24+
25+
To store the contract details off-chain, you will need to run a database server which stores the specific constructor arguments for each contract, this will translate into their respective smart contract addresses. Additionally, you should ensure that the CashScript source file or the contract artifact (which contains the static bytecode) is also stored for future reference.
26+
27+
### On-chain storage
28+
29+
Different applications like Tapswap and Cauldron have also started posting the `constructor` arguments with a contract-identifier to an `OP_RETURN` output during the contract creation. This way the contract details are available on-chain in a decentralized and recoverable way.
30+
31+
:::tip
32+
To store contract details on-chain, start the `OP_RETURN` data with an easily recognizable identifier, this way you can find all your smart contract UTXOs by checking the transactions including that identifier in the `OP_RETURN`.
33+
:::
34+
35+
## Transaction server
36+
37+
When your smart contracts depend on "automatic settlement" or any events where transactions are invoked without the user being involved, you will need a transaction server to create and broadcast those transactions. Smart contracts on BCH are never self-executing, someone is always needed to invoke functionality on a smart contract by creating a transaction.
38+
39+
There are 2 main types of events which might need a transaction server to trigger a smart contract transaction, namely time-related events and oracle-related events.
40+
41+
### Time-related events
42+
43+
Time-related events are when your smart contract uses absolute or relative locktimes, which require a waiting period before certain transactions can happen. However, if you want those transactions to 'automatically' happen when this locktime is reached, then you will need to create a transaction server to monitor the blockheight on an ongoing basis.
44+
45+
:::tip
46+
Both the `Electrum` and `Chaingraph` indexers allow you to create websocket subscriptions to listen for blockheight updates.
47+
:::
48+
49+
### Oracle-related events
50+
51+
Oracle-related events are when your smart contract uses an oracle to listen for outside information, where some transactions can only happen if the oracle publishes certain information. However, if you want those transactions to 'automatically' happen when the oracle triggers this condition, then you will need to create a transaction server to monitor the oracle for triggers on an ongoing basis.

website/docs/guides/syntax-highlighting.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ au BufRead,BufNewFile *.cash setfiletype solidity
3636

3737
This associates `.cash` files with Solidity, and enables syntax highlighting for your CashScript files.
3838

39-
## GitHub & GitLab
40-
GitHub and GitLab have syntax highlighting for Solidity built in. To associate `.cash` files with Solidity highlighting, add a `.gitattributes` file to your repository with the following contents:
39+
## GitHub
40+
GitHub has highlighting for Solidity built in. To associate `.cash` files with Solidity highlighting, add a `.gitattributes` file to your repository with the following contents:
4141

4242
```python title=".gitattributes"
4343
*.cash linguist-language=Solidity # GitHub
44-
*.cash gitlab-language=solidity # GitLab
4544
```
4645

46+
Unfortunately Gitlab does not have properly working Soldidity highlighting through the `gitattributes` for now...
47+
4748
## Others
4849
If your editor is not mentioned above, the steps are likely very similar. Try to find a Solidity syntax highlighting plugin for your editor of choice and find a method to associate `.cash` files with this Solidity highlighting.

website/docs/language/globals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ The units `sats`, `finney`, `bits` and `bitcoin` can be used to denominate Bitco
255255
```solidity
256256
require(1 sats == 1);
257257
require(1 finney == 10);
258-
require(1 bit == 100);
258+
require(1 bits == 100);
259259
require(1 bitcoin == 1e8);
260260
```
261261

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
items: [
4949
'guides/syntax-highlighting',
5050
'guides/covenants',
51+
'guides/infrastructure',
5152
'guides/walletconnect',
5253
'guides/debugging',
5354
'guides/optimization'

0 commit comments

Comments
 (0)