0

I have a dispatching contract, that defines fallback function to send all "undefined" calls to implementation contract using delegatecall:

contract Dispatcher{
  address _implementation;
  function() public{
    bytes4 sig;
    assembly { sig := calldataload(0) }
    var len = _returnSizes[sig];
    var target = _implementation;

    assembly {
    // return _dest.delegatecall(msg.data)
        calldatacopy(0x0, 0x0, calldatasize)
        let ret := delegatecall(sub(gas, 10000), target, 0x0, calldatasize, 0, len)
        jumpi(0xffffffff, iszero(ret))
        return(0, len)
    }
}

However, I have no clue how to write tests in truffle in javascript, e.g. to call Implementation.methodA(). I guess I have to SOMEHOW format data in sendTransaction, but can't find any documentation or sample code. Help is appreciated.

  • Looking for something like await disp = Dispatcher.new(); await disp.sendTransaction("nonDefinedMethod", "param1", "param2", {from: account[1]}). No idea how to define the parameters to sendTransaction, if thast is the right way forward. – ejossev Jan 18 '18 at 20:24
1

I assume you have the implementation contract the dispatcher is dispatching too. If that is the case then you can just cast the address of the dispatcher as the implementation. Eg dispatchedImplementation = Implementation.at(Dispatcher.address) then you should be able call the functions on dispatchedImplementation as you would if it was an instance of the implementation contract. This assumes the _implementation address in the Dispatcher contract is the address of the implementation contract.

As a side note, sendTransaction() in truffle takes the same arguments as using web3.eth.sendTransaction() which can be seen here in the old web3 doc. To format the data to send a raw transaction to the dispatcher calling the fallback function requires more thought though. You shouldn't have to worry about that with the above method.

  • That is what I was looking for! Gonna try it. Thanks! – ejossev Jan 19 '18 at 7:55
0

Not exactly sure if this answers your questions, but tests in truffle are written using Mocha style. Probably you could check out their documentation! You can find the documentation at https://mochajs.org/

  • Unfortunately not, the answer is not in the mocha - the question is, how to manually create transaction, calling non-defined method of a contract, since neither mocha or truffle provide proper tools for it :/ – ejossev Jan 18 '18 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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