You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/smart.contract.guide.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
This guide outlines the essential structural and utility rules for creating a functional smart contract in the PHPCoin environment.
4
4
5
-
## ⚙️ 1. Structural Rules
5
+
## ⚙️ Structural Rules
6
6
7
7
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.
8
8
@@ -12,7 +12,7 @@ Every PHPCoin smart contract is a PHP class with specific structural requirement
12
12
|**Class Name Constant**| A `const SC_CLASS_NAME` must be defined, holding the name of the class. |`const SC_CLASS_NAME = "YourContract";`|
13
13
|**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) { ... }`|
14
14
15
-
## 💾 2. State Management
15
+
## 💾 State Management
16
16
17
17
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.
18
18
@@ -32,7 +32,7 @@ Interaction with state variables is designed to be intuitive, using standard PHP
32
32
|**Write (Map)**|`$this->balances[$userAddress] = 100;`| Set a value for a key in a `SmartContractMap`. |
33
33
|**Read (Map)**|`$userBalance = $this->balances[$userAddress];`| Get the value for a key from a `SmartContractMap`. |
34
34
35
-
## 🏷️ 3. Annotations
35
+
## 🏷️ Annotations
36
36
37
37
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.
38
38
@@ -44,7 +44,7 @@ PHPCoin uses docblock annotations to define the type and behavior of smart contr
44
44
|**`@SmartContractVar`**| Declares a public property as a persistent, single-value state variable. |`/** @SmartContractVar */ public $owner;`|
45
45
|**`@SmartContractMap`**| Declares a public property of type `SmartContractMap` as a persistent key-value store. |`/** @SmartContractMap */ public SmartContractMap $balances;`|
46
46
47
-
## 🛡️ 4. Core Transaction Templates
47
+
## 🛡️ Core Transaction Templates
48
48
49
49
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.
50
50
@@ -56,7 +56,7 @@ All methods marked with `@SmartContractTransact` should include checks to ensure
56
56
|**Data Size Limit**|`if (strlen($input) > MAX_INPUT_SIZE) { $this->error("INPUT_TOO_LONG"); }`| Enforces storage and processing limits on user-provided data. |
57
57
|**Non-Overwrite**|`if ($this->records[$id] !== null) { $this->error("RECORD_EXISTS"); }`| Prevents the accidental or malicious overwriting of existing state. |
58
58
59
-
## 🔩 5. Built-In Utilities
59
+
## 🔩 Built-In Utilities
60
60
61
61
The `SmartContractBase` class provides a set of properties and methods for accessing transaction data, controlling execution, and interacting with the blockchain environment.
62
62
@@ -72,7 +72,7 @@ The `SmartContractBase` class provides a set of properties and methods for acces
72
72
|**Inter-Contract**|`void $this->execSmartContract(string $contract, string $method, array $params)`| Executes a state-changing transaction on another smart contract. |
73
73
|**Outgoing TX**|`Transaction::send(string $to, float $amount)`| Executes an outgoing transaction from the contract's address to another address. |
74
74
75
-
## 🪙 6. ERC20 Token Templates
75
+
## 🪙 ERC20 Token Templates
76
76
77
77
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.
78
78
@@ -83,11 +83,11 @@ The repository includes a set of pre-built ERC20 token templates that you can ex
83
83
|`erc_20_token_mintable.php`| An ERC20 token that allows for the creation of new tokens, increasing the total supply. |
84
84
|`erc_20_token_burnable_mintable.php`| An ERC20 token that is both burnable and mintable. |
85
85
86
-
## 📦 7. Deployment
86
+
## 📦 Deployment
87
87
88
88
This section covers the tools and procedures for deploying your smart contracts.
89
89
90
-
### 7.1. Compiling the Contract
90
+
### Compiling the Contract
91
91
92
92
The `utils/sc_compile.php` script is used to package your smart contract's source code into a `.phar` file.
-`[source_file.php]`: The path to your smart contract's PHP source file.
102
102
-`[output_file.phar]`: The path where the compiled `.phar` file will be saved.
103
103
104
-
### 7.2. Deploying the Contract
104
+
### Deploying the Contract
105
105
106
106
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.
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.
134
134
@@ -146,11 +146,11 @@ The `scutil.php` script is not intended to be run directly, but rather included
146
146
|`generateScExecTx`| Generates a transaction to execute a `@SmartContractTransact` method. |
147
147
|`generateScSendTx`| Generates a transaction to send PHPCoin from a smart contract to another address. |
148
148
149
-
## 🚀 9. Examples
149
+
## 🚀 Examples
150
150
151
151
This section provides a set of examples, starting from a very basic "Hello, World!" to a more feature-rich application.
152
152
153
-
### 9.1. Example 1: Hello, World!
153
+
### Example 1: Hello, World!
154
154
155
155
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.
156
156
@@ -183,7 +183,7 @@ class HelloWorld extends SmartContractBase
183
183
}
184
184
```
185
185
186
-
### 9.2. Example 2: Basic State Control
186
+
### Example 2: Basic State Control
187
187
188
188
This contract demonstrates how to manage state. It uses a `@SmartContractVar` to store a single `message` and a `@SmartContractMap` to store key-value `records`.
189
189
@@ -260,7 +260,7 @@ class StateControl extends SmartContractBase
260
260
}
261
261
```
262
262
263
-
### 9.3. Example 3: Full Feature App (Advanced Poll)
263
+
### Example 3: Full Feature App (Advanced Poll)
264
264
265
265
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).
266
266
@@ -393,7 +393,7 @@ class AdvancedPoll extends SmartContractBase
393
393
}
394
394
```
395
395
396
-
### 9.4. Example 4: Wrapped PHPCoin (WPHP)
396
+
### Example 4: Wrapped PHPCoin (WPHP)
397
397
398
398
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.
0 commit comments