Nervosj开发中部署合约后相关问题

最近在研究相关nervosj部署和调用合约,遇到以下问题:
1,部署完合约后,得到的合约地址是不是要我们自己做本地或者数据库保存
2,调用合约时,如果返回的是一个数组,该怎么取到
3,如何可以查看调用合约的历史记录

  1. 是的
  2. 数组返回只能通过call可以取到。正常的ABI decode就可以
  3. 用log,合约中用event。log也是存在区块链上的,最后可以查询所有的历史记录

好的,这个有没有相关的小demo,可以参考一下

Hi Wei,你能不能把你的合约发出来,我帮你写个demo。

pragma solidity ^0.4.19;

contract Ballot {

//投票人
struct Voter{
    bool voted; //判断是否投票
    address delegate; //投票人
    uint256 vote;
}

//竞选人
struct Person{
    address addr;
    uint256 count;
}

address public chairperson;

mapping(address => Voter) public voters;

Person[] persons;

function Ballot() public{
    chairperson = msg.sender;
}

function addPerson(address kk) public {
    require(chairperson == msg.sender, "Only chairperson can give right to vote.");
       persons.push(Person({
        addr:kk,
        count:0
    }));

}

function giveRightToVote(address vot) public{
    require(chairperson == msg.sender,  "Only chairperson can give right to vote.");
    require(!voters[vot].voted,"the voter already voted");
    voters[vot].vote = 1;
}

function vote(address to) public{
    Voter storage sender = voters[msg.sender];
    require(!sender.voted,"Already voted");
    sender.voted = true;
    sender.delegate = to;
    for(uint i = 0; i < persons.length; i++){
        if(persons[i].addr == to)
            persons[i].count += 1;
    }
}


function winningPerson() public returns(address winningPerson_){
    uint winningPerson = 0 ;
    for(uint i = 0 ; i < persons.length; i++){
        if(persons[i].count > winningPerson){
            winningPerson = persons[i].count;
            winningPerson_ = persons[i].addr;
        }
    }
}

}

很简单的一个合约,麻烦了。

我研究了一下,我这成功了,你可以试试这样做

AppCall appCall = service.appCall(call, DefaultBlockParameter.valueOf("latest")).send();
String callResult = appCall.getValue();

Function func = new Function(
                "winningPerson",
                Collections.emptyList(),
                Arrays.asList(new TypeReference<DynamicArray<Address>>(){}));
        List<Address> addressList = (List<Address>)FunctionReturnDecoder.decode(
                callResult, func.getOutputParameters()).get(0).getValue();
        if(addressList != null && addressList.size()>0){
            int i = 0 ;
            for (Type resultAddress : addressList) {
                i++;
                System.out.println("resultAddress "+ i +" : " + resultAddress.getValue());
            }
        }

运行结果:

前两行代码需要你自己写

这些resultAddress是你通过addPerson加进去的竞选人嘛

我跟您的合约不一样,只是有一个同样获取地址集合的方法帮你借鉴一下

哦哦,那我想知道一下你第一行中用的call( Call call = new Call(from,contractAddress,callData);),他的callData ,你那边是用了合约里面的其中一个方法吗

不是的,明天给你发出来拼接参数的代码给你参考。

好的,顺便可以看看你的合约嘛

这个合约好像不可以顺便给你看看😂
但是你可以看看我之前的提问:nervosj开发中调用合约时传参问题

nice