web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Java wrapper generator does not properly handle payable functions

Open degloff opened this issue 4 years ago • 9 comments

Java wrapper generator for smart contract does not properly handle payable functions

There is no overload generated to send Ether into the smart contract function call, even though the contract function is marked payable.

Steps To Reproduce

Consider this solidity function

    function sendEthWithContract(address payable _recipient) external payable {
        require(msg.value > 0);
        _recipient.transfer(msg.value);
    }

Compile the contract and generate the Java wrapper. It generates

    public RemoteFunctionCall<TransactionReceipt> sendEthWithContract(String _recipient) {
        final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(
                FUNC_SENDETHWITHCONTRACT, 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _recipient)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

This is wrong as you have to generate an overload that passes Eth all the way down to the execution of the transaction. This is clear as the function is marked as payable.

Instead it forwards the execution of the transaction to

    protected TransactionReceipt executeTransaction(Function function)
            throws IOException, TransactionException {
        return executeTransaction(function, BigInteger.ZERO);
    }

This is clearly wrong.

Environment

  • Web3j version. core 4.6.0, codgen 4.5.18,
  • Java 8
  • Operating System Mac OS X Catalina

degloff avatar Sep 22 '20 11:09 degloff

Have the same issue;

mr13tech avatar Sep 23 '20 10:09 mr13tech

Known issue

the wrapper generator is expecting "payable: true" rather than "stateMutability: payable" to correctly generate wrapper functions for payable function

in the meantime here is an example of a function which will take msg.value and encode it correctly

    public RemoteFunctionCall<TransactionReceipt> newGreeting(String _greeting, BigInteger weiValue) {
        final Function function = new Function(
                FUNC_NEWGREETING, 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function, weiValue);
    }

you should just be able to add the weiValue as a function parameter and then add it to the executeRemoteCallTransaction parameters

iikirilov-wmt avatar Sep 30 '20 15:09 iikirilov-wmt

have the same issue, see https://github.com/web3j/web3j/issues/1306

andre77 avatar Dec 07 '20 10:12 andre77

Here is the line that produces the bug https://github.com/web3j/web3j/blob/87d2446324423c0109fc856b1b0026b4e1d85e23/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java#L1466

oscarguindzberg avatar Mar 25 '21 14:03 oscarguindzberg

Known issue

the wrapper generator is expecting "payable: true" rather than "stateMutability: payable" to correctly generate wrapper functions for payable function

in the meantime here is an example of a function which will take msg.value and encode it correctly

    public RemoteFunctionCall<TransactionReceipt> newGreeting(String _greeting, BigInteger weiValue) {
        final Function function = new Function(
                FUNC_NEWGREETING, 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function, weiValue);
    }

you should just be able to add the weiValue as a function parameter and then add it to the executeRemoteCallTransaction parameters

Thanks for this insight.

Any ETA on a fix for this so we don't have to manually apply the workaround?

sorinGrecu avatar Apr 27 '21 21:04 sorinGrecu

pls any idea, when this will be merged and released please?

thanks tomas

ertra avatar Mar 16 '22 00:03 ertra

This issue has been mentioned on Web3 Labs Community. There might be relevant details there:

https://community.web3labs.com/t/how-to-call-a-safemint-and-send-ether-at-the-same-time-to-pay-the-mint-nft-price/188/2

conor10 avatar Mar 16 '22 17:03 conor10

impacted today, subscribing to potential solution in https://github.com/web3j/web3j/pull/1647

jacques-martin10 avatar Apr 03 '22 11:04 jacques-martin10

https://github.com/web3j/web3j/pull/1647 has been merged, this issue ccan be closed.

jacques-martin10 avatar Apr 27 '22 12:04 jacques-martin10