1

I want to store some details from every transaction. These are the struct and the variable:

struct MoneyTransfer{
    address sender;
    string comment;
    uint creationTime;
    uint quantity;
}

struct Transfers {
    MoneyTransfer[] structArray;
}

mapping(address => Transfers) private moneyTransfers;

The information is stored within a method:

function sendMoney(address toAddress, string comment) public payable returns (bool)  {        
    toAddress.transfer(msg.value);        
    moneyTransfers[toAddress].structArray.push(MoneyTransfer({sender:msg.sender, 
        creationTime:now, quantity:msg.value, comment:comment}));        
    return true;
}

Now... the problem: the "comment" is stored as a long string (64 chars instead of 30) without any character, there's only a "?" (undefined char) at position 31. This problem occurs every time I try to store a MoneyTransfer for a specific address for the first time. From the second MoneyTransfer for the same address, the comment is regularly stored.

EDIT: The method is called from the Java Wrapper generated with web3j, the same to retrieve the comments

public RemoteCall<TransactionReceipt> sendMoney(String toAddress, String comment, BigInteger weiValue) {
    final Function function = new Function(
            "sendMoney", 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(toAddress), 
            new org.web3j.abi.datatypes.Utf8String(comment)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function, weiValue);
}

    public RemoteCall<Tuple4<List<String>, List<BigInteger>, List<BigInteger>, List<String>>> getTransfers(String toAddress) {
    final Function function = new Function("getTransfers", 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(toAddress)), 
            Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Address>>() {}, new TypeReference<DynamicArray<Uint256>>() {}, new TypeReference<DynamicArray<Uint256>>() {}, new TypeReference<DynamicArray<Utf8String>>() {}));
    return new RemoteCall<Tuple4<List<String>, List<BigInteger>, List<BigInteger>, List<String>>>(
            new Callable<Tuple4<List<String>, List<BigInteger>, List<BigInteger>, List<String>>>() {
                @Override
                public Tuple4<List<String>, List<BigInteger>, List<BigInteger>, List<String>> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);
                    return new Tuple4<List<String>, List<BigInteger>, List<BigInteger>, List<String>>(
                            convertToNative((List<Address>) results.get(0).getValue()), 
                            convertToNative((List<Uint256>) results.get(1).getValue()), 
                            convertToNative((List<Uint256>) results.get(2).getValue()), 
                            convertToNative((List<Utf8String>) results.get(3).getValue()));
                }
            });
}

EDIT 2: Example of the returned string:

"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000�\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001`\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001�\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u001e"
  • This code seems to work fine for me in Remix. Could you describe (and share code for, if relevant) how you're calling sendMoney and how you're viewing the comment field? – user19510 Mar 16 '18 at 10:22
  • I've shared the code in the message – Dave Mar 16 '18 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.