> For the complete documentation index, see [llms.txt](https://neochain.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://neochain.gitbook.io/project/ethereum/zhi-neng-he-yue-bian-xie-02.md).

# 智能合约编写02

## MyTransfer.sol

```
pragma solidity ^0.4.22;

contract MyTransfer {
  mapping (address => uint) public balances;

  constructor() public {
    balances[msg.sender] = 1000000;
  }

  function transfer(address _to, uint _amount) public {
    require(balances[msg.sender] < _amount, "you do not have enough money");
    balances[msg.sender] -= _amount;
    balances[_to] += _amount;
  }
}

```

## 编译合约

```bash
#编译方法有两种，一种是命令行编译（当前使用），一种是用Remid IDE编译（后面有介绍）
#从github上面下载solc编译工具
#用下面的命令行，生成abi和bin文件
../solc -o . --bin --abi MyTransfer.sol

ransAbi01 = JSON.parse('ABI文件的全部内容');
transBin01 = '0x'+'BIN文件的全部内容';
```

## 开启私链或测试链

测试链开启，请查看前面的章节

私链开启，请查看后面的章节

本例用了私链

## 部署并使用合约

```bash
#在BC01开启geth
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#在BC01解锁账号
personal.unlockAccount('c230847d2669091b77053df0cbc7f533a0a4cf1c','patient1',3600)

#在BC01部署合约
var ransAbi01 = JSON.parse('[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
var transFactory01 = eth.contract(transAbi01);   
var transBin01 = '0x608060405234801561001057600080fd5b50620f42406000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555061028a806100666000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806327e235e314610051578063a9059cbb146100a8575b600080fd5b34801561005d57600080fd5b50610092600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506100f5565b6040518082815260200191505060405180910390f35b3480156100b457600080fd5b506100f3600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061010d565b005b60006020528060005260406000206000915090505481565b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015156101c2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601c8152602001807f796f7520646f206e6f74206861766520656e6f756768206d6f6e65790000000081525060200191505060405180910390fd5b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555050505600a165627a7a72305820d191e2a6da7f5b4b821a2f410e9e415c609445d76bd604852807f1af1813429f0029';   
    
var mytrans01 = transFactory01.new({from: eth.accounts[0], data: transBin01, gas: '100000000'}, function (e, contract){
    if(e) {
      console.error(e);
      return;
    }

    if(!contract.address) {
      console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

    } else {
      console.log("Contract mined! Address: " + contract.address);
      console.log(contract);
    }
 });

#部署合约结果，一定要记录合约地址
INFO [05-01|21:43:26] Submitted contract creation              fullhash=0xd501d03d450eb51daa910fabb330d8831eafabcb037303aa1bd44982fcfba23e contract=0x952704a96A017ca3EB292947B91830dB25C48185
Contract transaction send: TransactionHash: 0xd501d03d450eb51daa910fabb330d8831eafabcb037303aa1bd44982fcfba23e waiting to be mined...

#等待合约被挖坑

#在BC01查看合约内容
mytrans01 = transFactory01.at('0x952704a96A017ca3EB292947B91830dB25C48185')  
mytrans01.balances("0xc230847d2669091b77053df0cbc7f533a0a4cf1c")
mytrans01.balances("0xc9fec0bd5aab52ba08a826af70bdecd2963bf5c9")

#在BC01进行合约转账
mytrans01.transfer.sendTransaction("0xc9fec0bd5aab52ba08a826af70bdecd2963bf5c9",100,{from:eth.accounts[0]})

#转账结果
INFO [05-01|22:18:25] Submitted transaction                    fullhash=0x6141f0524b8ca179eeee772f1ddf10ff50cb5e9f6c796a7e5de56675a1343640 recipient=0x952704a96A017ca3EB292947B91830dB25C48185

#等待被挖坑

#在BC02查看转账结果
var ransAbi01 = JSON.parse('[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
var transFactory01 = eth.contract(transAbi01);
#这个地址就是部署合约时，返回的合约地址
var mytrans01= transFactory01.at('0x952704a96A017ca3EB292947B91830dB25C48185')  
mytrans01.balances("0xc230847d2669091b77053df0cbc7f533a0a4cf1c")
mytrans01.balances("0xc9fec0bd5aab52ba08a826af70bdecd2963bf5c9")
```


---

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

```
GET https://neochain.gitbook.io/project/ethereum/zhi-neng-he-yue-bian-xie-02.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.
