18

I have a pre-deployed contractA with known ABI. I would like to call function func1(string,string) in the contractA (and pass the arguments) from a new contractB and send some value at the same time. So far, I managed to write the following (which does not send the arguments properly):

    contract contractB is mortal  {
        function invokeContractA() { 
            address contractAaddress= 0x1234567891234567891234567891234567891234;
            uint ValueToSend = 1234;
            contractAaddress.call.value(ValueToSend)(bytes4(sha3("func1(string,string)")),
                 "arg1TEXT", "arg2TEXT");
        }  
    }

Would appreciate your suggestions on what else is missing here (perhaps, need to convert the arguments into bytecode somehow?).

2 Answers 2

26

Here's an approach that's simpler and checked by the compiler:

contract contractA {
    function blah(int x, int y) payable {}
}

contract contractB {
    function invokeContractA() { 
        contractA a = contractA(0x1234567891234567891234567891234567891234);
        uint ValueToSend = 1234;
        a.blah{value: ValueToSend}(2, 3);
    }  
}
3
  • Thank you. What if I do not know the code of contractA, just the ABI?
    – k-zar
    Jun 30, 2016 at 2:43
  • You don't need to know all the code, just the signatures of the functions you want to call. Put those in your contractA definition like I did there, and you're good. Really you're just defining an interface. Jun 30, 2016 at 3:25
  • If you don't know the code of ContractA, you can call the function directly using .call . See here for more detail. Jan 25, 2022 at 23:20
6

A more modern style, as the last answer has gone a bit out of date:

(tokenInAfterFee, tokenOutAmt) = spclContract.swap{value: msg.value}(_spctToSwap);

(edit: the accepted answer was edited after I posted this to match mine. Not sure that's a good idea, it's good to have access to old code as well... Anyways...)

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.