1

I have trouble sending an address to a deployed contract, my function looks like this:

function addNote(uint _value, address _owner) public { 
... 
}

and when I'm sending the parameters:

contract.deployed().then(function(instance) {
    return instance.addNote(value,"0x092f90acAbb3b23Aded64D59FB6f6Be97615476b");
})
.then(function(result) {
    console.log(result);
})
.catch(function(error) {
    console.log(error);
});

I'm getting this error:

Error: invalid address
at inputAddressFormatter (/Desktop/Server/node_modules/truffle-contract/node_modules/web3/lib/web3/formatters.js:274:11)

Also when I'm adding the last parameter:

{
    from: "0x092f90acAbb3b23Aded64D59FB6f6Be97615476b",
    gas: 1000000
}

then the error is:

TypeError: Cannot read property 'constructor' of undefined
at /Desktop/Server/node_modules/truffle-contract/contract.js:96:1

What's the problem?

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
Pawelo
  • 43
  • 1
  • 6
  • are you using truffle ?, can you ad a larger output of the error ? Did you specify a sending address ? the one that you use to send the transaction as the function addNote ? – Kaki Master Of Time Aug 08 '18 at 12:40
  • yes, truffle, im edited questions with more details – Pawelo Aug 08 '18 at 13:12
  • please post where are you calling constructor that resulted in this error Cannot read property 'constructor' of undefined and it is clear that the invalid address error is gone when adding the fromattribute. – Kaki Master Of Time Aug 08 '18 at 13:28
  • an address internally is an array of 20 bytes. A string internally is prefixed with its length, and then the string data follows. String contains Unicode encoded data, while the address contains pure bytes. Both types are completely incompatible. – Nulik Aug 08 '18 at 13:29
  • @Nulik, it is true. but that would be something in solidity, not in javascript. so I presume web3 does the conversion and type verifications automatically. – Kaki Master Of Time Aug 08 '18 at 13:31
  • @KakiMasterOfTime, well, I am not a user of truffle, but in web3js this conversion is done automatically when you encode input for your contract as extra data parameter in the transaction. You just have to supply the correct ABI specification. Now, if you want to manage addresses as string inside the contract, that's totally different story and you can do everything since EVM is a Turing Complete machine , and can do any type of generic computation. – Nulik Aug 08 '18 at 13:38
  • why won't you deploy your contract with geth instead of truffle, you are going to understand better how it works. https://medium.com/@gus_tavo_guim/deploying-a-smart-contract-the-hard-way-8aae778d4f2a – Nulik Aug 08 '18 at 13:40
  • What is in contract.js at line 96? – Mikhail Vladimirov Oct 16 '19 at 20:46

1 Answers1

0

An address is a 20-byte hex number, so you shall not send it as a string. Try:

return instance.addNote(value, 0x092f90acAbb3b23Aded64D59FB6f6Be97615476b);

UPDATE: The problem is not related to the address stringification. Please read below.

The confusing part in your specific example is that you're getting an error which misleads you to believe there's something wrong with the function function addNote(uint _value, address _owner). However, the error is about the invalidity of your web3.eth.defaultAccount address, that is, where are your contract calls executed from.

Right after compiling and migrating your contracts, do this:

web3.eth.defaultAccount = web3.eth.accounts[0];
Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143