Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass value, from, gasPrice to the contract method call? #988

Closed
dodikk opened this issue Oct 16, 2018 · 18 comments
Closed

How to pass value, from, gasPrice to the contract method call? #988

dodikk opened this issue Oct 16, 2018 · 18 comments

Comments

@dodikk
Copy link

dodikk commented Oct 16, 2018

Overview

How to pass value, from, gasPrice to the contract method call?
The web3.js API do not seem to work for me.

        await splitterContractInstance.Split(
            firstStranger,
            secondStranger,
         {
             from    : samePerson ,
             value   : weiToSplit ,
             gasPrice: gasPriceWei
         }); 

This code works for my contract under truffle but I'm getting test failures under embark since no ether is being sent.

Extra Detail

Initialization in embark test :

      const AdkSplitterForTwo = embark.require("Embark/contracts/AdkSplitterForTwo");
      splitterContractInstance.Split = AdkSplitterForTwo;

Initialization in truffle test

const AdkSplitterForTwo = artifacts.require("./AdkSplitterForTwo.sol");
var splitterContractInstance = await AdkSplitterForTwo.deployed();

My embark project https://github.com/dodikk/eth-splitter-contract-exercise-embark
My truffle project https://github.com/dodikk/eth-splitter-contract-exercise

Steps to reproduce

  1. git clone https://github.com/dodikk/eth-splitter-contract-exercise-embark
  2. embark test

Expected result : all tests pass
Actual result : 4 tests fail due to an exception in sendTransaction. The contract gets reverted due to insufficient ether amount even though I am trying to send some wei.

Screenshots

N/A

Logs

--logfile log.txt for some reason this option does not work for me when I invoke embark test. Any suggestions appreciated

Context (Environment)

  • OS:
Linux adk-VirtualBox 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Embark Version:
embark version
3.2.2
  • Node Version:
 node -v
v8.12.0
  • NPM Version:
 npm version
{ npm: '6.4.1',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.32.0',
  node: '8.12.0',
  openssl: '1.0.2p',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.19.2',
  v8: '6.2.414.66',
  zlib: '1.2.11' }
@jrainville
Copy link
Collaborator

Hi. Embark uses Web3 1.0 (currently uses beta 34), so the syntax that you used above doesn't work anymore.

You need to use .call() and .send() and it's in those that you can pass the parameters.
Here is the documentation on how the new way of calling methods is done: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id12

For your example, it would be something like so (assuming Split to be a transaction):

await splitterContractInstance.Split(firstStranger, secondStranger).send({
             from    : samePerson ,
             value   : weiToSplit ,
             gasPrice: gasPriceWei
         }); 

@dodikk
Copy link
Author

dodikk commented Oct 16, 2018

@jrainville , thanks for fast reply.


TypeError: Cannot read property 'gasUsed' of undefined

Now I am getting this. What else am I missing?

    var splitTransactionReceipt = null;
    try
    {
        splitTransactionReceipt =
        await splitterContractInstance.methods
                                      .Split(firstStranger, secondStranger)
                                      .send(
         {
             from    : samePerson ,
             value   : weiToSplit ,
             gasPrice: gasPriceWei
         }); 

    }
    catch (ex)
    {
        console.log(ex);
        assert.fail("unexpected exception generated by the contract" + ex);

        return;
    }


    var splitTransactionHash = splitTransactionReceipt.tx;
    var splitTransaction = await web3.eth.getTransaction(splitTransactionHash);

    // the returned object of .send() does not seem to be a valid transaction ((
    // 
    var gasUsed_bn  = new BigNumber(splitTransactionReceipt.receipt.gasUsed);

    var gasPrice_bn = new BigNumber(gasPriceWei);

@dodikk
Copy link
Author

dodikk commented Oct 16, 2018

I also wonder : "How to trigger a fallback function of the contract?"

With truffle I was invoking a sendTransaction() method right on a contract instance. What is the embark way of doing that?

            await splitterContractInstance.sendTransaction(
            {
                from    : samePerson, 
                value   : 2000      ,
                gasPrice: 0
            });

dodikk pushed a commit to dodikk/eth-splitter-contract-exercise-embark that referenced this issue Oct 16, 2018
@jrainville
Copy link
Collaborator

For your first question, it is because splitTransactionReceipt is the receipt in itself. No need to call splitTransactionReceipt.receipt, you can just do splitTransactionReceipt.gasUsed

For the second one, I'm not sure what you mean. I am not that good in Solidity. You want to make a bad transaction on the contract?

@dodikk
Copy link
Author

dodikk commented Oct 16, 2018

Thanks. I'll take a look at the newer docs of web3.js. That was a bit confusing for a beginner like me.

@dodikk
Copy link
Author

dodikk commented Oct 16, 2018

For the second one, I'm not sure what you mean. I am not that good in Solidity. You want to make a bad transaction on the contract?

Yeah, sort of.
That's an "unhappy path" of my unit test.

@dodikk
Copy link
Author

dodikk commented Oct 16, 2018

@jrainville , one final question if you don't mind...


    const AdkSplitterForTwo = embark.require("Embark/contracts/AdkSplitterForTwo");
   
    var splitterContractInstance = 
        await AdkSplitterForTwo.deploy({ arguments: [] }).send();

I am trying to deploy the contract instance explicitly in a test. For some reason, I am getting a different behaviour comparing to the case when I use a global contract deployed by embark var splitterContractInstance = AdkSplitterForTwo;

Could you please suggest a piece of documentation I can look at to resolve this kind of errors?

P.S. Sorry if this entire thread was irrelevant and it was a wrong place to ask. And thank you for your time and patience.

@richard-ramos
Copy link
Member

If you wish to execute the fallback function for a contract with web3js, you could do something like this:

web3.eth.sendTransaction({to: your_contract_address, from: samePerson, value: 2000, gasPrice: 0 });

@jrainville
Copy link
Collaborator

For the contract deploy, here is the exact Embark doc for that:
https://embark.status.im/docs/contracts_testing.html#Creating-contract-instances-in-your-tests
The entire page explains to you what you can do with the tests

@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

The entire page explains to you what you can do with the tests

@jrainville , thank you for the link.
I've seen the page mentioned by you and tried to follow the instructions but ...

For some reason, I am getting a different behaviour comparing to the case when I use a global contract deployed by embark

@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

@richard-ramos , thanks I'll give that API a try.

@dodikk dodikk closed this as completed Oct 17, 2018
@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

@jrainville , @richard-ramos so I have applied your suggestions. My tests do compile and run properly. Still, the balance bookkeeping fails since

Some ether is consumed despite the gasPrice is set to zero

Here are my logs :
https://gist.github.com/dodikk/9b9e671fbb9fef1c466d759ffa0250e1

Any suggestions, please?
P.S. The same contract and the same set of tests succeeds for truffle.

@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

Any suggestions, please?

UPD: Seems like I am experiencing this issue of web3.js web3/web3.js#1458

@jrainville
Copy link
Collaborator

Can you try this workaround web3/web3.js#1458 (comment)?
Setting gasPrice to '0' (a string). Hopefully, web3 fixes that problem soon.

@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

Can you try this workaround web3/web3.js#1458 (comment)?
Setting gasPrice to '0' (a string). Hopefully, web3 fixes that problem soon.

Yeah. It works.
P.S. I thought I had a follow-up comment mentioning that. My bad.

I also had an issue with event log assertions since the logs array has turned out to be a dictionary. But I've figured things out using the web3.js docs : https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

@dodikk
Copy link
Author

dodikk commented Oct 17, 2018

My brief summary of porting the contract to web3.js v1.0

1). Transaction options are now passed to .send() part of the contract method call.
Contract args are passd to the JS method with the same name the contract has.

await splitterContractInstance.Split(firstStranger, secondStranger).send({
             from    : samePerson ,
             value   : weiToSplit ,
             gasPrice: gasPriceWei
         }); 

2). web3.eth.sendTransaction is the only way to invoke the fallback function as of web3.js v1.0

3). gasPrice zero must be a string constant. Numbers are misinterpreted.
web3/web3.js#1458

4). Contract method call produces transaction receipt only in web3.js v1.0.
It has nologs array.

The name has been changed to events. And now it is a dictionary.
https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

@MunhozThiago
Copy link

Sorry to add to this closed thread, but I didn't find anything related elsewhere.
From the yellowpaper, a Call function should accept the "value" parameter as well, but it seems that the current implementation of Web3js does not take it as a parameter. Is that correct? Anyone could explain why so? It would be great to test functions that use "msg.value" within a call, not requiring to use send.

@jrainville
Copy link
Collaborator

@MunhozThiago, sorry, I can't really answer your question.

Maybe one of the devs of Web3js would know more https://github.com/ethereum/web3.js/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants