合约样例2 - Simulate Bank
这里是另一个实现了资产管理的合约示例。
- contract SimulateBank{
- address owner;
- bytes32 bankName;
- uint bankNum;
- bool isInvalid;
- mapping(address => uint) public accounts;
- function SimulateBank( bytes32 _bankName,uint _bankNum,bool _isInvalid){
- bankName = _bankName;
- bankNum = _bankNum;
- isInvalid = _isInvalid;
- owner = msg.sender;
- }
- function issue(address addr,uint number) returns (bool){
- if(msg.sender==owner){
- accounts[addr] = accounts[addr] + number;
- return true;
- }
- return false;
- }
- function transfer(address addr1,address addr2,uint amount) returns (bool){
- if(accounts[addr1] >= amount){
- accounts[addr1] = accounts[addr1] - amount;
- accounts[addr2] = accounts[addr2] + amount;
- return true;
- }
- return false;
- }
- function getAccountBalance(address addr) returns(uint){
- return accounts[addr];
- }
- }
编译合约
如果您已经安装了solc
,您只需通过一个简单的命令就能得到合约的字节码。如果您没有安装编译器,也可以直接使用以下的字节码,该字节码是本合约的编译结果。
字节码
- 0x606060405260405160608061020083395060c06040525160805160a05160018390556002829055600380547f01000000000000000000000000000000000000000000000000000000000000008084020460ff19909116179055600080546c0100000000000000000000000033810204600160a060020a03199091161790555050506101728061008e6000396000f3606060405260e060020a60003504635e5c06e2811461003f578063867904b41461005c57806393423e9c146100a8578063beabacc8146100d1575b610002565b346100025761013760043560046020526000908152604090205481565b34610002576101496004356024356000805433600160a060020a039081169116141561015d5750600160a060020a03821660009081526004602052604090208054820190556001610161565b3461000257610137600435600160a060020a038116600090815260046020526040902054919050565b3461000257610149600435602435604435600160a060020a0383166000908152600460205260408120548290106101675750600160a060020a0380841660009081526004602052604080822080548590039055918416815220805482019055600161016b565b60408051918252519081900360200190f35b604080519115158252519081900360200190f35b5060005b92915050565b5060005b939250505056
假设您的智能合约文件名为sample2.sol
, 您可以通过以下命令获得合约字节码:
- solc --bin sample2.sol
部署合约
HyperCli提供了一个合约部署的功能,以下是该功能的参数:
- $ ./hypercli contract deploy --help
- NAME:
- hypercli contract deploy - Deploy a contract
- USAGE:
- hypercli contract deploy [command options] [arguments...]
- OPTIONS:
- --namespace value, -n value specify the namespace, default to global (default: "global")
- --from value, -f value specify the account (default: "000f1a7a08ccc48e5d30f80850cf1cf283aa3abd")
- --payload value, -p value specify the contract payload
- --extra value, -e value specify the extra information
- --simulate, -s simulate execute or not, default to false
- --directory value, -d value specify the contract file directory
您可以将合约的字节码作为--payload
选项的值,使用以下的命令来部署合约:
- ./hypercli contract deploy --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x606060405260405160608061020083395060c06040525160805160a05160018390556002829055600380547f01000000000000000000000000000000000000000000000000000000000000008084020460ff19909116179055600080546c0100000000000000000000000033810204600160a060020a03199091161790555050506101728061008e6000396000f3606060405260e060020a60003504635e5c06e2811461003f578063867904b41461005c57806393423e9c146100a8578063beabacc8146100d1575b610002565b346100025761013760043560046020526000908152604090205481565b34610002576101496004356024356000805433600160a060020a039081169116141561015d5750600160a060020a03821660009081526004602052604090208054820190556001610161565b3461000257610137600435600160a060020a038116600090815260046020526040902054919050565b3461000257610149600435602435604435600160a060020a0383166000908152600460205260408120548290106101675750600160a060020a0380841660009081526004602052604080822080548590039055918416815220805482019055600161016b565b60408051918252519081900360200190f35b604080519115158252519081900360200190f35b5060005b92915050565b5060005b939250505056
该命令的意思是HyperCli使用地址为000f1a7a08ccc48e5d30f80850cf1cf283aa3abd的账户来部署合约。
如果命令执行正确,您将看到以下输出结果:
- {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0x6790126ca4c072f53d1684dff9e080098db931358d8eca04c833373ae580ed9e","vmType":"EVM","contractAddress":"0xbbe2b6412ccf633222374de8958f2acc76cda9c9","gasUsed":109363,"ret":"0x606060405260e060020a60003504635e5c06e2811461003f578063867904b41461005c57806393423e9c146100a8578063beabacc8146100d1575b610002565b346100025761013760043560046020526000908152604090205481565b34610002576101496004356024356000805433600160a060020a039081169116141561015d5750600160a060020a03821660009081526004602052604090208054820190556001610161565b3461000257610137600435600160a060020a038116600090815260046020526040902054919050565b3461000257610149600435602435604435600160a060020a0383166000908152600460205260408120548290106101675750600160a060020a0380841660009081526004602052604080822080548590039055918416815220805482019055600161016b565b60408051918252519081900360200190f35b604080519115158252519081900360200190f35b5060005b92915050565b5060005b939250505056","log":[]}}
从结果中可以得到部署后的合约地址,在这个例子中,合约地址为:
- 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
之后它将会被用于合约调用的操作中。
调用合约
HyperCli提供了一个合约调用的功能,以下是该功能的参数:
- $ ./hypercli contract invoke --help
- NAME:
- hypercli contract invoke - Invoke a contract
- USAGE:
- hypercli contract invoke [command options] [arguments...]
- OPTIONS:
- --namespace value, -n value specify the namespace, default to global (default: "global")
- --from value, -f value specify the account (default: "000f1a7a08ccc48e5d30f80850cf1cf283aa3abd")
- --payload value, -p value specify the contract payload
- --to value, -t value specify the contract address
- --extra value, -e value specify the extra information
- --simulate, -s simulate execute or not, default to false
- --args value, -a value specify the args of invoke contract
在本例中,您至少需要指定两个参数的值,才能调用合约中的函数:
- payload选项: 函数调用的字节码
- to选项: 合约地址
我们提供了一些函数调用的字节码,您可以直接使用它们。
发布资产
调用issue函数,给账户0x1234567发布资产1,000,000,000,以下是该调用的字节码:
- 0x867904b40000000000000000000000000000000000000000000000000000000001234567000000000000000000000000000000000000000000000000000000003b9aca00
以下是该合约的地址:
- 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
您可以通过以下命令调用合约:
- ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x867904b40000000000000000000000000000000000000000000000000000000001234567000000000000000000000000000000000000000000000000000000003b9aca00 --to 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
如果命令执行正确,您将看到以下输出结果:
- {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0x9f602fc0c4ac12d383cf765e2931978a0e5daaec666888e242485bd6314e17f9","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":20498,"ret":"0x0000000000000000000000000000000000000000000000000000000000000001","log":[]}}
交易资产
调用transfer函数,从账户0x1234567向账户0x2345678转移1个资产,以下是该调用的字节码:
- 0xbeabacc8000000000000000000000000000000000000000000000000000000000123456700000000000000000000000000000000000000000000000000000000023456780000000000000000000000000000000000000000000000000000000000000001
以下是该合约的地址:
- 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
您可以通过以下命令调用合约:
- ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0xbeabacc8000000000000000000000000000000000000000000000000000000000123456700000000000000000000000000000000000000000000000000000000023456780000000000000000000000000000000000000000000000000000000000000001 --to 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
如果命令执行正确,您将看到以下输出结果:
- {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0x2d779df9db29541102e98ed9996263db42da6c13a2a6ac5c7ba9c606acecba28","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":25733,"ret":"0x0000000000000000000000000000000000000000000000000000000000000001","log":[]}}
查询资产
调用getAccountBalance函数,查询账户0x1234567的资产,以下是该调用的字节码:
- 0x93423e9c0000000000000000000000000000000000000000000000000000000001234567
以下是该合约的地址:
- 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
您可以通过以下命令调用合约:
- ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x93423e9c0000000000000000000000000000000000000000000000000000000001234567 --to 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
如果命令执行正确,您将看到以下输出结果:
- {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0xcca7b19dde84bf174243398de4107ee0b783b6e243176e162a2239bae1f475f7","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":353,"ret":"0x000000000000000000000000000000000000000000000000000000003b9ac9ff","log":[]}}
调用getAccountBalance函数,查询账户0x2345678的资产,以下是该调用的字节码:
- 0x93423e9c0000000000000000000000000000000000000000000000000000000002345678
以下是该合约的地址:
- 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
您可以通过以下命令调用合约:
- ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x93423e9c0000000000000000000000000000000000000000000000000000000002345678 --to 0x1e548137be17e1a11f0642c9e22dfda64e61fe6d
如果命令执行正确,您将看到以下输出结果:
- {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0x04b82a4fcdadcf102d559b4eb6a29030f7ef29195f40a5f9b986021b48b48552","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":353,"ret":"0x0000000000000000000000000000000000000000000000000000000000000001","log":[]}}