1

This is my function in solidity

function transferForm(address _from, address _to, uint256 _value) public returns (bool success) {
}

Now i am calling the same function in test file for testing my smart contract

tokenInstance.transferForm(spendingAccount,10,{from : fromAccount})

I have intialized from Account to accounts[3]

Why are we sending the data by using {from : fromAccount}

Why can't we send data such as

tokenInstance.transferForm(from,spendingAccount,10,)

Is there any difference?

How do function knows when we are changing the position of function input parameters we are sending

please help me in understanding the logic

  • transferForm should probably be transferFrom – eth Oct 6 '18 at 5:25
2

Even thought they have similarly confusing names, they refer to different things.

The format { key: value } is web3 format for providing extra meta data for the function. Those are not passed on as parameters for the function but are handled internally. For example the key from can be used to define from which of the available accounts the transaction is sent from. This value is not passed on directly to the function, but the transaction is created from that account.

The transferFrom function expects a first argument of type address and it has to be provided. The function signature doesn't have any idea from which account the actual transaction originates from - that can be checked later on in the function with msg.sender. So if you provide a { from: fromAccount } for the function call, the fromAccount will become msg.sender inside the function.

EDIT

Also, the function transferFrom expects 3 parameters but you are only passing it two parameters in your first example. The function can't "re-map" the parameters in any way from metadata, so you'll get an error as a function with your signature (2 parameters) does not exist.

|improve this answer|||||
  • Thanks But small confusing the function is expecting three parameters but we are giving only two parmeters directly than it should throw an error the third parmeter we are passing in metadata which is used internally right. correct me if i am wrong – Venkatesh Muthyala Oct 5 '18 at 11:19
  • Sorry, I missed that problem also. Now edited my answer. – Lauri Peltonen Oct 5 '18 at 11:23
  • Thanks One more thing is that but how web3.js is included i never injected but in browser we use metmask to inject but how can we use web3.js calls here – Venkatesh Muthyala Oct 5 '18 at 11:32

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.