1

I have a simple contract as below:-

contract MultiAsset {
    address public creator;
    mapping(string => address) assetOwner;

    event NewAsset(string  indexed assetName, address indexed to);
    event AssetTransferred(string indexed assetName, address indexed from, address indexed to);

    function MultiAsset() {
        creator = msg.sender;
    }

    function assignOwner(string assetName, address to) {
        if (msg.sender == creator) {
            //new asset entry
            if (assetOwner[assetName] != 0x0) {
                throw;
            } else {
                assetOwner[assetName] = to;
                NewAsset(assetName, to);
            }
        } else {
            throw;
        }
    }

    function getAssetOwner(string assetName) constant returns (address owner){
        owner = assetOwner[assetName];
        return owner;
    }


    function transfer(string assetName, address to) {
        //transfer can be done only by the owner
        if (msg.sender == assetOwner[assetName]) {
            assetOwner[assetName] = to;
            AssetTransferred(assetName, msg.sender, to);
        } else {
            throw;
        }
    }}

The contract is to save a key value pair of assets like as below:-

"Asset1" - Addr1 "Asset2" - Addr2

Now my assignOwner function is working fine but when I call transfer function it doesn't work. I get an invalid jump in the debug.traceTransaction of the transfer tx.

Is there something wrong with the transfer function code?

Also if I am watching for events, nothing is getting printed?

var event = assetContract.NewAsset({}, '', function (error, result) {
    if (!error) {
        console.log("Asset Creation: " + result.args.assetName + " to To address:" + result.args.to);
    } else {
        console.log('error:' + error);
    }
});
2
  • throw is implemented as invalid jump, and it reverts pretty much everything, including events. So this condition must be returning false msg.sender == assetOwner[assetName] for some reason.
    – eth
    Jun 30, 2016 at 10:17
  • But assignOwner function is working fine and I get the correct owner using getAssetOwner function. Even its event is not getting logged. Not able to make out why transfer is not working at all?
    – Aman C
    Jun 30, 2016 at 10:36

2 Answers 2

1

The key lies in the msg.sender and the throw;.

Here is what is happening.

creator address :0x111 ( you created this contract ,as user 0x111)

So when you added two assetOwners

assetOwner["Asset1"]=0x111; ( You entered your address here )
assetOwner["Asset2"]=0x112; ( Your entered your friends address here) 

And when you try to transfer Asset2 to lets say 0x113 it fails , because you are using 0x111 as current user to invoke the method transfer , thus your are the msg.sender that is 0x111 ,but you are not the owner of Asset2,so it enters the else block.

But it won't fail if you try transfer Asset1 to let say 0x113 ( given that you use 0x111 as current user to invoke transfer method). In this case the condition msg.sender==assetOwenr["Asset1"] i.e 0x111==assetOwner["Asset1"] which is true.

5
  • No its failing from both addresses, some other issue is there which I am unable to find.
    – Aman C
    Jun 30, 2016 at 11:53
  • Which tool are you using , Mix ? I could able to reproduce Using Mix, with a scenario I have given. @AmanC Jun 30, 2016 at 11:55
  • I am using geth and compiling my contract code using online solidity compiler.
    – Aman C
    Jun 30, 2016 at 12:45
  • Please use mix, you would have much better control on development and faster as well Jun 30, 2016 at 12:48
  • Can someone help on this?
    – Aman C
    Jul 4, 2016 at 4:35
1

I changed the name of the method from transfer to transferOwner and now things are working.

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.