合约样例1 - Set/Get Hash

这里是一个实现setHash和getHash功能的智能合约。

  1. contract Anchor{
  2. mapping(bytes32 => bytes32) hashMap;
  3.  
  4. function setHash(bytes32 key,bytes32 value) returns(bool,bytes32){
  5. if(hashMap[key] != 0x0){
  6. return (false,"the key exist");
  7. }
  8. hashMap[key] = value;
  9. return (true,"Success");
  10. }
  11.  
  12. function getHash(bytes32 key) returns(bool,bytes32,bytes32){
  13. if(hashMap[key] == 0x0){
  14. return (false,"the key is not exist",0x0);
  15. }
  16. return (true,"Success",hashMap[key]);
  17. }
  18. }

编译合约

如果您已经安装了solc,您只需通过一个简单的命令就能得到合约的字节码。如果您没有安装编译器,也可以直接使用以下的字节码,该字节码是本合约的编译结果。

字节码

  1. 0x606060405261015c806100126000396000f3606060405260e060020a60003504633cf5040a8114610029578063d7fa10071461007b575b610002565b34610002576100ca6004356000818152602081905260408120548190819015156101055750600091507f746865206b6579206973206e6f7420657869737400000000000000000000000090508161012a565b34610002576100ea6004356024356000828152602081905260408120548190156101315750600090507f746865206b657920657869737400000000000000000000000000000000000000610155565b604080519315158452602084019290925282820152519081900360600190f35b60408051921515835260208301919091528051918290030190f35b50505060008181526020819052604090205460019060c860020a665375636365737302905b9193909250565b50506000828152602081905260409020819055600160c860020a6653756363657373025b925092905056

假设您的智能合约文件名为sample1.sol, 您可以通过以下命令获得合约字节码:

  1. solc --bin sample1.sol

部署合约

HyperCli提供了一个合约部署的功能,以下是该功能的参数:

  1. $ ./hypercli contract deploy --help
  2.  
  3. NAME:
  4. hypercli contract deploy - Deploy a contract
  5.  
  6. USAGE:
  7. hypercli contract deploy [command options] [arguments...]
  8.  
  9. OPTIONS:
  10. --namespace value, -n value specify the namespace, default to global (default: "global")
  11. --from value, -f value specify the account (default: "000f1a7a08ccc48e5d30f80850cf1cf283aa3abd")
  12. --payload value, -p value specify the contract payload
  13. --extra value, -e value specify the extra information
  14. --simulate, -s simulate execute or not, default to false
  15. --directory value, -d value specify the contract file directory

您可以将合约的字节码作为--payload 选项的值,使用以下的命令来部署合约:

  1. ./hypercli contract deploy --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x606060405261015c806100126000396000f3606060405260e060020a60003504633cf5040a8114610029578063d7fa10071461007b575b610002565b34610002576100ca6004356000818152602081905260408120548190819015156101055750600091507f746865206b6579206973206e6f7420657869737400000000000000000000000090508161012a565b34610002576100ea6004356024356000828152602081905260408120548190156101315750600090507f746865206b657920657869737400000000000000000000000000000000000000610155565b604080519315158452602084019290925282820152519081900360600190f35b60408051921515835260208301919091528051918290030190f35b50505060008181526020819052604090205460019060c860020a665375636365737302905b9193909250565b50506000828152602081905260409020819055600160c860020a6653756363657373025b925092905056

该命令的意思是HyperCli使用地址为000f1a7a08ccc48e5d30f80850cf1cf283aa3abd的账户来部署合约。

如果命令执行正确,您将看到以下输出结果:

  1. {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0xb1b7d4f083ac65679ddd31a9b864fc8ca1ec75eee2f7a46cca1b223eae94527c","vmType":"EVM","contractAddress":"0xbbe2b6412ccf633222374de8958f2acc76cda9c9","gasUsed":69660,"ret":"0x606060405260e060020a60003504633cf5040a8114610029578063d7fa10071461007b575b610002565b34610002576100ca6004356000818152602081905260408120548190819015156101055750600091507f746865206b6579206973206e6f7420657869737400000000000000000000000090508161012a565b34610002576100ea6004356024356000828152602081905260408120548190156101315750600090507f746865206b657920657869737400000000000000000000000000000000000000610155565b604080519315158452602084019290925282820152519081900360600190f35b60408051921515835260208301919091528051918290030190f35b50505060008181526020819052604090205460019060c860020a665375636365737302905b9193909250565b50506000828152602081905260409020819055600160c860020a6653756363657373025b925092905056","log":[]}}

从结果中可以得到部署后的合约地址,在这个例子中,合约地址为:

  1. 0xbbe2b6412ccf633222374de8958f2acc76cda9c9

之后它将会被用于合约调用的操作中。

调用合约

HyperCli提供了一个合约调用的功能,以下是该功能的参数:

  1. $ ./hypercli contract invoke --help
  2.  
  3. NAME:
  4. hypercli contract invoke - Invoke a contract
  5.  
  6. USAGE:
  7. hypercli contract invoke [command options] [arguments...]
  8.  
  9. OPTIONS:
  10. --namespace value, -n value specify the namespace, default to global (default: "global")
  11. --from value, -f value specify the account (default: "000f1a7a08ccc48e5d30f80850cf1cf283aa3abd")
  12. --payload value, -p value specify the contract payload
  13. --to value, -t value specify the contract address
  14. --extra value, -e value specify the extra information
  15. --simulate, -s simulate execute or not, default to false
  16. --args value, -a value specify the args of invoke contract

在本例中,您至少需要指定两个参数的值,才能调用合约中的函数:

  • payload选项: 函数调用的字节码
  • to选项: 合约地址

我们提供了一些函数调用的字节码,您可以直接使用它们。

setHash

调用setHash函数,设置key1 = value1, 以下是该调用的字节码:

  1. 0xd7fa10076b6579310000000000000000000000000000000000000000000000000000000076616c7565310000000000000000000000000000000000000000000000000000

以下是该合约的地址:

  1. 0xbbe2b6412ccf633222374de8958f2acc76cda9c9

您可以通过以下命令调用合约:

  1. ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0xd7fa10076b6579310000000000000000000000000000000000000000000000000000000076616c7565310000000000000000000000000000000000000000000000000000 --to 0xbbe2b6412ccf633222374de8958f2acc76cda9c9

如果命令执行正确,您将看到以下输出结果:

  1. {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0xa28350777a964f5ab6f4ef355131c0c241388ac6e8548c191aa5b3b94af95571","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":20477,"ret":"0x00000000000000000000000000000000000000000000000000000000000000015375636365737300000000000000000000000000000000000000000000000000","log":[]}}

getHash

调用getHash函数,获得key1对应的value值, 以下是该调用的字节码:

  1. 0x3cf5040a6b65793100000000000000000000000000000000000000000000000000000000

以下是该合约的地址:

  1. 0xbbe2b6412ccf633222374de8958f2acc76cda9c9

您可以通过以下命令调用合约:

  1. ./hypercli contract invoke --from 000f1a7a08ccc48e5d30f80850cf1cf283aa3abd --payload 0x3cf5040a6b65793100000000000000000000000000000000000000000000000000000000 --to 0xbbe2b6412ccf633222374de8958f2acc76cda9c9

如果命令执行正确,您将看到以下输出结果:

  1. {"jsonrpc":"2.0","namespace":"global","id":1,"code":0,"message":"SUCCESS","result":{"version":"1.3","txHash":"0x185dba5451ace5ffcf4c11d10968e1e4ed299eb78ca6ddda65539dfca2fc56df","vmType":"EVM","contractAddress":"0x0000000000000000000000000000000000000000","gasUsed":523,"ret":"0x0000000000000000000000000000000000000000000000000000000000000001537563636573730000000000000000000000000000000000000000000000000076616c7565310000000000000000000000000000000000000000000000000000","log":[]}}