Skip to content

Commit bba68bf

Browse files
mr-zwetsrkalis
authored andcommitted
add to infrastructure guide
1 parent 81e7dc1 commit bba68bf

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

website/docs/guides/infrastructure.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,54 @@ title: Contract Infrastructure
33
---
44

55
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.
6+
Below we'll discuss the 2 types of contract infrastructure you might run into: the need to store contract details and the need for a transaction server.
77

88
## Storing Contract Details
99

1010
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.
1111

1212
:::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.
13+
Smart contract developers need to consider whether their contracts require storing contract details unique to each user.
1414
:::
1515

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.
16+
17+
For single instance contracts where there is only one smart contract address for a long-running contract, the full script information is available on-chain after the first contract interaction so doesn't require much extra thought.
18+
19+
20+
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. Because of this it becomes a requirement to store the unique contract details so this requires careful consideration!
21+
22+
Only the constructor arguments in the contract bytecode are variable, the rest of the bytecode for a contract is static. So the constructor arguments for user contracts are essential to store in a secure way.
23+
Also the static part of the contract needs to be stored but this is the same across the different contract instances so is not unique for each user.
1724

1825
:::note
1926
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.
2027
:::
2128

22-
2329
### Off-chain storage
2430

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.
31+
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. This is crucial data for users to spend from BCH locked in such a smart contract. So this approach does introduce a single point of failure.
32+
33+
:::caution
34+
When using off-chain storage, it is the crucial responsibility of smart contract service to keep track of the contract details making up the `P2SH` contract, as user-wallets currently aren't capable to keep track of contract details and are fully reliant on the app server to store this crtitical info.
35+
:::
2636

2737
### On-chain storage
2838

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.
39+
To avoid introducing a single point of failure, different applications like Tapswap and Cauldron have 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.
3040

3141
:::tip
3242
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`.
3343
:::
3444

3545
:::note
36-
The `OP_RETURN` data has a maximum standardness size of 220 bytes which might be limiting for contracts with many large `constructor` arguments. You can read more about the transaction limits [here](/docs/compiler/script-limits).
46+
The `OP_RETURN` data has a maximum standardness size of 220 bytes which might be limiting for contracts with many large `constructor` arguments. You can read more about the [BCH transaction limits here](/docs/compiler/script-limits).
3747
:::
3848

3949
## Transaction server
4050

4151
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.
4252

43-
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.
53+
There are 3 main types of events which might need a transaction server to trigger a smart contract transaction: time-related events, contract-related events and oracle-related events.
4454

4555
### Time-related events
4656

@@ -50,6 +60,14 @@ Time-related events are when your smart contract uses absolute or relative lockt
5060
Both the `Electrum` and `Chaingraph` indexers allow you to create websocket subscriptions to listen for block height updates.
5161
:::
5262

63+
### Contract-related events
64+
65+
Contract-related events are when you want to update the server state to reflect changes on-chain, for example new contracts being created or existing contracts changing their state in an important way. So contract related events often don't trigger an on-chain transaction directly, but they update the information about the contracts tracked for time/oracle events by the server.
66+
67+
:::tip
68+
Only the `Chaingraph` indexer allows for subscriptions to arbitrary on-chain events, with `Electrum` you can create subscriptions for changes in the transaction history of a specific (contract) address.
69+
:::
70+
5371
### Oracle-related events
5472

5573
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.

0 commit comments

Comments
 (0)