Skip to content

Commit 6513932

Browse files
feat: create smart contract builder's guide
Creates a comprehensive guide for building smart contracts on the PHPCoin platform. The guide covers: - Structural rules - State management - Annotations - Core transaction templates - Built-in utilities - ERC20 token templates - Deployment - Command-line tools - A series of examples from "Hello, World!" to a "Wrapped PHPCoin" token.
1 parent 4eb0acc commit 6513932

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

docs/smart.contract.guide.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This guide outlines the essential structural and utility rules for creating a functional smart contract in the PHPCoin environment.
44

5-
## ⚙️ 1. Structural Rules
5+
## ⚙️ Structural Rules
66

77
Every PHPCoin smart contract is a PHP class with specific structural requirements. These rules ensure that the contract can be correctly deployed and executed by the blockchain.
88

@@ -12,7 +12,7 @@ Every PHPCoin smart contract is a PHP class with specific structural requirement
1212
| **Class Name Constant** | A `const SC_CLASS_NAME` must be defined, holding the name of the class. | `const SC_CLASS_NAME = "YourContract";` |
1313
| **Deployment Method** | The `deploy()` method, annotated with `@SmartContractDeploy`, is executed once upon deployment to set the contract's initial state. | `/** @SmartContractDeploy */ public function deploy($param) { ... }` |
1414

15-
## 💾 2. State Management
15+
## 💾 State Management
1616

1717
PHPCoin smart contracts manage persistent state through class properties annotated with specific docblock tags. These properties are automatically backed by the blockchain's state database.
1818

@@ -32,7 +32,7 @@ Interaction with state variables is designed to be intuitive, using standard PHP
3232
| **Write (Map)** | `$this->balances[$userAddress] = 100;` | Set a value for a key in a `SmartContractMap`. |
3333
| **Read (Map)** | `$userBalance = $this->balances[$userAddress];` | Get the value for a key from a `SmartContractMap`. |
3434

35-
## 🏷️ 3. Annotations
35+
## 🏷️ Annotations
3636

3737
PHPCoin uses docblock annotations to define the type and behavior of smart contract methods and properties. These annotations are essential for the PHPCoin runtime to correctly interpret and execute the contract's code.
3838

@@ -44,7 +44,7 @@ PHPCoin uses docblock annotations to define the type and behavior of smart contr
4444
| **`@SmartContractVar`** | Declares a public property as a persistent, single-value state variable. | `/** @SmartContractVar */ public $owner;` |
4545
| **`@SmartContractMap`** | Declares a public property of type `SmartContractMap` as a persistent key-value store. | `/** @SmartContractMap */ public SmartContractMap $balances;` |
4646

47-
## 🛡️ 4. Core Transaction Templates
47+
## 🛡️ Core Transaction Templates
4848

4949
All methods marked with `@SmartContractTransact` should include checks to ensure the integrity and security of the contract. The following templates cover the most common requirements.
5050

@@ -56,7 +56,7 @@ All methods marked with `@SmartContractTransact` should include checks to ensure
5656
| **Data Size Limit** | `if (strlen($input) > MAX_INPUT_SIZE) { $this->error("INPUT_TOO_LONG"); }` | Enforces storage and processing limits on user-provided data. |
5757
| **Non-Overwrite** | `if ($this->records[$id] !== null) { $this->error("RECORD_EXISTS"); }` | Prevents the accidental or malicious overwriting of existing state. |
5858

59-
## 🔩 5. Built-In Utilities
59+
## 🔩 Built-In Utilities
6060

6161
The `SmartContractBase` class provides a set of properties and methods for accessing transaction data, controlling execution, and interacting with the blockchain environment.
6262

@@ -72,7 +72,7 @@ The `SmartContractBase` class provides a set of properties and methods for acces
7272
| **Inter-Contract** | `void $this->execSmartContract(string $contract, string $method, array $params)` | Executes a state-changing transaction on another smart contract. |
7373
| **Outgoing TX** | `Transaction::send(string $to, float $amount)` | Executes an outgoing transaction from the contract's address to another address. |
7474

75-
## 🪙 6. ERC20 Token Templates
75+
## 🪙 ERC20 Token Templates
7676

7777
The repository includes a set of pre-built ERC20 token templates that you can extend to create your own tokens. These templates are located in the `include/templates/tokens` directory.
7878

@@ -83,11 +83,11 @@ The repository includes a set of pre-built ERC20 token templates that you can ex
8383
| `erc_20_token_mintable.php` | An ERC20 token that allows for the creation of new tokens, increasing the total supply. |
8484
| `erc_20_token_burnable_mintable.php` | An ERC20 token that is both burnable and mintable. |
8585

86-
## 📦 7. Deployment
86+
## 📦 Deployment
8787

8888
This section covers the tools and procedures for deploying your smart contracts.
8989

90-
### 7.1. Compiling the Contract
90+
### Compiling the Contract
9191

9292
The `utils/sc_compile.php` script is used to package your smart contract's source code into a `.phar` file.
9393

@@ -101,7 +101,7 @@ php utils/sc_compile.php [contract_address] [source_file.php] [output_file.phar]
101101
- `[source_file.php]`: The path to your smart contract's PHP source file.
102102
- `[output_file.phar]`: The path where the compiled `.phar` file will be saved.
103103

104-
### 7.2. Deploying the Contract
104+
### Deploying the Contract
105105

106106
After compiling the contract, you need to create a deployment transaction. This is done by creating a separate PHP script that uses the `SCUtil` class.
107107

@@ -128,7 +128,7 @@ $tx_id = SCUtil::sendTx($node, $tx);
128128
echo "Deployment transaction sent: " . $tx_id . "\n";
129129
```
130130

131-
## 🛠️ 8. Command-Line Tools
131+
## 🛠️ Command-Line Tools
132132

133133
The `utils/` directory contains several PHP scripts for interacting with smart contracts from the command line. The most important of these is `scutil.php`, which provides a set of tools for deploying and managing contracts.
134134

@@ -146,11 +146,11 @@ The `scutil.php` script is not intended to be run directly, but rather included
146146
| `generateScExecTx` | Generates a transaction to execute a `@SmartContractTransact` method. |
147147
| `generateScSendTx` | Generates a transaction to send PHPCoin from a smart contract to another address. |
148148

149-
## 🚀 9. Examples
149+
## 🚀 Examples
150150

151151
This section provides a set of examples, starting from a very basic "Hello, World!" to a more feature-rich application.
152152

153-
### 9.1. Example 1: Hello, World!
153+
### Example 1: Hello, World!
154154

155155
This contract is the simplest possible smart contract. It has no state and one read-only method that returns a fixed string. It demonstrates the absolute minimum code required for a functional contract.
156156

@@ -183,7 +183,7 @@ class HelloWorld extends SmartContractBase
183183
}
184184
```
185185

186-
### 9.2. Example 2: Basic State Control
186+
### Example 2: Basic State Control
187187

188188
This contract demonstrates how to manage state. It uses a `@SmartContractVar` to store a single `message` and a `@SmartContractMap` to store key-value `records`.
189189

@@ -260,7 +260,7 @@ class StateControl extends SmartContractBase
260260
}
261261
```
262262

263-
### 9.3. Example 3: Full Feature App (Advanced Poll)
263+
### Example 3: Full Feature App (Advanced Poll)
264264

265265
This example builds on the previous concepts to create a more robust polling contract. It introduces owner-only actions, a fee requirement for voting, and a lifecycle (the poll can be opened and closed).
266266

@@ -393,7 +393,7 @@ class AdvancedPoll extends SmartContractBase
393393
}
394394
```
395395

396-
### 9.4. Example 4: Wrapped PHPCoin (WPHP)
396+
### Example 4: Wrapped PHPCoin (WPHP)
397397

398398
This contract creates a token that is pegged 1:1 with PHPCoin. Users can send PHPCoin to the contract to "wrap" it into a token, and burn tokens to "unwrap" them back into PHPCoin. This allows PHPCoin to be used in token-based applications.
399399

0 commit comments

Comments
 (0)