MacOS 部署CITA系列【三、测试合约部署过程】

|-----------------------------------------------------------------------------------------------------------------------------------------

|【说明】:我们要部署一条charge模型的测试链

| 【目标】:

| 1.交易手续费用发放到共识节点

| 2.admin能够进执行自动执行操作

| 3.普通节点能够发送交易、部署合约、调用合约

| 4.发行一定数量的原生token到admin账户

| 5.部署测试合约

|【说明2】:前两章,我们部署了一条charge模型的测试链,本章在此基础上,进行测试合约的部署

|【声明】:

| 感谢您的阅读,如果文中的内容有描述不当或者存在疑虑,请在留言区回复,我会及时进行更正

|-------------------------------------------------------------------------------------------------------------------------------------------

一、编辑合约在线网址

[https://remix.ethereum.org]


测试合约内容:

contract Person{

uint public age=10;

string public name = "";

function increseAge()public returns (uint){

    return ++age;

}

function getAget()public returns(uint){

    return age;

}

function say()public returns (string){

    return "hello";

}

}

点击Details显示如下界面:




二、部署合约

rpc sendRawTransaction --code 0x6080604052600a60005560206040519081016040528060008152506001908051906020019061002f929190610042565b5034801561003c57600080fd5b506100e7565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008357805160ff19168380011785556100b1565b828001600101855582156100b1579182015b828111156100b0578251825591602001919060010190610095565b5b5090506100be91906100c2565b5090565b6100e491905b808211156100e05760008160009055506001016100c8565b5090565b90565b61033e806100f66000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314610072578063262a9dff146101025780636d009f291461012d578063954ab4b214610158578063a01de6ab146101e8575b600080fd5b34801561007e57600080fd5b50610087610213565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b506101176102b1565b6040518082815260200191505060405180910390f35b34801561013957600080fd5b506101426102b7565b6040518082815260200191505060405180910390f35b34801561016457600080fd5b5061016d6102cc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101ad578082015181840152602081019050610192565b50505050905090810190601f1680156101da5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156101f457600080fd5b506101fd610309565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a95780601f1061027e576101008083540402835291602001916102a9565b820191906000526020600020905b81548152906001019060200180831161028c57829003601f168201915b505050505081565b60005481565b60008060008154600101919050819055905090565b60606040805190810160405280600581526020017f68656c6c6f000000000000000000000000000000000000000000000000000000815250905090565b600080549050905600a165627a7a72305820d3ea6379de354182945b0435d658b4efbed45d69310553d4385b2a162d284bf00029 --private-key 0x195d11f76cd552cf911fed11128085f5bca3566c153a508a71c8412e93231dd7

【参数说明】:
–code,指的是合约数据;

 --private-key,指的是合约创建者的私钥;

【注意】:部署合约时,需要保证部署合约账户中存在足够的余额


执行rpc getTransactionReceipt --hash 获取交易回执,查看交易处理后的结果信息

得到合约地址contractAddress: 0xad5cdfa04759e93486e86c50c846bbcaf28179a4

三、调用合约函数

rpc sendRawTransaction --code 0xa01de6ab --private-key 0x195d11f76cd552cf911fed11128085f5bca3566c153a508a71c8412e93231dd7 --address 0xad5cdfa04759e93486e86c50c846bbcaf28179a4

【参数说明】:

 --code,指的是合约函数地址,在编译合约时可获取;

 --private-key,指的是合约创建者的私钥;

 --address,指的是合约地址


四、查询调用结果

rpc call --to 0xad5cdfa04759e93486e86c50c846bbcaf28179a4 --data 0xa01de6ab

【参数说明】:–to合约地址;–data调用的合约函数地址

【声明】:

        感谢您的阅读,如果文中的内容有描述不当或者存在疑虑,请在留言区回复,我会及时进行更正



两个问题:

  1. 0xa01de6ab 对应的是 getAget() 方法,是个只读方法,我理解直接 call 就可以了,这里为什么要 sendRawTransaction 呢?
  2. 如果是调用赋值方法,比如有个叫 setName(string newName) 的方法如下,那通过 sendRawTransaction 调用时 --data 字段怎么填?(主要是不知道参数该怎么传)
function setName(string newName) public {
   name = newName;
}

@jack @leeyr 求指导

我理解,只读方法的确只需 call 就可以了。

至于 String 怎么传,你需要了解 合约的 ABI 规范

@jack, 下面这条是不是写错了?

rpc sendRawTransaction --code 0xa01de6ab --private-key 0x195d11f76cd552cf911fed11128085f5bca3566c153a508a71c8412e93231dd7 --address 0xad5cdfa04759e93486e86c50c846bbcaf28179a4

这里的 --code 应该指的是 increseAge 方法?用 6d009f29 才对?

大致看了下,太反人类了,数0要数死 :sweat_smile:
有木有『高级』点的玩法?

我能想到的就是用对应 SDK 封装好的接口。

@kaikai, 不知道你有没有更好的建议?

对,使用 sdk。 这么理解吧, sendRawTransaction 应该就是为 sdk 准备的接口,数零这个当然交给电脑来做

这个是调用getAget()合约函数

调用 getAget() 的话,直接用 call 不就行了吗?
为什么这里需要使用 sendRawTransaction ?