3

I'm trying to make a frontend of calling a function with an address attached:

function getAvailableAllocation(address _addr)
  constant
  returns (uint ethAvailableAllocation)
{
  return investorActions.getAvailableAllocation(_addr);
}

Here is onSubmit function:

onSubmit = async event => {
  event.preventDefault();

  const accounts = await web3.eth.getAccounts();

  await fund.methods.getAvailableAllocation(this.state.value).call({
    from: accounts[0],
    address: this.state.value
  }).then(console.log);

Here is onSubmit form

<form onSubmit={this.onSubmit}>
  <h4>Available allocation</h4>
  <h5>Get the remaining available amount in Ether that an investor can subscribe for</h5>
  <div>
    <label>Your ethereum address</label>
    <input
      value={this.state.address}
      onChange={event => this.setState({ address: event.target.value })}
    />
    </div>
    <button>Get available allocation</button>
</form>

When I use an address passed it consolelog the right result. Like so: fund.methods.getAvailableAllocation('0xbe4B62ac6C98bf755dBf217e24FA502A00eC9cf3').call

What I'm doing wrong?

5
  • what do you mean to say in this sentence: "When I use an address passed it consolelog the right result." – Jaime Dec 14 '18 at 17:45
  • 1
    Don't you want this.state.address instead of this.state.value? – user19510 Dec 14 '18 at 19:00
  • @Jaime I mean the function works and I get the expected result by console.log. – Vladimir B. Dec 14 '18 at 20:07
  • 1
    Get rid of address: this.state.value. – goodvibration Dec 15 '18 at 9:22
  • 1
    And in the then, I suggest that you do something a little more explicit. For example: then(function(result, error) {console.log(JSON.stringify(result));});. – goodvibration Dec 15 '18 at 9:23
2

If you want to call a solidity function from javascript using web3 v1 you have to do the following

Let's suppose we have this in a contract

event FooTriggered(string message, address who, uint amount);

function foo(address aaa, uint bbb, string sss) public view returns (uint) {
    return 43214321;
}

To call this function from javascript using web3

const user = '0xabcdefabcdefabcdef...'
const val = 1234
const text = 'Hello'

const res = await myContract.methods.foo(user, val, text).call()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.