> For the complete documentation index, see [llms.txt](https://andelf.gitbook.io/tron/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://andelf.gitbook.io/tron/smart-contract/use-solidity-like-a-professional.md).

# Use Solidity Like a Professional

## 标准 - The Standard

不要自己发明标准。用别人发明好的。

### TRC20, ERC20

最常见的代币合约，来自 ERC20.

### ERC223

兼容 ERC20.

定义了向合约转 token 的标准处理方式，要求合约支持 tokenFallback 调用，否则失败。（参数检查）

避免 ERC20 token 被发入黑洞地址（发往合约，如果该合约不能支持提token出来，那么就是黑洞）

### TRC721, ERC721

非同质化代币。例如以太猫。 NFT.

[ITRC721.sol](https://github.com/tronprotocol/sun-network/blob/develop/dapp-chain/contract/common/token/TRC721/ITRC721.sol)

### ERC1155

The Final Token Standard on Ethereum.

是 ERC20 和 ERC721 的结合体.。支持批量转账。

impl: [erc-1155](https://github.com/enjin/erc-1155)

### ERC1538

Future Proofing Smart Contracts and Tokens.

通过修改 delegate address 方式，使合约可以持续升级.

### ERC165

检查合约支持的接口。

```
function supportsInterface(bytes4 interfaceId) external view returns (bool)
```

### ERC1820

Contract interface Registry.

### ERC777

又一 ERC20 替代，兼容, 必须在 ERC1820 注册。方法较多。

### ERC1046

token(erc20) metadata uri. 链上管理 ERC20 token 的配置源信息。

但实际上是提供一个 URL 去下载一个固定格式的 JSON 文件。

1047 是其 metadata 格式 = ERC20Metadata

```
function tokenURI() external view returns (string);
```

Metadata 格式参考: [eip-1046](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1046.md)

## OpenZeppelin

合约实现的业界标杆。人类希望。

[OpenZeppelin/openzeppelin-contracts](https://github.com/OpenZeppelin/openzeppelin-contracts)

* AccessControl: role based access contrl
* Ownable: 提供 onlyOwner modifier, ownership 管理
* SafeMath/SignedSafeMath
* Pausable: 合约的紧急停止手段
* ReentrancyGuard: 防止函数被重入，重要的防攻击手段
* 及若干 TOKEN 实现

## 常见问题

### 合约代码升级

参考 ERC1538.

[tronprotocol/sun-network:dapp-chain/contract/common/delegatecallable/Delegatecallable.sol](https://github.com/tronprotocol/sun-network/blob/develop/dapp-chain/contract/common/delegatecallable/Delegatecallable.sol)

### 避免给合约转 Token

参考 ERC223.

### 如何编写安全的合约

* 先更改合约本地变量，再进行外部调用的风格
* 使用预言机机制获取外部参数时候，应该保存重要参数例如价格，与上一次做对比，波动过大时候暂停合约

### 2-step 随机数

波场缺乏链上随机变量，即所有TVM中可获取的外部变量均是可预测的。所以建议使用两步法产生随机变量。

概念验证代码（含合约和触发后端）

{% embed url="<https://gist.github.com/andelf/3f605596c19b1612cf7eaf8ed429e416>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://andelf.gitbook.io/tron/smart-contract/use-solidity-like-a-professional.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
