0

I have created a smart contract in which a "stop()" function should be called once a deadline is reached. Dapp is keeping check of the deadline and as soon as the deadline is reached I need to call this function, the function itself actually sends some amount of ethers to 3 different addresses.

Here is the stop function:

function stop() public{
    require(now > deadline, "deadline expired");
    require(address(this).balance >= 1 ether, "contract doesnot have sufficient ethers");
   // quickSort(top, 0, top.length-1);
   for(uint i = 0; i < registeredStudents; i++)
        sort(i);
    // distribute the tokens
    for(uint i = 0; i <= 2; i++){
        students[top[i]].wallet.transfer((3-i) * 100000000000000000);
        emit Transferred(students[top[i]].wallet, top[i], 3-i);
        students[top[i]].totalBalance += (3-i) * 100000000000000000;
    }
}

How can I call this function from the Dapp? I tried using call() but it is not working and I tried send() too but send() wants me to give from address as well, which I dont know should be what in this case.

Please suggest. It is urgent task. Thanks in advance.

1 Answer 1

2

Yes you need to use send. Here is the code:

    const result = await contract.methods.stop()
          .send( { gas: '1500000', gasPrice: `30000000000000`, from:accounts[0]});

This code needs to be placed in an async function as it uses the async/await pattern!

Regarding your question with the from address. This is a state changing transaction. Therefore, it needs to be mined and costs gas!!

You need to access one of you accounts. Initialize web3 with your provider.

const web3 = new Web3(provider);

Now get the accounts.

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

This array now gets passed into the send function you see above.

EDIT: Adjust value of gas and gasPrice to fit your needs.

2
  • thanks, I know this way but isn't there any automatic way to do this? because in reality I want the dapp to do itself instead of asking user to confirm the transaction. Commented Nov 25, 2019 at 19:15
  • I don't know how your dapp exactly works, but it seems like you need to call this function a lot of times. There is the option that you pay for the tx cost (as the transaction SURELY costs gas).
    – cqx
    Commented Nov 25, 2019 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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