안녕하세요.
Caver-Java 1.5.6 을 사용해서 개발하다가 DynamicArray 의 디코딩 결과가 이상하게 찍혀서 문의드립니다.
- 테스트 컨트렉트 코드
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
contract TestWeb3JDynamicArray {
struct Node {
string parent;
string[] nodes;
}
mapping(string => Node) internal nodes;
function init(string memory parent) public {
string[] memory emptyNodeList;
nodes[parent] = Node({
parent: parent,
nodes: emptyNodeList
});
}
function setting(string memory parent) public {
Node storage node = nodes[parent];
string[] memory values = new string[](2);
values[0] = "abc";
node.nodes.push(values[0]);
values[1] = "def";
node.nodes.push(values[1]);
}
function getNode(string memory parent) view public returns(string memory, string[] memory) {
return (nodes[parent].parent, nodes[parent].nodes);
}
}
- caver-java 테스트코드
import com.klaytn.caver.Caver;
import com.klaytn.caver.contract.Contract;
import com.klaytn.caver.utils.ChainId;
import okhttp3.Credentials;
import org.junit.jupiter.api.Test;
import org.web3j.abi.datatypes.Type;
import org.web3j.protocol.http.HttpService;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class TestWeb3JDynamicArrayTests {
// [testnet] & [mainnet] 과 동일
public static final String accessKeyId = "엑세스키아이디";
public static final String secretAccessKey = "시크릿";
protected static final String url = "https://node-api.klaytnapi.com/v1/klaytn";
public static final String abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"parent\",\"type\":\"string\"}],\"name\":\"init\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"parent\",\"type\":\"string\"}],\"name\":\"setting\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"parent\",\"type\":\"string\"}],\"name\":\"getNode\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]";
@Test
public void test() throws Exception {
getNode(abi, "0x2EE49DD49CCde12798761A566b5a4c859365822F", "0");
}
private void getNode(String abi, String contractAddress, String parent) throws Exception {
Caver caver = getCaver();
Contract contract = new Contract(caver, abi, contractAddress);
List<Type> result = contract.call("getNode", parent);
System.out.println("result(0) = " + result.get(0).getValue());
System.out.println("result(1) = " + result.get(1).getValue());
}
private Caver getCaver() {
HttpService httpService = new HttpService(url);
String auth = Credentials.basic(accessKeyId, secretAccessKey, StandardCharsets.UTF_8);
httpService.addHeader("Authorization", auth);
httpService.addHeader("x-chain-id", ChainId.BAOBAB_TESTNET +"");
Caver caver = new Caver(httpService);
return caver;
}
}
- 위 test() 실행 결과콘솔
result(0) = 0
result(1) = [�, def]
ide.klaytn.com 에서는 정상적으로 찍혀서 나옵니다.
-
ide.klaytn.com 에서의 정상적인 출력
어떤점이 잘못 되었을까요 ??
감사합니다.