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?).

k-zar
  • 442
  • 1
  • 4
  • 8

2 Answers2

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); }
}

alexnavratil
  • 103
  • 3
Dennis Peterson
  • 1,936
  • 13
  • 15
  • Thank you. What if I do not know the code of contractA, just the ABI? – k-zar Jun 30 '16 at 02: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. – Dennis Peterson Jun 30 '16 at 03:25
  • If you don't know the code of ContractA, you can call the function directly using .call . See here for more detail. – emilebaizel Jan 25 '22 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...)

Kyle Baker
  • 752
  • 5
  • 16