# 编写简单的智能合约

## 基本步骤

EOS的智能合约，是用CPP语言写的，通过eosiocpp工具，分别生成wasm编码（执行），和abi文件（描述）

然后通过cleos set contract工具加载到区块链，最后通过cleos push actoin执行

## Hello.cpp

```cpp
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class nhello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action
      void hi( account_name user ) {
         //限定输入的user必须是执行合约的用户
         require_auth( user );
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( nhello, (hi) )

```

## 编译

```
eosiocpp -o neo.hello.wast neo.hello.cpp
eosiocpp -g neo.hello.abi neo.hello.cpp
```

## 加载及运行

```
#开启挖坑
nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin  eosio::account_history_api_plugin

#打开并解锁钱包
cleos wallet open
cleos wallet unlock
"PW5JAj9qw2tQsuVajLVXsiKUD7ULQnZ83qxpaB5YVFcs6qbhQ84J1"

cleos wallet open -n hospital2
cleos wallet unlock -n hospital2
"PW5JxnbBTTbTQKUHHNvYojU1xrcjxQduz2wU6gfMEgiZjwT5S6xJ6"

cleos wallet list

#创建运行合约的账号
cleos create account eosio hello1 EOS8K9wqRtD8cCuHvbdzcquEhYHnSaFvyQSWm8kVxYFHavraNeue2 EOS8K9wqRtD8cCuHvbdzcquEhYHnSaFvyQSWm8kVxYFHavraNeue2

#加载合约
cleos set contract hello1 neo.hello -p hello1

#运行合约
cleos push action hello1 hi '["hello1"]' -p hello1
```


---

# Agent Instructions: 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:

```
GET https://neochain.gitbook.io/project/eos/bian-xie-jian-dan-de-zhi-neng-he-yue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
