1

I called a function of contract.

function createTrade(string memory _name ,uint _price) public returns(address)
    {
        Trade newTrade = new Trade(_name, _price, _currencyAddress, msg.sender);
        products[msg.sender].push(product(_name, address(newTrade)));
        return  address(newTrade);
    }

my javaScript code

export var createTrade = async () => {
  const res = await TradeManagerContract.methods
  .createTrade("cake", 1000).send({from:Account[0]});
  console.log(res)
  return res;
}

Transactions are well sent. In the Remix I can check new Tradecontract .But in response, only the information on the block comes, not the address. I want a return that address. What`s the problem? please help me.

1 Answer 1

0

You are calling a non-view/non-pure function. You are calling a function that modifies the state of the smart contract in the blockchain. This means that your transaction will be broadcasted through the network for miners to mine it and include it in a new block, probably the next block.

So, this is an asynchronous operation. What you get as a response is the transaction receipt, not the result of the function in the smart contract.

If the contract function you call is a view or pure function, then you would get the result right away because that transaction would not be broadcasted through the network because any node can provide you that information if the state will not be modified.

So, you cannot get the response you are expecting this way.

For you to get the data that you need, you have a couple of options:

  1. Create an event and emit it in the createTrade with the information you need. Then, subscribe to that event from the frontend using web3js or ethersjs. When the transaction is mined and the smart contract's function executed, the event will be emitted, you will be notified and you will get the event data passed.

  2. Assign the result of the function to a state variable, like an array or mapping, then wait for the transaction to be mined, then read the state of the contract requesting the data that you need.

There's no need for your function to have a return type, unless this function will also be called from another smart contract. The smart contract will be running on the same EVM and on the same transaction, so the smart contract that calls this function will be able to get the value right away and do something with it.

For example:

Smart contract (I removed the returns(address) part, but it could have it if you know this contract will also be called from other contracts):

    event TradeCreated(address indexed tradeAddress);

    function createTrade(string memory _name ,uint _price) public {
        Trade newTrade = new Trade(_name, _price, _currencyAddress, msg.sender);
        products[msg.sender].push(product(_name, address(newTrade)));
        emit TradeCreated(address(newTrade));
    }

Javascript:

const subscription = web3.eth.subscribe('logs', {
    address: '0x123456..', // The smart contract address
    topics: ['0x12345...'] // The topics of your event
}, function(error, result){
    if (!error)
        console.log(result);
});

Check the docs: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-subscribe.html

1
  • 1
    thank you. You are real angel!!!
    – 이건희
    Sep 15 at 3:43

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.